001: package migration_is60.modules.srap.netfile;
002:
003: /*
004: * NetfileMigrate.java
005: *
006: * Created on July 2, 2003, 4:04 PM
007: */
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 mm132998
020: * @version
021: */
022: public class NetfileMigrate {
023:
024: public static void main(String args[]) {
025:
026: if (args.length != 2) {
027: System.out
028: .println("Usage : <input ldiff file> , <output xml file>");
029: System.exit(0);
030: }
031: NetfileMigrate mig = new NetfileMigrate(args[0], args[1]);
032: mig.migrate();
033: System.exit(0);
034: }
035:
036: private String inputFile;
037: private String outputFile;
038:
039: public NetfileMigrate(String inputFile, String outputFile) {
040: this .inputFile = inputFile;
041: this .outputFile = outputFile;
042: }
043:
044: /*
045: * Steps to be done :
046: * 1) Read an entry block.
047: * 2) Get the org name.
048: * 3) Get the attributes.
049: * 4) Construct the corresponding xml snippet.
050: * 5) Repeat from 1 until done
051: */
052: void migrate() {
053: NetfileOrgData data;
054: FileWriter outFile;
055: int count = 0;
056: LDIF l1;
057:
058: try {
059: l1 = new LDIF(inputFile);
060: outFile = new FileWriter(outputFile);
061: LDIFRecord tmp = l1.nextRecord();
062: printHeader(outFile);
063: while (tmp != null) {
064: System.out.println("\n # Entry id:" + count);
065: count++;
066: data = ConvertRecord(tmp);
067: try {
068: OutputRecord(outFile, data);
069: } catch (Exception e) {
070: System.out.println("Error writing to output file:"
071: + outputFile);
072: e.printStackTrace();
073: }
074: tmp = l1.nextRecord();
075: }
076: outFile.write("\n</Requests>");
077: outFile.close();
078: System.out.println("Processed " + count + " entries");
079: System.out
080: .println("Output available in file " + outputFile);
081: } catch (IOException e) {
082: System.out.println("Error:" + e.toString());
083: e.printStackTrace();
084: }
085: }
086:
087: NetfileOrgData ConvertRecord(LDIFRecord toConvert) {
088: LDIFAttributeContent con;
089: LDAPAttributeSet theAttrSet;
090: String[] allAttrs;
091: String attrName;
092: String org;
093:
094: LDAPAttribute[] attrList;
095:
096: org = getOrgName(toConvert.getDN());
097:
098: con = (LDIFAttributeContent) toConvert.getContent();
099: attrList = con.getAttributes();
100: theAttrSet = new LDAPAttributeSet(attrList);
101:
102: NetfileOrgData data = new NetfileOrgData(org);
103:
104: for (int i = 0; i < attrList.length; ++i) {
105: attrName = attrList[i].getName();
106: allAttrs = (theAttrSet.getAttribute(attrList[i].getName()))
107: .getStringValueArray();
108: data.handleAttribute(attrName, allAttrs);
109: }
110: return data;
111: }
112:
113: static final String SERVICE_NAME_DELIM = "ou=srapNetFileService,ou=services,";
114: static final int SERVICE_NAME_DELIM_LEN = "ou=srapNetFileService,ou=services,"
115: .length();
116:
117: static String getOrgName(String dn) {
118: int indx = dn.indexOf(SERVICE_NAME_DELIM);
119:
120: if (indx == -1) {
121: System.out.println("Inavlid org name : " + dn);
122: throw new RuntimeException("Inavlid org name : " + dn);
123: }
124:
125: return dn.substring(indx + SERVICE_NAME_DELIM_LEN);
126: }
127:
128: static void printHeader(FileWriter outFile) throws IOException {
129:
130: ResourceBundle ambundle;
131: ambundle = ResourceBundle.getBundle("AMConfig");
132: String IDSAMEBaseDir = new String();
133:
134: if (ambundle.getObject("com.iplanet.am.installdir") != null) {
135: IDSAMEBaseDir = (String) (ambundle
136: .getObject("com.iplanet.am.installdir"));
137: IDSAMEBaseDir = IDSAMEBaseDir.substring(0, IDSAMEBaseDir
138: .indexOf("SUNWam"));
139: }
140: outFile
141: .write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
142: outFile
143: .write("\n<!-- PROPRIETARY/CONFIDENTIAL/ Use of this product is subject");
144: outFile
145: .write("\n to license terms. Copyright 2001 Sun Microsystems Inc.");
146: outFile
147: .write("Some preexisting portions Copyright 2001 Netscape");
148: outFile.write("Communications Corp. All rights reserved. -->");
149: outFile
150: .write("\n<!DOCTYPE Requests PUBLIC \"-//iPlanet//iDSAME 5.0 Admin CLI DTD//EN \" ");
151: outFile.write("\"file:" + IDSAMEBaseDir
152: + "SUNWam/dtd/amAdmin.dtd\">");
153:
154: outFile.write("\n<Requests>");
155: return;
156: }
157:
158: void OutputRecord(FileWriter outFile, NetfileOrgData data)
159: throws IOException {
160: if (data.isNull()) {
161: return;
162: }
163: if (data.tempDir != null) {
164: outFile.write("\n<OrganizationRequests DN=\"" + data.org
165: + "\">");
166: outFile
167: .write("\n <ModifyServiceTemplate serviceName=\"srapNetFileService\" schemaType=\"Dynamic\">");
168: outFile.write("\n <AttributeValuePair>");
169: outFile
170: .write("\n <Attribute name=\"sunPortalNetFileTempDir\"/>");
171: outFile.write("\n <Value>" + data.tempDir);
172: outFile.write("</Value>");
173: outFile.write("\n </AttributeValuePair>");
174: outFile.write("\n </ModifyServiceTemplate>");
175: outFile.write("\n</OrganizationRequests>");
176: }
177: if (data.osCharSet != null) {
178: outFile.write("\n<OrganizationRequests DN=\"" + data.org
179: + "\">");
180: outFile
181: .write("\n <ModifyServiceTemplate serviceName=\"srapNetFileService\" schemaType=\"Dynamic\">");
182: outFile.write("\n <AttributeValuePair>");
183: outFile
184: .write("\n <Attribute name=\"sunPortalNetFileOSCharSet\"/>");
185: outFile.write("\n <Value>" + data.osCharSet);
186: outFile.write("</Value>");
187: outFile.write("\n </AttributeValuePair>");
188: outFile.write("\n </ModifyServiceTemplate>");
189: outFile.write("\n</OrganizationRequests>");
190: }
191: if (data.mimetypesConfigFileLocation != null) {
192: outFile.write("\n<OrganizationRequests DN=\"" + data.org
193: + "\">");
194: outFile
195: .write("\n <ModifyServiceTemplate serviceName=\"srapNetFileService\" schemaType=\"Dynamic\">");
196: outFile.write("\n <AttributeValuePair>");
197: outFile
198: .write("\n <Attribute name=\"sunPortalNetFileMimetypesConfigFileLocation\"/>");
199: outFile.write("\n <Value>"
200: + data.mimetypesConfigFileLocation);
201: outFile.write("</Value>");
202: outFile.write("\n </AttributeValuePair>");
203: outFile.write("\n </ModifyServiceTemplate>");
204: outFile.write("\n</OrganizationRequests>");
205: }
206: }
207: }
208:
209: class NetfileOrgData {
210:
211: String tempDir;
212: String osCharSet;
213: String mimetypesConfigFileLocation;
214: String org;
215:
216: private static final String TEMP_DIR_ATTR = "sunPortalNetFileTempDir";
217: private static final String OS_CHARSET_ATTR = "sunPortalNetFileOSCharSet";
218: private static final String MIME_TYPE_CONFIG_FILE_LOC_ATTR = "sunPortalNetFileMimetypesConfigFileLocation";
219:
220: private static final int TEMP_DIR_ATTR_LEN;
221: private static final int OS_CHARSET_ATTR_LEN;
222: private static final int MIME_TYPE_CONFIG_FILE_LOC_ATTR_LEN;
223:
224: static {
225: TEMP_DIR_ATTR_LEN = TEMP_DIR_ATTR.length();
226: OS_CHARSET_ATTR_LEN = OS_CHARSET_ATTR.length();
227: MIME_TYPE_CONFIG_FILE_LOC_ATTR_LEN = MIME_TYPE_CONFIG_FILE_LOC_ATTR
228: .length();
229: }
230:
231: NetfileOrgData(String org) {
232: tempDir = null;
233: osCharSet = null;
234: mimetypesConfigFileLocation = null;
235: this .org = org;
236: }
237:
238: public void handleAttribute(String attrName, String[] attribute) {
239: if (attrName.equalsIgnoreCase("iplanetkeyvalue")
240: || attrName.equalsIgnoreCase("sunkeyvalue")) {
241: int count;
242:
243: for (count = 0; count < attribute.length; count++) {
244: String nameValue = attribute[count].trim();
245: if (nameValue.regionMatches(true, 0, TEMP_DIR_ATTR, 0,
246: TEMP_DIR_ATTR_LEN)) {
247: tempDir = nameValue.substring(
248: nameValue.indexOf('=') + 1).trim();
249: } else if (nameValue.regionMatches(true, 0,
250: OS_CHARSET_ATTR, 0, OS_CHARSET_ATTR_LEN)) {
251: osCharSet = nameValue.substring(
252: nameValue.indexOf('=') + 1).trim();
253: } else if (nameValue.regionMatches(true, 0,
254: MIME_TYPE_CONFIG_FILE_LOC_ATTR, 0,
255: MIME_TYPE_CONFIG_FILE_LOC_ATTR_LEN)) {
256: mimetypesConfigFileLocation = nameValue.substring(
257: nameValue.indexOf('=') + 1).trim();
258: }
259: }
260: }
261: }
262:
263: public boolean isNull() {
264: return tempDir == null && osCharSet == null
265: && mimetypesConfigFileLocation == null;
266: }
267: }
|