001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.3 $
022: */package org.apache.harmony.jndi.provider;
023:
024: import java.util.Hashtable;
025:
026: import javax.naming.ConfigurationException;
027: import javax.naming.Context;
028: import javax.naming.Name;
029: import javax.naming.NamingException;
030:
031: import javax.naming.spi.ObjectFactory;
032:
033: import org.apache.harmony.jndi.internal.nls.Messages;
034:
035: /**
036: * Base class for URL naming context factory implementations.
037: *
038: * In many cases, subclasses should only override
039: * {@link #createURLContext(Hashtable)} method and provide public no-args
040: * constructor.
041: */
042: public abstract class GenericURLContextFactory implements ObjectFactory {
043:
044: /**
045: * Default constructor for subclasses.
046: */
047: protected GenericURLContextFactory() {
048: super ();
049: }
050:
051: /**
052: * Lookups the specified object in the underlying context. Underlying
053: * context instance is provided by {@link #createURLContext(Hashtable)}.
054: *
055: * Follows the guidelines for URL context factories described in
056: * {@link ObjectFactory#getObjectInstance(Object, Name, Context, Hashtable)}
057: * specification.
058: *
059: * If <code>obj</code> is <code>null</code>, just creates and returns
060: * an underlying context.
061: *
062: * If <code>obj</code> is a proper URL string, lookups and returns an
063: * object specified by that string.
064: *
065: * If <code>obj</code> is an array of URL strings, tries to lookup each of
066: * them sequentially until lookup succeeds, then returns the result. If no
067: * lookup succeeds, throws {@link NamingException} describing the fail of a
068: * last lookup.
069: *
070: * <code>name</code> and <code>nameCtx</code> parameters are ignored.
071: *
072: * @param obj
073: * Object to lookup, can be <code>null</code>.
074: *
075: * @param name
076: * Ignored.
077: *
078: * @param nameCtx
079: * Ignored.
080: *
081: * @param environment
082: * Environment to use in creating the underlying context, can be
083: * <code>null</code>.
084: *
085: * @return The object created.
086: *
087: * @throws ConfigurationException
088: * If <code>obj</code> is neither <code>null</code> nor a
089: * string, nor a string array, or is an empty string array.
090: *
091: * @throws NamingException
092: * If lookup attempt failed.
093: */
094: public Object getObjectInstance(Object obj, Name name,
095: Context nameCtx, Hashtable<?, ?> environment)
096: throws NamingException {
097: Context context = createURLContext(environment);
098:
099: if (obj == null) {
100: // For null object - just return context.
101: return context;
102: }
103:
104: try {
105: if (obj instanceof String) {
106: // For string object - return the object it names.
107: return context.lookup((String) obj);
108: }
109:
110: if (obj instanceof String[]) {
111: // For string array object - search it through.
112: String[] strings = (String[]) obj;
113:
114: if (strings.length < 1) {
115: // jndi.2C=obj is an empty string array
116: throw new ConfigurationException(Messages
117: .getString("jndi.2C")); //$NON-NLS-1$
118: }
119:
120: NamingException exception = null;
121:
122: for (String element : strings) {
123: try {
124: // If the valid object is found - return it.
125: return context.lookup(element);
126: } catch (NamingException e) {
127: // Invalid object, store the exception
128: // to throw it later if no valid object is found.
129: exception = e;
130: }
131: }
132:
133: // No valid object is found.
134: throw exception;
135: }
136:
137: // Unknown object type.
138: // jndi.2D=obj is neither null, nor a string, nor a string array:
139: // {0}
140: throw new IllegalArgumentException(Messages.getString(
141: "jndi.2D", obj)); //$NON-NLS-1$
142: } finally {
143: context.close();
144: }
145: }
146:
147: /**
148: * Returns new instance of the necessary context. Used by
149: * {@link #getObjectInstance(Object, Name, Context, Hashtable)}.
150: *
151: * Must be overridden by particular URL context factory implementations.
152: *
153: * @param environment
154: * Environment.
155: *
156: * @return New context instance.
157: */
158: protected abstract Context createURLContext(
159: Hashtable<?, ?> environment);
160:
161: }
|