View Javadoc

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