View Javadoc

1   // ========================================================================
2   // $Id: javaURLContextFactory.java 231 2006-02-19 15:09:58Z janb $
3   // Copyright 1999-2006 Mort Bay Consulting Pty. Ltd.
4   // ------------------------------------------------------------------------
5   // Licensed under the Apache License, Version 2.0 (the "License");
6   // you may not use this file except in compliance with the License.
7   // You may obtain a copy of the License at 
8   // http://www.apache.org/licenses/LICENSE-2.0
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // ========================================================================
15  
16  package org.mortbay.naming.java;
17  
18  import java.util.Hashtable;
19  
20  import javax.naming.Context;
21  import javax.naming.Name;
22  import javax.naming.NamingException;
23  import javax.naming.spi.ObjectFactory;
24  
25  import org.mortbay.log.Log;
26  
27  
28  /** javaURLContextFactory
29   * <p>This is the URL context factory for the java: URL.
30   *
31   * <p><h4>Notes</h4>
32   * <p>
33   *
34   * <p><h4>Usage</h4>
35   * <pre>
36   */
37  /*
38  * </pre>
39  *
40  * @see
41  *
42  * @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>
43  * @version 1.0
44  */
45  public class javaURLContextFactory implements ObjectFactory 
46  {
47          
48      /**
49       * Either return a new context or the resolution of a url.
50       *
51       * @param url an <code>Object</code> value
52       * @param name a <code>Name</code> value
53       * @param ctx a <code>Context</code> value
54       * @param env a <code>Hashtable</code> value
55       * @return a new context or the resolved object for the url
56       * @exception Exception if an error occurs
57       */
58      public Object getObjectInstance(Object url, Name name, Context ctx, Hashtable env)
59          throws Exception 
60      {
61          // null object means return a root context for doing resolutions
62          if (url == null)
63          {
64              if(Log.isDebugEnabled())Log.debug(">>> new root context requested ");
65              return new javaRootURLContext(env);
66          }
67          
68          // return the resolution of the url
69          if (url instanceof String)
70          {
71              if(Log.isDebugEnabled())Log.debug(">>> resolution of url "+url+" requested");
72              Context rootctx = new javaRootURLContext (env);
73              return rootctx.lookup ((String)url);
74          }
75  
76          // return the resolution of at least one of the urls
77          if (url instanceof String[])
78          {
79              if(Log.isDebugEnabled())Log.debug(">>> resolution of array of urls requested");
80              String[] urls = (String[])url; 
81              Context rootctx = new javaRootURLContext (env);
82              Object object = null;
83              NamingException e = null;
84              for (int i=0;(i< urls.length) && (object == null); i++)
85              {
86                  try
87                  {
88                      object = rootctx.lookup (urls[i]);
89                  }
90                  catch (NamingException x)
91                  {
92                      e = x;
93                  }
94              }
95  
96              if (object == null)
97                  throw e;
98              else
99                  return object;
100         }
101 
102         if(Log.isDebugEnabled())Log.debug(">>> No idea what to do, so return a new root context anyway");
103         return new javaRootURLContext (env);
104     }
105 };