001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.deployment;
023:
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.LinkedHashMap;
027: import java.util.Map;
028:
029: import javax.management.MBeanServer;
030: import javax.management.ObjectName;
031:
032: import org.jboss.metadata.MetaData;
033: import org.jboss.mx.util.MBeanServerLocator;
034: import org.jboss.security.SecurityRoleMetaData;
035: import org.w3c.dom.Element;
036:
037: /**
038: * A representation of the application.xml and jboss-app.xml deployment
039: * descriptors.
040: *
041: * @author Thomas.Diesler@jboss.org
042: * @author Scott.Stark@jboss.org
043: * @version $Revision: 60677 $
044: * @see org.jboss.metadata.XmlLoadable
045: */
046: public class J2eeApplicationMetaData extends MetaData {
047: // Constants -----------------------------------------------------
048:
049: // Attributes ----------------------------------------------------
050: private String displayName;
051: private String description;
052: private String smallIcon;
053: private String largeIcon;
054:
055: /**
056: * The security-roles
057: */
058: private HashMap securityRoles = new HashMap();
059: /**
060: * The jboss-app.xml JNDI name of the security domain implementation
061: */
062: private String securityDomain;
063: /**
064: * The unauthenticated-principal value assigned to the application
065: */
066: private String unauthenticatedPrincipal;
067: /** The application.xml name->modules in definition order */
068: private Map modules = new LinkedHashMap();
069:
070: /** The jmx name */
071: private String jmxName;
072:
073: /** The library-directory name */
074: private String libraryDirectory;
075:
076: /**
077: * Module order will be based on the deployment sorter (implicit) or on the
078: * ordering of modules defined in application.xml and jboss-app.xml (strict).
079: */
080: private String moduleOrder = "implicit";
081:
082: // Static --------------------------------------------------------
083:
084: // Public --------------------------------------------------------
085:
086: public String getDisplayName() {
087: return displayName;
088: }
089:
090: public String getDescription() {
091: return description;
092: }
093:
094: public String getSmallIcon() {
095: return smallIcon;
096: }
097:
098: public String getLargeIcon() {
099: return largeIcon;
100: }
101:
102: public Iterator getModules() {
103: return modules.values().iterator();
104: }
105:
106: public boolean hasModule(String name) {
107: return modules.containsKey(name);
108: }
109:
110: public Map getSecurityRoles() {
111: return new HashMap(securityRoles);
112: }
113:
114: public String getSecurityDomain() {
115: return securityDomain;
116: }
117:
118: public String getUnauthenticatedPrincipal() {
119: return unauthenticatedPrincipal;
120: }
121:
122: public String getJMXName() {
123: return jmxName;
124: }
125:
126: public String getLibraryDirectory() {
127: return libraryDirectory;
128: }
129:
130: public String getModuleOrder() {
131: return moduleOrder;
132: }
133:
134: /**
135: * Imports either the application.xml or jboss-app.xml from the given element.
136: *
137: * @param rootElement The element to import.
138: * @throws DeploymentException Unrecognized root tag.
139: */
140: public void importXml(Element rootElement)
141: throws DeploymentException {
142: String rootTag = rootElement.getOwnerDocument()
143: .getDocumentElement().getTagName();
144: if (rootTag.equals("application")) {
145: importApplicationXml(rootElement);
146: } else if (rootTag.equals("jboss-app")) {
147: importJBossAppXml(rootElement);
148: } else {
149: throw new DeploymentException("Unrecognized root tag: "
150: + rootTag);
151: }
152: }
153:
154: protected void importApplicationXml(Element rootElement)
155: throws DeploymentException {
156: // j2ee_1_4.xsd describes display-name as minOccurs="0" to maxOccurs="unbounded"
157: displayName = super .getOptionalChildContent(rootElement,
158: "display-name", "");
159:
160: Element descrElement = getOptionalChild(rootElement,
161: "description");
162: description = descrElement != null ? getElementContent(descrElement)
163: : "";
164:
165: Element iconElement = getOptionalChild(rootElement, "icon");
166: if (iconElement != null) {
167: Element element = getOptionalChild(iconElement,
168: "small-icon");
169: smallIcon = element != null ? getElementContent(element)
170: : "";
171:
172: element = getOptionalChild(iconElement, "large-icon");
173: largeIcon = element != null ? getElementContent(element)
174: : "";
175: } else {
176: smallIcon = "";
177: largeIcon = "";
178: }
179:
180: // extract modules...
181: for (Iterator it = getChildrenByTagName(rootElement, "module"); it
182: .hasNext();) {
183: J2eeModuleMetaData moduleMetaData = new J2eeModuleMetaData();
184: moduleMetaData.importXml((Element) it.next());
185: modules.put(moduleMetaData.getFileName(), moduleMetaData);
186: }
187: }
188:
189: protected void importJBossAppXml(Element rootElement)
190: throws DeploymentException {
191: // Get the security domain name
192: Element securityDomainElement = getOptionalChild(rootElement,
193: "security-domain");
194: if (securityDomainElement != null) {
195: securityDomain = getElementContent(securityDomainElement);
196: }
197:
198: // Get the unauthenticated-principal name
199: Element unauth = getOptionalChild(rootElement,
200: "unauthenticated-principal");
201: if (unauth != null) {
202: unauthenticatedPrincipal = getElementContent(unauth);
203: } else {
204: try {
205: MBeanServer server = MBeanServerLocator.locateJBoss();
206: ObjectName oname = new ObjectName(
207: "jboss.security:service=JaasSecurityManager");
208: unauthenticatedPrincipal = (String) server
209: .getAttribute(oname,
210: "DefaultUnauthenticatedPrincipal");
211: } catch (Exception e) {
212: log.error("Cannot obtain unauthenticated principal");
213: }
214: }
215:
216: // set the security roles (optional)
217: Iterator iterator = getChildrenByTagName(rootElement,
218: "security-role");
219: while (iterator.hasNext()) {
220: Element securityRole = (Element) iterator.next();
221: String roleName = getElementContent(getUniqueChild(
222: securityRole, "role-name"));
223: SecurityRoleMetaData srMetaData = new SecurityRoleMetaData(
224: roleName);
225:
226: Iterator itPrincipalNames = getChildrenByTagName(
227: securityRole, "principal-name");
228: while (itPrincipalNames.hasNext()) {
229: String principalName = getElementContent((Element) itPrincipalNames
230: .next());
231: srMetaData.addPrincipalName(principalName);
232: }
233: securityRoles.put(roleName, srMetaData);
234: }
235:
236: // Get any user defined JMX name
237: Element jmxNameElement = getOptionalChild(rootElement,
238: "jmx-name");
239: if (jmxNameElement != null)
240: jmxName = getElementContent(jmxNameElement);
241:
242: // Get any library-directory. The default empty elements means there is no library directory
243: Element libDirElement = getOptionalChild(rootElement,
244: "library-directory");
245: if (libDirElement != null)
246: libraryDirectory = getElementContent(libDirElement, "");
247:
248: // extract modules...
249: for (Iterator it = getChildrenByTagName(rootElement, "module"); it
250: .hasNext();) {
251: J2eeModuleMetaData moduleMetaData = new J2eeModuleMetaData();
252: moduleMetaData.importXml((Element) it.next());
253: modules.put(moduleMetaData.getFileName(), moduleMetaData);
254: }
255:
256: //Get the Deployment Ordering style
257: Element moduleOrderElement = getOptionalChild(rootElement,
258: "module-order");
259: if (moduleOrderElement != null) {
260: moduleOrder = getElementContent(moduleOrderElement);
261: }
262: }
263: }
|