KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > J2eeApplicationMetaData


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployment;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedHashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.metadata.IconMetaData;
33 import org.jboss.metadata.MetaData;
34 import org.jboss.metadata.SecurityRoleMetaData;
35 import org.jboss.mx.loading.LoaderRepositoryFactory;
36 import org.jboss.mx.util.MBeanServerLocator;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * A representation of the application.xml and jboss-app.xml deployment
41  * descriptors.
42  *
43  * @author Thomas.Diesler@jboss.org
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 58284 $
46  */

47 public class J2eeApplicationMetaData
48         extends MetaData
49 {
50    // Constants -----------------------------------------------------
51

52    // Attributes ----------------------------------------------------
53
/** application/display-name */
54    private String JavaDoc displayName;
55    /** application/description */
56    private String JavaDoc description;
57    /** application/icon */
58    private IconMetaData icon = new IconMetaData();
59    /** The application/library-directory name, defaults to lib */
60    private String JavaDoc libDirName = "lib";
61    /** The application element version */
62    private String JavaDoc version;
63    /** jboss-app/loader-repository */
64    private LoaderRepositoryFactory.LoaderRepositoryConfig loaderCfg;
65
66    /**
67     * The security-roles
68     */

69    private HashMap JavaDoc<String JavaDoc, SecurityRoleMetaData> securityRoles = new HashMap JavaDoc<String JavaDoc, SecurityRoleMetaData>();
70    /**
71     * The jboss-app.xml/security-domain name
72     */

73    private String JavaDoc securityDomain;
74    /**
75     * The unauthenticated-principal value assigned to the application
76     */

77    private String JavaDoc unauthenticatedPrincipal;
78    /** The application.xml name->modules in definition order */
79    private Map JavaDoc<String JavaDoc, J2eeModuleMetaData> modules = new LinkedHashMap JavaDoc<String JavaDoc, J2eeModuleMetaData>();
80    
81    /** The jmx name */
82    private String JavaDoc jmxName;
83
84    // Static --------------------------------------------------------
85

86    // Public --------------------------------------------------------
87

88    public String JavaDoc getVersion()
89    {
90       return version;
91    }
92    public void setVersion(String JavaDoc version)
93    {
94       this.version = version;
95    }
96
97    public String JavaDoc getDisplayName()
98    {
99       return displayName;
100    }
101    public void setDisplayName(String JavaDoc name)
102    {
103       this.displayName = name;
104    }
105
106    public String JavaDoc getDescription()
107    {
108       return description;
109    }
110    public void setDescription(String JavaDoc description)
111    {
112       this.description = description;
113    }
114
115    public String JavaDoc getSmallIcon()
116    {
117       return icon.getSmallIcon();
118    }
119
120    public String JavaDoc getLargeIcon()
121    {
122       return icon.getLargeIcon();
123    }
124
125    /**
126     * Get the module metadata for an application module
127     * @param path - the J2eeModuleMetaData.fileName specified in application.xml
128     * @return the module if it exists
129     */

130    public J2eeModuleMetaData getModule(String JavaDoc path)
131    {
132       J2eeModuleMetaData module = modules.get(path);
133       if( path.startsWith("/") )
134       {
135          // try a non-absolute path
136
path = path.substring(1);
137          module = modules.get(path);
138       }
139       return module;
140    }
141    /**
142     * Get all modules defined in the application
143     * @return the modules iterator
144     */

145    public Iterator JavaDoc<J2eeModuleMetaData> getModules()
146    {
147       return modules.values().iterator();
148    }
149
150    public boolean hasModule(String JavaDoc name)
151    {
152       return modules.containsKey(name);
153    }
154    
155    public Map JavaDoc<String JavaDoc, SecurityRoleMetaData> getSecurityRoles()
156    {
157       return new HashMap JavaDoc<String JavaDoc, SecurityRoleMetaData>(securityRoles);
158    }
159    public void addSecurityRole(SecurityRoleMetaData role)
160    {
161       securityRoles.put(role.getRoleName(), role);
162    }
163    public SecurityRoleMetaData getSecurityRole(String JavaDoc name)
164    {
165       return securityRoles.get(name);
166    }
167
168    public String JavaDoc getSecurityDomain()
169    {
170       return securityDomain;
171    }
172    public void setSecurityDomain(String JavaDoc domain)
173    {
174       this.securityDomain = domain;
175    }
176
177    public String JavaDoc getUnauthenticatedPrincipal()
178    {
179       return unauthenticatedPrincipal;
180    }
181    public void setUnauthenticatedPrincipal(String JavaDoc principal)
182    {
183       this.unauthenticatedPrincipal = principal;
184    }
185
186    public String JavaDoc getJMXName()
187    {
188       return jmxName;
189    }
190    public void setJMXName(String JavaDoc name)
191    {
192       this.jmxName = name;
193    }
194
195    public String JavaDoc getLibraryDirectory()
196    {
197       return libDirName;
198    }
199    public void setLibraryDirectory(String JavaDoc name)
200    {
201       this.libDirName = name;
202    }
203
204    public LoaderRepositoryFactory.LoaderRepositoryConfig getLoaderCfg()
205    {
206       return loaderCfg;
207    }
208    public void setLoaderCfg(LoaderRepositoryFactory.LoaderRepositoryConfig loaderCfg)
209    {
210       this.loaderCfg = loaderCfg;
211    }
212    /**
213     * Add a module to the module map.
214     * @param moduleMetaData
215     */

216    public void addModule(J2eeModuleMetaData moduleMetaData)
217    {
218       this.modules.put(moduleMetaData.getFileName(), moduleMetaData);
219    }
220
221    /**
222     * Imports either the application.xml or jboss-app.xml from the given element.
223     *
224     * @param rootElement The element to import.
225     * @throws DeploymentException Unrecognized root tag.
226     */

227    public void importXml(Element JavaDoc rootElement) throws DeploymentException
228    {
229       String JavaDoc rootTag = rootElement.getOwnerDocument().getDocumentElement().getTagName();
230       if (rootTag.equals("application"))
231       {
232          importApplicationXml(rootElement);
233       }
234       else if (rootTag.equals("jboss-app"))
235       {
236          importJBossAppXml(rootElement);
237       }
238       else
239       {
240          throw new DeploymentException("Unrecognized root tag: " + rootTag);
241       }
242    }
243
244    protected void importApplicationXml(Element JavaDoc rootElement) throws DeploymentException
245    {
246       version = getElementAttribute(rootElement, "version");
247       // j2ee_1_4.xsd describes display-name as minOccurs="0" to maxOccurs="unbounded"
248
displayName = super.getOptionalChildContent(rootElement, "display-name", "");
249
250       Element JavaDoc descrElement = getOptionalChild(rootElement, "description");
251       description = descrElement != null ? getElementContent(descrElement) : "";
252
253       Element JavaDoc iconElement = getOptionalChild(rootElement, "icon");
254       if (iconElement != null)
255       {
256          Element JavaDoc element = getOptionalChild(iconElement, "small-icon");
257          String JavaDoc value = element != null ? getElementContent(element) : "";
258          icon.setSmallIcon(value);
259
260          element = getOptionalChild(iconElement, "large-icon");
261          value = element != null ? getElementContent(element) : "";
262          icon.setLargeIcon(value);
263       }
264
265       // Look for an ear library-directory
266
libDirName = getOptionalChildContent(rootElement, "library-directory", "lib");
267       if( libDirName != null && libDirName.length() == 0 )
268          libDirName = null;
269
270       // extract modules...
271
for (Iterator JavaDoc it = getChildrenByTagName(rootElement, "module"); it.hasNext();)
272       {
273          J2eeModuleMetaData moduleMetaData = new J2eeModuleMetaData();
274          moduleMetaData.importXml((Element JavaDoc) it.next());
275          addModule(moduleMetaData);
276       }
277    }
278
279    protected void importJBossAppXml(Element JavaDoc rootElement) throws DeploymentException
280    {
281       // Get the security domain name
282
Element JavaDoc securityDomainElement = getOptionalChild(rootElement, "security-domain");
283       if (securityDomainElement != null)
284       {
285          securityDomain = getElementContent(securityDomainElement);
286       }
287
288       // Get the unauthenticated-principal name
289
Element JavaDoc unauth = getOptionalChild(rootElement, "unauthenticated-principal");
290       if (unauth != null)
291       {
292          unauthenticatedPrincipal = getElementContent(unauth);
293       }
294       else
295       {
296          try
297          {
298             MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
299             ObjectName JavaDoc oname = new ObjectName JavaDoc("jboss.security:service=JaasSecurityManager");
300             unauthenticatedPrincipal = (String JavaDoc) server.getAttribute(oname, "DefaultUnauthenticatedPrincipal");
301          }
302          catch (Exception JavaDoc e)
303          {
304             log.error("Cannot obtain unauthenticated principal");
305          }
306       }
307
308       // set the security roles (optional)
309
Iterator JavaDoc iterator = getChildrenByTagName(rootElement, "security-role");
310       while (iterator.hasNext())
311       {
312          Element JavaDoc securityRole = (Element JavaDoc) iterator.next();
313          String JavaDoc roleName = getElementContent(getUniqueChild(securityRole, "role-name"));
314          SecurityRoleMetaData srMetaData = new SecurityRoleMetaData(roleName);
315
316          Iterator JavaDoc itPrincipalNames = getChildrenByTagName(securityRole, "principal-name");
317          while (itPrincipalNames.hasNext())
318          {
319             String JavaDoc principalName = getElementContent((Element JavaDoc) itPrincipalNames.next());
320             srMetaData.addPrincipalName(principalName);
321          }
322          securityRoles.put(roleName, srMetaData);
323       }
324
325       // Get any user defined JMX name
326
Element JavaDoc jmxNameElement = getOptionalChild(rootElement, "jmx-name");
327       if (jmxNameElement != null)
328          jmxName = getElementContent(jmxNameElement);
329
330       // extract modules...
331
for (Iterator JavaDoc it = getChildrenByTagName(rootElement, "module"); it.hasNext();)
332       {
333          J2eeModuleMetaData moduleMetaData = new J2eeModuleMetaData();
334          moduleMetaData.importXml((Element JavaDoc) it.next());
335          addModule(moduleMetaData);
336       }
337    }
338 }
339
Popular Tags