1 25 26 package org.objectweb.easybeans.deployment.xml.parsing; 27 28 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.POST_CONSTRUCT; 29 import static org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback.PRE_DESTROY; 30 31 import java.net.URL ; 32 import java.util.List ; 33 34 import org.objectweb.easybeans.deployment.xml.struct.AbsBean; 35 import org.objectweb.easybeans.deployment.xml.struct.EJB3; 36 import org.objectweb.easybeans.deployment.xml.struct.EnterpriseBeans; 37 import org.objectweb.easybeans.deployment.xml.struct.LifeCycleCallback; 38 import org.objectweb.easybeans.deployment.xml.struct.Session; 39 import org.objectweb.easybeans.deployment.xml.struct.common.AbsEnvironment; 40 import org.objectweb.easybeans.deployment.xml.struct.common.EnvEntry; 41 import org.objectweb.easybeans.log.JLog; 42 import org.objectweb.easybeans.log.JLogFactory; 43 import org.objectweb.easybeans.util.xml.DocumentParser; 44 import org.objectweb.easybeans.util.xml.DocumentParserException; 45 import org.objectweb.easybeans.util.xml.XMLUtils; 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Element ; 48 import org.w3c.dom.NodeList ; 49 50 55 public final class EJB3DeploymentDescLoader { 56 57 60 private static final String JAVAEE_NS = "http://java.sun.com/xml/ns/javaee"; 61 62 65 private static JLog logger = JLogFactory.getLog(EJB3DeploymentDescLoader.class); 66 67 70 private static boolean validating = true; 71 72 75 private EJB3DeploymentDescLoader() { 76 77 } 78 79 85 public static EJB3 loadDeploymentDescriptor(final URL url) throws ParsingException { 86 logger.debug("Analyzing url {0}", url); 87 88 Document document = null; 90 try { 91 document = DocumentParser.getDocument(url, validating, new EJB3EntityResolver()); 92 } catch (DocumentParserException e) { 93 throw new ParsingException("Cannot parse the url", e); 94 } 95 96 EJB3 ejb3 = new EJB3(); 97 98 Element ejbJarRootElement = document.getDocumentElement(); 100 101 NodeList enterprisebeansList = ejbJarRootElement.getElementsByTagNameNS(JAVAEE_NS, EnterpriseBeans.NAME); 102 if (enterprisebeansList.getLength() == 1) { 103 Element enterpriseBeansElement = (Element ) enterprisebeansList.item(0); 104 105 EnterpriseBeans enterpriseBeans = new EnterpriseBeans(); 107 ejb3.setEnterpriseBeans(enterpriseBeans); 108 109 analyzeBeans(enterpriseBeansElement, enterpriseBeans); 111 112 } 113 114 return ejb3; 115 116 } 117 118 123 private static void analyzeBeans(final Element enterpriseBeansElement, final EnterpriseBeans enterpriseBeans) { 124 NodeList sessionBeansList = enterpriseBeansElement.getElementsByTagNameNS(JAVAEE_NS, Session.NAME); 126 127 for (int i = 0; i < sessionBeansList.getLength(); i++) { 129 Element sessionElement = (Element ) sessionBeansList.item(i); 130 131 Session session = new Session(); 133 134 enterpriseBeans.addSession(session); 135 136 analyzeSessionBean(sessionElement, session); 138 } 139 140 } 141 142 147 private static void analyzeBean(final Element beanElement, final AbsBean bean) { 148 String ejbName = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "ejb-name"); 150 bean.setEjbName(ejbName); 151 152 String ejbClass = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "ejb-class"); 154 bean.setEjbClass(ejbClass); 155 156 analyzeEnvironement(beanElement, bean); 158 } 159 160 165 private static void analyzeSessionBean(final Element beanElement, final Session bean) { 166 analyzeBean(beanElement, bean); 167 168 List <String > businessRemoteList = XMLUtils.getStringListValueElement(JAVAEE_NS, beanElement, "business-remote"); 170 for (String businessRemote : businessRemoteList) { 171 bean.addBusinessRemote(businessRemote); 172 } 173 174 List <String > businessLocalList = XMLUtils.getStringListValueElement(JAVAEE_NS, beanElement, "business-local"); 176 for (String businessLocal : businessLocalList) { 177 bean.addBusinessLocal(businessLocal); 178 } 179 180 String sessionType = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "session-type"); 182 bean.setSessionType(sessionType); 183 184 String transactionType = XMLUtils.getStringValueElement(JAVAEE_NS, beanElement, "transaction-type"); 186 bean.setTransactionType(transactionType); 187 188 } 189 190 195 private static void analyzeEnvironement(final Element envElement, final AbsEnvironment bean) { 196 analyzeLifeCycle(envElement, bean, POST_CONSTRUCT); 197 analyzeLifeCycle(envElement, bean, PRE_DESTROY); 198 analyzeEnvEnvtry(envElement, bean); 199 } 200 201 207 private static void analyzeLifeCycle(final Element envElement, final AbsEnvironment bean, final String lifecycleName) { 208 NodeList lifecycleList = envElement.getElementsByTagNameNS(JAVAEE_NS, lifecycleName); 210 211 for (int i = 0; i < lifecycleList.getLength(); i++) { 213 Element lifeCycleElement = (Element ) lifecycleList.item(i); 214 215 LifeCycleCallback lifecycleCallback = new LifeCycleCallback(); 217 if (POST_CONSTRUCT.equals(lifecycleName)) { 218 bean.addPostConstruct(lifecycleCallback); 219 } else if (PRE_DESTROY.equals(lifecycleName)) { 220 bean.addPreDestroy(lifecycleCallback); 221 } else { 222 throw new IllegalArgumentException ("Unknown lifecycle named '" + lifecycleName + "'."); 223 } 224 225 String className = XMLUtils.getStringValueElement(JAVAEE_NS, lifeCycleElement, "lifecycle-callback-class"); 227 lifecycleCallback.setClassName(className); 228 229 String method = XMLUtils.getStringValueElement(JAVAEE_NS, lifeCycleElement, "lifecycle-callback-method"); 231 lifecycleCallback.setMethod(method); 232 233 } 234 235 } 236 237 242 private static void analyzeEnvEnvtry(final Element envElement, final AbsEnvironment bean) { 243 NodeList envEntryList = envElement.getElementsByTagNameNS(JAVAEE_NS, EnvEntry.NAME); 245 246 for (int i = 0; i < envEntryList.getLength(); i++) { 248 Element envEntryElement = (Element ) envEntryList.item(i); 249 250 EnvEntry envEntry = new EnvEntry(); 252 bean.addEnvEntry(envEntry); 253 254 String envEntryName = XMLUtils.getStringValueElement(JAVAEE_NS, envEntryElement, "env-entry-name"); 256 envEntry.setEnvEntryName(envEntryName); 257 258 String envEntryType = XMLUtils.getStringValueElement(JAVAEE_NS, envEntryElement, "env-entry-type"); 260 envEntry.setEnvEntryType(envEntryType); 261 262 String envEntryValue = XMLUtils.getStringValueElement(JAVAEE_NS, envEntryElement, "env-entry-value"); 264 envEntry.setEnvEntryValue(envEntryValue); 265 } 266 } 267 268 } 269 | Popular Tags |