001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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 02111-1307
019: * USA
020: *
021: * Initial developer(s): Florent BENOIT & Ludovic BERT
022: * --------------------------------------------------------------------------
023: * $Id: WebContainerDeploymentDesc.java 5201 2004-07-29 12:19:56Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_web.deployment.api;
026:
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.HashMap;
030: import java.util.Hashtable;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.Vector;
035:
036: import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
037: import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc;
038: import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
039: import org.objectweb.jonas_lib.deployment.api.EnvEntryDesc;
040: import org.objectweb.jonas_lib.deployment.api.JndiEnvRefsGroupDesc;
041: import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc;
042: import org.objectweb.jonas_lib.deployment.api.ResourceEnvRefDesc;
043: import org.objectweb.jonas_lib.deployment.api.ResourceRefDesc;
044: import org.objectweb.jonas_lib.deployment.xml.SecurityRole;
045:
046: import org.objectweb.jonas_web.deployment.xml.JonasWebApp;
047: import org.objectweb.jonas_web.deployment.xml.Servlet;
048: import org.objectweb.jonas_web.deployment.xml.ServletMapping;
049: import org.objectweb.jonas_web.deployment.xml.WebApp;
050:
051: import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
052:
053: /**
054: * This class do the parsing of the web.xml file and jonas-web.xml files and
055: * contruct a data structure associated to these two files.
056: * 03/03 : Can read web.xml and jonas-web.xml if the url of the war is a directory.
057: * @author Ludovic Bert
058: * @author Florent Benoit
059: * @author Philippe Coq
060: */
061: public class WebContainerDeploymentDesc extends JndiEnvRefsGroupDesc {
062:
063: /**
064: * The host on which the web application must be depoyed (can be null in
065: * ear case).
066: */
067: private String host = null;
068:
069: /**
070: * The context root of the web application (can be null in ear case).
071: */
072: private String contextRoot = null;
073:
074: /**
075: * The port number of the prefered connector to access the web application
076: * (can be null if defaults values can be found). Used only for WebServices.
077: */
078: private String port = null;
079:
080: /**
081: * The delegation model used by the class loader context follows the java 2 delegation model ?
082: */
083: private boolean java2DelegationModel = true;
084:
085: /**
086: * List of mapping between servlets names and servlets classes
087: */
088: private Map servlets = new HashMap();
089:
090: /**
091: * List of mapping between servlets names and servlets url-mapping
092: */
093: private Hashtable servletsUrlMapping = new Hashtable();
094:
095: /**
096: * Xml content of the web.xml file
097: */
098: private String xmlContent = "";
099:
100: /**
101: * Xml content of the jonas-web.xml file
102: */
103: private String jonasXmlContent = "";
104:
105: /**
106: * list of URLs
107: */
108: private List securityRoleList;
109:
110: /**
111: * URLPattern Constraints
112: */
113: private SecurityConstraintListDesc securityConstraintListDesc;
114:
115: /**
116: * Construct an instance of a WebContainerDeploymentDesc.<BR>
117: * Constructor is private, call one of the static getInstance
118: * method instead.
119: * @param fileName the name of the warFile
120: * @param classLoader the classloader for the classes.
121: * @param webApp the data structure of the web-app (web.xml)
122: * @param jonasWebApp the data structure of the jonas-web-app
123: * (jonas-web.xml)
124: * @throws DeploymentDescException if the deployment
125: * descriptors are corrupted.
126: */
127: public WebContainerDeploymentDesc(String fileName,
128: ClassLoader classLoader, WebApp webApp,
129: JonasWebApp jonasWebApp) throws DeploymentDescException {
130:
131: super (classLoader, webApp, jonasWebApp, fileName);
132:
133: // host
134: if (jonasWebApp.getHost() != null) {
135: host = jonasWebApp.getHost();
136: } else {
137: host = null;
138: }
139:
140: // context-root
141: if (jonasWebApp.getContextRoot() != null) {
142: contextRoot = jonasWebApp.getContextRoot();
143: } else {
144: contextRoot = null;
145: }
146:
147: // port
148: if (jonasWebApp.getPort() != null) {
149: port = jonasWebApp.getPort();
150: } else {
151: port = null;
152: }
153:
154: // Class loader delegation model
155: String delegationModel = null;
156: if (jonasWebApp.getJava2DelegationModel() != null) {
157: delegationModel = jonasWebApp.getJava2DelegationModel();
158: } else {
159: delegationModel = "true";
160: }
161:
162: if (delegationModel.equalsIgnoreCase("false")) {
163: java2DelegationModel = false;
164: } else if (delegationModel.equalsIgnoreCase("true")) {
165: java2DelegationModel = true;
166: } else {
167: throw new WebContainerDeploymentDescException(
168: "The java2 delegation model could be 'true' or 'false', not '"
169: + delegationModel + "'.");
170: }
171:
172: // Security Role List
173: SecurityRole securityRole = null;
174: securityRoleList = new ArrayList();
175: for (Iterator itSecurityRole = webApp.getSecurityRoleList()
176: .iterator(); itSecurityRole.hasNext();) {
177: securityRole = (SecurityRole) itSecurityRole.next();
178: securityRoleList.add(new SecurityRoleDesc(securityRole));
179: }
180:
181: // servlet classes + names
182: List servletList = webApp.getServletList();
183: for (Iterator i = servletList.iterator(); i.hasNext();) {
184: Servlet servlet = (Servlet) i.next();
185: if (servlet.getServletName() != null) {
186: ServletDesc servletDesc = new ServletDesc(servlet);
187: servlets.put(servlet.getServletName(), servletDesc);
188: }
189: }
190:
191: // Construct servletsUrlMapping
192: List urlMappings = webApp.getServletMappingList();
193: for (Iterator i = servletList.iterator(); i.hasNext();) {
194: Servlet servlet = (Servlet) i.next();
195: String name = servlet.getServletName().trim();
196: Vector mappings = new Vector();
197: for (Iterator m = urlMappings.iterator(); m.hasNext();) {
198: ServletMapping sm = (ServletMapping) m.next();
199: if (sm.getServletName().trim().equals(name)) {
200: String pattern = sm.getUrlPattern().trim();
201: if (pattern.indexOf('\n') != -1) {
202: throw new WebContainerDeploymentDescException(
203: "There is a '\\n' character inside the url pattern for servlet named '"
204: + sm.getServletName()
205: + "' in the file '" + fileName
206: + "'.");
207: }
208: mappings.add(pattern);
209: }
210: }
211: servletsUrlMapping.put(name, mappings);
212: }
213:
214: // Build SecurityConstraintListDesc
215: securityConstraintListDesc = new SecurityConstraintListDesc(
216: webApp);
217:
218: /*
219: * See SRV 13.2 of servlet spec 2.4 <br>
220: * The sub elements under web-app can be in an arbitrary order in this
221: * version of the specification. Because of the restriction of XML
222: * Schema, The multiplicity of the elements distributable,
223: * session-config, welcome-file-list, jspconfig, login-config, and
224: * locale-encoding-mapping-list was changed from optional to 0 or more .
225: * The containers must inform the developer with a descriptive error
226: * message when the deployment descriptor contains more than one element
227: * of session-config, jsp-config, and login-config.
228: */
229: if (webApp.getJspConfigNumber() > 1) {
230: throw new WebContainerDeploymentDescException(
231: "The web-app element must contain only one element jsp-config in file '"
232: + fileName + "'");
233: }
234:
235: if (webApp.getLoginConfigNumber() > 1) {
236: throw new WebContainerDeploymentDescException(
237: "The web-app element must contain only one element login-config in file '"
238: + fileName + "'");
239: }
240:
241: if (webApp.getSessionConfigNumber() > 1) {
242: throw new WebContainerDeploymentDescException(
243: "The web-app element must contain only one element session-config in file '"
244: + fileName + "'");
245: }
246:
247: }
248:
249: /**
250: * Return the content of the web.xml file
251: * @return the content of the web.xml file
252: */
253: public String getXmlContent() {
254: return xmlContent;
255: }
256:
257: /**
258: * Return the content of the jonas-web.xml file
259: * @return the content of the jonas-web.xml file
260: */
261: public String getJOnASXmlContent() {
262: return jonasXmlContent;
263: }
264:
265: /**
266: * Set the content of the web.xml file
267: * @param xml the content of the file
268: */
269: public void setXmlContent(String xml) {
270: xmlContent = xml;
271: }
272:
273: /**
274: * Set the content of the jonas-web.xml file
275: * @param jXml the content of the file
276: */
277: public void setJOnASXmlContent(String jXml) {
278: jonasXmlContent = jXml;
279: }
280:
281: /**
282: * Get the context root of this web application.
283: * @return the context root of this web application.
284: */
285: public String getContextRoot() {
286: return contextRoot;
287: }
288:
289: /**
290: * Context classloader must follow the java2 delegation model ?
291: * @return true if the context's classloader must follow the java 2 delegation model.
292: */
293: public boolean getJava2DelegationModel() {
294: return java2DelegationModel;
295: }
296:
297: /**
298: * Get the host on which the web application must be deployed.
299: * @return the host on which the web application must be deployed.
300: */
301: public String getHost() {
302: return host;
303: }
304:
305: /**
306: * Get the prefered port of the connector used to access the web application.
307: * @return the prefered port of the connector used to access the web application.
308: */
309: public String getPort() {
310: return port;
311: }
312:
313: /**
314: * Return a String representation of the WebContainerDeploymentDesc.
315: * @return a String representation of the WebContainerDeploymentDesc.
316: */
317: public String toString() {
318: StringBuffer ret = new StringBuffer();
319:
320: // Return the displayName
321: ret.append("\ngetDisplayName()=" + getDisplayName());
322:
323: // Return the resource-env-ref
324: ResourceEnvRefDesc[] rer = getResourceEnvRefDesc();
325: for (int i = 0; i < rer.length; i++) {
326: ret.append("\ngetResourceEnvRefDesc(" + i + ")="
327: + rer[i].getClass().getName());
328: ret.append(rer[i].toString());
329: }
330:
331: // Return the resource-ref
332: ResourceRefDesc[] resourceRefDesc = getResourceRefDesc();
333: for (int i = 0; i < resourceRefDesc.length; i++) {
334: ret.append("\ngetResourceRefDesc(" + i + ")="
335: + resourceRefDesc[i].getClass().getName());
336: ret.append(resourceRefDesc[i].toString());
337: }
338:
339: // Return the env-entry
340: EnvEntryDesc[] envEntries = getEnvEntryDesc();
341: for (int i = 0; i < envEntries.length; i++) {
342: ret.append("\ngetEnvEntryDesc(" + i + ")="
343: + envEntries[i].getClass().getName());
344: ret.append(envEntries[i].toString());
345: }
346:
347: // Return the ejb-ref
348: EjbRefDesc[] ejbRefDesc = getEjbRefDesc();
349: for (int i = 0; i < ejbRefDesc.length; i++) {
350: ret.append("\ngetEjbRefDesc(" + i + ")="
351: + ejbRefDesc[i].getClass().getName());
352: ret.append(ejbRefDesc[i].toString());
353: }
354:
355: // Return the ejb-local-ref
356: EjbLocalRefDesc[] ejbLocalRefDesc = getEjbLocalRefDesc();
357: for (int i = 0; i < ejbLocalRefDesc.length; i++) {
358: ret.append("\ngetEjbLocalRefDesc(" + i + ")="
359: + ejbLocalRefDesc[i].getClass().getName());
360: ret.append(ejbLocalRefDesc[i].toString());
361: }
362:
363: // Return the service-ref-name
364: ServiceRefDesc[] svcRef = getServiceRefDesc();
365: for (int i = 0; i < svcRef.length; i++) {
366: ret.append("\ngetServiceRefDesc(" + i + ")="
367: + svcRef[i].getClass().getName());
368: ret.append(svcRef[i].toString());
369: }
370:
371: // Return the message-destination-ref
372: MessageDestinationRefDesc[] mdRefDesc = getMessageDestinationRefDesc();
373: for (int i = 0; i < mdRefDesc.length; i++) {
374: ret.append("\ngetMessageDestinationRefDesc(" + i + ")="
375: + mdRefDesc[i].getClass().getName());
376: ret.append(mdRefDesc[i].toString());
377: }
378:
379: // Return the host
380: ret.append("\ngetHost()=" + getHost());
381:
382: // Return the context-root
383: ret.append("\ngetContextRoot()=" + getContextRoot());
384:
385: return ret.toString();
386: }
387:
388: /**
389: * Gets the list of Servlets
390: * @return list of Servlets
391: */
392: public Collection getServletDescList() {
393: return servlets.values();
394: }
395:
396: /**
397: * Return a list of all servlets name available
398: * @return a list of all servlets name available
399: */
400: public String[] getServletsName() {
401: String[] st = new String[servlets.size()];
402: return (String[]) servlets.keySet().toArray(st);
403: }
404:
405: /**
406: * Return the classname of the given servlet
407: * @param servName name of the given servlet
408: * @return the classname of the given servlet
409: */
410: public String getServletClassname(String servName) {
411: return ((ServletDesc) servlets.get(servName)).getServletClass();
412: }
413:
414: /**
415: * Gets the constraint list
416: * @return the constraint list
417: */
418: public SecurityConstraintListDesc getSecurityConstraintListDesc() {
419: return securityConstraintListDesc;
420: }
421:
422: /**
423: * Gets the list of security roles
424: * @return the list of security roles
425: */
426: public List getSecurityRoleList() {
427: return securityRoleList;
428: }
429:
430: /**
431: * Return the list of urlMapping of the given servlet
432: * @param servName name of the given servlet
433: * @return the list of urlMapping of the given servlet
434: */
435: public List getServletMappings(String servName) {
436: return (List) servletsUrlMapping.get(servName);
437: }
438:
439: }
|