001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: MozillaImporter.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.dsml.mozilla;
045:
046: import java.util.Enumeration;
047: import netscape.ldap.LDAPConnection;
048: import netscape.ldap.LDAPEntry;
049: import netscape.ldap.LDAPException;
050: import netscape.ldap.LDAPModification;
051: import netscape.ldap.LDAPModificationSet;
052: import netscape.ldap.LDAPAttribute;
053: import netscape.ldap.LDAPAttributeSet;
054: import org.exolab.castor.dsml.Consumer;
055: import org.exolab.castor.dsml.ImportEventListener;
056: import org.exolab.castor.dsml.Importer;
057: import org.exolab.castor.dsml.ImportDescriptor;
058: import org.exolab.castor.dsml.ImportExportException;
059:
060: /**
061: *
062: *
063: * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
064: * @version $Revision: 5951 $ $Date: 2004-12-13 14:00:30 -0700 (Mon, 13 Dec 2004) $
065: */
066: public class MozillaImporter extends Importer {
067:
068: private LDAPConnection _conn;
069:
070: public MozillaImporter(LDAPConnection conn) {
071: _conn = conn;
072: }
073:
074: protected Consumer createConsumer() {
075: return new MozillaConsumer();
076: }
077:
078: public void importEntry(LDAPEntry entry, int policy)
079: throws LDAPException {
080: LDAPEntry existing;
081: LDAPModificationSet modifs;
082: LDAPAttributeSet attrSet;
083: LDAPAttribute attr;
084: int i;
085: Enumeration enumeration;
086:
087: if (entry.getAttributeSet() == null
088: || entry.getAttributeSet().size() == 0) {
089:
090: if ((policy & ImportDescriptor.Policy.DeleteEmpty) != 0) {
091: try {
092: _conn.read(entry.getDN());
093: _conn.delete(entry.getDN());
094: notify(entry.getDN(), ImportEventListener.Deleted);
095: } catch (LDAPException except) {
096: // Object does not exist, was not removed, ignore.
097: // Anything else, we must complain.
098: if (except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT)
099: throw except;
100: notify(entry.getDN(), ImportEventListener.Ignored);
101: }
102: } else {
103: notify(entry.getDN(), ImportEventListener.Ignored);
104: }
105:
106: } else {
107:
108: try {
109: existing = _conn.read(entry.getDN());
110:
111: modifs = new LDAPModificationSet();
112: attrSet = entry.getAttributeSet();
113: for (i = 0; i < attrSet.size(); ++i) {
114: attr = attrSet.elementAt(i);
115: if (existing.getAttributeSet().getAttribute(
116: attr.getName()) != null) {
117: if ((policy & ImportDescriptor.Policy.NewAttrOnly) == 0) {
118: if (attr.size() > 0) {
119: modifs.add(LDAPModification.REPLACE,
120: attr);
121: } else {
122: modifs.add(LDAPModification.DELETE,
123: attr);
124: }
125: }
126: } else {
127: if ((policy & ImportDescriptor.Policy.UpdateOnly) == 0) {
128: if (attr.size() > 0) {
129: modifs.add(LDAPModification.ADD, attr);
130: }
131: }
132: }
133: }
134: if ((policy & ImportDescriptor.Policy.ReplaceAttr) != 0) {
135: enumeration = existing.getAttributeSet()
136: .getAttributes();
137: while (enumeration.hasMoreElements()) {
138: attr = (LDAPAttribute) enumeration
139: .nextElement();
140: if (entry.getAttribute(attr.getName()) == null) {
141: modifs.add(LDAPModification.DELETE, attr);
142: }
143: }
144: }
145: if (modifs.size() > 0) {
146: _conn.modify(entry.getDN(), modifs);
147: notify(entry.getDN(), ImportEventListener.Refreshed);
148: } else {
149: notify(entry.getDN(), ImportEventListener.Ignored);
150: }
151: } catch (LDAPException except) {
152: // Object does not exist, we create a new one.
153: // Anything else, we must complain.
154: if (except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT)
155: throw except;
156: if ((policy & ImportDescriptor.Policy.RefreshOnly) == 0) {
157: _conn.add(entry);
158: notify(entry.getDN(), ImportEventListener.Created);
159: } else {
160: notify(entry.getDN(), ImportEventListener.Ignored);
161: }
162: }
163: }
164: }
165:
166: public void importEntries(Enumeration entries)
167: throws ImportExportException {
168: LDAPEntry entry;
169:
170: if (getImportDescriptor() == null)
171: setImportDescriptor(new ImportDescriptor());
172: try {
173: while (entries.hasMoreElements()) {
174: entry = (LDAPEntry) entries.nextElement();
175: importEntry(entry, getImportDescriptor().getPolicy(
176: entry.getDN()));
177: }
178: } catch (LDAPException except) {
179: throw new ImportExportException(except);
180: }
181: }
182:
183: }
|