001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
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: package org.apache.hivemind.lib.impl;
016:
017: import java.util.Hashtable;
018:
019: import javax.naming.Context;
020: import javax.naming.InitialContext;
021: import javax.naming.NamingException;
022:
023: import org.apache.hivemind.ApplicationRuntimeException;
024: import org.apache.hivemind.HiveMind;
025: import org.apache.hivemind.lib.NameLookup;
026: import org.apache.hivemind.lib.RemoteExceptionCoordinator;
027: import org.apache.hivemind.lib.RemoteExceptionEvent;
028: import org.apache.hivemind.lib.RemoteExceptionListener;
029:
030: /**
031: * Standard implementation of the {@link org.apache.hivemind.lib.NameLookup}
032: * service interface.
033: *
034: * @author Howard Lewis Ship
035: */
036: public class NameLookupImpl implements NameLookup,
037: RemoteExceptionListener {
038: private RemoteExceptionCoordinator _coordinator;
039: private Context _initialContext;
040: private String _initialFactory;
041: private String _URLPackages;
042: private String _providerURL;
043:
044: public Object lookup(String name, Class expected) {
045: int i = 0;
046:
047: while (true) {
048: Context context = null;
049: Object raw = null;
050:
051: try {
052: context = getInitialContext();
053:
054: raw = context.lookup(name);
055: } catch (NamingException ex) {
056: if (i++ == 0)
057: _coordinator.fireRemoteExceptionDidOccur(this , ex);
058: else
059: throw new ApplicationRuntimeException(ImplMessages
060: .unableToLookup(name, context), ex);
061: continue;
062: }
063:
064: if (raw == null)
065: throw new ApplicationRuntimeException(ImplMessages
066: .noObject(name, expected));
067:
068: if (!expected.isAssignableFrom(raw.getClass()))
069: throw new ApplicationRuntimeException(ImplMessages
070: .wrongType(name, raw, expected));
071:
072: return raw;
073: }
074: }
075:
076: private Context getInitialContext() throws NamingException {
077: if (_initialContext == null) {
078:
079: Hashtable properties = new Hashtable();
080:
081: if (!HiveMind.isBlank(_initialFactory))
082: properties.put(Context.INITIAL_CONTEXT_FACTORY,
083: _initialFactory);
084:
085: if (!HiveMind.isBlank(_providerURL))
086: properties.put(Context.PROVIDER_URL, _providerURL);
087:
088: if (!HiveMind.isBlank(_URLPackages))
089: properties.put(Context.URL_PKG_PREFIXES, _URLPackages);
090:
091: _initialContext = constructContext(properties);
092: }
093:
094: return _initialContext;
095: }
096:
097: /**
098: * Constructs the InitialContext (this is separated out in a standalone
099: * method so that it may be overridden in a testing subclass).
100: */
101: protected Context constructContext(Hashtable properties)
102: throws NamingException {
103: return new InitialContext(properties);
104: }
105:
106: /**
107: * Sets the InitialContext to null.
108: */
109: public void remoteExceptionDidOccur(RemoteExceptionEvent event) {
110: _initialContext = null;
111: }
112:
113: /**
114: * Sets the initial factory used to create the initial JNDI context.
115: * Equivalent to the system property <code>java.naming.factory.initial</code>.
116: */
117: public void setInitialFactory(String string) {
118: _initialFactory = string;
119: }
120:
121: /**
122: * Sets the JNDI provider URL, used to create the initial JNDI context.
123: * Equivalent to the system property <code>java.naming.provider.url</code>.
124: */
125: public void setProviderURL(String string) {
126: _providerURL = string;
127: }
128:
129: /**
130: * Sets the URL packages, used to create the initial JNDI context.
131: * Equivalent to the system property
132: * <code>java.naming.factory.url.pkgs</code>
133: */
134:
135: public void setURLPackages(String string) {
136: _URLPackages = string;
137: }
138:
139: public void setCoordinator(RemoteExceptionCoordinator coordinator) {
140: _coordinator = coordinator;
141: }
142:
143: }
|