View Javadoc

1   // ========================================================================
2   // Copyright (c) 1999-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  
14  package org.eclipse.jetty.jndi.java;
15  
16  import java.util.Hashtable;
17  
18  import javax.naming.Context;
19  import javax.naming.Name;
20  import javax.naming.NamingException;
21  import javax.naming.spi.ObjectFactory;
22  
23  import org.eclipse.jetty.util.log.Log;
24  
25  
26  /** javaURLContextFactory
27   * <p>This is the URL context factory for the java: URL.
28   *
29   * <p><h4>Notes</h4>
30   * <p>
31   *
32   * <p><h4>Usage</h4>
33   * <pre>
34   */
35  /*
36  * </pre>
37  *
38  * @see
39  *
40  * 
41  * @version 1.0
42  */
43  public class javaURLContextFactory implements ObjectFactory 
44  {
45          
46      /**
47       * Either return a new context or the resolution of a url.
48       *
49       * @param url an <code>Object</code> value
50       * @param name a <code>Name</code> value
51       * @param ctx a <code>Context</code> value
52       * @param env a <code>Hashtable</code> value
53       * @return a new context or the resolved object for the url
54       * @exception Exception if an error occurs
55       */
56      public Object getObjectInstance(Object url, Name name, Context ctx, Hashtable env)
57          throws Exception 
58      {
59          // null object means return a root context for doing resolutions
60          if (url == null)
61          {
62              if(Log.isDebugEnabled())Log.debug(">>> new root context requested ");
63              return new javaRootURLContext(env);
64          }
65          
66          // return the resolution of the url
67          if (url instanceof String)
68          {
69              if(Log.isDebugEnabled())Log.debug(">>> resolution of url "+url+" requested");
70              Context rootctx = new javaRootURLContext (env);
71              return rootctx.lookup ((String)url);
72          }
73  
74          // return the resolution of at least one of the urls
75          if (url instanceof String[])
76          {
77              if(Log.isDebugEnabled())Log.debug(">>> resolution of array of urls requested");
78              String[] urls = (String[])url; 
79              Context rootctx = new javaRootURLContext (env);
80              Object object = null;
81              NamingException e = null;
82              for (int i=0;(i< urls.length) && (object == null); i++)
83              {
84                  try
85                  {
86                      object = rootctx.lookup (urls[i]);
87                  }
88                  catch (NamingException x)
89                  {
90                      e = x;
91                  }
92              }
93  
94              if (object == null)
95                  throw e;
96              else
97                  return object;
98          }
99  
100         if(Log.isDebugEnabled())Log.debug(">>> No idea what to do, so return a new root context anyway");
101         return new javaRootURLContext (env);
102     }
103 };