001: /**
002: * $Id: LDAPUtil.java,v 1.2 2005/11/11 15:55:31 rc135440 Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.util;
014:
015: import java.io.IOException;
016:
017: import netscape.ldap.LDAPAttributeSet;
018: import netscape.ldap.LDAPConnection;
019: import netscape.ldap.LDAPEntry;
020: import netscape.ldap.LDAPException;
021: import netscape.ldap.LDAPModification;
022: import netscape.ldap.LDAPSchema;
023: import netscape.ldap.factory.JSSESocketFactory;
024: import netscape.ldap.util.LDIF;
025: import netscape.ldap.util.LDIFAddContent;
026: import netscape.ldap.util.LDIFAttributeContent;
027: import netscape.ldap.util.LDIFContent;
028: import netscape.ldap.util.LDIFModDNContent;
029: import netscape.ldap.util.LDIFModifyContent;
030: import netscape.ldap.util.LDIFRecord;
031:
032: /**
033: * This class provides LDAP utility methods for Portal Admin Server
034: * clients and MBeans.
035: */
036: public class LDAPUtil {
037:
038: public static LDAPConnection getLDAPConnection(String host,
039: String port, String principal, String credentials,
040: boolean secure) {
041: int portInt = 0;
042: LDAPConnection connection = null;
043:
044: try {
045: portInt = Integer.parseInt(port);
046: if (secure) {
047: connection = new LDAPConnection(new JSSESocketFactory(
048: null));
049: } else {
050: connection = new LDAPConnection();
051: }
052: connection.getSearchConstraints().setReferrals(true);
053: connection.connect(host, portInt, principal, credentials);
054: } catch (NumberFormatException e) {
055: // Ignore it.
056: } catch (LDAPException e) {
057: // Ignore it.
058: }
059:
060: return connection;
061: }
062:
063: /**
064: * Loads the given ldif file into the LDAP server with the given
065: * connection.
066: *
067: * @param ldifFile the ldif file to be loaded.
068: * @param connection the connection of the LDAP server.
069: * @exception IOException if an I/O error occurs.
070: * @exception LDAPException if an opertion cannot be performed on
071: * the LDAP server.
072: */
073: public static void loadLDIF(String ldifFile,
074: LDAPConnection connection) throws IOException,
075: LDAPException {
076:
077: LDIF ldif = new LDIF(ldifFile);
078: LDIFRecord nextRecord = ldif.nextRecord();
079:
080: while (nextRecord != null) {
081: String dn = nextRecord.getDN();
082: LDIFContent content = nextRecord.getContent();
083: LDAPAttributeSet attrs = null;
084:
085: switch (content.getType()) {
086: case LDIFContent.ATTRIBUTE_CONTENT:
087: LDIFAttributeContent attributeContent = (LDIFAttributeContent) content;
088:
089: attrs = new LDAPAttributeSet(attributeContent
090: .getAttributes());
091: connection.add(new LDAPEntry(dn, attrs));
092: break;
093: case LDIFContent.ADD_CONTENT:
094: LDIFAddContent addContent = (LDIFAddContent) content;
095: attrs = new LDAPAttributeSet(addContent.getAttributes());
096: connection.add(new LDAPEntry(dn, attrs));
097: break;
098: case LDIFContent.DELETE_CONTENT:
099: connection.delete(dn);
100: break;
101: case LDIFContent.MODIFICATION_CONTENT:
102: LDAPModification[] modifications = ((LDIFModifyContent) content)
103: .getModifications();
104:
105: connection.modify(dn, modifications);
106: break;
107: case LDIFContent.MODDN_CONTENT:
108: LDIFModDNContent modDNContent = (LDIFModDNContent) content;
109: String rdn = modDNContent.getRDN();
110: String newParent = modDNContent.getNewParent();
111: boolean deleteOldRDN = modDNContent.getDeleteOldRDN();
112: connection.rename(dn, rdn, newParent, deleteOldRDN);
113: break;
114: }
115:
116: nextRecord = ldif.nextRecord();
117: }
118: }
119:
120: /**
121: * Checks if the given object class exists in the LDAP server with
122: * the given connection.
123: *
124: * @param objectClass the object class to be checked for existence.
125: * @param connection the connection to the LDAP server.
126: * @return <code>true</code> if the given object class exists in
127: * the LDAP server; <code>false</code> otherwise.
128: * @exception LDAPException if fetching the schema from the LDAP
129: * server fails.
130: */
131: public static boolean objectClassExists(String objectClass,
132: LDAPConnection connection) throws LDAPException {
133:
134: LDAPSchema schema = new LDAPSchema();
135: schema.fetchSchema(connection);
136: return schema.getObjectClass(objectClass) != null;
137: }
138: }
|