View Javadoc

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