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.assembler.classic;
017:
018: import org.apache.openejb.assembler.classic.EjbResolver;
019: import org.apache.openejb.loader.SystemInstance;
020: import org.apache.openejb.util.Messages;
021: import org.apache.openejb.spi.ContainerSystem;
022: import org.apache.openejb.DeploymentInfo;
023: import org.apache.openejb.InterfaceType;
024: import org.apache.openejb.core.ivm.naming.Reference;
025: import org.apache.openejb.core.ivm.naming.CrossClassLoaderJndiReference;
026: import org.apache.openejb.core.ivm.naming.IntraVmJndiReference;
027:
028: import javax.naming.NamingException;
029: import javax.naming.NameNotFoundException;
030: import java.net.URI;
031:
032: /**
033: * @version $Rev$ $Date$
034: */
035: public class LazyEjbReference extends Reference {
036:
037: private static final Messages messages = new Messages(
038: LazyEjbReference.class);
039:
040: private final EjbResolver.Reference info;
041:
042: private Reference reference;
043: private final URI moduleUri;
044: private final boolean useCrossClassLoaderRef;
045:
046: public LazyEjbReference(EjbResolver.Reference info, URI moduleUri,
047: boolean useCrossClassLoaderRef) {
048: super ();
049: this .info = info;
050: this .moduleUri = moduleUri;
051: this .useCrossClassLoaderRef = useCrossClassLoaderRef;
052: }
053:
054: public Object getObject() throws NamingException {
055: if (reference != null) {
056: return reference.getObject();
057: }
058:
059: SystemInstance systemInstance = SystemInstance.get();
060:
061: EjbResolver resolver = systemInstance
062: .getComponent(EjbResolver.class);
063:
064: String deploymentId = resolver.resolve(info, moduleUri);
065:
066: if (deploymentId == null) {
067: String key = "lazyEjbRefNotResolved";
068: if (info.getHome() != null) {
069: key += ".home";
070: }
071: String message = messages.format(key, info.getName(), info
072: .getEjbLink(), info.getHome(), info.getInterface());
073: throw new NameNotFoundException(message);
074: }
075:
076: ContainerSystem containerSystem = systemInstance
077: .getComponent(ContainerSystem.class);
078:
079: DeploymentInfo deploymentInfo = containerSystem
080: .getDeploymentInfo(deploymentId);
081:
082: if (deploymentId == null) {
083: String message = messages.format("deploymentNotFound", info
084: .getName(), deploymentId);
085: throw new NameNotFoundException(message);
086: }
087:
088: String jndiName = "java:openejb/Deployment/" + deploymentId
089: + "/" + info.getInterface();
090:
091: if (useCrossClassLoaderRef && isRemote(deploymentInfo)) {
092: reference = new CrossClassLoaderJndiReference(jndiName);
093: } else {
094: reference = new IntraVmJndiReference(jndiName);
095: }
096:
097: return reference.getObject();
098: }
099:
100: private boolean isRemote(DeploymentInfo deploymentInfo) {
101: switch (info.getRefType()) {
102: case REMOTE:
103: return true;
104: case LOCAL:
105: return false;
106: case UNKNOWN: {
107: for (Class clazz : deploymentInfo
108: .getInterfaces(InterfaceType.BUSINESS_REMOTE)) {
109: if (clazz.getName().equals(info.getInterface()))
110: return true;
111: }
112: }
113: ;
114: default:
115: return false;
116: }
117: }
118: }
|