001: package migration_is60.modules.srap.netfile;
002:
003: /*
004: * NetfileDynamicMigration.java
005: *
006: * Created on July 7, 2003, 4:51 PM
007: */
008:
009: import netscape.ldap.util.*;
010: import netscape.ldap.LDAPAttribute;
011: import netscape.ldap.LDAPAttributeSet;
012:
013: import java.util.*;
014: import java.io.*;
015:
016: /**
017: *
018: * @author mm132998
019: * @version
020: */
021: public class NetfileDynamicMigration {
022:
023: private static final char c_eol = '\n';
024: private String s_empty_string = "";
025: private ResourceBundle nfRes = null;
026:
027: public static void main(String args[]) {
028:
029: if (args.length != 2) {
030: // blah blah blah ...
031: System.out
032: .println("Usage : <input ldiff file> , <output xml file>");
033: System.exit(0);
034: }
035: NetfileDynamicMigration mig = new NetfileDynamicMigration(
036: args[0], args[1]);
037: mig.migrate();
038: System.exit(0);
039: }
040:
041: private String inputFile;
042: private String outputFile;
043:
044: public NetfileDynamicMigration(String inputFile, String outputFile) {
045: this .inputFile = inputFile;
046: this .outputFile = outputFile;
047: try {
048: nfRes = ResourceBundle
049: .getBundle("srapNetfileDynamicMigration");
050: } catch (MissingResourceException mrbEx) {
051: System.out
052: .println("Unable to lead resource bundle : srapNetfileDynamicMigration");
053: mrbEx.printStackTrace();
054: System.exit(0);
055: }
056: }
057:
058: /*
059: * Steps to be done :
060: * 1) Read an entry block.
061: * 2) Find if org , role or User value.
062: * 3) Get the attributes.
063: * 4) Construct the corresponding xml snippet.
064: * 5) Repeat from 1 until done
065: */
066: void migrate() {
067: DataHolder data;
068: FileWriter outFile;
069: int count = 0;
070: LDIF l1;
071:
072: try {
073: l1 = new LDIF(inputFile);
074: outFile = new FileWriter(outputFile);
075: LDIFRecord tmp = l1.nextRecord();
076: printHeader(outFile);
077: while (tmp != null) {
078: System.out.println("\n # Entry id:" + count);
079: count++;
080: data = ConvertRecord(tmp);
081: try {
082: OutputRecord(outFile, data);
083: } catch (Exception e) {
084: System.out.println("Error writing to output file:"
085: + outputFile);
086: e.printStackTrace();
087: }
088: tmp = l1.nextRecord();
089: }
090: outFile.write("\n</Requests>");
091: outFile.close();
092: System.out.println("Processed " + count + " entries");
093: System.out
094: .println("Output available in file " + outputFile);
095: } catch (IOException e) {
096: System.out.println("Error:" + e.toString());
097: e.printStackTrace();
098: }
099: }
100:
101: DataHolder ConvertRecord(LDIFRecord toConvert) {
102: DataHolder dataHolder = new DataHolder(toConvert.getDN());
103:
104: LDIFAttributeContent ldifAttrContent = (LDIFAttributeContent) toConvert
105: .getContent();
106: LDAPAttribute[] attrList = ldifAttrContent.getAttributes();
107: LDAPAttributeSet theAttrSet = new LDAPAttributeSet(attrList);
108:
109: int tmpcount;
110: List data = new ArrayList();
111: for (int i = 0; i < attrList.length; ++i) {
112: String attrName = attrList[i].getName();
113: if (attrName
114: .equalsIgnoreCase("sunPortalNetFileCommonHostData")) {
115: String[] allValues = theAttrSet.getAttribute(attrName)
116: .getStringValueArray();
117: for (tmpcount = 0; tmpcount < allValues.length; tmpcount++) {
118: List list = parseCommonHostsData(allValues[tmpcount]);
119: data.addAll(list);
120: }
121: }
122: }
123: dataHolder.setData(data);
124: return dataHolder;
125: }
126:
127: static void printHeader(FileWriter outFile) throws IOException {
128:
129: ResourceBundle ambundle;
130: ambundle = ResourceBundle.getBundle("AMConfig");
131: String IDSAMEBaseDir = new String();
132:
133: if (ambundle.getObject("com.iplanet.am.installdir") != null) {
134: IDSAMEBaseDir = (String) (ambundle
135: .getObject("com.iplanet.am.installdir"));
136: IDSAMEBaseDir = IDSAMEBaseDir.substring(0, IDSAMEBaseDir
137: .indexOf("SUNWam"));
138: }
139: outFile
140: .write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
141: outFile
142: .write("\n<!-- PROPRIETARY/CONFIDENTIAL/ Use of this product is subject");
143: outFile
144: .write("\n to license terms. Copyright 2001 Sun Microsystems Inc.");
145: outFile
146: .write("Some preexisting portions Copyright 2001 Netscape");
147: outFile.write("Communications Corp. All rights reserved. -->");
148: outFile
149: .write("\n<!DOCTYPE Requests PUBLIC \"-//iPlanet//iDSAME 5.0 Admin CLI DTD//EN \" ");
150: outFile.write("\"file:" + IDSAMEBaseDir
151: + "SUNWam/dtd/amAdmin.dtd\">");
152:
153: outFile.write("\n<Requests>");
154: return;
155: }
156:
157: void writeData(FileWriter outFile, DataHolder data)
158: throws IOException {
159: Iterator iter = data.data.iterator();
160:
161: while (iter.hasNext()) {
162: outFile.write("\n <Value>");
163: outFile.write(iter.next().toString());
164: outFile.write("\n </Value>");
165: }
166: }
167:
168: void OutputRecord(FileWriter outFile, DataHolder data)
169: throws IOException {
170:
171: if (data.isNull()) {
172: return;
173: }
174:
175: if (data.dataType == data.ORG_DATA_TYPE) {
176: outFile.write("\n<OrganizationRequests DN=\"" + data.dn
177: + "\">");
178: outFile
179: .write("\n <ModifyServiceTemplate serviceName=\"srapNetFileService\" schemaType=\"Dynamic\">");
180: outFile.write("\n <AttributeValuePair>");
181: outFile
182: .write("\n <Attribute name=\"sunPortalNetFileCommonHostData\"/>");
183: outFile.write("\n ");
184: writeData(outFile, data);
185: outFile.write("\n </AttributeValuePair>");
186: outFile.write("\n </ModifyServiceTemplate>");
187: outFile.write("\n</OrganizationRequests>");
188: } else if (data.dataType == data.ROLE_DATA_TYPE) {
189: outFile.write("\n<RoleRequests DN=\"" + data.dn + "\">");
190: outFile
191: .write("\n <ModifyServiceTemplate serviceName=\"srapNetFileService\" schemaType=\"1.0\">");
192: outFile.write("\n <AttributeValuePair>");
193: outFile
194: .write("\n <Attribute name=\"sunPortalNetFileCommonHostData\"/>");
195: writeData(outFile, data);
196: outFile.write("\n </AttributeValuePair>");
197: outFile.write("\n </ModifyServiceTemplate>");
198: outFile.write("\n</RoleRequests>");
199: } else {
200: outFile.write("\n<PeopleContainerRequests DN=\""
201: + data.dn.substring(data.dn.indexOf(',') + 1)
202: + "\">");
203: outFile.write("\n <ModifyUser modifyDN=\"" + data.dn
204: + "\" >");
205: outFile.write("\n <AttributeValuePair>");
206: outFile
207: .write("\n <Attribute name=\"sunPortalNetFileCommonHostData\"/>");
208: writeData(outFile, data);
209: outFile.write("\n </AttributeValuePair>");
210: outFile.write("\n </ModifyUser>");
211: outFile.write("\n</PeopleContainerRequests>");
212: }
213: }
214:
215: List parseCommonHostsData(String szCommonHosts) {
216:
217: StringTokenizer tokens = new StringTokenizer(szCommonHosts,
218: "\n");
219: ArrayList listComHosts = new ArrayList();
220:
221: StringBuffer sbHostData = null;
222:
223: while (tokens.hasMoreTokens()) {
224: String szToken = tokens.nextToken();
225:
226: if (szToken.startsWith("machine_name=")) {
227: if (sbHostData != null) {
228: listComHosts.add(sbHostData.toString());
229: }
230: sbHostData = new StringBuffer();
231: sbHostData.append(szToken).append(c_eol);
232: } else {
233: sbHostData.append(szToken).append(c_eol);
234: }
235: }
236: if (sbHostData != null) {
237: listComHosts.add(sbHostData.toString());
238: }
239:
240: for (int i = 0; i < listComHosts.size(); i++) {
241: // If there is any problem in parsing the host we skip it and move on
242: try {
243: String s_common_host_data = (String) listComHosts
244: .get(i);
245:
246: HashMap ht_s_common_host_data = parseHost(
247: s_common_host_data, "\n");
248: ht_s_common_host_data = verifyHostData(ht_s_common_host_data);
249: } catch (Exception e) {
250: e.printStackTrace();
251: System.out.println("Unable to parse common host data "
252: + e.getMessage());
253: listComHosts.remove(i);
254: }
255: }
256: return listComHosts;
257: }
258:
259: HashMap parseHost(String s_user_added_host_share, String separator)
260: throws Exception {
261:
262: StringTokenizer st_tokens = new StringTokenizer(
263: s_user_added_host_share, separator);
264: HashMap ht_host = new HashMap(st_tokens.countTokens() + 2);
265:
266: try {
267: ArrayList v_shares = new ArrayList();
268: ArrayList v_share_passwords = new ArrayList();
269: ht_host.put("shares", v_shares);
270: ht_host.put("share_passwords", v_share_passwords);
271: String s_name = null;
272:
273: while (st_tokens.hasMoreTokens()) {
274: String token = st_tokens.nextToken();
275: if (token.startsWith("machine_name=")) {
276: s_name = removePrefix("machine_name=", token);
277: if ((s_name == null) || (s_name.equals(""))) {
278: throw new Exception("illegal_machine_name");
279: }
280: ht_host.put("machine_name", s_name);
281: } else if (token.startsWith("machine_type=")) {
282: s_name = removePrefix("machine_type=", token);
283: if ((s_name == null) || (s_name.equals(""))) {
284: throw new Exception("illegal_machine_type");
285: }
286: ht_host.put("machine_type", s_name);
287: } else if (token.startsWith("machine_encoding=")) {
288: s_name = removePrefix("machine_encoding=", token);
289: ht_host.put("machine_encoding", s_name);
290: } else if (token.startsWith("machine_domain=")) {
291: s_name = removePrefix("machine_domain=", token);
292: ht_host.put("machine_domain", s_name);
293: } else if (token.startsWith("machine_password=")) {
294: s_name = removePrefix("machine_password=", token);
295: ht_host.put("machine_password", s_name);
296: } else if (token.startsWith("machine_user_name=")) {
297: s_name = removePrefix("machine_user_name=", token);
298: ht_host.put("machine_user_name", s_name);
299: } else if (token.startsWith("share_name=")) {
300: s_name = removePrefix("share_name=", token);
301: v_shares.add(s_name);
302: String s_password = st_tokens.nextToken();
303: v_share_passwords.add(removePrefix(
304: "share_password=", s_password));
305: }
306: }
307: } catch (Exception e) {
308: System.out.println("Unable to parse host data " + e);
309: throw new Exception("Illegal data format for common host ");
310: }
311: return ht_host;
312: }
313:
314: String removePrefix(String prefix, String szValue) throws Exception {
315: if (!szValue.startsWith(prefix)) {
316: throw new Exception("illegal_machine_data_format");
317: }
318: szValue = szValue.substring(prefix.length(), szValue.length());
319: return removeCtrlMChar(szValue);
320: }
321:
322: void removeCtrlMChar(String szPrefix, String szValue,
323: StringBuffer sbHostInfo) {
324: int i = szValue.lastIndexOf('\n');
325: int j = szValue.lastIndexOf('\r');
326: if ((i < 0) && (j < 0))
327: sbHostInfo.append(szPrefix).append(szValue).append(c_eol);
328: else if ((j < 0) && (i >= 0))
329: sbHostInfo.append(szPrefix).append(szValue.substring(0, i))
330: .append(c_eol);
331: else {
332: System.out.println("i is " + i + " j is " + j
333: + " value is " + szValue.substring(0, j));
334: sbHostInfo.append(szPrefix).append(szValue.substring(0, j))
335: .append(c_eol);
336: }
337: }
338:
339: String removeCtrlMChar(String szValue) {
340: int i = szValue.lastIndexOf('\n');
341: int j = szValue.lastIndexOf('\r');
342: if ((i < 0) && (j < 0))
343: return szValue + c_eol;
344: else if ((j < 0) && (i >= 0))
345: return szValue.substring(0, i) + c_eol;
346: else
347: return szValue.substring(0, j) + c_eol;
348: }
349:
350: HashMap verifyHostData(HashMap ht_s_common_host_data)
351: throws Exception {
352:
353: if (ht_s_common_host_data.get("machine_name") == null) {
354: throw new Exception("illegal_machine_name");
355: }
356: ht_s_common_host_data = verifyCharacterEncoding(ht_s_common_host_data);
357: verifyMachineType(ht_s_common_host_data);
358: if (ht_s_common_host_data.get("machine_domain") == null) {
359: ht_s_common_host_data.put("machine_domain", s_empty_string);
360: }
361: if (ht_s_common_host_data.get("machine_password") == null) {
362: ht_s_common_host_data.put("machine_password",
363: s_empty_string);
364: }
365: if (ht_s_common_host_data.get("machine_user_name") == null) {
366: ht_s_common_host_data.put("machine_user_name",
367: s_empty_string);
368: }
369:
370: return ht_s_common_host_data;
371: }
372:
373: void verifyMachineType(HashMap ht_s_common_host_data)
374: throws Exception {
375: Object o_type = ht_s_common_host_data.get("machine_type");
376: if (o_type == null) {
377: throw new Exception("illegal_machine_type");
378: } else {
379: String s_type = (String) o_type;
380: s_type = s_type.trim();
381: if (!(s_type.equals("FTP") || s_type.equals("NETWARE")
382: || s_type.equals("NFS") || s_type.equals("NT") || s_type
383: .equals("WIN"))) {
384: throw new Exception("illegal_machine_type");
385: }
386: }
387: System.out.println("Machine Type verified is "
388: + (String) o_type);
389: }
390:
391: HashMap verifyCharacterEncoding(HashMap ht_host) throws Exception {
392:
393: Object o_encoding = ht_host.remove("machine_encoding");
394:
395: String s_encoding = s_empty_string;
396: if (o_encoding != null) {
397: s_encoding = (String) (o_encoding);
398: s_encoding = s_encoding.trim();
399: }
400:
401: int index = s_encoding.indexOf('(');
402:
403: if (index == -1) {
404: throw new Exception("Bad encoding : " + s_encoding);
405: }
406:
407: int index1 = s_encoding.indexOf(')', index);
408: if (index1 == -1) {
409: throw new Exception("Bad encoding : " + s_encoding);
410: }
411:
412: s_encoding = s_encoding.substring(index + 1, index1);
413:
414: String s_encoding_display_name = nfRes.getString("encoding_"
415: + s_encoding);
416:
417: if (s_encoding_display_name == null
418: || s_encoding_display_name.trim()
419: .equals(s_empty_string)
420: || s_encoding_display_name.trim().equals(
421: "encoding_" + s_encoding)) {
422: throw new Exception("Unknown encoding : " + s_encoding);
423: }
424: System.out.println("Machine encoding being placed is "
425: + s_encoding_display_name);
426: return ht_host;
427: }
428: }
429:
430: class DataHolder {
431:
432: public final int USER_DATA_TYPE = 0;
433: public final int ROLE_DATA_TYPE = 1;
434: public final int ORG_DATA_TYPE = 2;
435: public final int INVALID_DATA_TYPE = -1;
436:
437: int dataType = INVALID_DATA_TYPE;
438: String dn = null;
439: List data = null;
440:
441: private static final String ORG_DN_PREFIX = "cn=\"cn=ContainerDefaultTemplateRole,";
442: private static final String ROLE_DN_PREFIX = "cn=\"cn=";
443:
444: private static String orgDNStartPrefix = "\",cn=srapNetFileService,";
445:
446: private static final int ORG_DN_PREFIX_LEN;
447: private static final int ROLE_DN_PREFIX_LEN;
448: private static final int orgDNStartPrefixLen;
449:
450: static {
451: ORG_DN_PREFIX_LEN = ORG_DN_PREFIX.length();
452: ROLE_DN_PREFIX_LEN = ROLE_DN_PREFIX.length();
453: orgDNStartPrefixLen = orgDNStartPrefix.length();
454: }
455:
456: DataHolder(String dn) {
457: // Find out which type of entry this is and extract the corresponding dn.
458: if (dn.regionMatches(true, 0, ORG_DN_PREFIX, 0,
459: ORG_DN_PREFIX_LEN)) {
460: dataType = ORG_DATA_TYPE;
461: this .dn = dn.substring(dn.indexOf(orgDNStartPrefix)
462: + orgDNStartPrefixLen);
463: } else if (dn.regionMatches(true, 0, ROLE_DN_PREFIX, 0,
464: ROLE_DN_PREFIX_LEN)) {
465: dataType = ROLE_DATA_TYPE;
466: // Skip "cn=\""
467: dn = dn.trim().substring(4);
468: this .dn = dn.substring(0, dn.indexOf(orgDNStartPrefix));
469: } else {
470: dataType = USER_DATA_TYPE;
471: this .dn = dn;
472: }
473: }
474:
475: public void setData(List data) {
476: this .data = data;
477: }
478:
479: public List getData(List data) {
480: return data;
481: }
482:
483: public boolean isNull() {
484: return data == null || dn == null
485: || dataType == INVALID_DATA_TYPE || data.size() <= 0;
486: }
487: }
|