001: package migration.modules.ldap;
002:
003: import netscape.ldap.util.*;
004:
005: import netscape.ldap.LDAPAttribute;
006: import netscape.ldap.LDAPAttributeSet;
007:
008: import java.util.*;
009: import java.io.*;
010:
011: public class AppendUserInfo {
012:
013: static Map LISTOFUIDS = new HashMap();
014:
015: public static void main(String args[]) {
016:
017: String outputfile, ldiffile;
018:
019: ldiffile = new String();// Input file, the LDIF file to be converted....
020: outputfile = new String();
021: if (args.length == 2) {
022: outputfile = args[1];
023: ldiffile = args[0];
024: } else if (args.length == 1) {
025: outputfile = new String("default.ldif");
026: ldiffile = args[0];
027: } else if (args.length == 0) {
028: System.out
029: .println("Input file to be converted not specified!");
030: System.out
031: .println("Invokation Format is:java UsersConvert infile outfile");
032: System.exit(1);
033: }
034:
035: doConvert(ldiffile, outputfile);
036: }
037:
038: public static void doConvert(String ldiffile, String outputfile) {
039: LDIFRecord out;
040: OutputStreamWriter outFile;
041: int count = 0;
042: LDIF l1;
043:
044: String LDIFDIR = System.getProperty("LDIFDIR");
045: File ldiffile2 = new File(LDIFDIR + "/userDNs.ldif");
046:
047: InputStreamReader fr;
048: try {
049: l1 = new LDIF(ldiffile);
050:
051: if (!ldiffile2.exists()) {
052: ldiffile2.createNewFile();
053: }
054: fr = new InputStreamReader(new FileInputStream(ldiffile2),
055: "UTF-8");
056: outFile = new OutputStreamWriter(new FileOutputStream(
057: outputfile), "UTF-8");
058: //System.out.println("Converting LDIF entries corr. to users from file:"+ldiffile+".....\n");
059:
060: LDIFRecord tmp = l1.nextRecord();
061:
062: while (tmp != null) {
063: try {
064: PopulateMap(tmp);
065: } catch (Exception e) {
066: System.out.println("Error writing to output file:"
067: + outputfile);
068: e.printStackTrace();
069: }
070: tmp = l1.nextRecord();
071: }
072: try {
073: BufferedReader buffRead = new BufferedReader(fr);
074: String currLine;
075: while ((currLine = buffRead.readLine()) != null) {
076: currLine = (currLine.trim());
077: OutputRecord(currLine, outFile);
078: }
079: fr.close();
080: } catch (Exception ep) {
081:
082: }
083: outFile.close();
084: } catch (IOException e) {
085: System.out.println("Error:" + e.toString());
086: e.printStackTrace();
087: }
088: }
089:
090: static void PopulateMap(LDIFRecord tmp) {
091: LDIFAttributeContent con;
092: LDAPAttribute[] list;
093: LDAPAttributeSet theAttrSet;
094: String[] values;
095: String uid, password;
096:
097: uid = new String();
098: password = new String();
099: con = (LDIFAttributeContent) tmp.getContent();
100: list = con.getAttributes();
101: theAttrSet = new LDAPAttributeSet(list);
102:
103: for (int i = 0; i < theAttrSet.size(); ++i) {
104: values = (String[]) (theAttrSet.elementAt(i))
105: .getStringValueArray();
106: if (((theAttrSet.elementAt(i)).getName())
107: .equalsIgnoreCase("userPassword"))
108: password = values[0];
109: if (((theAttrSet.elementAt(i)).getName())
110: .equalsIgnoreCase("uid"))
111: uid = values[0].toLowerCase();
112: }
113: LISTOFUIDS.put(uid, password);
114: return;
115:
116: }
117:
118: static void OutputRecord(String toModify, OutputStreamWriter fw)
119: throws Exception {
120: String password = new String();
121: String uid = new String();
122:
123: uid = toModify.substring(toModify.indexOf("uid=") + 4, toModify
124: .indexOf(","));
125: password = PasswordPresent(uid);
126: if (password != null) {
127: fw.write("dn:" + toModify);
128: fw.write("\nchangetype:modify");
129: fw.write("\nadd:userPassword");
130: fw.write("\nuserPassword:" + password + "\n");
131: fw.write("\n");
132: }
133: return;
134: }
135:
136: static String PasswordPresent(String attrName) {
137:
138: return (String) (LISTOFUIDS.get(attrName.toLowerCase()));
139:
140: }
141:
142: }
|