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