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      public static Object lookup(Object scope, String jndiName) throws NamingException
100     {
101         Name scopeName = getNameForScope(scope);
102         InitialContext ic = new InitialContext();
103         NameParser parser = ic.getNameParser("");
104         scopeName.addAll(parser.parse(jndiName));
105         return ic.lookup(scopeName);
106     }
107 
108     /** 
109      * Get all NameEntries of a certain type in the given naming
110      * environment scope (server-wide names or context-specific names)
111      * 
112      * @param scope 
113      * @param clazz the type of the entry
114      * @return all NameEntries of a certain type in the given naming environment scope (server-wide names or context-specific names)
115      * @throws NamingException
116      */
117     public static List<Object> lookupNamingEntries (Object scope, Class<?> clazz)
118     throws NamingException
119     { 
120         try
121         {
122             Context scopeContext = getContextForScope(scope);
123             Context namingEntriesContext = (Context)scopeContext.lookup(NamingEntry.__contextName);
124             ArrayList<Object> list = new ArrayList<Object>();
125             lookupNamingEntries(list, namingEntriesContext, clazz);
126             return list;
127         }
128         catch (NameNotFoundException e)
129         {
130             return Collections.emptyList(); 
131         }
132     }
133     
134     
135     public static Name makeNamingEntryName (NameParser parser, NamingEntry namingEntry)
136     throws NamingException
137     {
138         return makeNamingEntryName(parser, (namingEntry==null?null:namingEntry.getJndiName()));
139     }
140     
141     public static Name makeNamingEntryName (NameParser parser, String jndiName)
142     throws NamingException
143     {
144         if (jndiName==null)
145             return null;
146         
147         if (parser==null)
148         {
149             InitialContext ic = new InitialContext();
150             parser = ic.getNameParser("");
151         }
152         
153         Name name = parser.parse("");
154         name.add(NamingEntry.__contextName);
155         name.addAll(parser.parse(jndiName));
156         return name;
157     }
158     
159 
160     public static Name getNameForScope (Object scope)
161     {
162         try
163         {
164             InitialContext ic = new InitialContext();
165             NameParser parser = ic.getNameParser("");
166             Name name = parser.parse("");
167             if (scope != null)
168             {
169                 name.add(canonicalizeScope(scope));
170             }  
171             return name;
172         }
173         catch (NamingException e)
174         {
175             __log.warn(e);
176             return null;
177         }
178     }
179 
180     public static Context getContextForScope(Object scope)
181     throws NamingException
182     {
183         InitialContext ic = new InitialContext();
184         NameParser parser = ic.getNameParser("");
185         Name name = parser.parse("");
186         if (scope != null)
187         {
188             name.add(canonicalizeScope(scope));
189         }  
190         return (Context)ic.lookup(name);
191     }
192     
193     public static Context getContextForNamingEntries (Object scope)
194     throws NamingException
195     {
196         Context scopeContext = getContextForScope(scope);
197         return (Context)scopeContext.lookup(NamingEntry.__contextName);
198     }
199 
200     /**
201      * Build up a list of NamingEntry objects that are of a specific type.
202      * 
203      * @param list
204      * @param context
205      * @param clazz
206      * @return
207      * @throws NamingException
208      */
209     private static List<Object> lookupNamingEntries (List<Object> list, Context context, Class<?> clazz)
210     throws NamingException
211     {
212         try
213         {
214             NamingEnumeration<Binding> nenum = context.listBindings("");
215             while (nenum.hasMoreElements())
216             {
217                 Binding binding = nenum.next();
218                 if (binding.getObject() instanceof Context)
219                     lookupNamingEntries (list, (Context)binding.getObject(), clazz);
220                 else if (clazz.isInstance(binding.getObject()))
221                   list.add(binding.getObject());
222             }
223         }
224         catch (NameNotFoundException e)
225         {
226             __log.debug("No entries of type "+clazz.getName()+" in context="+context);
227         }
228 
229         return list;
230     }
231 
232     private static String canonicalizeScope(Object scope)
233     {
234         if (scope==null)
235             return "";
236 
237         String str = scope.getClass().getName()+"@"+Long.toHexString(scope.hashCode());
238         str=str.replace('/', '_').replace(' ', '_');
239         return str;
240     }
241 }