View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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  
25  import javax.naming.Binding;
26  import javax.naming.Context;
27  import javax.naming.InitialContext;
28  import javax.naming.Name;
29  import javax.naming.NameNotFoundException;
30  import javax.naming.NameParser;
31  import javax.naming.NamingEnumeration;
32  import javax.naming.NamingException;
33  
34  import org.eclipse.jetty.jndi.NamingUtil;
35  import org.eclipse.jetty.util.log.Logger;
36  
37  
38  public class NamingEntryUtil
39  {
40      private static Logger __log = NamingUtil.__log;
41  
42      /**
43       * Link a name in a webapp's java:/comp/evn namespace to a pre-existing
44       * resource. The pre-existing resource can be either in the webapp's
45       * naming environment, or in the container's naming environment. Webapp's
46       * environment takes precedence over the server's namespace.
47       *
48       * @param scope the scope of the lookup
49       * @param asName the name to bind as
50       * @param mappedName the name from the environment to link to asName
51       * @return true if bind success, false if not bound
52       * @throws NamingException if unable to bind
53       */
54      public static boolean bindToENC (Object scope, String asName, String mappedName)
55      throws NamingException
56      {
57          if (asName==null||asName.trim().equals(""))
58              throw new NamingException ("No name for NamingEntry");
59  
60          if (mappedName==null || "".equals(mappedName))
61              mappedName=asName;
62  
63          NamingEntry entry = lookupNamingEntry (scope, mappedName);
64          if (entry == null)
65              return false;
66  
67          entry.bindToENC(asName);
68          return true;
69       }
70  
71  
72  
73  
74  
75      /**
76       * Find a NamingEntry in the given scope.
77       *
78       * @param scope the object scope
79       * @param jndiName the jndi name
80       * @return the naming entry for the given scope
81       * @throws NamingException if unable to lookup naming entry
82       */
83      public static NamingEntry lookupNamingEntry (Object scope, String jndiName)
84      throws NamingException
85      {
86          NamingEntry entry = null;
87          try
88          {
89              Name scopeName = getNameForScope(scope);
90              InitialContext ic = new InitialContext();
91              NameParser parser = ic.getNameParser("");
92              Name namingEntryName = makeNamingEntryName(parser, jndiName);
93              scopeName.addAll(namingEntryName);
94              entry =  (NamingEntry)ic.lookup(scopeName);
95          }
96          catch (NameNotFoundException ee)
97          {
98          }
99  
100         return entry;
101     }
102 
103     public static Object lookup(Object scope, String jndiName) throws NamingException
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      * 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 the object 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 if unable to lookup the naming entries
120      */
121     public static List<Object> 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<Object> list = new ArrayList<Object>();
129             lookupNamingEntries(list, namingEntriesContext, clazz);
130             return list;
131         }
132         catch (NameNotFoundException e)
133         {
134             return Collections.emptyList();
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         InitialContext ic = new InitialContext();
188         NameParser parser = ic.getNameParser("");
189         Name name = parser.parse("");
190         if (scope != null)
191         {
192             name.add(canonicalizeScope(scope));
193         }
194         return (Context)ic.lookup(name);
195     }
196 
197     public static Context getContextForNamingEntries (Object scope)
198     throws NamingException
199     {
200         Context scopeContext = getContextForScope(scope);
201         return (Context)scopeContext.lookup(NamingEntry.__contextName);
202     }
203 
204     /**
205      * Build up a list of NamingEntry objects that are of a specific type.
206      *
207      * @param list
208      * @param context
209      * @param clazz
210      * @return
211      * @throws NamingException
212      */
213     private static List<Object> lookupNamingEntries (List<Object> list, Context context, Class<?> clazz)
214     throws NamingException
215     {
216         try
217         {
218             NamingEnumeration<Binding> nenum = context.listBindings("");
219             while (nenum.hasMoreElements())
220             {
221                 Binding binding = nenum.next();
222                 if (binding.getObject() instanceof Context)
223                     lookupNamingEntries (list, (Context)binding.getObject(), clazz);
224                 else if (clazz.isInstance(binding.getObject()))
225                   list.add(binding.getObject());
226             }
227         }
228         catch (NameNotFoundException e)
229         {
230             __log.debug("No entries of type "+clazz.getName()+" in context="+context);
231         }
232 
233         return list;
234     }
235 
236     private static String canonicalizeScope(Object scope)
237     {
238         if (scope==null)
239             return "";
240 
241         String str = scope.getClass().getName()+"@"+Long.toHexString(scope.hashCode());
242         str=str.replace('/', '_').replace(' ', '_');
243         return str;
244     }
245 }