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  package org.exolab.castor.dsml.jndi;
47  
48  import java.util.Vector;
49  import java.util.Enumeration;
50  import javax.naming.NamingException;
51  import javax.naming.NameNotFoundException;
52  import javax.naming.NamingEnumeration;
53  import javax.naming.directory.Attribute;
54  import javax.naming.directory.Attributes;
55  import javax.naming.directory.ModificationItem;
56  import javax.naming.directory.DirContext;
57  import javax.naming.directory.SearchResult;
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   * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
66   * @version $Revision$ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
67   */
68  public class JNDIImporter extends Importer {
69      private DirContext _ctx;
70  
71      public JNDIImporter(final DirContext ctx) {
72          _ctx = ctx;
73      }
74  
75      protected Consumer createConsumer() {
76          return new JNDIConsumer();
77      }
78  
79      public void importEntry(final SearchResult result, final int policy) throws NamingException {
80          Attributes existing;
81          Attributes attrSet;
82          Attribute attr;
83          NamingEnumeration<? extends Attribute> enumeration;
84  
85          if (result.getAttributes().size() == 0) {
86              if ((policy & ImportDescriptor.Policy.DELETE_EMPTY) != 0) {
87                  try {
88                      _ctx.lookup(result.getName());
89                      _ctx.unbind(result.getName());
90                      notify(result.getName(), ImportEventListener.DELETED);
91                  } catch (NameNotFoundException except) {
92                      // Object does not exist, was not removed, ignore.
93                      notify(result.getName(), ImportEventListener.IGNORED);
94                  }
95              } else {
96                  notify(result.getName(), ImportEventListener.IGNORED);
97              }
98          } else {
99              try {
100                 existing = _ctx.getAttributes(result.getName());
101 
102                 Vector<ModificationItem> modifs = new Vector<ModificationItem>();
103                 attrSet = result.getAttributes();
104                 enumeration = attrSet.getAll();
105                 while (enumeration.hasMore()) {
106                     attr = enumeration.next();
107                     if (existing.get(attr.getID()) != null) {
108                         if ((policy & ImportDescriptor.Policy.NEW_ATTRIBUTE_ONLY) == 0) {
109                             if (attr.size() > 0) {
110                                 modifs.addElement(new ModificationItem(
111                                         DirContext.REPLACE_ATTRIBUTE, attr));
112                             } else {
113                                 modifs.addElement(new ModificationItem(
114                                         DirContext.REMOVE_ATTRIBUTE, attr));
115                             }
116                         }
117                     } else {
118                         if ((policy & ImportDescriptor.Policy.UPDATE_ONLY) == 0) {
119                             if (attr.size() > 0) {
120                                 modifs.addElement(new ModificationItem(
121                                         DirContext.ADD_ATTRIBUTE, attr));
122                             }
123                         }
124                     }
125                 }
126                 if ((policy & ImportDescriptor.Policy.REPLACE_ATTRIBUTE) != 0) {
127                     enumeration = existing.getAll();
128                     while (enumeration.hasMore()) {
129                         attr = enumeration.next();
130                         if (attrSet.get(attr.getID()) == null) {
131                             modifs.addElement(new ModificationItem(
132                                     DirContext.REMOVE_ATTRIBUTE, attr));
133                         }
134                     }
135                 }
136                 if (modifs.size() > 0) {
137                     ModificationItem[] array;
138 
139                     array = new ModificationItem[modifs.size()];
140                     modifs.copyInto(array);
141                     _ctx.modifyAttributes(result.getName(), array);
142                     notify(result.getName(), ImportEventListener.REFRESHED);
143                 } else {
144                     notify(result.getName(), ImportEventListener.IGNORED);
145                 }
146             } catch (NameNotFoundException except) {
147                 // Object does not exist, we create a new one.
148                 if ((policy & ImportDescriptor.Policy.REFRESH_ONLY) == 0) {
149                     _ctx.bind(result.getName(), null, result.getAttributes());
150                     notify(result.getName(), ImportEventListener.CREATED);
151                 } else {
152                     notify(result.getName(), ImportEventListener.IGNORED);
153                 }
154             }
155         }
156     }
157 
158     public void importEntries(final NamingEnumeration<SearchResult> results)
159     throws NamingException {
160         if (getImportDescriptor() == null) {
161             setImportDescriptor(new ImportDescriptor());
162         }
163         while (results.hasMore()) {
164             SearchResult result = results.next();
165             importEntry(result, getImportDescriptor().getPolicy(result.getName()));
166         }
167     }
168 
169     public void importEntries(final Enumeration<SearchResult> results)
170     throws ImportExportException {
171         if (getImportDescriptor() == null) {
172             setImportDescriptor(new ImportDescriptor());
173         }
174         try {
175             while (results.hasMoreElements()) {
176                 SearchResult result = results.nextElement();
177                 importEntry(result, getImportDescriptor().getPolicy(result.getName()));
178             }
179         } catch (NamingException except) {
180             throw new ImportExportException(except);
181         }
182     }
183 }