KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > naming > LDAPEntryManager


1 /*
2 * Copyright 2005 The Apache Software Foundation or its licensors,
3 * as applicable.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */

17 package org.apache.cocoon.components.naming;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NamingEnumeration JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.naming.directory.Attribute JavaDoc;
29 import javax.naming.directory.Attributes JavaDoc;
30 import javax.naming.directory.BasicAttribute JavaDoc;
31 import javax.naming.directory.BasicAttributes JavaDoc;
32 import javax.naming.directory.DirContext JavaDoc;
33 import javax.naming.directory.InitialDirContext JavaDoc;
34 import javax.naming.directory.SearchResult JavaDoc;
35
36 import org.apache.avalon.excalibur.pool.Recyclable;
37 import org.apache.avalon.framework.activity.Disposable;
38 import org.apache.avalon.framework.logger.AbstractLogEnabled;
39 import org.apache.avalon.framework.parameters.ParameterException;
40 import org.apache.avalon.framework.parameters.Parameterizable;
41 import org.apache.avalon.framework.parameters.Parameters;
42 import org.apache.cocoon.ProcessingException;
43
44 /**
45  * The <code>LDAPEntryManager</code> is an Avalon Component for managing Entries in a Javax Naming Directory.
46  * This is the LDAP implementation of the {@link org.apache.cocoon.components.naming.EntryManager EntryManager} interface.
47  * This is designed to be used from FlowScript, it uses Maps instead of NamingEnumerations and Attributes.
48  * @author Jeremy Quinn <a HREF="http://apache.org/~jeremy">http://apache.org/~jeremy</a>.
49  *
50  * Example configuration (goes in cocoon.xconf)
51  * <pre><tt>
52  * &lt;component role="org.apache.cocoon.component.EntryManager" class="org.apache.cocoon.components.naming.LDAPEntryManager" logger="flow.ldap"&gt;
53  * &lt;parameter name="ldap-host" value="hostname:port"/&gt;
54  * &lt;parameter name="ldap-base" value="dc=example,dc=com"/&gt;
55  * &lt;parameter name="ldap-user" value="username"/&gt;
56  * &lt;parameter name="ldap-pass" value="password"/&gt;
57  * &lt;/component&gt;
58  * </tt></pre></p>
59  */

