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.core.ivm.naming;
17:
18: import org.apache.openejb.spi.ContainerSystem;
19: import org.apache.openejb.loader.SystemInstance;
20:
21: import java.util.Hashtable;
22:
23: import javax.naming.Context;
24: import javax.naming.InitialContext;
25: import javax.naming.NamingException;
26:
27: public class JndiReference extends Reference {
28:
29: private Context context;
30: private Hashtable envProperties;
31: private String jndiName;
32: private String contextJndiName;
33:
34: /*
35: * This constructor is used when the object to be referenced is accessible through
36: * some other JNDI name space. The context is provided and the lookup name, but the
37: * object is not resolved until it's requested.
38: */
39: public JndiReference(javax.naming.Context linkedContext,
40: String jndiName) {
41: this .context = linkedContext;
42: this .jndiName = jndiName;
43: }
44:
45: /*
46: */
47: public JndiReference(String contextJndiName, String jndiName) {
48: this .contextJndiName = contextJndiName;
49: this .jndiName = jndiName;
50: }
51:
52: public JndiReference(Hashtable envProperties, String jndiName) {
53: if (envProperties == null || envProperties.size() == 0) {
54: this .envProperties = null;
55: } else {
56: this .envProperties = envProperties;
57: }
58: this .jndiName = jndiName;
59: }
60:
61: public Object getObject() throws NamingException {
62: Context externalContext = getContext();
63: synchronized (externalContext) {
64: /* According to the JNDI SPI specification multiple threads may not access the same JNDI
65: Context *instance* concurrently. Since we don't know the origines of the federated context we must
66: synchonrize access to it. JNDI SPI Sepecifiation 1.2 Section 2.2
67: */
68: return externalContext.lookup(jndiName);
69: }
70: }
71:
72: protected Context getContext() throws NamingException {
73: if (context == null) {
74: if (contextJndiName != null) {
75: ContainerSystem containerSystem = SystemInstance.get()
76: .getComponent(ContainerSystem.class);
77: context = (Context) containerSystem.getJNDIContext()
78: .lookup(contextJndiName);
79: } else {
80: context = new InitialContext(envProperties);
81: }
82: }
83: return context;
84: }
85: }
|