001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.naming.component;
016:
017: import java.util.Collections;
018: import java.util.HashMap;
019: import java.util.Hashtable;
020: import java.util.Map;
021:
022: import javax.naming.ConfigurationException;
023: import javax.naming.Context;
024: import javax.naming.Name;
025: import javax.naming.NamingException;
026: import javax.naming.spi.ObjectFactory;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /** A 'component:/....' URL context factory. */
032: public class componentURLContextFactory implements ObjectFactory {
033: private static final Log sLogger = LogFactory
034: .getLog(componentURLContextFactory.class);
035:
036: // Name of the system property which controlls if we should cache the factories once they are found for the particular caller
037: private static final String sEnableContextCachingSystemPropertyKey = "com.metaboss.naming.component.EnableContextCaching";
038: private static Map sContextsMap = null;
039: private static boolean sEnableContextCaching = true;
040:
041: static {
042: // Find out if context caching is enabled or disabled
043: sEnableContextCaching = Boolean
044: .valueOf(
045: System.getProperty(
046: sEnableContextCachingSystemPropertyKey,
047: "true")).booleanValue();
048: if (sEnableContextCaching)
049: sContextsMap = Collections.synchronizedMap(new HashMap());
050:
051: if (sLogger.isDebugEnabled())
052: sLogger.debug("Component factory caching is "
053: + (sEnableContextCaching ? "enabled" : "disabled"));
054: }
055:
056: /** Must have public no-arg constructor. */
057: public componentURLContextFactory() {
058: }
059:
060: /** The "component" url is of the form component:/<name of interface>. */
061: public Object getObjectInstance(Object urlInfo, Name name,
062: Context nameCtx, Hashtable env) throws Exception {
063:
064: // Case 1: urlInfo is null
065: // This means to create a URL context that can accept
066: // arbitrary "foo" URLs.
067: if (urlInfo == null) {
068: return createURLContext(env);
069: }
070:
071: // Case 2: urlInfo is a single string
072: // This means to create/get the object named by urlInfo
073: if (urlInfo instanceof String) {
074: Context urlCtx = createURLContext(env);
075: try {
076: return urlCtx.lookup((String) urlInfo);
077: } finally {
078: urlCtx.close();
079: }
080: }
081:
082: // Case 3: urlInfo is an array of strings
083: // This means each entry in array is equal alternative; create/get
084: // the object named by one of the URls
085: if (urlInfo instanceof String[]) {
086:
087: // Try each URL until lookup() succeeds for one of them.
088: // If all URLs fail, throw one of the exceptions arbitrarily.
089: String[] urls = (String[]) urlInfo;
090: if (urls.length == 0) {
091: throw (new ConfigurationException(
092: "componentURLContextFactory: empty URL array"));
093: }
094: Context urlCtx = createURLContext(env);
095: try {
096: NamingException ne = null;
097: for (int i = 0; i < urls.length; i++) {
098: try {
099: return urlCtx.lookup(urls[i]);
100: } catch (NamingException e) {
101: ne = e;
102: }
103: }
104: throw ne;
105: } finally {
106: urlCtx.close();
107: }
108: }
109:
110: // Case 4: urlInfo is of an unknown type
111: // Provider-specific action: reject input
112:
113: throw new IllegalArgumentException(
114: "Argument must be a component URL string or an array of them");
115: }
116:
117: protected Context createURLContext(Hashtable env)
118: throws NamingException {
119: if (sEnableContextCaching) {
120: // Work with the cache of the contexts
121: Context lContext = (Context) sContextsMap.get(env);
122: if (lContext == null) {
123: if (sLogger.isDebugEnabled()) {
124: sLogger
125: .debug("Creating new JNDI context for the 'component:.....' URLs env = "
126: + env.toString());
127: }
128: sContextsMap.put(env, lContext = new componentContext(
129: env));
130: } else {
131: if (sLogger.isDebugEnabled()) {
132: sLogger
133: .debug("Reusing existing JNDI context for the 'component:.....' URLs env = "
134: + env.toString());
135: }
136: }
137: return lContext;
138: } else {
139: // Always return new context
140: return new componentContext(env);
141: }
142: }
143: }
|