View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  
37  package org.exolab.castor.dsml.mozilla;
38  
39  
40  import java.util.Enumeration;
41  import netscape.ldap.LDAPConnection;
42  import netscape.ldap.LDAPEntry;
43  import netscape.ldap.LDAPException;
44  import netscape.ldap.LDAPModification;
45  import netscape.ldap.LDAPModificationSet;
46  import netscape.ldap.LDAPAttribute;
47  import netscape.ldap.LDAPAttributeSet;
48  import org.exolab.castor.dsml.Consumer;
49  import org.exolab.castor.dsml.ImportEventListener;
50  import org.exolab.castor.dsml.Importer;
51  import org.exolab.castor.dsml.ImportDescriptor;
52  import org.exolab.castor.dsml.ImportExportException;
53  
54  
55  /**
56   *
57   *
58   * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
59   * @version $Revision$ $Date: 2004-12-13 14:00:30 -0700 (Mon, 13 Dec 2004) $
60   */
61  public class MozillaImporter extends Importer {
62  
63  
64    private LDAPConnection _conn;
65  
66  
67    public MozillaImporter(LDAPConnection conn) {
68      _conn = conn;
69    }
70  
71  
72    protected Consumer createConsumer() {
73      return new MozillaConsumer();
74    }
75  
76  
77    public void importEntry(LDAPEntry entry, int policy) throws LDAPException {
78      LDAPEntry existing;
79      LDAPModificationSet modifs;
80      LDAPAttributeSet attrSet;
81      LDAPAttribute attr;
82      int i;
83      Enumeration enumeration;
84  
85      if (entry.getAttributeSet() == null || entry.getAttributeSet().size() == 0) {
86  
87        if ((policy & ImportDescriptor.Policy.DELETE_EMPTY) != 0) {
88          try {
89            _conn.read(entry.getDN());
90            _conn.delete(entry.getDN());
91            notify(entry.getDN(), ImportEventListener.DELETED);
92          } catch (LDAPException except) {
93            // Object does not exist, was not removed, ignore.
94            // Anything else, we must complain.
95            if (except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT)
96              throw except;
97            notify(entry.getDN(), ImportEventListener.IGNORED);
98          }
99        } else {
100         notify(entry.getDN(), ImportEventListener.IGNORED);
101       }
102 
103     } else {
104 
105       try {
106         existing = _conn.read(entry.getDN());
107 
108         modifs = new LDAPModificationSet();
109         attrSet = entry.getAttributeSet();
110         for (i = 0; i < attrSet.size(); ++i) {
111           attr = attrSet.elementAt(i);
112           if (existing.getAttributeSet().getAttribute(attr.getName()) != null) {
113             if ((policy & ImportDescriptor.Policy.NEW_ATTRIBUTE_ONLY) == 0) {
114               if (attr.size() > 0) {
115                 modifs.add(LDAPModification.REPLACE, attr);
116               } else {
117                 modifs.add(LDAPModification.DELETE, attr);
118               }
119             }
120           } else {
121             if ((policy & ImportDescriptor.Policy.UPDATE_ONLY) == 0) {
122               if (attr.size() > 0) {
123                 modifs.add(LDAPModification.ADD, attr);
124               }
125             }
126           }
127         }
128         if ((policy & ImportDescriptor.Policy.REPLACE_ATTRIBUTE) != 0) {
129           enumeration = existing.getAttributeSet().getAttributes();
130           while (enumeration.hasMoreElements()) {
131             attr = (LDAPAttribute) enumeration.nextElement();
132             if (entry.getAttribute(attr.getName()) == null) {
133               modifs.add(LDAPModification.DELETE, attr);
134             }
135           }
136         }
137         if (modifs.size() > 0) {
138           _conn.modify(entry.getDN(), modifs);
139           notify(entry.getDN(), ImportEventListener.REFRESHED);
140         } else {
141           notify(entry.getDN(), ImportEventListener.IGNORED);
142         }
143       } catch (LDAPException except) {
144         // Object does not exist, we create a new one.
145         // Anything else, we must complain.
146         if (except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT)
147           throw except;
148         if ((policy & ImportDescriptor.Policy.REFRESH_ONLY) == 0) {
149           _conn.add(entry);
150           notify(entry.getDN(), ImportEventListener.CREATED);
151         } else {
152           notify(entry.getDN(), ImportEventListener.IGNORED);
153         }
154       }
155     }
156   }
157 
158 
159   public void importEntries(Enumeration entries) throws ImportExportException {
160     LDAPEntry entry;
161 
162     if (getImportDescriptor() == null)
163       setImportDescriptor(new ImportDescriptor());
164     try {
165       while (entries.hasMoreElements()) {
166         entry = (LDAPEntry) entries.nextElement();
167         importEntry(entry, getImportDescriptor().getPolicy(entry.getDN()));
168       }
169     } catch (LDAPException except) {
170       throw new ImportExportException(except);
171     }
172   }
173 
174 
175 }