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: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.client;
017:
018: import javax.naming.NamingException;
019: import javax.naming.Context;
020: import java.net.URI;
021:
022: /**
023: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
024: */
025: public class GenericServiceLocator extends ServiceLocator {
026:
027: private final String commonPrefix;
028:
029: public GenericServiceLocator(URI serverUri, String commonPrefix)
030: throws NamingException {
031: super (serverUri);
032: this .commonPrefix = commonPrefix;
033: }
034:
035: public GenericServiceLocator(URI serverUri, String username,
036: String password, String commonPrefix)
037: throws NamingException {
038: super (serverUri, username, password);
039: this .commonPrefix = commonPrefix;
040: }
041:
042: public GenericServiceLocator(URI serverUri, String username,
043: String password, String realm, String commonPrefix)
044: throws NamingException {
045: super (serverUri, username, password, realm);
046: this .commonPrefix = commonPrefix;
047: }
048:
049: public GenericServiceLocator(Context context, String commonPrefix) {
050: super (context);
051: this .commonPrefix = commonPrefix;
052: }
053:
054: @Override
055: public Object lookup(String name) {
056: if (commonPrefix != null)
057: name = commonPrefix + "/" + name;
058: return super .lookup(name);
059: }
060:
061: /**
062: * Usable with JNDI name formats ending in the full class name of the interface
063: *
064: * Such as:
065: * - {interfaceClass}
066: *
067: * Or with commonPrefix (supplied in constructor) such as:
068: * - {moduleId}/{interfaceClass}
069: * - ejb/{moduleId}/{interfaceClass}
070: *
071: * @param type the interfaceClass
072: * @return (T) lookup(type.getName())
073: */
074: public <T> T lookup(Class<T> type) {
075: return (T) lookup(type.getName());
076: }
077:
078: /**
079: * Usable with JNDI name formats including a varying prefix such as ejbName or deploymentID
080: * and ending in the full class name of the interface
081: *
082: * Such as:
083: * - {ejbName}/{interfaceClass}
084: * - {deploymentId}/{interfaceClass}
085: *
086: * Or with commonPrefix (supplied in constructor) such as:
087: * - {moduleId}/{ejbName}/{interfaceClass}
088: * - ejb/{moduleId}/{deploymentId}/{interfaceClass}
089: *
090: * @param prefix such as ejbName or deploymentId
091: * @param type the interfaceClass
092: * @return (T) lookup(prefix + "/" + type.getName())
093: */
094: public <T> T lookup(String prefix, Class<T> type) {
095: return (T) lookup(prefix + "/" + type.getName());
096: }
097:
098: /**
099: * Usable with JNDI name formats comprised of the interfaceClass and ejbClass
100: *
101: * For variation, the interface class is the prefix and the ejb class is the
102: * suffix. This is neat as the the prefix (the interface class name) becomes
103: * a jndi context with one binding in it for each implementing ejb class.
104: *
105: * Works with:
106: * - {interfaceClass}/{ejbClass}
107: *
108: * Or with commonPrefix (supplied in constructor) such as:
109: * - {moduleId}/{interfaceClass}/{ejbClass}
110: * - ejb/{moduleId}/{interfaceClass}/{ejbClass}
111: *
112: * @param type the interfaceClass
113: * @param ejbClass the ejbClass
114: * @return (T) lookup(type.getName() + "/" + ejbClass.getName())
115: */
116: public <T, B> T lookup(Class<T> type, Class<B> ejbClass) {
117: return (T) lookup(type.getName() + "/" + ejbClass.getName());
118: }
119: }
|