001: package migration.modules.ldap;
002:
003: import java.util.*;
004: import java.io.*;
005:
006: import netscape.ldap.LDAPAttribute;
007: import netscape.ldap.LDAPAttributeSet;
008: import netscape.ldap.util.LDIF;
009: import netscape.ldap.util.LDIFRecord;
010: import netscape.ldap.util.LDIFAttributeContent;
011:
012: public class Common {
013: public static String rootsuffix = "";
014: public static String orgNaming = "o=";
015: public static String roleNaming = "cn=";
016: public static String uidNaming = "uid=";
017: public static String fs = System.getProperty("file.separator");
018: public static String IDSAMEBaseDir = fs + "opt";
019: public static String hostname = "";
020:
021: public static final String cBasicOrgFile = "BasicOrganization.ldif";
022: public static final String cBasicUserFile = "BasicUser.ldif";
023: public static final String cBasicGroupFile = "BasicGroup.ldif";
024: public static final String cLdif = "ldif";
025: public static final String cAMConfigFileName = "AMConfig";
026:
027: /*
028: * Set Organization Naming Attribute
029: *
030: * @param attr representing the organization naming attribute, i.e. o=
031: */
032: public static void setOrgNaming(String attr) {
033: orgNaming = attr;
034: }
035:
036: /*
037: * Set Role Naming Attribute
038: *
039: * @param attr representing the role naming attribute, i.e. cn=
040: */
041: public static void setRoleNaming(String attr) {
042: roleNaming = attr;
043: }
044:
045: /*
046: * Set User Id Naming Attribute
047: *
048: * @param attr representing the uid naming attribute, i.e. uid=
049: */
050: public static void setUidNaming(String attr) {
051: uidNaming = attr;
052: }
053:
054: /*
055: * Construct Role DN with naming attributes
056: *
057: * @param role representing role
058: * @param org representing org
059: * @return String representing dn value
060: */
061: public static String constructRoleDN(String role, String org) {
062: return constructRoleDN(role, org, null, false);
063: }
064:
065: /*
066: * Construct Role DN with naming attributes
067: *
068: * @param role representing role
069: * @param org representing org
070: * @param rootSuffix representing the root suffix
071: * @return String representing dn value
072: */
073: public static String constructRoleDN(String role, String org,
074: String rootSuffix) {
075: return constructRoleDN(role, org, rootSuffix, false);
076: }
077:
078: /*
079: * Construct Role DN with naming attributes
080: *
081: * @param role representing role
082: * @param org representing org
083: * @param rootSuffix representing the root suffix
084: * @param noOrgNaming if true do not attach organization naming
085: * attribute to org value
086: * @return String representing dn value
087: */
088: public static String constructRoleDN(String role, String org,
089: String rootSuffix, boolean noOrgNaming) {
090:
091: String dn = roleNaming + role;
092:
093: if (noOrgNaming) {
094: dn += "," + org;
095: } else {
096: dn += "," + constructOrgDN(org);
097: }
098:
099: if (rootSuffix != null) {
100: dn += "," + rootSuffix;
101: }
102:
103: return dn;
104: }
105:
106: /*
107: * Construct Org DN with naming attributes
108: *
109: * @param org representing org
110: * @param rootSuffix representing the rootSuffix
111: * @return String representing dn value
112: */
113: public static String constructOrgDN(String org) {
114: return constructOrgDN(org, null);
115: }
116:
117: /*
118: * Construct Org DN with naming attributes
119: *
120: * @param org representing org
121: * @param rootSuffix representing the root suffix
122: * @return String representing dn value
123: */
124: public static String constructOrgDN(String org, String rootSuffix) {
125: String dn = orgNaming + org;
126:
127: if (rootSuffix != null) {
128: dn += "," + rootSuffix;
129: }
130:
131: return dn;
132: }
133:
134: /*
135: * Responsible for getting and setting the Naming Atributes
136: *
137: * @param importDir String representing the importDir
138: */
139: public static void getNamingAttributes(String importDir) {
140:
141: orgNaming = getNamingAttribute(importDir + fs + cLdif + fs
142: + cBasicOrgFile, orgNaming);
143: uidNaming = getNamingAttribute(importDir + fs + cLdif + fs
144: + cBasicUserFile, uidNaming);
145: roleNaming = getNamingAttribute(importDir + fs + cLdif + fs
146: + cBasicGroupFile, roleNaming);
147:
148: }
149:
150: /*
151: * Responsible for getting the Naming Attribute from the ldif record
152: *
153: * @param namingFile String representing the naming file location (ldif record)
154: * @param def String representing the default naming attribute if not found
155: * @return String representing the naming attribute
156: */
157: public static String getNamingAttribute(String namingFile,
158: String def) {
159: LDIFRecord lrecord = null;
160: LDIF ldif = null;
161: String naming = null;
162:
163: try {
164: ldif = new LDIF(namingFile);
165:
166: while ((lrecord = ldif.nextRecord()) != null) {
167: LDIFAttributeContent lattrContent = (LDIFAttributeContent) lrecord
168: .getContent();
169: LDAPAttributeSet lattrSet = new LDAPAttributeSet(
170: lattrContent.getAttributes());
171:
172: if (lattrSet != null) {
173: LDAPAttribute lattr = lattrSet
174: .getAttribute("sunkeyvalue");
175:
176: if (lattr != null) {
177: String[] keys = lattr.getStringValueArray();
178:
179: for (int i = 0; i < keys.length; i++) {
180: String key = keys[i];
181:
182: if (key.indexOf("namingattribute") != -1) {
183: StringTokenizer tokenizer = new StringTokenizer(
184: key, "=");
185:
186: if (tokenizer.countTokens() >= 2) {
187: naming = tokenizer.nextToken();
188: naming = tokenizer.nextToken()
189: + "=";
190: }
191:
192: }
193: }
194:
195: }
196: }
197: }
198:
199: } catch (IOException e) {
200: System.out.println("Error:" + e.toString());
201: e.printStackTrace();
202: }
203:
204: if (naming == null) {
205: naming = def;
206: }
207:
208: return naming;
209: }
210:
211: /*
212: * Responsible for getting properties within AMConfig.properties
213: *
214: */
215: public static void getAMConfigProperties() {
216:
217: Locale locale = Locale.getDefault();
218: ResourceBundle ambundle = ResourceBundle
219: .getBundle(cAMConfigFileName);
220:
221: if (ambundle.getObject("com.iplanet.am.rootsuffix") != null) {
222: rootsuffix = (String) (ambundle
223: .getObject("com.iplanet.am.rootsuffix"));
224: }
225:
226: if (ambundle.getObject("com.iplanet.am.installdir") != null) {
227: IDSAMEBaseDir = (String) (ambundle
228: .getObject("com.iplanet.am.installdir"));
229: }
230:
231: if (ambundle.getObject("com.iplanet.am.directory.host") != null) {
232: hostname = (String) (ambundle
233: .getObject("com.iplanet.am.directory.host"));
234: }
235:
236: }
237:
238: }
|