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.config;
017:
018: import static org.apache.openejb.assembler.classic.EjbResolver.Scope.EJBJAR;
019: import static org.apache.openejb.assembler.classic.EjbResolver.Scope.EAR;
020: import org.apache.openejb.OpenEJBException;
021: import org.apache.openejb.loader.SystemInstance;
022: import org.apache.openejb.assembler.classic.AppInfo;
023: import org.apache.openejb.assembler.classic.EjbLocalReferenceInfo;
024: import org.apache.openejb.assembler.classic.EjbReferenceInfo;
025: import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
026: import org.apache.openejb.assembler.classic.EnvEntryInfo;
027: import org.apache.openejb.assembler.classic.InjectionInfo;
028: import org.apache.openejb.assembler.classic.JndiEncInfo;
029: import org.apache.openejb.assembler.classic.PersistenceContextReferenceInfo;
030: import org.apache.openejb.assembler.classic.PersistenceUnitReferenceInfo;
031: import org.apache.openejb.assembler.classic.PortRefInfo;
032: import org.apache.openejb.assembler.classic.ReferenceLocationInfo;
033: import org.apache.openejb.assembler.classic.ResourceEnvReferenceInfo;
034: import org.apache.openejb.assembler.classic.ResourceReferenceInfo;
035: import org.apache.openejb.assembler.classic.ServiceReferenceInfo;
036: import org.apache.openejb.assembler.classic.EjbResolver;
037: import org.apache.openejb.assembler.classic.EjbJarInfo;
038: import org.apache.openejb.jee.EjbLocalRef;
039: import org.apache.openejb.jee.EnvEntry;
040: import org.apache.openejb.jee.Injectable;
041: import org.apache.openejb.jee.InjectionTarget;
042: import org.apache.openejb.jee.JndiConsumer;
043: import org.apache.openejb.jee.JndiReference;
044: import org.apache.openejb.jee.MessageDestinationRef;
045: import org.apache.openejb.jee.PersistenceContextRef;
046: import org.apache.openejb.jee.PersistenceContextType;
047: import org.apache.openejb.jee.PersistenceUnitRef;
048: import org.apache.openejb.jee.PortComponentRef;
049: import org.apache.openejb.jee.Property;
050: import org.apache.openejb.jee.ResAuth;
051: import org.apache.openejb.jee.ResourceEnvRef;
052: import org.apache.openejb.jee.ResourceRef;
053: import org.apache.openejb.jee.ServiceRef;
054: import org.apache.openejb.jee.EjbReference;
055: import org.apache.openejb.util.LogCategory;
056: import org.apache.openejb.util.Logger;
057: import org.apache.openejb.util.Messages;
058:
059: import java.net.URI;
060: import java.net.URISyntaxException;
061: import java.util.ArrayList;
062: import java.util.Collection;
063: import java.util.List;
064: import java.util.Map;
065: import java.util.HashMap;
066:
067: /**
068: * @version $Rev: 634559 $ $Date: 2008-03-06 22:38:37 -0800 $
069: */
070: public class JndiEncInfoBuilder {
071:
072: public static final Logger logger = Logger.getInstance(
073: LogCategory.OPENEJB_STARTUP, JndiEncInfoBuilder.class);
074: protected static final Messages messages = new Messages(
075: JndiEncInfoBuilder.class);
076:
077: private final EjbResolver earResolver;
078: private final Map<String, EjbResolver> ejbJarResolvers = new HashMap<String, EjbResolver>();
079:
080: public JndiEncInfoBuilder(AppInfo appInfo) {
081:
082: // Global-scoped EJB Resolver
083:
084: EjbResolver globalResolver = SystemInstance.get().getComponent(
085: EjbResolver.class);
086:
087: // EAR-scoped EJB Resolver
088:
089: earResolver = new EjbResolver(globalResolver, EAR,
090: appInfo.ejbJars);
091:
092: // EJBJAR-scoped EJB Resolver(s)
093:
094: for (EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
095:
096: EjbResolver ejbJarResolver = new EjbResolver(earResolver,
097: EJBJAR, ejbJarInfo);
098:
099: ejbJarResolvers.put(ejbJarInfo.moduleId, ejbJarResolver);
100: }
101: }
102:
103: private EjbResolver getEjbResolver(String moduleId) {
104: EjbResolver resolver = ejbJarResolvers.get(moduleId);
105: if (resolver == null) {
106: resolver = earResolver;
107: }
108: return resolver;
109: }
110:
111: public JndiEncInfo build(JndiConsumer jndiConsumer, String ejbName,
112: String moduleId) throws OpenEJBException {
113: URI moduleUri = null;
114: if (moduleId != null) {
115: try {
116: moduleUri = new URI(moduleId);
117: } catch (URISyntaxException e) {
118: throw new OpenEJBException("Illegal moduleId "
119: + moduleId, e);
120: }
121: }
122:
123: JndiEncInfo jndi = new JndiEncInfo();
124:
125: /* Build Environment entries *****************/
126: jndi.envEntries.addAll(buildEnvEntryInfos(jndiConsumer));
127:
128: /* Build Resource References *****************/
129: jndi.resourceRefs.addAll(buildResourceRefInfos(jndiConsumer));
130:
131: /* Build Resource Environment References *****************/
132: jndi.resourceEnvRefs
133: .addAll(buildResourceEnvRefInfos(jndiConsumer));
134:
135: buildEjbRefs(jndiConsumer, moduleUri, moduleId, ejbName, jndi);
136:
137: jndi.persistenceUnitRefs.addAll(buildPersistenceUnitRefInfos(
138: jndiConsumer, moduleUri));
139:
140: jndi.persistenceContextRefs
141: .addAll(buildPersistenceContextRefInfos(jndiConsumer,
142: moduleUri));
143:
144: jndi.serviceRefs.addAll(buildServiceRefInfos(jndiConsumer));
145:
146: return jndi;
147: }
148:
149: private void buildEjbRefs(JndiConsumer jndiConsumer, URI moduleUri,
150: String moduleId, String ejbName, JndiEncInfo jndi)
151: throws OpenEJBException {
152:
153: int size = jndiConsumer.getEjbRef().size()
154: + jndiConsumer.getEjbLocalRef().size();
155:
156: ArrayList<EjbReference> references = new ArrayList<EjbReference>(
157: size);
158:
159: references.addAll(jndiConsumer.getEjbRef());
160: references.addAll(jndiConsumer.getEjbLocalRef());
161:
162: for (EjbReference ref : references) {
163:
164: EjbReferenceInfo info = new EjbReferenceInfo();
165:
166: info.homeType = ref.getHome();
167: info.interfaceType = ref.getInterface();
168: info.referenceName = ref.getName();
169: info.link = ref.getEjbLink();
170: info.location = buildLocationInfo(ref);
171: info.targets.addAll(buildInjectionInfos(ref));
172:
173: if (info.location != null) {
174: if (ref.getRefType() == EjbReference.Type.LOCAL) {
175: jndi.ejbLocalReferences.add(toLocal(info));
176: } else {
177: jndi.ejbReferences.add(info);
178: }
179: continue;
180: }
181:
182: EjbResolver ejbResolver = getEjbResolver(moduleId);
183:
184: String deploymentId = ejbResolver.resolve(new Ref(ref),
185: moduleUri);
186:
187: info.ejbDeploymentId = deploymentId;
188:
189: if (info.ejbDeploymentId == null) {
190: if (info.link != null) {
191: logger.warning("config.noBeanFoundEjbLink", ref
192: .getName(), ejbName, ref.getEjbLink());
193: } else {
194: logger.warning("config.noBeanFound", ref.getName(),
195: ejbName, ref.getEjbLink());
196: }
197: if (ref.getRefType() == EjbReference.Type.LOCAL) {
198: jndi.ejbLocalReferences.add(toLocal(info));
199: } else {
200: jndi.ejbReferences.add(info);
201: }
202: continue;
203: }
204:
205: EjbResolver.Scope scope = ejbResolver
206: .getScope(deploymentId);
207:
208: info.externalReference = (scope != EAR && scope != EJBJAR);
209:
210: if (ref.getRefType() == EjbReference.Type.UNKNOWN) {
211: EnterpriseBeanInfo otherBean = ejbResolver
212: .getEnterpriseBeanInfo(deploymentId);
213: if (otherBean != null) {
214: if (otherBean.businessRemote.contains(ref
215: .getInterface())) {
216: ref.setRefType(EjbReference.Type.REMOTE);
217: } else {
218: ref.setRefType(EjbReference.Type.LOCAL);
219: jndiConsumer.getEjbRef().remove(ref);
220: jndiConsumer.getEjbLocalRef().add(
221: new EjbLocalRef(ref));
222: }
223: }
224: }
225:
226: if (ref.getRefType() == EjbReference.Type.LOCAL) {
227: jndi.ejbLocalReferences.add(toLocal(info));
228: } else {
229: jndi.ejbReferences.add(info);
230: }
231:
232: }
233: }
234:
235: private EjbLocalReferenceInfo toLocal(EjbReferenceInfo r) {
236: EjbLocalReferenceInfo l = new EjbLocalReferenceInfo();
237: l.ejbDeploymentId = r.ejbDeploymentId;
238: l.externalReference = r.externalReference;
239: l.homeType = r.homeType;
240: l.interfaceType = r.interfaceType;
241: l.referenceName = r.referenceName;
242: l.link = r.link;
243: l.location = r.location;
244: l.targets.addAll(r.targets);
245: return l;
246: }
247:
248: private List<ServiceReferenceInfo> buildServiceRefInfos(
249: JndiConsumer jndiConsumer) {
250: ArrayList<ServiceReferenceInfo> infos = new ArrayList<ServiceReferenceInfo>();
251: for (ServiceRef ref : jndiConsumer.getServiceRef()) {
252: ServiceReferenceInfo info = new ServiceReferenceInfo();
253: info.referenceName = ref.getName();
254: info.location = buildLocationInfo(ref);
255: info.targets.addAll(buildInjectionInfos(ref));
256: infos.add(info);
257:
258: if (System.getProperty("duct tape") != null)
259: continue;
260:
261: info.id = ref.getMappedName();
262: info.serviceQName = ref.getServiceQname();
263: info.serviceType = ref.getServiceInterface();
264: info.referenceType = ref.getServiceRefType();
265: info.wsdlFile = ref.getWsdlFile();
266: info.jaxrpcMappingFile = ref.getJaxrpcMappingFile();
267: info.handlerChains.addAll(ConfigurationFactory
268: .toHandlerChainInfo(ref.getAllHandlers()));
269: for (PortComponentRef portComponentRef : ref
270: .getPortComponentRef()) {
271: PortRefInfo portRefInfo = new PortRefInfo();
272: portRefInfo.qname = portComponentRef.getQName();
273: portRefInfo.serviceEndpointInterface = portComponentRef
274: .getServiceEndpointInterface();
275: portRefInfo.enableMtom = portComponentRef
276: .isEnableMtom();
277: portRefInfo.properties.putAll(portComponentRef
278: .getProperties());
279: info.portRefs.add(portRefInfo);
280: }
281: }
282: return infos;
283: }
284:
285: private List<PersistenceUnitReferenceInfo> buildPersistenceUnitRefInfos(
286: JndiConsumer jndiConsumer, URI moduleId) {
287: ArrayList<PersistenceUnitReferenceInfo> infos = new ArrayList<PersistenceUnitReferenceInfo>();
288: for (PersistenceUnitRef puRef : jndiConsumer
289: .getPersistenceUnitRef()) {
290: PersistenceUnitReferenceInfo info = new PersistenceUnitReferenceInfo();
291: info.referenceName = puRef.getPersistenceUnitRefName();
292: info.persistenceUnitName = puRef.getPersistenceUnitName();
293: info.unitId = puRef.getMappedName();
294: info.location = buildLocationInfo(puRef);
295: info.targets.addAll(buildInjectionInfos(puRef));
296: infos.add(info);
297: }
298: return infos;
299: }
300:
301: private List<PersistenceContextReferenceInfo> buildPersistenceContextRefInfos(
302: JndiConsumer jndiConsumer, URI moduleId) {
303: ArrayList<PersistenceContextReferenceInfo> infos = new ArrayList<PersistenceContextReferenceInfo>();
304:
305: for (PersistenceContextRef contextRef : jndiConsumer
306: .getPersistenceContextRef()) {
307: PersistenceContextReferenceInfo info = new PersistenceContextReferenceInfo();
308: info.referenceName = contextRef
309: .getPersistenceContextRefName();
310: info.persistenceUnitName = contextRef
311: .getPersistenceUnitName();
312: info.unitId = contextRef.getMappedName();
313: info.location = buildLocationInfo(contextRef);
314: info.extended = (contextRef.getPersistenceContextType() == PersistenceContextType.EXTENDED);
315: List<Property> persistenceProperty = contextRef
316: .getPersistenceProperty();
317: for (Property property : persistenceProperty) {
318: String name = property.getName();
319: String value = property.getValue();
320: info.properties.setProperty(name, value);
321: }
322: info.targets.addAll(buildInjectionInfos(contextRef));
323: infos.add(info);
324: }
325: return infos;
326: }
327:
328: private List<ResourceReferenceInfo> buildResourceRefInfos(
329: JndiConsumer item) {
330: List<ResourceReferenceInfo> infos = new ArrayList<ResourceReferenceInfo>();
331: for (ResourceRef res : item.getResourceRef()) {
332: ResourceReferenceInfo info = new ResourceReferenceInfo();
333:
334: if (res.getResAuth() != null) {
335: info.referenceAuth = res.getResAuth().toString();
336: } else {
337: info.referenceAuth = ResAuth.CONTAINER.toString();
338: }
339: info.referenceName = res.getResRefName();
340: info.referenceType = res.getResType();
341: info.resourceID = res.getMappedName();
342: info.location = buildLocationInfo(res);
343: info.targets.addAll(buildInjectionInfos(res));
344: infos.add(info);
345: }
346: return infos;
347: }
348:
349: private List<ResourceEnvReferenceInfo> buildResourceEnvRefInfos(
350: JndiConsumer item) {
351: List<ResourceEnvReferenceInfo> infos = new ArrayList<ResourceEnvReferenceInfo>();
352: for (ResourceEnvRef res : item.getResourceEnvRef()) {
353: ResourceEnvReferenceInfo info = new ResourceEnvReferenceInfo();
354: info.resourceEnvRefName = res.getResourceEnvRefName();
355: info.resourceEnvRefType = res.getResourceEnvRefType();
356: info.resourceID = res.getMappedName();
357: info.location = buildLocationInfo(res);
358: info.targets.addAll(buildInjectionInfos(res));
359: infos.add(info);
360: }
361: for (MessageDestinationRef res : item
362: .getMessageDestinationRef()) {
363: ResourceEnvReferenceInfo info = new ResourceEnvReferenceInfo();
364: info.resourceEnvRefName = res
365: .getMessageDestinationRefName();
366: info.resourceEnvRefType = res.getMessageDestinationType();
367: info.resourceID = res.getMappedName();
368: info.location = buildLocationInfo(res);
369: info.targets.addAll(buildInjectionInfos(res));
370: infos.add(info);
371: }
372: return infos;
373: }
374:
375: private List<EnvEntryInfo> buildEnvEntryInfos(JndiConsumer item) {
376: List<EnvEntryInfo> infos = new ArrayList<EnvEntryInfo>();
377: for (EnvEntry env : item.getEnvEntry()) {
378: // ignore env entries without a value
379: if (env.getEnvEntryValue() == null) {
380: continue;
381: }
382:
383: EnvEntryInfo info = new EnvEntryInfo();
384:
385: info.name = env.getEnvEntryName();
386: info.type = env.getEnvEntryType();
387: info.value = env.getEnvEntryValue();
388: info.location = buildLocationInfo(env);
389: info.targets.addAll(buildInjectionInfos(env));
390: infos.add(info);
391: }
392: return infos;
393: }
394:
395: public ReferenceLocationInfo buildLocationInfo(
396: JndiReference reference) {
397: String mappedName = reference.getMappedName();
398: if (mappedName == null || !mappedName.startsWith("jndi:")) {
399: return null;
400: }
401: ReferenceLocationInfo location = new ReferenceLocationInfo();
402: location.jndiName = mappedName.replaceFirst("^jndi:", "");
403: return location;
404: }
405:
406: public Collection<? extends InjectionInfo> buildInjectionInfos(
407: Injectable injectable) {
408: ArrayList<InjectionInfo> infos = new ArrayList<InjectionInfo>();
409: for (InjectionTarget target : injectable.getInjectionTarget()) {
410: InjectionInfo info = new InjectionInfo();
411: info.className = target.getInjectionTargetClass();
412: info.propertyName = target.getInjectionTargetName();
413: infos.add(info);
414: }
415: return infos;
416: }
417:
418: /**
419: * The assembler package cannot have a dependency on org.apache.openejb.jee
420: * so we simply have a trimmed down copy of the org.apache.openejb.jee.EjbReference interface
421: * and we adapt to it here.
422: */
423: private static class Ref implements EjbResolver.Reference {
424: private final EjbReference ref;
425:
426: public Ref(EjbReference ref) {
427: this .ref = ref;
428: }
429:
430: public String getName() {
431: return ref.getName();
432: }
433:
434: public String getEjbLink() {
435: return ref.getEjbLink();
436: }
437:
438: public String getHome() {
439: return ref.getHome();
440: }
441:
442: public String getInterface() {
443: return ref.getInterface();
444: }
445:
446: public String getMappedName() {
447: return ref.getMappedName();
448: }
449:
450: public EjbResolver.Type getRefType() {
451: // Could have used EjbResolver.Type.valueOf(..)
452: // but this protects against an renaming
453: switch (ref.getRefType()) {
454: case LOCAL:
455: return EjbResolver.Type.LOCAL;
456: case REMOTE:
457: return EjbResolver.Type.REMOTE;
458: case UNKNOWN:
459: return EjbResolver.Type.UNKNOWN;
460: default:
461: return EjbResolver.Type.UNKNOWN;
462: }
463: }
464: }
465: }
|