60
61 public class LDAPEntryManager
62     extends AbstractLogEnabled
63     implements EntryManager, Parameterizable, Disposable, Recyclable {
64
65     /* congiguration parameter names */
66     protected final static String JavaDoc LDAP_HOST_PARAM = "ldap-host";
67     protected final static String JavaDoc LDAP_USER_PARAM = "ldap-user";
68     protected final static String JavaDoc LDAP_PASS_PARAM = "ldap-pass";
69     protected final static String JavaDoc LDAP_BASE_PARAM = "ldap-base";
70     
71     /* internal state */
72     private boolean disposed = false;
73     private boolean recycled = false;
74     
75     /* internal instance variables */
76     protected DirContext JavaDoc context = null;
77     protected Hashtable JavaDoc environment = null;
78
79     /* default constructor */
80     public LDAPEntryManager() {
81     }
82
83     /** Avalon, Parameterize this Class */
84     public void parameterize(Parameters params) throws ParameterException {
85         if (getLogger ().isDebugEnabled ()) {
86             getLogger ().debug ("LDAPEntryManager parameterizing");
87         }
88         if (this.environment == null) {
89             String JavaDoc host = params.getParameter (LDAP_HOST_PARAM);
90             if (getLogger ().isDebugEnabled ()) {
91                 getLogger ().debug ("LDAP using host: " + host);
92             }
93             String JavaDoc base = params.getParameter (LDAP_BASE_PARAM);
94             if (getLogger ().isDebugEnabled ()) {
95                 getLogger ().debug ("LDAP using base: " + base);
96             }
97             String JavaDoc user = params.getParameter (LDAP_USER_PARAM);
98             if (getLogger ().isDebugEnabled ()) {
99                 getLogger ().debug ("LDAP using user: " + user);
100             }
101             String JavaDoc pass = params.getParameter (LDAP_PASS_PARAM);
102             if (getLogger ().isDebugEnabled ()) {
103                 getLogger ().debug ("LDAP using pass: " + pass);
104             }
105             this.environment = new Hashtable JavaDoc ();
106             this.environment.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
107             this.environment.put (Context.PROVIDER_URL, host + "/" + base);
108             if (user != null) {
109                 this.environment.put (Context.SECURITY_AUTHENTICATION, "simple");
110                 this.environment.put (Context.SECURITY_PRINCIPAL, user);
111                 this.environment.put (Context.SECURITY_CREDENTIALS, pass);
112             }
113         }
114     }
115
116     /* Avalon, Recycle this Class */
117     public final void recycle() {
118         if (getLogger ().isDebugEnabled ()) {
119             getLogger ().debug ("LDAPEntryManager recycling");
120         }
121         try {
122             this.context.close();
123         } catch ( Exception JavaDoc e) {
124             getLogger().error ("LDAPEntryManager.recycle() :" + e.getMessage());
125         } finally {
126             this.context = null;
127         }
128         this.recycled = true;
129     }
130
131     /* Avalon, Dispose of this Class */
132     public final void dispose() {
133         if (getLogger ().isDebugEnabled ()) {
134             getLogger ().debug ("LDAPEntryManager disposing");
135         }
136         try {
137             if (context != null) {
138                 this.context.close();
139             }
140         } catch ( Exception JavaDoc e) {
141             getLogger().error ("LDAPEntryManager.recycle() :" + e.getMessage());
142         } finally {
143             this.context = null;
144             this.environment = null;
145             this.disposed = true;
146         }
147     }
148
149     /* lazy initialise this class */
150     protected void initialize() throws Exception JavaDoc {
151         if (getLogger ().isDebugEnabled ()) {
152             getLogger ().debug ("LDAPEntryManager initialising");
153         }
154         if (null == this.environment) {
155             throw new IllegalStateException JavaDoc("LDAPEntryManager.initialize() : Not Configured");
156         }
157         if (this.disposed) {
158             throw new IllegalStateException JavaDoc("LDAPEntryManager.initialize() : Already disposed");
159         }
160         try {
161             this.context = new InitialDirContext JavaDoc (this.environment);
162             if (getLogger ().isDebugEnabled ()) {
163                 getLogger ().debug ("LDAPEntryManager new context: " + this.context.getNameInNamespace ());
164             }
165         } catch ( Exception JavaDoc e) {
166             getLogger().error ("LDAPEntryManager.initialize()" + e.getMessage());
167             return;
168         }
169     }
170
171     /**
172      * Creates a new Entry
173      *
174      * @param name The name of the Entry to create
175      * @param attributes The Map of Attributes to create it with
176      */

177     public void create(String JavaDoc name, Map JavaDoc attributes) throws ProcessingException {
178         Context JavaDoc newContext = null;
179         try {
180             if (this.context == null) initialize ();
181             if (getLogger ().isDebugEnabled ()) {
182                 getLogger ().debug ("LDAPEntryManager creating new Context: " + name);
183             }
184             newContext = context.createSubcontext (name, map2Attributes (attributes));
185         } catch (Exception JavaDoc e) {
186             getLogger ().error ("LDAPEntryManager.create() :" + e.getMessage());
187             throw new ProcessingException (e);
188         } finally {
189             try {
190                 if (newContext != null) newContext.close ();
191             } catch (NamingException JavaDoc ne) {
192                 throw new ProcessingException (ne);
193             }
194         }
195     }
196     
197     /**
198      * Retrieves a named Entry's Attributes
199      *
200      * @param name The name of the Entry to modify
201      * @return a Map of the Attributes
202      */

203     public Map JavaDoc get(String JavaDoc name) throws ProcessingException {
204         try {
205             if (this.context == null) initialize ();
206             if (getLogger ().isDebugEnabled ()) {
207                 getLogger ().debug ("LDAPEntryManager retrieving Entry: " + name);
208             }
209             return attributes2Map (context.getAttributes (name));
210         } catch (Exception JavaDoc e) {
211             getLogger ().error ("LDAPEntryManager.get() :" + e.getMessage());
212             throw new ProcessingException (e);
213         }
214     }
215
216     /**
217      * Finds Entries based on matching their Attributes
218      *
219      * @param attributes The Attributes to match
220      * @return a Map of the results, each with a Map of their Attributes
221      */

222     public Map JavaDoc find(Map JavaDoc attributes) throws ProcessingException {
223         return find("", attributes);
224     }
225
226     /**
227      * Finds Entries based on their Attributes
228      *
229      * @param cntx The sub-context to search
230      * @param attributes The Attributes to match
231      * @return a Map of the results, each with a Map of their Attributes
232      */

233     public Map JavaDoc find(String JavaDoc cntx, Map JavaDoc attributes) throws ProcessingException {
234         try {
235             if (this.context == null) initialize ();
236             if (getLogger ().isDebugEnabled ()) {
237                 getLogger ().debug ("LDAPEntryManager finding Entries in: " + cntx);
238             }
239             return namingEnumeration2Map (context.search (cntx, map2Attributes (attributes)));
240         } catch (Exception JavaDoc e) {
241             getLogger ().error ("LDAPEntryManager.find() :" + e.getMessage());
242             throw new ProcessingException (e);
243         }
244     }
245     
246     /**
247      * Modifies an existing Entry
248      *
249      * @param name The name of the Entry to modify
250      * @param mod_op The modification mode to use
251      * @param attributes The Map of modifications
252      */

253     public void modify(String JavaDoc name, int mod_op, Map JavaDoc attributes) throws ProcessingException {
254         try {
255             if (this.context == null) initialize ();
256             if (getLogger ().isDebugEnabled ()) {
257                 getLogger ().debug ("LDAPEntryManager modifying Entry: " + name);
258             }
259             context.modifyAttributes (name, mod_op, map2Attributes (attributes));
260         } catch (Exception JavaDoc e) {
261             getLogger ().error ("LDAPEntryManager.modify() :" + e.getMessage());
262             throw new ProcessingException (e);
263         }
264     }
265
266     /*
267         Converts an Attributes Enumeration into a Map of those Attributes
268         Should be easier to manupulate in FlowScript and display in JXTemplate
269         Keep in mind that because there can be many entries for each Attribute
270         we store each value of the Map as an Array
271     */

272     private Map JavaDoc attributes2Map (Attributes JavaDoc attributes) throws NamingException JavaDoc {
273         Map JavaDoc map = new HashMap JavaDoc ();
274         for (NamingEnumeration JavaDoc atts = attributes.getAll(); atts.hasMore();) {
275             Attribute JavaDoc attr = (Attribute JavaDoc)atts.next();
276             String JavaDoc id = attr.getID();
277             List JavaDoc val = new java.util.ArrayList JavaDoc ();
278             NamingEnumeration JavaDoc vals = attr.getAll();
279             while (vals.hasMore ()) {
280                 val.add (vals.next ());
281             }
282             map.put (id, val);
283         }
284         return map;
285     }
286
287     /*
288         Converts a Map into an Enumeration of Attributes
289         Should be easier to provide from FlowScript
290     */

291     private Attributes JavaDoc map2Attributes (Map JavaDoc map) {
292         Attributes JavaDoc attrs = new BasicAttributes JavaDoc (false);
293         for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext(); ) {
294             Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
295             String JavaDoc key = (String JavaDoc)me.getKey();
296             Attribute JavaDoc attr = new BasicAttribute JavaDoc(key);
297             for (Iterator JavaDoc vals = ((List JavaDoc)me.getValue()).iterator(); vals.hasNext(); ) {
298                 attr.add(vals.next());
299             }
300             attrs.put(attr);
301         }
302         return attrs;
303     }
304
305     /*
306         Converts a NamingEnumeration into a Map of those Entries, with Attributes
307         Should be easier to manupulate in FlowScript and display in JXTemplate
308     */

309     private Map JavaDoc namingEnumeration2Map (NamingEnumeration JavaDoc enumeration) throws NamingException JavaDoc {
310         Map JavaDoc map = new HashMap JavaDoc ();
311         while (enumeration.hasMore ()) {
312             SearchResult JavaDoc sr = (SearchResult JavaDoc)enumeration.next ();
313             map.put (sr.getName (), attributes2Map(sr.getAttributes ()));
314         }
315         return map;
316     }
317 }
318
319
320
Popular Tags