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