KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > ejb > project > support > HeirMemoryMap


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.ejb.project.support;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.naming.Binding JavaDoc;
30 import javax.naming.CompositeName JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InvalidNameException JavaDoc;
33 import javax.naming.Name JavaDoc;
34 import javax.naming.NameAlreadyBoundException JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.NameNotFoundException JavaDoc;
38 import javax.naming.NameParser JavaDoc;
39 import javax.naming.NotContextException JavaDoc;
40 import javax.naming.OperationNotSupportedException JavaDoc;
41 import javax.naming.directory.Attribute JavaDoc;
42 import javax.naming.directory.Attributes JavaDoc;
43 import javax.naming.directory.BasicAttributes JavaDoc;
44 import javax.naming.directory.DirContext JavaDoc;
45 import javax.naming.directory.ModificationItem JavaDoc;
46 import javax.naming.directory.SearchControls JavaDoc;
47
48 /** A simple in memory implementation of DirContext that uses a HashMap as the
49  store and unix style path names.
50
51 @author Scott_Stark@displayscape.com
52 @version $Id: HeirMemoryMap.java 37406 2005-10-29 23:41:24Z starksm $
53 */

54 public class HeirMemoryMap extends DirContextStringImpl implements DirContext JavaDoc, Serializable JavaDoc
55 {
56     private static NameParser JavaDoc nameParser = DefaultName.getNameParser();
57     private HashMap JavaDoc bindings = new HashMap JavaDoc();
58     private HashMap JavaDoc bindingAttrs = new HashMap JavaDoc();
59     private HeirMemoryMap parent;
60     private String JavaDoc contextName;
61     private Hashtable JavaDoc env;
62
63     /** Creates new HeirMemoryMap */
64     public HeirMemoryMap()
65     {
66         this.contextName = "";
67     }
68     public HeirMemoryMap(String JavaDoc contextName, HeirMemoryMap parent, Attributes JavaDoc attributes) throws NamingException JavaDoc
69     {
70         this(contextName, parent, attributes, null);
71     }
72     public HeirMemoryMap(String JavaDoc contextName, HeirMemoryMap parent, Attributes JavaDoc attributes, Hashtable JavaDoc env) throws NamingException JavaDoc
73     {
74         this.contextName = contextName == null ? "" : contextName;
75         this.parent = parent;
76         bindingAttrs.put("", attributes.clone());
77         if( parent != null )
78             parent.bind(contextName, this);
79         this.env = env;
80     }
81
82     public String JavaDoc toString()
83     {
84         Name JavaDoc name = null;
85         try
86         {
87             name = getFullName();
88         }
89         catch(NamingException JavaDoc e)
90         {
91         }
92         return name.toString();
93     }
94
95     String JavaDoc getName()
96     {
97         return contextName;
98     }
99     void setName(String JavaDoc contextName)
100     {
101         this.contextName = contextName;
102     }
103     Name JavaDoc getFullName() throws NamingException JavaDoc
104     {
105         CompositeName JavaDoc name = new CompositeName JavaDoc(getName());
106         HeirMemoryMap context = parent;
107         if( context == null )
108             return name;
109
110         try
111         {
112             while( context.parent != null )
113             {
114                 name.add(0, context.getName());
115                 context = context.parent;
116             }
117         }
118         catch(NamingException JavaDoc e)
119         {
120         }
121         return name;
122     }
123
124 // ---
125
public Object JavaDoc addToEnvironment(String JavaDoc p1,Object JavaDoc p2) throws NamingException JavaDoc
126     {
127         return null;
128     }
129     public Object JavaDoc removeFromEnvironment(String JavaDoc p1) throws NamingException JavaDoc
130     {
131         return null;
132     }
133     
134     public void bind(Name JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc
135     {
136         bind(name, value, null);
137     }
138     
139     public void bind(Name JavaDoc name, Object JavaDoc value, Attributes JavaDoc attributes) throws NamingException JavaDoc
140     {
141         if( name.isEmpty() )
142         {
143             throw new InvalidNameException JavaDoc("Cannot bind empty name");
144         }
145
146         internalBind(name, value, attributes, true);
147     }
148
149     public void close() throws NamingException JavaDoc
150     {
151     }
152     
153     public Name JavaDoc composeName(Name JavaDoc p1,Name JavaDoc p2) throws NamingException JavaDoc {
154         return null;
155     }
156     
157     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc
158     {
159         return createSubcontext(name, null);
160     }
161     
162     public DirContext JavaDoc createSubcontext(Name JavaDoc name, Attributes JavaDoc attributes) throws NamingException JavaDoc
163     {
164         if( name.isEmpty() )
165         {
166             throw new InvalidNameException JavaDoc("Cannot createSubcontext with empty name");
167         }
168
169         DirContext JavaDoc subctx = null;
170         String JavaDoc atom = name.get(0);
171         if( name.size() == 1 )
172         {
173             subctx = new HeirMemoryMap(atom, this, attributes, env);
174         }
175         else
176         {
177             DirContext JavaDoc context = (DirContext JavaDoc) bindings.get(atom);
178             subctx = context.createSubcontext(name.getSuffix(1), attributes);
179         }
180
181         return subctx;
182     }
183     
184     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc
185     {
186         unbind(name);
187     }
188
189     public Attributes JavaDoc getAttributes(Name JavaDoc name) throws NamingException JavaDoc
190     {
191         return getAttributes(name, null);
192     }
193
194     public Attributes JavaDoc getAttributes(Name JavaDoc name, String JavaDoc[] attrIDs) throws NamingException JavaDoc
195     {
196         Attributes JavaDoc nameAttributes = null;
197         String JavaDoc atom = name.get(0);
198         if( name.isEmpty() == true )
199         {
200             nameAttributes = (Attributes JavaDoc) bindingAttrs.get("");
201         }
202         else if( name.size() == 1 )
203         {
204             Object JavaDoc binding = bindings.get(atom);
205             if( binding != null )
206             {
207                 if( binding instanceof DirContext JavaDoc )
208                 {
209                     DirContext JavaDoc dirCtx = (DirContext JavaDoc) binding;
210                     return dirCtx.getAttributes(name.getSuffix(1), attrIDs);
211                 }
212             }
213             nameAttributes = (Attributes JavaDoc) bindingAttrs.get(atom);
214         }
215         else
216         {
217             DirContext JavaDoc context = (DirContext JavaDoc) bindings.get(atom);
218             nameAttributes = context.getAttributes(name.getSuffix(1), attrIDs);
219         }
220
221         if( nameAttributes != null && attrIDs != null )
222         {
223             BasicAttributes JavaDoc matches = new BasicAttributes JavaDoc(nameAttributes.isCaseIgnored());
224             for(int a = 0; a < attrIDs.length; a ++)
225             {
226                 Attribute JavaDoc attr = nameAttributes.get(attrIDs[a]);
227                 if( attr != null )
228                     matches.put(attr);
229             }
230             nameAttributes = matches;
231         }
232         return nameAttributes;
233     }
234
235     public java.util.Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc
236     {
237         return env;
238     }
239
240     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc
241     {
242         return toString();
243     }
244
245     public NameParser JavaDoc getNameParser(Name JavaDoc p1) throws NamingException JavaDoc
246     {
247         return nameParser;
248     }
249
250     public DirContext JavaDoc getSchema(Name JavaDoc p1) throws NamingException JavaDoc
251     {
252         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
253     }
254     
255     public DirContext JavaDoc getSchemaClassDefinition(Name JavaDoc p1) throws NamingException JavaDoc
256     {
257         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
258     }
259     
260     public NamingEnumeration JavaDoc list(Name JavaDoc p1) throws NamingException JavaDoc
261     {
262         return null;
263     }
264
265     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc
266     {
267         NamingEnumeration JavaDoc iter = null;
268
269         if( name.isEmpty() == true )
270         {
271             Iterator JavaDoc keys = bindings.keySet().iterator();
272             ArrayList JavaDoc tmp = new ArrayList JavaDoc();
273             while( keys.hasNext() )
274             {
275                 String JavaDoc key = (String JavaDoc) keys.next();
276                 Object JavaDoc value = bindings.get(key);
277                 Attributes JavaDoc attributes = (Attributes JavaDoc) bindingAttrs.get(key);
278                 DirBinding tuple = new DirBinding(key, value, attributes);
279                 tmp.add(tuple);
280             }
281             iter = new NameBindingIterator(tmp.iterator(), this);
282         }
283         else
284         {
285             String JavaDoc atom = name.get(0);
286             Context JavaDoc context = (Context JavaDoc) bindings.get(atom);
287             iter = context.listBindings(name.getSuffix(1));
288         }
289
290         return iter;
291     }
292
293     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc
294     {
295         if( name.isEmpty() == true )
296             return this;
297
298         String JavaDoc atom = name.get(0);
299         Object JavaDoc binding = bindings.get(atom);
300         if( name.size() == 1 )
301         { /* Need to check that binding is null and atom is not a key
302                 since a null value could have been bound.
303             */

304             if( binding == null && bindings.containsKey(atom) == false )
305             {
306                 NameNotFoundException JavaDoc e = new NameNotFoundException JavaDoc("Failed to find: "+atom);
307                 e.setRemainingName(name);
308                 e.setResolvedObj(this);
309                 throw e;
310             }
311         }
312         else if( (binding instanceof Context JavaDoc) )
313         {
314             Context JavaDoc context = (Context JavaDoc) binding;
315             binding = context.lookup(name.getSuffix(1));
316         }
317         else
318         {
319             NotContextException JavaDoc e = new NotContextException JavaDoc(atom + " does not name a directory context that supports attributes");
320             e.setRemainingName(name);
321             e.setResolvedObj(binding);
322             throw e;
323         }
324         return binding;
325     }
326
327     public Object JavaDoc lookupLink(Name JavaDoc p1) throws NamingException JavaDoc
328     {
329         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
330     }
331     
332     public void modifyAttributes(Name JavaDoc p1,ModificationItem JavaDoc[] p2) throws NamingException JavaDoc
333     {
334         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
335     }
336     
337     public void modifyAttributes(Name JavaDoc p1,int p2,Attributes JavaDoc p3) throws NamingException JavaDoc
338     {
339         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
340     }
341     
342     public void rebind(Name JavaDoc name, Object JavaDoc value) throws NamingException JavaDoc
343     {
344         rebind(name, value, null);
345     }
346     
347     public void rebind(Name JavaDoc name, Object JavaDoc value, Attributes JavaDoc attributes) throws NamingException JavaDoc
348     {
349         if( name.isEmpty() )
350         {
351             throw new InvalidNameException JavaDoc("Cannot bind empty name");
352         }
353
354         internalBind(name, value, attributes, false);
355     }
356
357     public void rename(Name JavaDoc p1,Name JavaDoc p2) throws NamingException JavaDoc
358     {
359         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
360     }
361
362     public NamingEnumeration JavaDoc search(Name JavaDoc p1,Attributes JavaDoc p2) throws NamingException JavaDoc
363     {
364         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
365     }
366
367     public NamingEnumeration JavaDoc search(Name JavaDoc p1,String JavaDoc p2,SearchControls JavaDoc p3) throws NamingException JavaDoc
368     {
369         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
370     }
371
372     public NamingEnumeration JavaDoc search(Name JavaDoc p1,Attributes JavaDoc p2,String JavaDoc[] p3) throws NamingException JavaDoc
373     {
374         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
375     }
376
377     public NamingEnumeration JavaDoc search(Name JavaDoc p1,String JavaDoc p2,Object JavaDoc[] p3,SearchControls JavaDoc p4) throws NamingException JavaDoc
378     {
379         throw new OperationNotSupportedException JavaDoc("Not implemented yet");
380     }
381
382     public void unbind(Name JavaDoc name) throws NamingException JavaDoc
383     {
384         if( name.isEmpty() )
385         {
386             throw new InvalidNameException JavaDoc("Cannot unbind empty name");
387         }
388
389         String JavaDoc atom = name.get(0);
390         Object JavaDoc binding = bindings.get(atom);
391         if( name.size() == 1 )
392         { /* Need to check that binding is null and atom is not a key
393                 since a null value could have been bound.
394             */

395             if( binding == null && bindings.containsKey(atom) == false )
396             {
397                 NameNotFoundException JavaDoc e = new NameNotFoundException JavaDoc("Failed to find: "+atom);
398                 e.setRemainingName(name);
399                 e.setResolvedObj(this);
400                 throw e;
401             }
402             bindings.remove(atom);
403             bindingAttrs.remove(atom);
404         }
405         else if( (binding instanceof Context JavaDoc) )
406         {
407             Context JavaDoc context = (Context JavaDoc) binding;
408             context.unbind(name.getSuffix(1));
409         }
410         else
411         {
412             NotContextException JavaDoc e = new NotContextException JavaDoc(atom + " does not name a directory context that supports attributes");
413             e.setRemainingName(name);
414             e.setResolvedObj(binding);
415             throw e;
416         }
417     }
418 // ---
419

420     private void internalBind(Name JavaDoc name, Object JavaDoc value, Attributes JavaDoc attributes, boolean isBind) throws NamingException JavaDoc
421     {
422         String JavaDoc atom = name.get(0);
423         Object JavaDoc binding = bindings.get(atom);
424
425         if( name.size() == 1 )
426         {
427             if( binding != null && isBind == false )
428             {
429                 throw new NameAlreadyBoundException JavaDoc("Use rebind to override");
430             }
431
432             // Add object to internal data structure
433
bindings.put(atom, value);
434
435             // Add attributes
436
if( attributes != null )
437             {
438                 bindingAttrs.put(atom, attributes);
439             }
440         }
441         else
442         {
443             // Intermediate name: Consume name in this context and continue
444
if( (binding instanceof Context JavaDoc) == false )
445             {
446                 NotContextException JavaDoc e = new NotContextException JavaDoc(atom + " does not name a context");
447                 e.setRemainingName(name);
448                 e.setResolvedObj(binding);
449                 throw e;
450             }
451
452             if( attributes == null )
453             {
454                 Context JavaDoc context = (Context JavaDoc) binding;
455                 if( isBind == true )
456                     context.bind(name.getSuffix(1), value);
457                 else
458                     context.rebind(name.getSuffix(1), value);
459             }
460             else if( (binding instanceof DirContext JavaDoc) == false )
461             {
462                 NotContextException JavaDoc e = new NotContextException JavaDoc(atom + " does not name a directory context that supports attributes");
463                 e.setRemainingName(name);
464                 e.setResolvedObj(binding);
465                 throw e;
466             }
467             else
468             {
469                 DirContext JavaDoc context = (DirContext JavaDoc) binding;
470                 if( isBind == true )
471                     context.bind(name.getSuffix(1), value, attributes);
472                 else
473                     context.rebind(name.getSuffix(1), value, attributes);
474             }
475         }
476     }
477 }
478
Popular Tags