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.util.LinkResolver;
019:
020: import java.net.URI;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.TreeMap;
024: import java.util.Arrays;
025:
026: /**
027: * @version $Rev$ $Date$
028: */
029: public class EjbResolver {
030:
031: public static interface Reference {
032:
033: String getName();
034:
035: Type getRefType();
036:
037: String getHome();
038:
039: String getInterface();
040:
041: String getMappedName();
042:
043: String getEjbLink();
044: }
045:
046: public static enum Type {
047: UNKNOWN, LOCAL, REMOTE;
048: }
049:
050: public static enum Scope {
051: GLOBAL, EAR, EJBJAR;
052: }
053:
054: private final Map<String, EnterpriseBeanInfo> deployments = new TreeMap<String, EnterpriseBeanInfo>();
055: private final LinkResolver<String> resolver = new LinkResolver<String>();
056: private final Map<Interfaces, String> remoteInterfaces = new TreeMap<Interfaces, String>();
057: private final Map<Interfaces, String> localInterfaces = new TreeMap<Interfaces, String>();
058:
059: private EjbResolver parent;
060:
061: private final Scope scope;
062:
063: public EjbResolver(EjbResolver parent, Scope scope,
064: EjbJarInfo... ejbJars) {
065: this (parent, scope, Arrays.asList(ejbJars));
066: }
067:
068: public EjbResolver(EjbResolver parent, Scope scope,
069: List<EjbJarInfo> ejbJars) {
070: this .scope = scope;
071: this .parent = parent;
072:
073: for (EjbJarInfo ejbJarInfo : ejbJars) {
074: add(ejbJarInfo);
075: }
076: }
077:
078: /**
079: * Possible syncronization issue here
080: */
081: public void addAll(List<EjbJarInfo> ejbJars) {
082: for (EjbJarInfo ejbJarInfo : ejbJars) {
083: add(ejbJarInfo);
084: }
085: }
086:
087: private void add(EjbJarInfo ejbJarInfo) {
088: for (EnterpriseBeanInfo bean : ejbJarInfo.enterpriseBeans) {
089: index(ejbJarInfo.moduleId, bean);
090: }
091: }
092:
093: private void index(String moduleId, EnterpriseBeanInfo bean) {
094: // All deployments: deploymentId -> bean
095: deployments.put(bean.ejbDeploymentId, bean);
096:
097: // add to the link resolver
098: resolver.add(moduleId, bean.ejbName, bean.ejbDeploymentId);
099:
100: // Remote: Interfaces(home,object) -> deploymentId
101: if (bean.remote != null) {
102: remoteInterfaces.put(
103: new Interfaces(bean.home, bean.remote),
104: bean.ejbDeploymentId);
105: remoteInterfaces.put(new Interfaces(bean.remote),
106: bean.ejbDeploymentId);
107: }
108: for (String businessRemote : bean.businessRemote) {
109: remoteInterfaces.put(new Interfaces(businessRemote),
110: bean.ejbDeploymentId);
111: }
112:
113: // Local: Interfaces(home,object) -> deploymentId
114: if (bean.local != null) {
115: localInterfaces.put(new Interfaces(bean.localHome,
116: bean.local), bean.ejbDeploymentId);
117: localInterfaces.put(new Interfaces(bean.local),
118: bean.ejbDeploymentId);
119: }
120: for (String businessLocal : bean.businessLocal) {
121: localInterfaces.put(new Interfaces(businessLocal),
122: bean.ejbDeploymentId);
123: }
124: }
125:
126: private String resolveLink(String link, URI moduleUri) {
127: if (link == null || link.length() == 0)
128: return null;
129:
130: String id = resolver.resolveLink(link, moduleUri);
131: if (id == null && parent != null) {
132: id = parent.resolveLink(link, moduleUri);
133: }
134: return id;
135: }
136:
137: private String resolveInterface(Type type, String homeInterface,
138: String objectInterface) {
139: Interfaces interfaces = new Interfaces(homeInterface,
140: objectInterface);
141: return resolveInterface(type, interfaces);
142: }
143:
144: private String resolveInterface(Type type, Interfaces interfaces) {
145: String id;
146: switch (type) {
147: case UNKNOWN:
148: case REMOTE: {
149: id = remoteInterfaces.get(interfaces);
150: id = (id != null) ? id : localInterfaces.get(interfaces);
151: }
152: break;
153: case LOCAL: {
154: id = localInterfaces.get(interfaces);
155: id = (id != null) ? id : remoteInterfaces.get(interfaces);
156: }
157: break;
158: default:
159: id = null;
160: }
161:
162: if (id == null && parent != null) {
163: id = parent.resolveInterface(type, interfaces);
164: }
165:
166: return id;
167: }
168:
169: public Scope getScope(String deploymentId) {
170: if (deployments.containsKey(deploymentId))
171: return scope;
172:
173: if (parent != null)
174: return parent.getScope(deploymentId);
175:
176: return null;
177: }
178:
179: public EnterpriseBeanInfo getEnterpriseBeanInfo(String deploymentId) {
180: EnterpriseBeanInfo info = deployments.get(deploymentId);
181: if (info == null && parent != null) {
182: info = parent.getEnterpriseBeanInfo(deploymentId);
183: }
184: return info;
185: }
186:
187: public String resolve(Reference ref, URI moduleUri) {
188:
189: if (ref.getMappedName() != null
190: && !ref.getMappedName().equals("")) {
191: return ref.getMappedName();
192: }
193:
194: String targetId = this .resolveLink(ref.getEjbLink(), moduleUri);
195:
196: if (targetId == null && ref.getEjbLink() == null) {
197: targetId = this .resolveInterface(ref.getRefType(), ref
198: .getHome(), ref.getInterface());
199: }
200:
201: return targetId;
202: }
203:
204: private static class Interfaces implements Comparable {
205: private final String homeInterface;
206: private final String objectInterface;
207:
208: public Interfaces(String objectInterface) {
209: if (objectInterface == null)
210: throw new NullPointerException(
211: "objectInterface is null");
212: this .homeInterface = "<none>";
213: this .objectInterface = objectInterface;
214: }
215:
216: public Interfaces(String homeInterface, String objectInterface) {
217: if (homeInterface == null)
218: homeInterface = "<none>";
219: if (objectInterface == null)
220: throw new NullPointerException(
221: "objectInterface is null");
222: this .homeInterface = homeInterface;
223: this .objectInterface = objectInterface;
224: }
225:
226: public boolean equals(Object o) {
227: if (this == o)
228: return true;
229: if (o == null || getClass() != o.getClass())
230: return false;
231:
232: Interfaces that = (Interfaces) o;
233:
234: return homeInterface.equals(that.homeInterface)
235: && objectInterface.equals(that.objectInterface);
236: }
237:
238: public int hashCode() {
239: int result;
240: result = homeInterface.hashCode();
241: result = 31 * result + objectInterface.hashCode();
242: return result;
243: }
244:
245: public int compareTo(Object o) {
246: if (this == o)
247: return 0;
248:
249: Interfaces that = (Interfaces) o;
250: return toString().compareTo(that.toString());
251: }
252:
253: public String toString() {
254: return homeInterface + ":" + objectInterface;
255: }
256: }
257:
258: }
|