KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > J2EEDeployedObject


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.management.j2ee;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.security.InvalidParameterException JavaDoc;
31
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 import org.jboss.logging.Logger;
36
37 /**
38  * Root class of the JBoss JSR-77 implementation of J2EEDeployedObject.
39  *
40  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
41  * @author <a HREF="thomas.diesler@jboss.org">Thomas Diesler</a>
42  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
43  * @version $Revision: 40550 $
44  */

45 public abstract class J2EEDeployedObject extends J2EEManagedObject
46    implements J2EEDeployedObjectMBean
47 {
48    // Constants -----------------------------------------------------
49

50    public static final int APPLICATION = 0;
51    public static final int WEB = 1;
52    public static final int EJB = 2;
53    public static final int RAR = 3;
54    public static final int SAR = 4;
55    public static final int JBOSS = 5;
56    public static final int JAWS = 6;
57    public static final int CMP = 7;
58    public static final int JBOSS_WEB = 8;
59
60    /** The logger */
61    private static final Logger log = Logger.getLogger(J2EEDeployedObject.class);
62    
63    private static final String JavaDoc[] sDescriptors = new String JavaDoc[]{
64       "META-INF/application.xml",
65       "WEB-INF/web.xml",
66       "META-INF/ejb-jar.xml",
67       "META-INF/ra.xml",
68       "META-INF/jboss-service.xml",
69       "META-INF/jboss.xml",
70       "META-INF/jaws.xml",
71       "META-INF/jbosscmp-jdbc.xml",
72       "WEB-INF/jboss-web.xml",
73    };
74
75    // Attributes ----------------------------------------------------
76

77    private String JavaDoc mDeploymentDescriptor;
78
79    // Static --------------------------------------------------------
80

81    public static String JavaDoc getDeploymentDescriptor(URL JavaDoc pJarUrl, int pType)
82    {
83       return getDeploymentDescriptor(pJarUrl, sDescriptors[pType]);
84    }
85
86    /**
87     * Loads and returns in a string form the deployment descriptor
88     * for a module. If the descriptor relative path is null the
89     * baseJarUrl is used as the descriptor itself. Otherwise baseJarUrl
90     * and descriptor are combined to form the final descriptor.
91     */

92    public static String JavaDoc getDeploymentDescriptor(URL JavaDoc baseJarUrl, String JavaDoc descriptor)
93    {
94       if (baseJarUrl == null)
95       {
96          // Return if the given URL is null
97
return null;
98       }
99       String JavaDoc lDD = null;
100       Reader JavaDoc lInput = null;
101       StringWriter JavaDoc lOutput = null;
102       try
103       {
104          if (descriptor == null)
105          {
106             // Use the baseJarUrl as the descriptor
107
lInput = new InputStreamReader JavaDoc(baseJarUrl.openStream());
108          }
109          else
110          {
111             // Look for an embedded descriptor
112
log.debug("File: " + baseJarUrl + ", descriptor: " + descriptor);
113             ClassLoader JavaDoc localCl = new URLClassLoader JavaDoc(new URL JavaDoc[]{baseJarUrl});
114             InputStream JavaDoc lStream = localCl.getResourceAsStream(descriptor);
115             if (lStream == null)
116             {
117                // If DD not found then return a null indicating the file is not available
118
return null;
119             }
120             lInput = new InputStreamReader JavaDoc(lStream);
121          }
122          lOutput = new StringWriter JavaDoc();
123          char[] lBuffer = new char[1024];
124          int lLength = 0;
125          while ((lLength = lInput.read(lBuffer)) > 0)
126          {
127             lOutput.write(lBuffer, 0, lLength);
128          }
129          lDD = lOutput.toString();
130       }
131       catch (Exception JavaDoc e)
132       {
133          log.error("failed to get deployment descriptor", e);
134       }
135       finally
136       {
137          if (lInput != null)
138          {
139             try
140             {
141                lInput.close();
142             }
143             catch (Exception JavaDoc e)
144             {
145             }
146          }
147          if (lOutput != null)
148          {
149             try
150             {
151                lOutput.close();
152             }
153             catch (Exception JavaDoc e)
154             {
155             }
156          }
157       }
158       return lDD;
159    }
160    
161    // Constructors --------------------------------------------------
162

163    /**
164     * Constructor taking the Name of this Object
165     *
166     * @param pName Name to be set which must not be null
167     * @param pDeploymentDescriptor
168     * @throws InvalidParameterException If the given Name is null
169     */

170    public J2EEDeployedObject(String JavaDoc pType,
171                              String JavaDoc pName,
172                              ObjectName JavaDoc pParent,
173                              String JavaDoc pDeploymentDescriptor)
174            throws
175            MalformedObjectNameException JavaDoc,
176            InvalidParentException
177    {
178       super(pType, pName, pParent);
179       mDeploymentDescriptor = pDeploymentDescriptor;
180    }
181
182    // Public --------------------------------------------------------
183

184    // javax.management.j2ee.J2EEDeployedObject implementation -------
185

186    /**
187     * @jmx:managed-attribute
188     */

189    public String JavaDoc getdeploymentDescriptor()
190    {
191       return mDeploymentDescriptor;
192    }
193
194    /**
195     * @jmx:managed-attribute
196     */

197    public String JavaDoc getserver()
198    {
199       //[TODO] Need to be implemented
200
return "unknown server name";
201    }
202
203    // java.lang.Object overrides ------------------------------------
204

205    public String JavaDoc toString()
206    {
207       return "J2EEDeployedObject { " + super.toString() + " } [ " +
208               "deployment descriptor: " + mDeploymentDescriptor +
209               " ]";
210    }
211
212    // Package protected ---------------------------------------------
213

214    // Protected -----------------------------------------------------
215

216    // Private -------------------------------------------------------
217

218    // Inner classes -------------------------------------------------
219

220 }
221
Popular Tags