View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.plus.jndi;
14  
15  
16  
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import javax.naming.Binding;
23  import javax.naming.Context;
24  import javax.naming.InitialContext;
25  import javax.naming.Name;
26  import javax.naming.NameNotFoundException;
27  import javax.naming.NameParser;
28  import javax.naming.NamingEnumeration;
29  import javax.naming.NamingException;
30  
31  import org.eclipse.jetty.jndi.NamingUtil;
32  import org.eclipse.jetty.util.log.Logger;
33  
34  
35  public class NamingEntryUtil
36  {
37      private static Logger __log = NamingUtil.__log;
38      
39      /**
40       * Link a name in a webapp's java:/comp/evn namespace to a pre-existing
41       * resource. The pre-existing resource can be either in the webapp's
42       * naming environment, or in the container's naming environment. Webapp's 
43       * environment takes precedence over the server's namespace.
44       *
45       * @param scope the scope of the lookup
46       * @param asName the name to bind as
47       * @param mappedName the name from the environment to link to asName
48       * @throws NamingException
49       */
50      public static boolean bindToENC (Object scope, String asName, String mappedName)
51      throws NamingException
52      {  
53          if (asName==null||asName.trim().equals(""))
54              throw new NamingException ("No name for NamingEntry");
55  
56          if (mappedName==null || "".equals(mappedName))
57              mappedName=asName;
58          
59          NamingEntry entry = lookupNamingEntry (scope, mappedName);
60          if (entry == null)
61              return false;
62          
63          entry.bindToENC(asName);
64          return true;
65       }
66  
67      
68      
69   
70  
71      /**
72       * Find a NamingEntry in the given scope.
73       * 
74       * @param scope
75       * @param jndiName
76       * @return the naming entry for the given scope
77       * @throws NamingException
78       */
79      public static NamingEntry lookupNamingEntry (Object scope, String jndiName)
80      throws NamingException
81      {
82          NamingEntry entry = null;
83          try
84          {         
85              Name scopeName = getNameForScope(scope);
86              InitialContext ic = new InitialContext();   
87              NameParser parser = ic.getNameParser("");
88              Name namingEntryName = makeNamingEntryName(parser, jndiName);  
89              scopeName.addAll(namingEntryName);           
90              entry =  (NamingEntry)ic.lookup(scopeName);
91          }
92          catch (NameNotFoundException ee)
93          {
94          }
95  
96          return entry;
97      }
98      
99      
100     public static Object lookup (Object scope, String jndiName)
101     throws NamingException
102     {
103         Object o = null;
104                 
105             Name scopeName = getNameForScope(scope);
106             InitialContext ic = new InitialContext();   
107             NameParser parser = ic.getNameParser("");          
108             scopeName.addAll(parser.parse(jndiName));           
109             return ic.lookup(scopeName);
110     }
111 
112     
113     /** 
114      * Get all NameEntries of a certain type in the given naming
115      * environment scope (server-wide names or context-specific names)
116      * 
117      * @param scope 
118      * @param clazz the type of the entry
119      * @return all NameEntries of a certain type in the given naming environment scope (server-wide names or context-specific names)
120      * @throws NamingException
121      */
122     public static List lookupNamingEntries (Object scope, Class clazz)
123     throws NamingException
124     { 
125         try
126         {
127             Context scopeContext = getContextForScope(scope);
128             Context namingEntriesContext = (Context)scopeContext.lookup(NamingEntry.__contextName);
129             ArrayList list = new ArrayList();
130             lookupNamingEntries(list, namingEntriesContext, clazz);
131             return list;
132         }
133         catch (NameNotFoundException e)
134         {
135             return Collections.EMPTY_LIST;
136         }
137     }
138     
139     
140     public static Name makeNamingEntryName (NameParser parser, NamingEntry namingEntry)
141     throws NamingException
142     {
143         return makeNamingEntryName(parser, (namingEntry==null?null:namingEntry.getJndiName()));
144     }
145     
146     public static Name makeNamingEntryName (NameParser parser, String jndiName)
147     throws NamingException
148     {
149         if (jndiName==null)
150             return null;
151         
152         if (parser==null)
153         {
154             InitialContext ic = new InitialContext();
155             parser = ic.getNameParser("");
156         }
157         
158         Name name = parser.parse("");
159         name.add(NamingEntry.__contextName);
160         name.addAll(parser.parse(jndiName));
161         return name;
162     }
163     
164 
165     public static Name getNameForScope (Object scope)
166     {
167         try
168         {
169             InitialContext ic = new InitialContext();
170             NameParser parser = ic.getNameParser("");
171             Name name = parser.parse("");
172             if (scope != null)
173             {
174                 name.add(canonicalizeScope(scope));
175             }  
176             return name;
177         }
178         catch (NamingException e)
179         {
180             __log.warn(e);
181             return null;
182         }
183     }
184 
185     public static Context getContextForScope(Object scope)
186     throws NamingException
187     {
188         InitialContext ic = new InitialContext();
189         NameParser parser = ic.getNameParser("");
190         Name name = parser.parse("");
191         if (scope != null)
192         {
193             name.add(canonicalizeScope(scope));
194         }  
195         return (Context)ic.lookup(name);
196     }
197     
198     public static Context getContextForNamingEntries (Object scope)
199     throws NamingException
200     {
201         Context scopeContext = getContextForScope(scope);
202         return (Context)scopeContext.lookup(NamingEntry.__contextName);
203     }
204 
205     /**
206      * Build up a list of NamingEntry objects that are of a specific type.
207      * 
208      * @param list
209      * @param context
210      * @param clazz
211      * @return
212      * @throws NamingException
213      */
214     private static List lookupNamingEntries (List list, Context context, Class clazz)
215     throws NamingException
216     {
217         try
218         {
219             NamingEnumeration nenum = context.listBindings("");
220             while (nenum.hasMoreElements())
221             {
222                 Binding binding = (Binding)nenum.next();
223                 if (binding.getObject() instanceof Context)
224                     lookupNamingEntries (list, (Context)binding.getObject(), clazz);
225                 else if (clazz.isInstance(binding.getObject()))
226                   list.add(binding.getObject());
227             }
228         }
229         catch (NameNotFoundException e)
230         {
231             __log.debug("No entries of type "+clazz.getName()+" in context="+context);
232         }
233 
234         return list;
235     }
236 
237     private static String canonicalizeScope(Object scope)
238     {
239         if (scope==null)
240             return "";
241 
242         String str = scope.getClass().getName()+"@"+Long.toHexString(scope.hashCode());
243         str=str.replace('/', '_').replace(' ', '_');
244         return str;
245     }
246 }