001: /*
002: * CreateTemplates.java
003: *
004: * Created on April 1, 2003, 11:51 AM
005: */
006:
007: package migration.modules.srap.ldap;
008:
009: import netscape.ldap.util.*;
010:
011: import netscape.ldap.LDAPAttribute;
012: import netscape.ldap.LDAPAttributeSet;
013:
014: import java.util.*;
015: import java.io.*;
016:
017: /**
018: *
019: * @author Mridul Muralidharan
020: * @version
021: */
022: public class CreateTemplates {
023: static FileWriter outFile;
024: static String rootsuffix = "o=isp";
025: static LinkedList orgList = new LinkedList();
026:
027: public static void main(String args[]) {
028:
029: String outputDir, ldifDir;
030:
031: ldifDir = new String();// Input file, the LDIF file to be converted....
032: outputDir = new String();
033: if (args.length == 2) {
034: outputDir = args[1];
035: ldifDir = args[0];
036: } else if (args.length == 1) {
037: outputDir = new String("/tmp/xml");
038: ldifDir = args[0];
039: } else if (args.length == 0) {
040: System.out
041: .println("Input file to be converted not specified!");
042: System.out
043: .println("Invokation Format is:java CreateTemplates ldif_directory output_dir");
044: System.exit(1);
045: }
046: try {
047: ResourceBundle ambundle;
048: ambundle = ResourceBundle.getBundle("AMConfig");
049:
050: if (ambundle.getObject("com.iplanet.am.rootsuffix") != null) {
051: rootsuffix = ambundle.getObject(
052: "com.iplanet.am.rootsuffix").toString();
053: }
054: } catch (Exception ex) {
055: System.out.println("Error getting resource bundle !\n");
056: ex.printStackTrace();
057: }
058:
059: doConvert(ldifDir + "/domain.ldif");
060: doConvert(ldifDir + "/role.ldif");
061: dumpXmls(outputDir + "/authConfig.xml");
062: System.out.println("Exiting ...");
063: System.exit(0);
064: }
065:
066: private static void dumpXmls(String fileName) {
067: try {
068: outFile = new FileWriter(fileName);
069: printHeader();
070:
071: List orgContainerList = OrgContainerParser.parse(orgList,
072: rootsuffix);
073: Iterator iter = orgContainerList.iterator();
074:
075: while (iter.hasNext()) {
076: // OrgContainer's toString returns the org name.
077: String org = iter.next().toString();
078: System.out.println("dumpXmls :: org : " + org);
079: writeAuthConfigTemplate(org);
080: writePolicyTemplate(org);
081: }
082: System.out.println("dumpXmls :: Done !!");
083: outFile.write("\n</Requests>");
084: outFile.close();
085: } catch (Exception ex) {
086: System.out
087: .println("Exception while dumping policies xml file.\n");
088: ex.printStackTrace();
089: }
090: }
091:
092: private static void writeAuthConfigTemplate(String org)
093: throws IOException {
094: /*
095: outFile.write(" <OrganizationRequests DN=\""+org+"\">\n");
096: outFile.write(" <RegisterServices>\n");
097: outFile.write(" <Service_Name>iPlanetAMAuthConfiguration</Service_Name>\n");
098: outFile.write(" </RegisterServices>\n");
099: outFile.write(" </OrganizationRequests>\n");
100: outFile.write("\n <OrganizationRequests DN=\""+org+"\">\n");
101: outFile.write(" <AddSubConfiguration subConfigName = \"Configurations/gatewaypdc\"\n");
102: outFile.write(" subConfigId = \"NamedConfiguration\"\n");
103: outFile.write(" priority = \"0\"\n");
104: outFile.write(" serviceName=\"iPlanetAMAuthConfiguration\">\n");
105: outFile.write(" <AttributeValuePair>\n");
106: outFile.write(" <Attribute name=\"iplanet-am-auth-configuration\"/>\n");
107: outFile.write(" <Value><AttributeValuePair><");
108: outFile.write("Value>com.sun.identity.authentication.modules.cert.Cert REQUIRED </Value");
109: outFile.write("></AttributeValuePair></Value>\n");
110: outFile.write(" </AttributeValuePair>\n");
111: outFile.write(" </AddSubConfiguration>\n");
112: outFile.write(" </OrganizationRequests>\n");
113: */
114: }
115:
116: private static void writePolicyTemplate(String org)
117: throws IOException {
118: outFile.write(" <OrganizationRequests DN=\"" + org + "\">\n");
119: outFile.write(" <RegisterServices>\n");
120: outFile
121: .write(" <Service_Name>iPlanetAMPolicyConfigService</Service_Name>\n");
122: outFile.write(" </RegisterServices>\n");
123: outFile.write(" </OrganizationRequests>\n");
124: outFile.write(" <OrganizationRequests DN=\"" + org + "\">\n");
125: outFile.write(" <CreateServiceTemplate>\n");
126: outFile
127: .write(" <Service_Name>iPlanetAMPolicyConfigService</Service_Name>\n");
128: outFile.write(" </CreateServiceTemplate>\n");
129: outFile.write(" </OrganizationRequests>\n");
130: }
131:
132: public static void doConvert(String ldiffile) {
133: LDIFRecord out;
134: int count = 0;
135: LDIF l1;
136:
137: try {
138: l1 = new LDIF(ldiffile);
139:
140: System.out
141: .println("Converting LDIF entries corr. to users from file:"
142: + ldiffile + ".....\n");
143:
144: LDIFRecord tmp = l1.nextRecord();
145:
146: while (tmp != null) {
147: ProcessRecord(tmp);
148: tmp = l1.nextRecord();
149: count++;
150: }
151: System.out.println("Processed " + count + " entries");
152: } catch (Exception e) {
153: System.out.println("Error:" + e.toString());
154: e.printStackTrace();
155: }
156: }
157:
158: static void printHeader() throws Exception {
159:
160: Locale locale = Locale.getDefault();
161: ResourceBundle ambundle;
162: ambundle = ResourceBundle.getBundle("AMConfig");
163: String IDSAMEBaseDir = new String();
164:
165: if (ambundle.getObject("com.iplanet.am.installdir") != null) {
166: IDSAMEBaseDir = (String) (ambundle
167: .getObject("com.iplanet.am.installdir"));
168: IDSAMEBaseDir = IDSAMEBaseDir.substring(0, IDSAMEBaseDir
169: .indexOf("SUNWam"));
170: }
171: outFile
172: .write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
173: outFile
174: .write("\n<!-- PROPRIETARY/CONFIDENTIAL/ Use of this product is subject");
175: outFile
176: .write("\n to license terms. Copyright 2001 Sun Microsystems Inc.");
177: outFile
178: .write("Some preexisting portions Copyright 2001 Netscape");
179: outFile.write("Communications Corp. All rights reserved. -->");
180: outFile
181: .write("\n<!DOCTYPE Requests PUBLIC \"-//iPlanet//iDSAME 5.0 Admin CLI DTD//EN \" ");
182: outFile.write("\"file:" + IDSAMEBaseDir
183: + "SUNWam/dtd/amAdmin.dtd\">");
184:
185: outFile.write("\n<Requests>");
186: return;
187: }
188:
189: static void ProcessRecord(LDIFRecord toConvert) throws Exception {
190: LDIFAttributeContent con;
191: LDAPAttributeSet theAttrSet;
192: String[] allAttrs, accesslist, denylist;
193: String attrName;
194: String org, profiletype, rolename, parent, dn, profileid;
195: boolean tabContainerFound = false;
196: boolean authModulesToAdd = false;
197: boolean alist_set, dlist_set;
198:
199: LDAPAttribute[] attrList;
200:
201: profiletype = parent = rolename = profileid = null;
202: System.out.print("ProcessRecord : ");
203: System.out.flush();
204: org = getOrgName(toConvert.getDN());
205: System.out.println("org : " + org);
206: dn = new String();
207:
208: alist_set = dlist_set = false;
209: org = getOrgName(toConvert.getDN());
210: con = (LDIFAttributeContent) toConvert.getContent();
211: attrList = con.getAttributes();
212: theAttrSet = new LDAPAttributeSet(attrList);
213: accesslist = null;
214: denylist = null;
215:
216: for (int i = 0; i < attrList.length; ++i) {
217: if ((attrList[i].getName()).equalsIgnoreCase("profiletype")) {
218: allAttrs = (((LDAPAttribute) (theAttrSet
219: .getAttribute(attrList[i].getName())))
220: .getStringValueArray());
221: profiletype = allAttrs[0];
222: }
223: if ((attrList[i].getName()).equalsIgnoreCase("profileid")) {
224: allAttrs = (((LDAPAttribute) (theAttrSet
225: .getAttribute(attrList[i].getName())))
226: .getStringValueArray());
227: profileid = allAttrs[0];
228: }
229: if ((attrList[i].getName()).equalsIgnoreCase("cn")) {
230: allAttrs = (((LDAPAttribute) (theAttrSet
231: .getAttribute(attrList[i].getName())))
232: .getStringValueArray());
233: rolename = allAttrs[0];
234: }
235: if ((attrList[i].getName()).equalsIgnoreCase("parent")) {
236: allAttrs = (((LDAPAttribute) (theAttrSet
237: .getAttribute(attrList[i].getName())))
238: .getStringValueArray());
239: parent = allAttrs[0];
240: }
241: }
242: if (profiletype.equalsIgnoreCase("role"))
243: dn = ConstructDN(profileid);
244:
245: String fqOrgDN = null;
246: if (profiletype.equalsIgnoreCase("domain")) {
247: fqOrgDN = "o=" + org + "," + rootsuffix;
248: } else if (profiletype.equalsIgnoreCase("role")) {
249: fqOrgDN = dn.substring(1) + "," + rootsuffix;
250: } else {
251: System.out.println("Unknown org type : " + profiletype
252: + " !!\n");
253: throw new RuntimeException("Unknown org type");
254: }
255:
256: if (fqOrgDN != null && !orgList.contains(fqOrgDN)) {
257: orgList.add(fqOrgDN);
258: }
259: return;
260: }
261:
262: static String getOrgName(String dn) {
263: String orgName = new String();
264:
265: orgName = dn.substring(3, dn.indexOf(","));
266: return orgName;
267: }
268:
269: static String ConstructDN(String roleFileName) {
270: String rolename;
271: String tmp;
272: String dnToRet;//The DN to be returned
273:
274: // Given the relative path of the current file. This function constructs the DN of the required Role i.e SUbORganization..*
275:
276: dnToRet = new String();
277: tmp = roleFileName;
278: while (tmp.indexOf("/") >= 0) {
279: dnToRet += ",o=" + tmp.substring(tmp.lastIndexOf("/") + 1);
280: tmp = tmp.substring(0, tmp.lastIndexOf("/"));
281: }
282:
283: return dnToRet;
284: }
285: }
|