001: // ========================================================================
002: // $Id: javaURLContextFactory.java 231 2006-02-19 15:09:58Z janb $
003: // Copyright 1999-2006 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.naming.java;
017:
018: import java.util.Hashtable;
019:
020: import javax.naming.Context;
021: import javax.naming.Name;
022: import javax.naming.NamingException;
023: import javax.naming.spi.ObjectFactory;
024:
025: import org.mortbay.log.Log;
026:
027: /** javaURLContextFactory
028: * <p>This is the URL context factory for the java: URL.
029: *
030: * <p><h4>Notes</h4>
031: * <p>
032: *
033: * <p><h4>Usage</h4>
034: * <pre>
035: */
036: /*
037: * </pre>
038: *
039: * @see
040: *
041: * @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>
042: * @version 1.0
043: */
044: public class javaURLContextFactory implements ObjectFactory {
045:
046: /**
047: * Either return a new context or the resolution of a url.
048: *
049: * @param url an <code>Object</code> value
050: * @param name a <code>Name</code> value
051: * @param ctx a <code>Context</code> value
052: * @param env a <code>Hashtable</code> value
053: * @return a new context or the resolved object for the url
054: * @exception Exception if an error occurs
055: */
056: public Object getObjectInstance(Object url, Name name, Context ctx,
057: Hashtable env) throws Exception {
058: // null object means return a root context for doing resolutions
059: if (url == null) {
060: if (Log.isDebugEnabled())
061: Log.debug(">>> new root context requested ");
062: return new javaRootURLContext(env);
063: }
064:
065: // return the resolution of the url
066: if (url instanceof String) {
067: if (Log.isDebugEnabled())
068: Log
069: .debug(">>> resolution of url " + url
070: + " requested");
071: Context rootctx = new javaRootURLContext(env);
072: return rootctx.lookup((String) url);
073: }
074:
075: // return the resolution of at least one of the urls
076: if (url instanceof String[]) {
077: if (Log.isDebugEnabled())
078: Log.debug(">>> resolution of array of urls requested");
079: String[] urls = (String[]) url;
080: Context rootctx = new javaRootURLContext(env);
081: Object object = null;
082: NamingException e = null;
083: for (int i = 0; (i < urls.length) && (object == null); i++) {
084: try {
085: object = rootctx.lookup(urls[i]);
086: } catch (NamingException x) {
087: e = x;
088: }
089: }
090:
091: if (object == null)
092: throw e;
093: else
094: return object;
095: }
096:
097: if (Log.isDebugEnabled())
098: Log
099: .debug(">>> No idea what to do, so return a new root context anyway");
100: return new javaRootURLContext(env);
101: }
102: };
|