0001: /**
0002: * EasyBeans
0003: * Copyright (C) 2006 Bull S.A.S.
0004: * Contact: easybeans@ow2.org
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation; either
0009: * version 2.1 of the License, or any later version.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU Lesser General Public
0017: * License along with this library; if not, write to the Free Software
0018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
0019: * USA
0020: *
0021: * --------------------------------------------------------------------------
0022: * $Id: EJB3DeploymentDescLoader.java 2052 2007-11-20 14:38:00Z benoitf $
0023: * --------------------------------------------------------------------------
0024: */package org.ow2.easybeans.deployment.xml.parsing;
0025:
0026: import static org.ow2.easybeans.deployment.xml.struct.common.LifeCycleCallback.POST_ACTIVATE;
0027: import static org.ow2.easybeans.deployment.xml.struct.common.LifeCycleCallback.POST_CONSTRUCT;
0028: import static org.ow2.easybeans.deployment.xml.struct.common.LifeCycleCallback.PRE_DESTROY;
0029: import static org.ow2.easybeans.deployment.xml.struct.common.LifeCycleCallback.PRE_PASSIVATE;
0030:
0031: import java.net.URL;
0032: import java.util.List;
0033:
0034: import javax.ejb.TransactionAttributeType;
0035:
0036: import org.ow2.easybeans.deployment.annotations.impl.JApplicationException;
0037: import org.ow2.easybeans.deployment.xml.struct.AbsBean;
0038: import org.ow2.easybeans.deployment.xml.struct.AssemblyDescriptor;
0039: import org.ow2.easybeans.deployment.xml.struct.ContainerTransaction;
0040: import org.ow2.easybeans.deployment.xml.struct.EJB3;
0041: import org.ow2.easybeans.deployment.xml.struct.EnterpriseBeans;
0042: import org.ow2.easybeans.deployment.xml.struct.Interceptor;
0043: import org.ow2.easybeans.deployment.xml.struct.InterceptorBinding;
0044: import org.ow2.easybeans.deployment.xml.struct.Interceptors;
0045: import org.ow2.easybeans.deployment.xml.struct.MessageDriven;
0046: import org.ow2.easybeans.deployment.xml.struct.MethodPermission;
0047: import org.ow2.easybeans.deployment.xml.struct.RemoveMethod;
0048: import org.ow2.easybeans.deployment.xml.struct.Session;
0049: import org.ow2.easybeans.deployment.xml.struct.common.AbsEJBRef;
0050: import org.ow2.easybeans.deployment.xml.struct.common.AbsEnvironment;
0051: import org.ow2.easybeans.deployment.xml.struct.common.AbsResourceGroup;
0052: import org.ow2.easybeans.deployment.xml.struct.common.ActivationConfig;
0053: import org.ow2.easybeans.deployment.xml.struct.common.AroundInvoke;
0054: import org.ow2.easybeans.deployment.xml.struct.common.EJBLocalRef;
0055: import org.ow2.easybeans.deployment.xml.struct.common.EJBRef;
0056: import org.ow2.easybeans.deployment.xml.struct.common.EnvEntry;
0057: import org.ow2.easybeans.deployment.xml.struct.common.InjectionTarget;
0058: import org.ow2.easybeans.deployment.xml.struct.common.LifeCycleCallback;
0059: import org.ow2.easybeans.deployment.xml.struct.common.MessageDestinationRef;
0060: import org.ow2.easybeans.deployment.xml.struct.common.MethodDD;
0061: import org.ow2.easybeans.deployment.xml.struct.common.ResourceEnvRef;
0062: import org.ow2.easybeans.deployment.xml.struct.common.ResourceRef;
0063: import org.ow2.easybeans.util.xml.DocumentParser;
0064: import org.ow2.easybeans.util.xml.DocumentParserException;
0065: import org.ow2.easybeans.util.xml.XMLUtils;
0066: import org.ow2.util.log.Log;
0067: import org.ow2.util.log.LogFactory;
0068: import org.w3c.dom.Document;
0069: import org.w3c.dom.Element;
0070: import org.w3c.dom.NodeList;
0071:
0072: /**
0073: * This class analyzes the EJB3 deployment descriptor and fill a struct with
0074: * getters.
0075: * @author Florent Benoit
0076: */
0077: public final class EJB3DeploymentDescLoader extends
0078: AbsDeploymentDescLoader {
0079:
0080: /**
0081: * Logger.
0082: */
0083: private static Log logger = LogFactory
0084: .getLog(EJB3DeploymentDescLoader.class);
0085:
0086: /**
0087: * EJB3 instance object that is the result of the parsing.
0088: */
0089: private EJB3 ejb3 = null;
0090:
0091: /**
0092: * Utility class, no public constructor.
0093: * @param url the url that is pointing to the EJB Xml file.
0094: */
0095: private EJB3DeploymentDescLoader(final URL url) {
0096: super (url);
0097: this .ejb3 = new EJB3();
0098: }
0099:
0100: /**
0101: * Load the ejb-jar.xml file.
0102: * @param url the URL of the XML file.
0103: * @throws ParsingException if parsing of XML file fails.
0104: * @return an application object.
0105: */
0106: public static EJB3 loadDeploymentDescriptor(final URL url)
0107: throws ParsingException {
0108: logger.debug("Analyzing url {0}", url);
0109:
0110: EJB3DeploymentDescLoader ejbDeploymentDescLoader = new EJB3DeploymentDescLoader(
0111: url);
0112: ejbDeploymentDescLoader.parse();
0113: return ejbDeploymentDescLoader.getEJB3();
0114: }
0115:
0116: /**
0117: * Analyze the URL and build an EJB3 object which contains data of the XML.
0118: * @throws ParsingException if the analyze of the file fails.
0119: */
0120: public void parse() throws ParsingException {
0121: // Get document
0122: Document document = null;
0123: try {
0124: document = DocumentParser.getDocument(getURL(),
0125: isValidating(), new EJB3EntityResolver());
0126: } catch (DocumentParserException e) {
0127: throw new ParsingException("Cannot parse the url", e);
0128: }
0129:
0130: // Root element = <ejb-jar>
0131: Element ejbJarRootElement = document.getDocumentElement();
0132:
0133: // Define NS
0134: defineXMLNS(ejbJarRootElement);
0135:
0136: // enterprise-beans
0137: NodeList enterprisebeansList = ejbJarRootElement
0138: .getElementsByTagNameNS(getXMLNS(),
0139: EnterpriseBeans.NAME);
0140: if (enterprisebeansList.getLength() == 1) {
0141: Element enterpriseBeansElement = (Element) enterprisebeansList
0142: .item(0);
0143:
0144: // Create struct
0145: EnterpriseBeans enterpriseBeans = new EnterpriseBeans();
0146: ejb3.setEnterpriseBeans(enterpriseBeans);
0147:
0148: // Analyze beans
0149: analyzeBeans(enterpriseBeansElement, enterpriseBeans);
0150:
0151: }
0152:
0153: // interceptors
0154: NodeList interceptorsList = ejbJarRootElement
0155: .getElementsByTagNameNS(getXMLNS(), Interceptors.NAME);
0156: if (interceptorsList.getLength() == 1) {
0157: Element interceptorsElement = (Element) interceptorsList
0158: .item(0);
0159:
0160: // Create struct
0161: Interceptors interceptors = new Interceptors();
0162: ejb3.setInterceptors(interceptors);
0163:
0164: // Analyze the interceptors element
0165: analyzeInterceptorsElement(interceptorsElement,
0166: interceptors);
0167: }
0168:
0169: // assembly-descriptor
0170: NodeList assemblyDescriptorList = ejbJarRootElement
0171: .getElementsByTagNameNS(getXMLNS(),
0172: AssemblyDescriptor.NAME);
0173: if (assemblyDescriptorList.getLength() == 1) {
0174: Element assemblyDescriptorElement = (Element) assemblyDescriptorList
0175: .item(0);
0176:
0177: // Create struct
0178: AssemblyDescriptor assemblyDescriptor = new AssemblyDescriptor();
0179: ejb3.setAssemblyDescriptor(assemblyDescriptor);
0180:
0181: // Analyze the assembly-descriptor element
0182: analyzeAssemblyDescriptor(assemblyDescriptorElement,
0183: assemblyDescriptor);
0184: }
0185:
0186: }
0187:
0188: /**
0189: * Analyze common stuff of beans.
0190: * @param enterpriseBeansElement the dom element.
0191: * @param enterpriseBeans the structure representing object.
0192: */
0193: private void analyzeBeans(final Element enterpriseBeansElement,
0194: final EnterpriseBeans enterpriseBeans) {
0195: // Get session beans
0196: NodeList sessionBeansList = enterpriseBeansElement
0197: .getElementsByTagNameNS(getXMLNS(), Session.NAME);
0198:
0199: // Loop on session beans
0200: for (int i = 0; i < sessionBeansList.getLength(); i++) {
0201: Element sessionElement = (Element) sessionBeansList.item(i);
0202:
0203: // Build instance that is created.
0204: Session session = new Session();
0205: enterpriseBeans.addSession(session);
0206:
0207: // Analyze session bean
0208: analyzeSessionBean(sessionElement, session);
0209: }
0210:
0211: // Get MDB beans
0212: NodeList messageDrivenBeansList = enterpriseBeansElement
0213: .getElementsByTagNameNS(getXMLNS(), MessageDriven.NAME);
0214:
0215: // Loop on MDB
0216: for (int i = 0; i < messageDrivenBeansList.getLength(); i++) {
0217: Element mdbElement = (Element) messageDrivenBeansList
0218: .item(i);
0219:
0220: // Build instance that is created.
0221: MessageDriven messageDriven = new MessageDriven();
0222: enterpriseBeans.addMessageDriven(messageDriven);
0223:
0224: // Analyze MDB
0225: analyzeMessageDrivenBean(mdbElement, messageDriven);
0226: }
0227:
0228: }
0229:
0230: /**
0231: * Analyze common stuff of a bean.
0232: * @param beanElement the dom element.
0233: * @param bean the structure representing object.
0234: */
0235: private void analyzeBean(final Element beanElement,
0236: final AbsBean bean) {
0237: // Get ejb-name
0238: String ejbName = XMLUtils.getStringValueElement(getXMLNS(),
0239: beanElement, "ejb-name");
0240: bean.setEjbName(ejbName);
0241:
0242: // Get ejb-class
0243: String ejbClass = XMLUtils.getStringValueElement(getXMLNS(),
0244: beanElement, "ejb-class");
0245: bean.setEjbClass(ejbClass);
0246:
0247: // Security identity (run-as) if any
0248: String runAsRole = XMLUtils.getStringValueElement(getXMLNS(),
0249: beanElement, "role-name");
0250: if (runAsRole != null) {
0251: bean.setRunAsRole(runAsRole);
0252: }
0253:
0254: // Mapped Name
0255: String mappedName = XMLUtils.getChildStringValueForElement(
0256: beanElement, "mapped-name");
0257: if (mappedName != null) {
0258: bean.setMappedName(mappedName);
0259: }
0260:
0261: // Get transaction-type
0262: String transactionType = XMLUtils.getStringValueElement(
0263: getXMLNS(), beanElement, "transaction-type");
0264: bean.setTransactionType(transactionType);
0265:
0266: // Analyze common things
0267: analyzeEnvironment(beanElement, bean);
0268: }
0269:
0270: /**
0271: * Analyze XML of a MDB.
0272: * @param beanElement the dom element.
0273: * @param bean the structure representing object.
0274: */
0275: private void analyzeMessageDrivenBean(final Element beanElement,
0276: final MessageDriven bean) {
0277: analyzeBean(beanElement, bean);
0278:
0279: // Message destination type
0280: String messageDestinationType = XMLUtils.getStringValueElement(
0281: getXMLNS(), beanElement, "message-destination-type");
0282: bean.setMessageDestinationType(messageDestinationType);
0283:
0284: // Message destination link
0285: String messageDestinationLink = XMLUtils.getStringValueElement(
0286: getXMLNS(), beanElement, "message-destination-link");
0287: bean.setMessageDestinationLink(messageDestinationLink);
0288:
0289: // Messaging type
0290: String messagingType = XMLUtils.getStringValueElement(
0291: getXMLNS(), beanElement, "messaging-type");
0292: bean.setMessagingType(messagingType);
0293:
0294: // Get activation-config
0295: NodeList activationConfigList = beanElement
0296: .getElementsByTagNameNS(getXMLNS(),
0297: ActivationConfig.NAME);
0298: // Loop on the activation-config
0299: for (int i = 0; i < activationConfigList.getLength(); i++) {
0300: Element activationConfigElement = (Element) activationConfigList
0301: .item(i);
0302:
0303: // Get activation-config-property
0304: NodeList activationConfigPropertyList = activationConfigElement
0305: .getElementsByTagNameNS(getXMLNS(),
0306: "activation-config-property");
0307: for (int j = 0; j < activationConfigPropertyList
0308: .getLength(); j++) {
0309: Element activationConfigPropertyElement = (Element) activationConfigPropertyList
0310: .item(j);
0311:
0312: // Build Object
0313: ActivationConfig activationConfig = new ActivationConfig();
0314: bean.addActivationConfig(activationConfig);
0315: String name = XMLUtils.getStringValueElement(
0316: getXMLNS(), activationConfigPropertyElement,
0317: "activation-config-property-name");
0318: String value = XMLUtils.getStringValueElement(
0319: getXMLNS(), activationConfigPropertyElement,
0320: "activation-config-property-value");
0321: activationConfig.setName(name);
0322: activationConfig.setValue(value);
0323: }
0324: }
0325:
0326: }
0327:
0328: /**
0329: * Analyze XML of a session bean.
0330: * @param beanElement the dom element.
0331: * @param bean the structure representing object.
0332: */
0333: private void analyzeSessionBean(final Element beanElement,
0334: final Session bean) {
0335: analyzeBean(beanElement, bean);
0336:
0337: // Get business-remote
0338: List<String> businessRemoteList = XMLUtils
0339: .getStringListValueElement(getXMLNS(), beanElement,
0340: "business-remote");
0341: for (String businessRemote : businessRemoteList) {
0342: bean.addBusinessRemote(businessRemote);
0343: }
0344:
0345: // Get business-local
0346: List<String> businessLocalList = XMLUtils
0347: .getStringListValueElement(getXMLNS(), beanElement,
0348: "business-local");
0349: for (String businessLocal : businessLocalList) {
0350: bean.addBusinessLocal(businessLocal);
0351: }
0352:
0353: // Remove Methods
0354: NodeList removeMethodList = beanElement.getElementsByTagNameNS(
0355: getXMLNS(), RemoveMethod.NAME);
0356:
0357: // Loop on the remove-method
0358: for (int i = 0; i < removeMethodList.getLength(); i++) {
0359: Element removeMethodElement = (Element) removeMethodList
0360: .item(i);
0361:
0362: RemoveMethod removeMethod = new RemoveMethod();
0363: bean.addRemoveMethod(removeMethod);
0364:
0365: // Analyze method
0366: NodeList beanMethodList = removeMethodElement
0367: .getElementsByTagNameNS(getXMLNS(), "bean-method");
0368: removeMethod
0369: .setMethod(getMethodFromElement((Element) beanMethodList
0370: .item(0)));
0371:
0372: // Retain if exception
0373: String retainIfExceptionStr = XMLUtils
0374: .getStringValueElement(getXMLNS(),
0375: removeMethodElement, "retain-if-exception");
0376: if (retainIfExceptionStr != null) {
0377: // convert into boolean value
0378: boolean retainIfException = false;
0379: retainIfException = Boolean
0380: .parseBoolean(retainIfExceptionStr);
0381: removeMethod.setRetainIfException(retainIfException);
0382: }
0383: }
0384:
0385: // Get remote home
0386: String remoteHome = XMLUtils.getChildStringValueForElement(
0387: beanElement, "home");
0388: bean.setRemoteHome(remoteHome);
0389:
0390: // Get Local home
0391: String localHome = XMLUtils.getChildStringValueForElement(
0392: beanElement, "local-home");
0393: bean.setLocalHome(localHome);
0394:
0395: // Get session-type
0396: String sessionType = XMLUtils.getStringValueElement(getXMLNS(),
0397: beanElement, "session-type");
0398: bean.setSessionType(sessionType);
0399:
0400: }
0401:
0402: /**
0403: * Gets the methodDD from the given dom element.
0404: * @param methodElement the dom element to analyze
0405: * @return the given method object
0406: */
0407: private MethodDD getMethodFromElement(final Element methodElement) {
0408: // create object
0409: MethodDD method = new MethodDD();
0410:
0411: // Analyze name
0412: String methodName = XMLUtils.getStringValueElement(getXMLNS(),
0413: methodElement, "method-name");
0414: method.setName(methodName);
0415:
0416: // Sometime there is an ejb-name
0417: String ejbName = XMLUtils.getStringValueElement(getXMLNS(),
0418: methodElement, "ejb-name");
0419: method.setEjbName(ejbName);
0420:
0421: // Params
0422: NodeList methodParamsList = methodElement
0423: .getElementsByTagNameNS(getXMLNS(), "method-params");
0424: if (methodParamsList != null
0425: && methodParamsList.getLength() == 1) {
0426: // first item is the method-params element
0427: Element methodParamsElement = (Element) methodParamsList
0428: .item(0);
0429:
0430: List<String> paramList = XMLUtils
0431: .getStringListValueElement(getXMLNS(),
0432: methodParamsElement, "method-param");
0433: for (String param : paramList) {
0434: method.addParam(param);
0435: }
0436:
0437: }
0438: return method;
0439: }
0440:
0441: /**
0442: * Analyze the environment (could be a bean, servlet, etc).
0443: * @param envElement the dom element.
0444: * @param bean the structure representing object.
0445: */
0446: private void analyzeEnvironment(final Element envElement,
0447: final AbsEnvironment bean) {
0448: analyzeLifeCycle(envElement, bean, POST_CONSTRUCT);
0449: analyzeLifeCycle(envElement, bean, PRE_DESTROY);
0450: analyzeLifeCycle(envElement, bean, PRE_PASSIVATE);
0451: analyzeLifeCycle(envElement, bean, POST_ACTIVATE);
0452: analyzeAroundInvoke(envElement, bean);
0453: analyzeEnvEnvtry(envElement, bean);
0454: analyzeEJBRef(envElement, bean);
0455: analyzeEJBLocalRef(envElement, bean);
0456: analyzeResourceRef(envElement, bean);
0457: analyzeResourceEnvRef(envElement, bean);
0458: analyzeMessageDestinationRef(envElement, bean);
0459: }
0460:
0461: /**
0462: * Analyze the ejb-ref/local-ref element and find the sub-elements.
0463: * @param refElement given XML element which contains ejb-ref-name,
0464: * ejb-ref-type, etc.
0465: * @param ref the struct object representing the XML element.
0466: */
0467: private void analyzeCommonEJBRef(final Element refElement,
0468: final AbsEJBRef ref) {
0469: // ejb-ref-name
0470: String ejbRefName = XMLUtils.getStringValueElement(getXMLNS(),
0471: refElement, "ejb-ref-name");
0472: ref.setEjbRefName(ejbRefName);
0473:
0474: // ejb-ref-type
0475: String ejbRefType = XMLUtils.getStringValueElement(getXMLNS(),
0476: refElement, "ejb-ref-type");
0477: ref.setEjbRefType(ejbRefType);
0478:
0479: // mapped-name
0480: String mappedName = XMLUtils.getStringValueElement(getXMLNS(),
0481: refElement, "mapped-name");
0482: ref.setMappedName(mappedName);
0483:
0484: // ejb-link
0485: String ejbLink = XMLUtils.getStringValueElement(getXMLNS(),
0486: refElement, "ejb-link");
0487: ref.setEjbLink(ejbLink);
0488:
0489: // Analyze injection-target
0490: analyzeInjectionTarget(refElement, ref);
0491: }
0492:
0493: /**
0494: * Analyze the injection-target element and add them to the given resource
0495: * group object used by ejb-ref, ejb-local-ref, resource-ref, etc.
0496: * @param refElement given XML element which contains a resource element.
0497: * @param resourceGroup the struct object representing the XML element.
0498: */
0499: private void analyzeInjectionTarget(final Element refElement,
0500: final AbsResourceGroup resourceGroup) {
0501: // injection target
0502: NodeList injectionTargetList = refElement
0503: .getElementsByTagNameNS(getXMLNS(), "injection-target");
0504: for (int j = 0; j < injectionTargetList.getLength(); j++) {
0505: Element injectionTargetElement = (Element) injectionTargetList
0506: .item(j);
0507:
0508: // Build Object and add it
0509: InjectionTarget injectionTarget = new InjectionTarget();
0510:
0511: // Get class name
0512: String className = XMLUtils.getStringValueElement(
0513: getXMLNS(), injectionTargetElement,
0514: "injection-target-class");
0515: injectionTarget.setClassname(className);
0516:
0517: // Get target
0518: String targetName = XMLUtils.getStringValueElement(
0519: getXMLNS(), injectionTargetElement,
0520: "injection-target-name");
0521: injectionTarget.setTargetName(targetName);
0522:
0523: resourceGroup.addInjectionTarget(injectionTarget);
0524: }
0525:
0526: }
0527:
0528: /**
0529: * Analyze the environment element and find the ejb-ref element.
0530: * @param envElement given XML element which contains ejb-ref.
0531: * @param env the struct object representing the XML element.
0532: */
0533: private void analyzeEJBRef(final Element envElement,
0534: final AbsEnvironment env) {
0535: NodeList ejbRefList = envElement.getElementsByTagNameNS(
0536: getXMLNS(), EJBRef.NAME);
0537:
0538: // Loop on this list
0539: for (int i = 0; i < ejbRefList.getLength(); i++) {
0540: Element ejbRefElement = (Element) ejbRefList.item(i);
0541: EJBRef ejbRef = new EJBRef();
0542:
0543: analyzeCommonEJBRef(ejbRefElement, ejbRef);
0544:
0545: // remote
0546: String remote = XMLUtils.getStringValueElement(getXMLNS(),
0547: ejbRefElement, "remote");
0548: ejbRef.setRemote(remote);
0549:
0550: // home
0551: String home = XMLUtils.getStringValueElement(getXMLNS(),
0552: ejbRefElement, "home");
0553: ejbRef.setHome(home);
0554:
0555: // add ejb-ref in environment element
0556: env.addEJBRef(ejbRef);
0557: }
0558: }
0559:
0560: /**
0561: * Analyze the environment element and find the resource-ref element.
0562: * @param envElement given XML element which contains resource-ref.
0563: * @param env the struct object representing the XML element.
0564: */
0565: private void analyzeResourceRef(final Element envElement,
0566: final AbsEnvironment env) {
0567: NodeList resourceRefList = envElement.getElementsByTagNameNS(
0568: getXMLNS(), ResourceRef.NAME);
0569:
0570: // Loop on this list
0571: for (int i = 0; i < resourceRefList.getLength(); i++) {
0572: Element resourceRefElement = (Element) resourceRefList
0573: .item(i);
0574: ResourceRef resourceRef = new ResourceRef();
0575:
0576: // name
0577: String resRefName = XMLUtils.getStringValueElement(
0578: getXMLNS(), resourceRefElement, "res-ref-name");
0579: resourceRef.setResRefName(resRefName);
0580:
0581: // type
0582: String resRefType = XMLUtils.getStringValueElement(
0583: getXMLNS(), resourceRefElement, "res-type");
0584: resourceRef.setResRefType(resRefType);
0585:
0586: // mapped-name
0587: String mappedName = XMLUtils.getStringValueElement(
0588: getXMLNS(), resourceRefElement, "mapped-name");
0589: resourceRef.setMappedName(mappedName);
0590:
0591: // Analyze injection-target
0592: analyzeInjectionTarget(resourceRefElement, resourceRef);
0593:
0594: // add resource-ref in environment element
0595: env.addResourceRef(resourceRef);
0596: }
0597: }
0598:
0599: /**
0600: * Analyze the environment element and find the message-destination-ref
0601: * element.
0602: * @param envElement given XML element which contains resource-ref.
0603: * @param env the struct object representing the XML element.
0604: */
0605: private void analyzeMessageDestinationRef(final Element envElement,
0606: final AbsEnvironment env) {
0607: NodeList messageDestinationRefList = envElement
0608: .getElementsByTagNameNS(getXMLNS(),
0609: MessageDestinationRef.NAME);
0610:
0611: // Loop on this list
0612: for (int i = 0; i < messageDestinationRefList.getLength(); i++) {
0613: Element messageDestinationRefElement = (Element) messageDestinationRefList
0614: .item(i);
0615: MessageDestinationRef messageDestinationRef = new MessageDestinationRef();
0616:
0617: // name
0618: String name = XMLUtils.getStringValueElement(getXMLNS(),
0619: messageDestinationRefElement,
0620: "message-destination-ref-name");
0621: messageDestinationRef.setName(name);
0622:
0623: // type
0624: String type = XMLUtils.getStringValueElement(getXMLNS(),
0625: messageDestinationRefElement,
0626: "message-destination-type");
0627: messageDestinationRef.setType(type);
0628:
0629: // usage
0630: String usage = XMLUtils.getStringValueElement(getXMLNS(),
0631: messageDestinationRefElement,
0632: "message-destination-usage");
0633: messageDestinationRef.setUsage(usage);
0634:
0635: // link
0636: String link = XMLUtils.getStringValueElement(getXMLNS(),
0637: messageDestinationRefElement,
0638: "message-destination-link");
0639: messageDestinationRef.setLink(link);
0640:
0641: // mapped-name
0642: String mappedName = XMLUtils.getStringValueElement(
0643: getXMLNS(), messageDestinationRefElement,
0644: "mapped-name");
0645: messageDestinationRef.setMappedName(mappedName);
0646:
0647: // Analyze injection-target
0648: analyzeInjectionTarget(messageDestinationRefElement,
0649: messageDestinationRef);
0650:
0651: // add message-destination-ref in environment element
0652: env.addMessageDestinationRef(messageDestinationRef);
0653: }
0654: }
0655:
0656: /**
0657: * Analyze the environment element and find the resource-env-ref element.
0658: * @param envElement given XML element which contains resource-ref.
0659: * @param env the struct object representing the XML element.
0660: */
0661: private void analyzeResourceEnvRef(final Element envElement,
0662: final AbsEnvironment env) {
0663: NodeList resourceEnvRefList = envElement
0664: .getElementsByTagNameNS(getXMLNS(), ResourceEnvRef.NAME);
0665:
0666: // Loop on this list
0667: for (int i = 0; i < resourceEnvRefList.getLength(); i++) {
0668: Element resourceEnvRefElement = (Element) resourceEnvRefList
0669: .item(i);
0670: ResourceEnvRef resourceEnvRef = new ResourceEnvRef();
0671:
0672: // name
0673: String resourceEnvRefName = XMLUtils.getStringValueElement(
0674: getXMLNS(), resourceEnvRefElement,
0675: "resource-env-ref-name");
0676: resourceEnvRef.setResourceEnvRefName(resourceEnvRefName);
0677:
0678: // type
0679: String resourceEnvRefType = XMLUtils.getStringValueElement(
0680: getXMLNS(), resourceEnvRefElement,
0681: "resource-env-ref-type");
0682: resourceEnvRef.setResourceEnvRefType(resourceEnvRefType);
0683:
0684: // mapped-name
0685: String mappedName = XMLUtils.getStringValueElement(
0686: getXMLNS(), resourceEnvRefElement, "mapped-name");
0687: resourceEnvRef.setMappedName(mappedName);
0688:
0689: // Analyze injection-target
0690: analyzeInjectionTarget(resourceEnvRefElement,
0691: resourceEnvRef);
0692:
0693: // add resource-ref in environment element
0694: env.addResourceEnvRef(resourceEnvRef);
0695: }
0696: }
0697:
0698: /**
0699: * Analyze the environment element and find the ejb-local-ref element.
0700: * @param envElement given XML element which contains ejb-local-ref.
0701: * @param env the struct object representing the XML element.
0702: */
0703: private void analyzeEJBLocalRef(final Element envElement,
0704: final AbsEnvironment env) {
0705: NodeList ejbLocalRefList = envElement.getElementsByTagNameNS(
0706: getXMLNS(), EJBLocalRef.NAME);
0707:
0708: // Loop on this list
0709: for (int i = 0; i < ejbLocalRefList.getLength(); i++) {
0710: Element ejbRefElement = (Element) ejbLocalRefList.item(i);
0711: EJBLocalRef ejbLocalRef = new EJBLocalRef();
0712:
0713: analyzeCommonEJBRef(ejbRefElement, ejbLocalRef);
0714:
0715: // local
0716: String local = XMLUtils.getStringValueElement(getXMLNS(),
0717: ejbRefElement, "local");
0718: ejbLocalRef.setLocal(local);
0719:
0720: // local-home
0721: String localHome = XMLUtils.getStringValueElement(
0722: getXMLNS(), ejbRefElement, "local-home");
0723: ejbLocalRef.setLocalHome(localHome);
0724:
0725: // add ejb-local-ref in environment element
0726: env.addEJBLocalRef(ejbLocalRef);
0727: }
0728: }
0729:
0730: /**
0731: * Analyze the lifecycle elements.
0732: * @param envElement the dom element.
0733: * @param env the structure representing object.
0734: */
0735: private void analyzeAroundInvoke(final Element envElement,
0736: final AbsEnvironment env) {
0737: // Get the around-invoke elements
0738: NodeList aroundInvokeList = envElement.getElementsByTagNameNS(
0739: getXMLNS(), "around-invoke");
0740:
0741: // Loop on the around-invoke
0742: for (int i = 0; i < aroundInvokeList.getLength(); i++) {
0743: Element aroundInvokeElement = (Element) aroundInvokeList
0744: .item(i);
0745:
0746: // Build instance that is created.
0747: AroundInvoke aroundInvoke = new AroundInvoke();
0748: env.addAroundInvoke(aroundInvoke);
0749:
0750: // lifecycle-callback-class
0751: String className = XMLUtils.getStringValueElement(
0752: getXMLNS(), aroundInvokeElement, "class");
0753: aroundInvoke.setClassName(className);
0754:
0755: // lifecycle-callback-method
0756: String method = XMLUtils.getStringValueElement(getXMLNS(),
0757: aroundInvokeElement, "method-name");
0758: aroundInvoke.setMethodName(method);
0759:
0760: }
0761:
0762: }
0763:
0764: /**
0765: * Analyze the lifecycle elements.
0766: * @param envElement the dom element.
0767: * @param env the structure representing object.
0768: * @param lifecycleName the name of lifecycle (post-construct, etc)
0769: */
0770: private void analyzeLifeCycle(final Element envElement,
0771: final AbsEnvironment env, final String lifecycleName) {
0772: // Get the lifecycle elements
0773: NodeList lifecycleList = envElement.getElementsByTagNameNS(
0774: getXMLNS(), lifecycleName);
0775:
0776: // Loop on the lifecycle
0777: for (int i = 0; i < lifecycleList.getLength(); i++) {
0778: Element lifeCycleElement = (Element) lifecycleList.item(i);
0779:
0780: // Build instance that is created.
0781: LifeCycleCallback lifecycleCallback = new LifeCycleCallback();
0782: if (POST_CONSTRUCT.equals(lifecycleName)) {
0783: env.addPostConstructCallback(lifecycleCallback);
0784: } else if (PRE_DESTROY.equals(lifecycleName)) {
0785: env.addPreDestroyCallback(lifecycleCallback);
0786: } else if (PRE_PASSIVATE.equals(lifecycleName)) {
0787: env.addPrePassivateCallback(lifecycleCallback);
0788: } else if (POST_ACTIVATE.equals(lifecycleName)) {
0789: env.addPostActivateCallback(lifecycleCallback);
0790: } else {
0791: throw new IllegalArgumentException(
0792: "Unknown lifecycle named '" + lifecycleName
0793: + "'.");
0794: }
0795:
0796: // lifecycle-callback-class
0797: String className = XMLUtils.getStringValueElement(
0798: getXMLNS(), lifeCycleElement,
0799: "lifecycle-callback-class");
0800: lifecycleCallback.setLifecycleCallbackClass(className);
0801:
0802: // lifecycle-callback-method
0803: String method = XMLUtils.getStringValueElement(getXMLNS(),
0804: lifeCycleElement, "lifecycle-callback-method");
0805: lifecycleCallback.setMethodName(method);
0806:
0807: }
0808:
0809: }
0810:
0811: /**
0812: * Analyze the env-entry element.
0813: * @param envElement the dom element.
0814: * @param bean the structure representing object.
0815: */
0816: private void analyzeEnvEnvtry(final Element envElement,
0817: final AbsEnvironment bean) {
0818: // Get env-entry
0819: NodeList envEntryList = envElement.getElementsByTagNameNS(
0820: getXMLNS(), EnvEntry.NAME);
0821:
0822: // Loop on env entry
0823: for (int i = 0; i < envEntryList.getLength(); i++) {
0824: Element envEntryElement = (Element) envEntryList.item(i);
0825:
0826: // Build instance that is created.
0827: EnvEntry envEntry = new EnvEntry();
0828: bean.addEnvEntry(envEntry);
0829:
0830: // Env-entry name
0831: String envEntryName = XMLUtils.getStringValueElement(
0832: getXMLNS(), envEntryElement, "env-entry-name");
0833: envEntry.setEnvEntryName(envEntryName);
0834:
0835: // Env-entry type
0836: String envEntryType = XMLUtils.getStringValueElement(
0837: getXMLNS(), envEntryElement, "env-entry-type");
0838: envEntry.setEnvEntryType(envEntryType);
0839:
0840: // Env-entry value
0841: String envEntryValue = XMLUtils.getStringValueElement(
0842: getXMLNS(), envEntryElement, "env-entry-value");
0843: envEntry.setEnvEntryValue(envEntryValue);
0844: }
0845: }
0846:
0847: /**
0848: * Analyze the interceptors element of an EJB-JAR.
0849: * @param interceptorsElement the dom element.
0850: * @param interceptors the structure representing object.
0851: */
0852: private void analyzeInterceptorsElement(
0853: final Element interceptorsElement,
0854: final Interceptors interceptors) {
0855: // Get interceptor list elements
0856: NodeList interceptorList = interceptorsElement
0857: .getElementsByTagNameNS(getXMLNS(), Interceptor.NAME);
0858:
0859: // Loop on <interceptor> elements
0860: for (int i = 0; i < interceptorList.getLength(); i++) {
0861: Element interceptorElement = (Element) interceptorList
0862: .item(i);
0863:
0864: Interceptor interceptor = new Interceptor();
0865: interceptors.addInterceptor(interceptor);
0866:
0867: // Extract interceptor class
0868: String interceptorClass = XMLUtils
0869: .getStringValueElement(getXMLNS(),
0870: interceptorElement, "interceptor-class");
0871: interceptor.setInterceptorClass(interceptorClass);
0872:
0873: // Analyze the object
0874: analyzeEnvironment(interceptorElement, interceptor);
0875: }
0876: }
0877:
0878: /**
0879: * Analyze the assembly descriptor of an EJB-JAR.
0880: * @param assemblyDescriptorElement the dom element.
0881: * @param assemblyDescriptor the structure representing object.
0882: */
0883: private void analyzeAssemblyDescriptor(
0884: final Element assemblyDescriptorElement,
0885: final AssemblyDescriptor assemblyDescriptor) {
0886: analyzeApplicationException(assemblyDescriptorElement,
0887: assemblyDescriptor);
0888:
0889: analyzeInterceptorBinding(assemblyDescriptorElement,
0890: assemblyDescriptor);
0891:
0892: analyzeContainerTransaction(assemblyDescriptorElement,
0893: assemblyDescriptor);
0894:
0895: analyzeSecurity(assemblyDescriptorElement, assemblyDescriptor);
0896: }
0897:
0898: /**
0899: * Analyze the interceptor binding of the assembly descriptor of an EJB-JAR.
0900: * @param assemblyDescriptorElement the dom element.
0901: * @param assemblyDescriptor the structure representing object.
0902: */
0903: private void analyzeInterceptorBinding(
0904: final Element assemblyDescriptorElement,
0905: final AssemblyDescriptor assemblyDescriptor) {
0906: // Get interceptor-binding list
0907: NodeList interceptorBindingList = assemblyDescriptorElement
0908: .getElementsByTagNameNS(getXMLNS(),
0909: InterceptorBinding.NAME);
0910:
0911: // Loop on interceptor-binding
0912: for (int i = 0; i < interceptorBindingList.getLength(); i++) {
0913: Element interceptorBindingElement = (Element) interceptorBindingList
0914: .item(i);
0915:
0916: // Build struct and set it in the assembly descriptor
0917: InterceptorBinding interceptorBinding = new InterceptorBinding();
0918: assemblyDescriptor
0919: .addInterceptorBinding(interceptorBinding);
0920:
0921: // name
0922: String ejbName = XMLUtils.getStringValueElement(getXMLNS(),
0923: interceptorBindingElement, "ejb-name");
0924: interceptorBinding.setEjbName(ejbName);
0925:
0926: // interceptor-order
0927: NodeList interceptorOrderList = interceptorBindingElement
0928: .getElementsByTagNameNS(getXMLNS(),
0929: "interceptor-order");
0930: if (interceptorOrderList != null
0931: && interceptorOrderList.getLength() == 1) {
0932: // first item is the interceptor-order element
0933: Element interceptorOrderElement = (Element) interceptorOrderList
0934: .item(0);
0935: List<String> interceptorOrderClassList = XMLUtils
0936: .getStringListValueElement(getXMLNS(),
0937: interceptorOrderElement,
0938: "interceptor-class");
0939: for (String interceptorClass : interceptorOrderClassList) {
0940: interceptorBinding
0941: .addOrderedInterceptorClass(interceptorClass);
0942: }
0943: } else {
0944: // interceptor-class (as there is no interceptor-order element)
0945: List<String> interceptorClassList = XMLUtils
0946: .getStringListValueElement(getXMLNS(),
0947: interceptorBindingElement,
0948: "interceptor-class");
0949: for (String interceptorClass : interceptorClassList) {
0950: interceptorBinding
0951: .addInterceptorClass(interceptorClass);
0952: }
0953: }
0954:
0955: // method
0956: NodeList methodList = interceptorBindingElement
0957: .getElementsByTagNameNS(getXMLNS(), "method");
0958: if (methodList.getLength() > 0) {
0959: interceptorBinding
0960: .setMethod(getMethodFromElement((Element) methodList
0961: .item(0)));
0962: }
0963:
0964: // exclude-class-interceptors
0965: String excludeClassInterceptorsStr = XMLUtils
0966: .getStringValueElement(getXMLNS(),
0967: interceptorBindingElement,
0968: "exclude-class-interceptors");
0969: if (excludeClassInterceptorsStr != null) {
0970: // convert into boolean value
0971: boolean excludeClassInterceptors = false;
0972: excludeClassInterceptors = Boolean
0973: .parseBoolean(excludeClassInterceptorsStr);
0974: interceptorBinding
0975: .setExcludeClassInterceptors(excludeClassInterceptors);
0976: }
0977:
0978: // exclude-default-interceptors
0979: String excludeDefaultInterceptorsStr = XMLUtils
0980: .getStringValueElement(getXMLNS(),
0981: interceptorBindingElement,
0982: "exclude-default-interceptors");
0983: if (excludeDefaultInterceptorsStr != null) {
0984: // convert into boolean value
0985: boolean excludeDefaultInterceptors = false;
0986: excludeDefaultInterceptors = Boolean
0987: .parseBoolean(excludeDefaultInterceptorsStr);
0988: interceptorBinding
0989: .setExcludeDefaultInterceptors(excludeDefaultInterceptors);
0990: }
0991:
0992: }
0993: }
0994:
0995: /**
0996: * Analyze the application Exception of the assembly descriptor of an
0997: * EJB-JAR.
0998: * @param assemblyDescriptorElement the dom element.
0999: * @param assemblyDescriptor the structure representing object.
1000: */
1001: private void analyzeApplicationException(
1002: final Element assemblyDescriptorElement,
1003: final AssemblyDescriptor assemblyDescriptor) {
1004: // Get application-exception list
1005: NodeList applicationExceptionList = assemblyDescriptorElement
1006: .getElementsByTagNameNS(getXMLNS(),
1007: JApplicationException.NAME);
1008:
1009: // Loop on application-exception
1010: for (int i = 0; i < applicationExceptionList.getLength(); i++) {
1011: Element applicationExceptionElement = (Element) applicationExceptionList
1012: .item(i);
1013:
1014: // name
1015: String className = XMLUtils.getStringValueElement(
1016: getXMLNS(), applicationExceptionElement,
1017: "exception-class");
1018:
1019: // rollback
1020: String rollbackStr = XMLUtils
1021: .getStringValueElement(getXMLNS(),
1022: applicationExceptionElement, "rollback");
1023:
1024: // convert into boolean value
1025: boolean rollback = false;
1026: if (rollbackStr != null) {
1027: rollback = Boolean.parseBoolean(rollbackStr);
1028: }
1029:
1030: // Build instance that is created.
1031: JApplicationException applicationException = new JApplicationException(
1032: className, rollback);
1033: assemblyDescriptor
1034: .addApplicationException(applicationException);
1035: }
1036: }
1037:
1038: /**
1039: * Analyze the container transaction element of the assembly descriptor of
1040: * an EJB-JAR.
1041: * @param assemblyDescriptorElement the dom element.
1042: * @param assemblyDescriptor the structure representing object.
1043: */
1044: private void analyzeContainerTransaction(
1045: final Element assemblyDescriptorElement,
1046: final AssemblyDescriptor assemblyDescriptor) {
1047:
1048: // Get container-transaction list
1049: NodeList containerTransactionList = assemblyDescriptorElement
1050: .getElementsByTagNameNS(getXMLNS(),
1051: ContainerTransaction.NAME);
1052:
1053: // Loop on container-transaction
1054: for (int i = 0; i < containerTransactionList.getLength(); i++) {
1055: Element containerTransactionElement = (Element) containerTransactionList
1056: .item(i);
1057:
1058: // Build struct and set it in the assembly descriptor
1059: ContainerTransaction containerTransaction = new ContainerTransaction();
1060: assemblyDescriptor
1061: .addContainerTransaction(containerTransaction);
1062:
1063: // Get Method element
1064: NodeList methodList = containerTransactionElement
1065: .getElementsByTagNameNS(getXMLNS(), "method");
1066: for (int j = 0; j < methodList.getLength(); j++) {
1067: Element methodElement = (Element) methodList.item(j);
1068:
1069: // Set method
1070: containerTransaction
1071: .addMethod(getMethodFromElement(methodElement));
1072: }
1073:
1074: // Tx attribute
1075: String transactionAttribute = XMLUtils
1076: .getStringValueElement(getXMLNS(),
1077: containerTransactionElement,
1078: "trans-attribute");
1079: if (transactionAttribute != null) {
1080: if ("NotSupported".equals(transactionAttribute)) {
1081: containerTransaction
1082: .setTransactionAttribute(TransactionAttributeType.NOT_SUPPORTED);
1083: } else if ("Supports".equals(transactionAttribute)) {
1084: containerTransaction
1085: .setTransactionAttribute(TransactionAttributeType.SUPPORTS);
1086: } else if ("Required".equals(transactionAttribute)) {
1087: containerTransaction
1088: .setTransactionAttribute(TransactionAttributeType.REQUIRED);
1089: } else if ("RequiresNew".equals(transactionAttribute)) {
1090: containerTransaction
1091: .setTransactionAttribute(TransactionAttributeType.REQUIRES_NEW);
1092: } else if ("Mandatory".equals(transactionAttribute)) {
1093: containerTransaction
1094: .setTransactionAttribute(TransactionAttributeType.MANDATORY);
1095: } else if ("Never".equals(transactionAttribute)) {
1096: containerTransaction
1097: .setTransactionAttribute(TransactionAttributeType.NEVER);
1098: } else {
1099: throw new IllegalStateException(
1100: "Invalid transaction attribute '"
1101: + transactionAttribute + "'");
1102: }
1103: }
1104: }
1105: }
1106:
1107: /**
1108: * Analyze the security includind security-role, method-permission and
1109: * exclude-list elements of the assembly descriptor of an EJB-JAR.
1110: * @param assemblyDescriptorElement the dom element.
1111: * @param assemblyDescriptor the structure representing object.
1112: */
1113: private void analyzeSecurity(
1114: final Element assemblyDescriptorElement,
1115: final AssemblyDescriptor assemblyDescriptor) {
1116:
1117: // Get security-role list
1118: NodeList securityRoleList = assemblyDescriptorElement
1119: .getElementsByTagNameNS(getXMLNS(), "security-role");
1120:
1121: // Loop on security-role
1122: for (int i = 0; i < securityRoleList.getLength(); i++) {
1123: Element securityRoleElement = (Element) securityRoleList
1124: .item(i);
1125:
1126: // Get the security role name
1127: String securityRole = XMLUtils.getStringValueElement(
1128: getXMLNS(), securityRoleElement, "role-name");
1129: if (securityRole != null) {
1130: assemblyDescriptor.addSecurityRole(securityRole);
1131: }
1132: }
1133:
1134: // Get method-permission
1135: NodeList methodPermissionList = assemblyDescriptorElement
1136: .getElementsByTagNameNS(getXMLNS(),
1137: MethodPermission.NAME);
1138:
1139: // Loop on method-permission
1140: for (int i = 0; i < methodPermissionList.getLength(); i++) {
1141: Element methodPermissionElement = (Element) methodPermissionList
1142: .item(i);
1143:
1144: // Build object
1145: MethodPermission methodPermission = new MethodPermission();
1146: assemblyDescriptor.addMethodPermission(methodPermission);
1147:
1148: // Get all the roles for this method permission
1149: List<String> roles = XMLUtils.getStringListValueElement(
1150: getXMLNS(), methodPermissionElement, "role-name");
1151: for (String roleName : roles) {
1152: methodPermission.addRoleName(roleName);
1153: }
1154:
1155: // Unchecked ?
1156: NodeList uncheckedList = methodPermissionElement
1157: .getElementsByTagNameNS(getXMLNS(), "unchecked");
1158: if (uncheckedList.getLength() > 0) {
1159: methodPermission.setUnchecked(true);
1160: }
1161:
1162: // Get Method element
1163: NodeList methodList = methodPermissionElement
1164: .getElementsByTagNameNS(getXMLNS(), "method");
1165: for (int j = 0; j < methodList.getLength(); j++) {
1166: Element methodElement = (Element) methodList.item(j);
1167:
1168: // Add method
1169: methodPermission
1170: .addMethod(getMethodFromElement(methodElement));
1171: }
1172: }
1173:
1174: // Get exclude-list
1175: NodeList excludeList = assemblyDescriptorElement
1176: .getElementsByTagNameNS(getXMLNS(), "exclude-list");
1177:
1178: // Loop on exclude-list (in fact only one element may be found on this
1179: // list)
1180: for (int i = 0; i < excludeList.getLength(); i++) {
1181: Element excludeListElement = (Element) excludeList.item(i);
1182:
1183: // Get Method element
1184: NodeList methodList = excludeListElement
1185: .getElementsByTagNameNS(getXMLNS(), "method");
1186: for (int j = 0; j < methodList.getLength(); j++) {
1187: Element methodElement = (Element) methodList.item(j);
1188:
1189: // Add method
1190: assemblyDescriptor
1191: .addExcludeList(getMethodFromElement(methodElement));
1192: }
1193: }
1194:
1195: }
1196:
1197: /**
1198: * @return the object resulting of the XML parsing.
1199: */
1200: public EJB3 getEJB3() {
1201: return ejb3;
1202: }
1203:
1204: }
|