001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021ejbJarDD11-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: DeploymentDesc.java 5450 2004-09-17 09:44:19Z joaninh $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.deployment.api;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.objectweb.jonas_ejb.deployment.xml.AssemblyDescriptor;
033: import org.objectweb.jonas_ejb.deployment.xml.EjbJar;
034: import org.objectweb.jonas_ejb.deployment.xml.Entity;
035: import org.objectweb.jonas_ejb.deployment.xml.JonasEjbJar;
036: import org.objectweb.jonas_ejb.deployment.xml.JonasEntity;
037: import org.objectweb.jonas_ejb.deployment.xml.JonasRunAsMapping;
038: import org.objectweb.jonas_ejb.deployment.xml.JonasSession;
039: import org.objectweb.jonas_ejb.deployment.xml.MethodPermission;
040: import org.objectweb.jonas_ejb.deployment.xml.Session;
041:
042: import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
043: import org.objectweb.jonas_lib.deployment.api.DescriptionGroupDesc;
044: import org.objectweb.jonas_lib.deployment.xml.JLinkedList;
045: import org.objectweb.jonas_lib.deployment.xml.JonasMessageDestination;
046: import org.objectweb.jonas_lib.deployment.xml.MessageDestination;
047:
048: import org.objectweb.util.monolog.api.Logger;
049:
050: /**
051: * Class to hold meta-information related to the deployment of an ejb-jar
052: * @author Christophe Ney [cney@batisseurs.com] : Initial developer
053: * @author Helene Joanin
054: * @author Philippe Durieux
055: * @author Markus Karg (Novell port)
056: * @author Philippe Coq
057: */
058: public abstract class DeploymentDesc extends DescriptionGroupDesc {
059:
060: /**
061: * Logger
062: */
063: protected Logger logger;
064:
065: /**
066: * Ejb spec version
067: */
068: protected String specVersion = null;
069:
070: /**
071: * Set of enterprise beans deployment descriptors
072: */
073: protected HashMap beanDesc = new HashMap();
074:
075: /**
076: * Assembly descriptor
077: */
078: protected AssemblyDescriptor asd = null;
079:
080: /**
081: * Deployment desc's file name (jar or directory)
082: */
083: protected String fileName = null;
084:
085: /**
086: * Deployment desc's file name (jar or directory)
087: */
088: protected String ejbClientJar = null;
089:
090: /**
091: * List of MethodPermissions
092: */
093: private List methodPermissionsDescList = null;
094:
095: /**
096: * ExcludeList in Assembly descriptor
097: */
098: private ExcludeListDesc excludeListDesc = null;
099:
100: /**
101: * List of JonasMessageDestinations
102: */
103: protected JLinkedList jonasMDList = null;
104:
105: /**
106: * Xml content of the standard deployement descriptor file
107: */
108: private String xmlContent = "";
109:
110: /**
111: * Xml content of the JOnAS deployement descriptor file
112: */
113: private String jonasXmlContent = "";
114:
115: /**
116: * Mapping for runAs principals
117: * Principal name --> list of roles (array)
118: */
119: private Map runAsMapping = null;
120:
121: /**
122: * Build the Meta-Information from the XML data binding trees
123: * containing the EJB and JOnAS deployment descriptors.
124: * @param classLoader The Class Loader to be used
125: * @param ejbJar The EjbJar information, from standard deployment descriptor.
126: * @param jonasEjbJar The JonasEjbJar information, from JOnAS specific deployment descriptor.
127: * @param l The logger to be used for tracing
128: * @param fileName deployment desc's jar or directory name
129: * @throws DeploymentDescException Cannot deploy bean
130: */
131: public DeploymentDesc(ClassLoader classLoader, EjbJar ejbJar,
132: JonasEjbJar jonasEjbJar, Logger l, String fileName)
133: throws DeploymentDescException {
134:
135: logger = l;
136:
137: // set jarFileName
138: this .fileName = fileName;
139:
140: // test classloader
141: if (classLoader == null) {
142: throw new DeploymentDescException(
143: "DeploymentDesc: Classloader is null");
144: }
145:
146: // test the validity of the ejbJar (EnterpriseBeans must be present)
147: if (ejbJar.getEnterpriseBeans() == null) {
148: throw new DeploymentDescException(
149: "invalid standard deployment descriptor (<enterprise-beans> element missing)");
150: }
151: // spec-version
152: specVersion = ejbJar.getVersion();
153:
154: // ejb-client-jar
155: ejbClientJar = ejbJar.getEjbClientJar();
156:
157: // assembly descriptor
158: asd = ejbJar.getAssemblyDescriptor();
159:
160: // Run-as mapping
161: runAsMapping = new HashMap();
162: for (Iterator i = jonasEjbJar.getJonasRunAsMappingList()
163: .iterator(); i.hasNext();) {
164: // Get Mapping
165: JonasRunAsMapping jonasRunAsMapping = (JonasRunAsMapping) i
166: .next();
167: String principalName = jonasRunAsMapping.getPrincipalName();
168: // Get existing roles if any
169: String[] existingRunAsRoleMapping = (String[]) runAsMapping
170: .get(principalName);
171: String[] newMappingRoles = null;
172: int r = 0;
173: if (existingRunAsRoleMapping == null) {
174: newMappingRoles = new String[jonasRunAsMapping
175: .getRoleNamesList().size()];
176: } else {
177: newMappingRoles = new String[jonasRunAsMapping
178: .getRoleNamesList().size()
179: + existingRunAsRoleMapping.length];
180: // Now add existing roles
181: System.arraycopy(existingRunAsRoleMapping, 0,
182: newMappingRoles, 0,
183: existingRunAsRoleMapping.length);
184: r = existingRunAsRoleMapping.length;
185: }
186: Iterator itR = jonasRunAsMapping.getRoleNamesList()
187: .iterator();
188: while (itR.hasNext()) {
189: newMappingRoles[r] = (String) itR.next();
190: r++;
191: }
192: runAsMapping.put(principalName, newMappingRoles);
193:
194: }
195:
196: // Use by PermissionManager for translating xml into EJBMethodPermission
197: methodPermissionsDescList = new LinkedList();
198: // Create EJBMEthodPermissions for each method-permission
199: if (asd != null) {
200: for (Iterator i = asd.getMethodPermissionList().iterator(); i
201: .hasNext();) {
202: MethodPermission methodPermission = (MethodPermission) i
203: .next();
204: methodPermissionsDescList.add(new MethodPermissionDesc(
205: methodPermission));
206: }
207: }
208:
209: // Use by PermissionManager for translating xml into EJBMethodPermission
210: if (asd != null && asd.getExcludeList() != null) {
211: excludeListDesc = new ExcludeListDesc(asd.getExcludeList());
212: }
213:
214: // jonas-message-destination
215: jonasMDList = jonasEjbJar.getJonasMessageDestinationList();
216:
217: // HashMap of jonas-session
218: HashMap jonasSession = new HashMap();
219: for (Iterator i = jonasEjbJar.getJonasSessionList().iterator(); i
220: .hasNext();) {
221: JonasSession jSes = (JonasSession) i.next();
222: jonasSession.put(jSes.getEjbName(), jSes);
223: }
224:
225: // session beans
226: for (Iterator i = ejbJar.getEnterpriseBeans().getSessionList()
227: .iterator(); i.hasNext();) {
228: BeanDesc bd = null;
229: Session ses = (Session) i.next();
230: // find corresponding jonas session
231: JonasSession jSes = (JonasSession) jonasSession.get(ses
232: .getEjbName());
233: if (jSes == null) {
234: // Build a default jonas-session if not exist
235: jSes = new JonasSession();
236: jSes.setEjbName(ses.getEjbName());
237: }
238: if (ses.getSessionType().equals("Stateful")) {
239: // stateful
240: bd = new SessionStatefulDesc(classLoader, ses, asd,
241: jSes, jonasMDList, fileName);
242: } else if (ses.getSessionType().equals("Stateless")) {
243: // stateless
244: bd = new SessionStatelessDesc(classLoader, ses, asd,
245: jSes, jonasMDList, fileName);
246: } else {
247: throw new DeploymentDescException(
248: "invalid session-type content for bean "
249: + ses.getEjbName());
250: }
251: bd.setDeploymentDesc(this );
252: bd.check();
253: beanDesc.put(bd.getEjbName(), bd);
254: }
255:
256: // HashMap of jonas-entity
257: HashMap jonasEntity = new HashMap();
258: for (Iterator i = jonasEjbJar.getJonasEntityList().iterator(); i
259: .hasNext();) {
260: JonasEntity jEnt = (JonasEntity) i.next();
261: jonasEntity.put(jEnt.getEjbName(), jEnt);
262: }
263: // entity beans
264: for (Iterator i = ejbJar.getEnterpriseBeans().getEntityList()
265: .iterator(); i.hasNext();) {
266: BeanDesc bd = null;
267: Entity ent = (Entity) i.next();
268: // find corresponding jonas entity
269: JonasEntity jEnt = (JonasEntity) jonasEntity.get(ent
270: .getEjbName());
271: if (jEnt == null) {
272: throw new DeploymentDescException(
273: "jonas-entity missing for bean "
274: + ent.getEjbName());
275: }
276: if (ent.getPersistenceType().equals("Bean")) {
277: // bean managed
278: bd = new EntityBmpDesc(classLoader, ent, asd, jEnt,
279: jonasMDList, fileName);
280: } else if (ent.getPersistenceType().equals("Container")) {
281: // container managed (always jdbc)
282: bd = newEntityBeanDesc(classLoader, ent, asd, jEnt,
283: jonasMDList);
284: } else {
285: throw new DeploymentDescException(
286: "Invalid persistence-type content for bean "
287: + ent.getEjbName());
288: }
289: bd.setDeploymentDesc(this );
290: bd.check();
291: beanDesc.put(bd.getEjbName(), bd);
292: }
293: }
294:
295: /**
296: * Get an Iterator on the Bean Desc list
297: * @return Iterator on BeanDesc
298: */
299: public Iterator getBeanDescIterator() {
300: return beanDesc.values().iterator();
301: }
302:
303: /**
304: * Get descriptors for all beans contained in jar file
305: * @return Array of bean's descriptors
306: */
307: public BeanDesc[] getBeanDesc() {
308: BeanDesc[] ret = new BeanDesc[beanDesc.size()];
309: int j = 0;
310: for (Iterator i = beanDesc.values().iterator(); i.hasNext(); j++) {
311: ret[j] = (BeanDesc) i.next();
312: }
313: return ret;
314: }
315:
316: /**
317: * Gets the Mapping for run-as principal
318: * @param principalName name of the run-as principal
319: * @return array of roles
320: */
321: public String[] getRolesForRunAsPrincipal(String principalName) {
322: return (String[]) runAsMapping.get(principalName);
323: }
324:
325: /**
326: * Get bean descriptor given its name
327: * @param ejbName the name of the bean in the Deployment Descriptor
328: * @return bean descriptor given its name
329: */
330: public BeanDesc getBeanDesc(String ejbName) {
331: return (BeanDesc) beanDesc.get(ejbName);
332: }
333:
334: /**
335: * Get bean descriptor given its abstract schema name
336: * @param asn Abstract Schema Name
337: * @return null if it doesn't exist.
338: */
339: public EntityCmp2Desc asn2BeanDesc(String asn) {
340: for (Iterator i = beanDesc.values().iterator(); i.hasNext();) {
341: BeanDesc bd = (BeanDesc) i.next();
342: if (bd instanceof EntityCmp2Desc) {
343: if (asn.equals(((EntityCmp2Desc) bd)
344: .getAbstractSchemaName())) {
345: return ((EntityCmp2Desc) bd);
346: }
347: }
348: }
349: return null;
350: }
351:
352: /**
353: * Get the list of the methodPermissionDesc objects which represent
354: * method-permission elements in assembly-descriptor
355: * @return the list of methodPermissionDesc objects
356: */
357: public List getMethodPermissionsDescList() {
358: return methodPermissionsDescList;
359: }
360:
361: /**
362: * Get the exclude list of the assembly descriptor
363: * @return the exclude list of the assembly descriptor
364: */
365: public ExcludeListDesc getExcludeListDesc() {
366: return excludeListDesc;
367: }
368:
369: /**
370: * Get bean descriptor given its interface local name
371: * @param itfLocalName local interface name
372: * @return null if it doesn't exist.
373: */
374: public BeanDesc getBeanDescWithLocalInterface(String itfLocalName) {
375: for (Iterator i = beanDesc.values().iterator(); i.hasNext();) {
376: BeanDesc bd = (BeanDesc) i.next();
377: if (bd.getLocalClass() != null) {
378: if (itfLocalName.equals(bd.getLocalClass().getName())) {
379: return bd;
380: }
381: }
382: }
383: return null;
384: }
385:
386: /**
387: * Find the JOnAS message destination for the given name
388: * @param mdLink the name of the message destination link in the Deployment Descriptor
389: * @return boolean if link was found
390: */
391: public boolean getMessageDestination(String mdLink) {
392: MessageDestination md = null;
393: if (asd != null && asd.getMessageDestinationList() != null) {
394: for (Iterator i = asd.getMessageDestinationList()
395: .iterator(); i.hasNext();) {
396: md = (MessageDestination) i.next();
397: if (md.getMessageDestinationName().equals(mdLink)) {
398: return true;
399: }
400: }
401: }
402: return false;
403: }
404:
405: /**
406: * Get the JOnAS message destination for the given name
407: * @param mdLink the name of the message destination link in the Deployment Descriptor
408: * @return the jonas message destination given the name
409: */
410: public JonasMessageDestination getJonasMessageDestination(
411: String mdLink) {
412: JonasMessageDestination jmd = null;
413: if (jonasMDList != null) {
414: for (Iterator i = jonasMDList.iterator(); i.hasNext();) {
415: jmd = (JonasMessageDestination) i.next();
416: if (jmd.getMessageDestinationName().equals(mdLink)) {
417: return jmd;
418: }
419: }
420: }
421: return null;
422: }
423:
424: /**
425: * In case of beans with old CMP1 persistance, we need to instanciate the old class,
426: * as if we were in an old Deployment Descriptor.
427: * Default is CMP2.x for entity beans with a EJB2.0 DD.
428: *
429: * @param cl The ClassLoader to be used
430: * @param ent Entity MetaInformation from XML files
431: * @param asd AssemblyDescriptor MetaInformation from XML files
432: * @param j JonasEntity MetaInformation from XML files
433: * @param jMDRList MessageDrivenRef list
434: *
435: * @return The Entity Bean Descriptor, for the good CMP version.
436: *
437: * @throws DeploymentDescException Cannot build Entity Descriptor
438: */
439: protected abstract BeanDesc newEntityBeanDesc(ClassLoader cl,
440: Entity ent, AssemblyDescriptor asd, JonasEntity j,
441: JLinkedList jMDRList) throws DeploymentDescException;
442:
443: /**
444: * Get the display name
445: * @return the Display name string, from the deployment descriptor.
446: */
447: public String getDisplayName() {
448: return displayName;
449: }
450:
451: /**
452: * Get the ejb-client-jar name
453: * @return the ejb-client-jar string, from the deployment descriptor (may be null).
454: */
455: public String getEjbClientJar() {
456: return ejbClientJar;
457: }
458:
459: /**
460: * get the current logger
461: * @return the Logger
462: */
463: public Logger getLogger() {
464: return logger;
465: }
466:
467: /**
468: * set the current logger
469: * @param logger the Logger
470: */
471: public void setLogger(Logger logger) {
472: this .logger = logger;
473: }
474:
475: /**
476: * Return the content of the web.xml file
477: * @return the content of the web.xml file
478: */
479: public String getXmlContent() {
480: return xmlContent;
481: }
482:
483: /**
484: * Return the content of the jonas-web.xml file
485: * @return the content of the jonas-web.xml file
486: */
487: public String getJOnASXmlContent() {
488: return jonasXmlContent;
489: }
490:
491: /**
492: * String representation of the object for test purpose
493: * @return String representation of this object
494: */
495: public String toString() {
496: StringBuffer ret = new StringBuffer();
497: ret.append("\ngetDisplayName()=" + getDisplayName());
498: ret.append("\ngetEjbClientJar()=" + getEjbClientJar());
499: BeanDesc[] b = getBeanDesc();
500: for (int i = 0; i < b.length; i++) {
501: ret.append("\ngetBeanDesc(" + i + ")="
502: + b[i].getClass().getName());
503: ret.append(b[i].toString());
504: }
505: return ret.toString();
506: }
507:
508: /**
509: * @param xmlContent XML Content
510: */
511: public void setXmlContent(String xmlContent) {
512: this .xmlContent = xmlContent;
513: }
514:
515: /**
516: * @param jonasXmlContent XML Content
517: */
518: public void setJOnASXmlContent(String jonasXmlContent) {
519: this.jonasXmlContent = jonasXmlContent;
520: }
521:
522: }
|