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