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