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.util.log.Log;
32  
33  
34  public class NamingEntryUtil
35  {
36   
37      
38      /**
39       * Link a name in a webapp's java:/comp/evn namespace to a pre-existing
40       * resource. The pre-existing resource can be either in the webapp's
41       * naming environment, or in the container's naming environment. Webapp's 
42       * environment takes precedence over the server's namespace.
43       *
44       * @param scope the scope of the lookup
45       * @param asName the name to bind as
46       * @param mappedName the name from the environment to link to asName
47       * @throws NamingException
48       */
49      public static boolean bindToENC (Object scope, String asName, String mappedName)
50      throws NamingException
51      {  
52          if (asName==null||asName.trim().equals(""))
53              throw new NamingException ("No name for NamingEntry");
54  
55          if (mappedName==null || "".equals(mappedName))
56              mappedName=asName;
57          
58          NamingEntry entry = lookupNamingEntry (scope, mappedName);
59          if (entry == null)
60              return false;
61          
62          entry.bindToENC(asName);
63          return true;
64       }
65  
66      
67      
68   
69  
70      /**
71       * Find a NamingEntry in the given scope.
72       * 
73       * @param scope
74       * @param jndiName
75       * @return the naming entry for the given scope
76       * @throws NamingException
77       */
78      public static NamingEntry lookupNamingEntry (Object scope, String jndiName)
79      throws NamingException
80      {
81          NamingEntry entry = null;
82          try
83          {         
84              Name scopeName = getNameForScope(scope);
85              InitialContext ic = new InitialContext();   
86              NameParser parser = ic.getNameParser("");
87              Name namingEntryName = makeNamingEntryName(parser, jndiName);  
88              scopeName.addAll(namingEntryName);           
89              entry =  (NamingEntry)ic.lookup(scopeName);
90          }
91          catch (NameNotFoundException ee)
92          {
93          }
94  
95          return entry;
96      }
97      
98      
99      public static Object lookup (Object scope, String jndiName)
100     throws NamingException
101     {
102         Object o = null;
103                 
104             Name scopeName = getNameForScope(scope);
105             InitialContext ic = new InitialContext();   
106             NameParser parser = ic.getNameParser("");          
107             scopeName.addAll(parser.parse(jndiName));           
108             return ic.lookup(scopeName);
109     }
110 
111     
112     /** 
113      * Get all NameEntries of a certain type in the given naming
114      * environment scope (server-wide names or context-specific names)
115      * 
116      * @param scope 
117      * @param clazz the type of the entry
118      * @return all NameEntries of a certain type in the given naming environment scope (server-wide names or context-specific names)
119      * @throws NamingException
120      */
121     public static List lookupNamingEntries (Object scope, Class clazz)
122     throws NamingException
123     { 
124         try
125         {
126             Context scopeContext = getContextForScope(scope);
127             Context namingEntriesContext = (Context)scopeContext.lookup(NamingEntry.__contextName);
128             ArrayList list = new ArrayList();
129             lookupNamingEntries(list, namingEntriesContext, clazz);
130             return list;
131         }
132         catch (NameNotFoundException e)
133         {
134             return Collections.EMPTY_LIST;
135         }
136     }
137     
138     
139     public static Name makeNamingEntryName (NameParser parser, NamingEntry namingEntry)
140     throws NamingException
141     {
142         return makeNamingEntryName(parser, (namingEntry==null?null:namingEntry.getJndiName()));
143     }
144     
145     public static Name makeNamingEntryName (NameParser parser, String jndiName)
146     throws NamingException
147     {
148         if (jndiName==null)
149             return null;
150         
151         if (parser==null)
152         {
153             InitialContext ic = new InitialContext();
154             parser = ic.getNameParser("");
155         }
156         
157         Name name = parser.parse("");
158         name.add(NamingEntry.__contextName);
159         name.addAll(parser.parse(jndiName));
160         return name;
161     }
162     
163 
164     public static Name getNameForScope (Object scope)
165     {
166         try
167         {
168             InitialContext ic = new InitialContext();
169             NameParser parser = ic.getNameParser("");
170             Name name = parser.parse("");
171             if (scope != null)
172             {
173                 name.add(canonicalizeScope(scope));
174             }  
175             return name;
176         }
177         catch (NamingException e)
178         {
179             Log.warn(e);
180             return null;
181         }
182     }
183 
184     public static Context getContextForScope(Object scope)
185     throws NamingException
186     {
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()+"@"+scope.hashCode();
243         str=str.replace('/', '_').replace(' ', '_');
244         return str;
245     }
246 }