01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.client;
17:
18: import java.util.Enumeration;
19: import java.util.Hashtable;
20: import java.util.Properties;
21:
22: import javax.naming.Context;
23: import javax.naming.InitialContext;
24: import javax.naming.Name;
25: import javax.naming.RefAddr;
26: import javax.naming.Reference;
27: import javax.naming.spi.ObjectFactory;
28:
29: public class TomcatEjbFactory implements ObjectFactory {
30: private final static String OPENEJB_PREFIX = "openejb.";
31:
32: private final static String JAVA_PREFIX = "java.";
33:
34: private final static String OPENEJB_EJB_LINK = "openejb.ejb-link";
35:
36: private final static int OPENEJB_PREFIX_LENGTH = OPENEJB_PREFIX
37: .length();
38:
39: public Object getObjectInstance(Object obj, Name name,
40: Context nameCtx, Hashtable environment) throws Exception {
41: Object beanObj = null;
42: Class ejbRefClass = Class.forName("org.apache.naming.EjbRef");
43: if (ejbRefClass.isAssignableFrom(obj.getClass())) {
44: RefAddr refAddr = null;
45: String addrType = null;
46: Properties env = new Properties();
47: String bean = null;
48:
49: Reference ref = (Reference) obj;
50:
51: Enumeration addresses = ref.getAll();
52: while (addresses.hasMoreElements()) {
53: refAddr = (RefAddr) addresses.nextElement();
54: addrType = refAddr.getType();
55: if (addrType.startsWith(OPENEJB_PREFIX)) {
56: String value = refAddr.getContent().toString();
57: if (addrType.equals(OPENEJB_EJB_LINK)) {
58: bean = value;
59: continue;
60: }
61: String key = addrType
62: .substring(OPENEJB_PREFIX_LENGTH);
63: key = JAVA_PREFIX + key;
64: env.put(key, value);
65: }
66: }
67:
68: if (bean != null) {
69: beanObj = (new InitialContext(env)).lookup(bean);
70: }
71: }
72: return beanObj;
73: }
74: }
|