01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.tomcat.common;
18:
19: import org.apache.openejb.loader.SystemInstance;
20: import org.apache.openejb.spi.ContainerSystem;
21: import static org.apache.openejb.tomcat.common.NamingUtil.JNDI_NAME;
22: import static org.apache.openejb.tomcat.common.NamingUtil.JNDI_PROVIDER_ID;
23: import static org.apache.openejb.tomcat.common.NamingUtil.getProperty;
24:
25: import javax.naming.Context;
26: import javax.naming.Name;
27: import javax.naming.NamingException;
28: import javax.naming.Reference;
29: import javax.naming.spi.ObjectFactory;
30: import java.util.Hashtable;
31:
32: public abstract class AbstractObjectFactory implements ObjectFactory {
33: public Object getObjectInstance(Object object, Name name,
34: Context context, Hashtable environment) throws Exception {
35: Reference ref = (Reference) object;
36:
37: // the jndi context to use for the lookup (usually null which is the default context)
38: String jndiProviderId = getProperty(ref, JNDI_PROVIDER_ID);
39:
40: // the jndi name
41: String jndiName = getProperty(ref, JNDI_NAME);
42: if (jndiName == null) {
43: jndiName = buildJndiName(ref);
44: }
45:
46: // look up the reference
47: Object value = lookup(jndiProviderId, jndiName);
48: return value;
49: }
50:
51: protected abstract String buildJndiName(Reference reference)
52: throws NamingException;
53:
54: protected Object lookup(String jndiProviderId, String jndiName)
55: throws NamingException {
56: Context externalContext = getContext(jndiProviderId);
57: synchronized (externalContext) {
58: /* According to the JNDI SPI specification multiple threads may not access the same JNDI
59: Context *instance* concurrently. Since we don't know the origines of the federated context we must
60: synchonrize access to it. JNDI SPI Sepecifiation 1.2 Section 2.2
61: */
62: return externalContext.lookup(jndiName);
63: }
64: }
65:
66: protected Context getContext(String jndiProviderId)
67: throws NamingException {
68: if (jndiProviderId != null) {
69: String contextJndiName = "java:openejb/remote_jndi_contexts/"
70: + jndiProviderId;
71: ContainerSystem containerSystem = SystemInstance.get()
72: .getComponent(ContainerSystem.class);
73: Context context = (Context) containerSystem
74: .getJNDIContext().lookup(contextJndiName);
75: return context;
76: } else {
77: ContainerSystem containerSystem = SystemInstance.get()
78: .getComponent(ContainerSystem.class);
79: Context context = containerSystem.getJNDIContext();
80: return context;
81: }
82: }
83:
84: }
|