Skip to content

Commit

Permalink
created function for create file user.ldif
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor authored and Victor committed Jan 25, 2023
1 parent 0ed28f9 commit 0800090
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 42 deletions.
7 changes: 7 additions & 0 deletions ldif/src/main/java/exceptions/WriteFileException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package exceptions;

public class WriteFileException extends RuntimeException {
public WriteFileException(String message) {
super(message);
}
}
130 changes: 88 additions & 42 deletions ldif/src/main/java/ldapCreater/LDAPCreater.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import exceptions.ReadFileException;
import exceptions.ThreadWorkError;
import exceptions.WriteFileException;
import transliteration.Transliteration;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -21,17 +19,15 @@ public class LDAPCreater {
private int sizeData;
private final String ou;
private final Transliteration[] trs;

private final int countThreads = Runtime.getRuntime().availableProcessors();
private String[] fileData;
private final List<String> userNames = new ArrayList<>();
private final StringBuffer userLdifData;

private synchronized int getId() {
return id++;
}

private String[] fileData;
private final List<String> userNames = new ArrayList<>();
private final StringBuffer userLdifData;

private synchronized int getIndex() {
return index++;
}
Expand All @@ -52,8 +48,7 @@ public LDAPCreater(String file, int idStart, String ou) {
private void readFile() {
String error = null;

try (FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr)) {
try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)) {

List<String> lines = new ArrayList<>();

Expand All @@ -78,6 +73,9 @@ private void readFile() {
}
}

/**
* Function for create data for user.ldif file.
*/
public void createLDAPInfo() {
readFile();

Expand All @@ -102,17 +100,33 @@ public void createLDAPInfo() {

} catch (InterruptedException e) {

error = String.format(
"Error in thread: %s",
e.getMessage()
);
error = String.format("Error in thread: %s", e.getMessage());
}

if (error != null) {
throw new ThreadWorkError(error);
}
}

String userFileName = "user.ldif";
String error = null;

try (FileWriter file = new FileWriter(userFileName);
BufferedWriter bw = new BufferedWriter(file)) {

bw.write(userLdifData.toString());

} catch (IOException e) {
error = String.format(
"Error write to file: %s\n%s",
userFileName,
e.getMessage()
);
}

if (error != null) {
throw new WriteFileException(error);
}
}

private synchronized boolean checkAndAddData(String LDAPData) {
Expand All @@ -138,7 +152,7 @@ private void work(int currentThreadIndex) {

String LDAPData = getOneLDAP(line, currentThreadIndex);


userLdifData.append(LDAPData);
}
}

Expand All @@ -153,35 +167,67 @@ public String getOneLDAP(String line, int i) {
int id = getId();

String[] lines = line.split(" ");
String transLit = trs[i].convert(lines[1], lines[0]);
String transLit = null;

int itr;
for (itr = 0; itr < 4; itr++) {

transLit = trs[i].convert(lines[1], lines[0], lines[2], itr);

boolean added = checkAndAddData(transLit);
boolean added = checkAndAddData(transLit);

if (added) {
break;
}
}

if (!added) {
transLit = trs[i].convert(lines[1], lines[0]);
String result = String.format("dn: cn=%1$s,ou=%2$s,dc=kubd,dc=kub\n"
+ "objectClass: top\n"
+ "objectClass: account\n"
+ "objectClass: posixAccount\n"
+ "objectClass: shadowAccount\n"
+ "cn: %3$s\n"
+ "uid: %1$s\n"
+ "uidNumber: %4$d\n"
+ "gidNumber: %4$d\n"
+ "homeDirectory: /home/%1$s\n"
+ "userPassword:\n"
+ "loginShell: /bin/bash\n"
+ "gecos: %1$s\n"
+ "shadowLastChange: -1\n"
+ "shadowMax: -1\n"
+ "shadowWarning: 0\n",
transLit, ou, line, id);

if (itr == 4) {
writeToErrorFile(result);

result = "";
}

return String.format(
"dn: cn=%1$s,ou=%2$s,dc=kubd,dc=kub\n" +
"objectClass: top\n" +
"objectClass: account\n" +
"objectClass: posixAccount\n" +
"objectClass: shadowAccount\n" +
"cn: %3$s\n" +
"uid: %1$s\n" +
"uidNumber: %4$d\n" +
"gidNumber: %4$d\n" +
"homeDirectory: /home/%1$s\n" +
"userPassword:\n" +
"loginShell: /bin/bash\n" +
"gecos: %1$s\n" +
"shadowLastChange: -1\n" +
"shadowMax: -1\n" +
"shadowWarning: 0\n",
transLit,
ou,
line,
id
);
return result;
}

private void writeToErrorFile(String line) {

String errorFileName = "errors.txt";
String error = null;

try (FileWriter file = new FileWriter(errorFileName, true);
BufferedWriter bw = new BufferedWriter(file)) {

bw.write(line);

} catch (IOException e) {
error = String.format(
"Error write to file: %s\n%s",
errorFileName,
e.getMessage()
);
}

if (error != null) {
throw new WriteFileException(error);
}
}
}
13 changes: 13 additions & 0 deletions ldif/src/main/java/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package main;

import ldapCreater.LDAPCreater;

public class Main {
private Main() {
}

public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Please enter arguments (filename, idStart, ou)");
return;
}

String filename = args[0];
int idStart = Integer.parseInt(args[1]);
String ou = args[2];

LDAPCreater ldapCreater = new LDAPCreater(filename, idStart, ou);

ldapCreater.createLDAPInfo();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

@SuppressWarnings("SpellCheckingInspection")
class TransliterationTest {
final Transliteration tr = new Transliteration();

Expand Down

0 comments on commit 0800090

Please sign in to comment.