KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > metadata > XMLMetaData


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.mx.metadata;
23
24 // $Id: XMLMetaData.java 38392 2005-11-22 23:03:42Z dimitris $
25

26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.management.MBeanInfo JavaDoc;
31 import javax.management.NotCompliantMBeanException JavaDoc;
32
33 import org.dom4j.Document;
34 import org.dom4j.DocumentException;
35 import org.dom4j.DocumentType;
36 import org.dom4j.Element;
37 import org.dom4j.io.SAXReader;
38 import org.jboss.mx.modelmbean.XMBeanConstants;
39 import org.jboss.mx.service.ServiceConstants;
40 import org.jboss.mx.util.JBossNotCompliantMBeanException;
41 import org.jboss.util.xml.JBossEntityResolver;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * Aggregate builder for XML schemas. This builder implementation is used
46  * as an aggregate for all XML based builder implementations. The correct
47  * XML parser is picked based on the schema declaration of the XML file.
48  *
49  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
50  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
51  * @author Matt Munz
52  */

53 public class XMLMetaData
54    extends AbstractBuilder
55    implements ServiceConstants, XMBeanConstants
56 {
57
58    // Attributes ----------------------------------------------------
59
private static final int NO_VERSION = -1;
60    private static final int JBOSS_XMBEAN_1_0 = 0;
61    private static final int JBOSS_XMBEAN_1_1 = 1;
62    private static final int JBOSS_XMBEAN_1_2 = 2;
63
64    /**
65     * The URL for the XML file.
66     */

67    private URL JavaDoc url = null;
68
69    private Element element;
70
71    private String JavaDoc versionString;
72
73    /**
74     * The class name of the resource class this Model MBean represents.
75     */

76    private String JavaDoc resourceClassName = null;
77
78    /**
79     * The class name of the Model MBean implementation class.
80     */

81    private String JavaDoc mmbClassName = null;
82
83
84    // Constructors --------------------------------------------------
85

86    /**
87     * Constructs an aggregate XML builder implementation.
88     *
89     * @param mmbClassName the class name of the Model MBean
90     * implementation
91     * @param resourceClassName the class name of the resource object the
92     * Model MBean represents
93     * @param url the URL for the XML definition of the
94     * management interface
95     */

96    public XMLMetaData(String JavaDoc mmbClassName, String JavaDoc resourceClassName, URL JavaDoc url)
97    {
98       super();
99
100       this.url = url;
101       this.mmbClassName = mmbClassName;
102       this.resourceClassName = resourceClassName;
103    }
104
105    /**
106     * Constructs an aggregate XML builder implementation.
107     *
108     * @param mmbClassName the class name of the Model MBean
109     * implementation
110     * @param resourceClassName the class name of the resource object the
111     * Model MBean represents
112     * @param url the URL for the XML definition of the
113     * management interface
114     *
115     * @throws MalformedURLException if the URL string could not be resolved
116     */

117    public XMLMetaData(String JavaDoc mmbClassName, String JavaDoc resourceClassName, String JavaDoc url)
118       throws MalformedURLException JavaDoc
119    {
120       this(mmbClassName, resourceClassName, new URL JavaDoc(url));
121    }
122
123    /**
124     * Constructs an aggregate XML builder implementation.
125     *
126     * @param mmbClassName the class name of the Model MBean
127     * implementation
128     * @param resourceClassName the class name of the resource object the
129     * Model MBean represents
130     * @param url the URL for the XML definition of the
131     * management interface
132     * @param properties Map of configuration properties for this
133     * builder. These properties will be passed
134     * to the appropriate XML schema specific builder
135     * when it is created.
136     */

137    public XMLMetaData(String JavaDoc mmbClassName, String JavaDoc resourceClassName, URL JavaDoc url, Map JavaDoc properties)
138    {
139       this(mmbClassName, resourceClassName, url);
140       setProperties(properties);
141    }
142
143    /**
144     * Constructs an aggregate XML builder implementation.
145     *
146     * @param mmbClassName the class name of the Model MBean
147     * implementation
148     * @param resourceClassName the class name of the resource object the
149     * Model MBean represents
150     * @param url the URL for the XML definition of the
151     * management interface
152     * @param properties Map of configuration properties for this
153     * builder. These properties will be passed
154     * to the appropriate XML schema specific builder
155     * when it is created.
156     *
157     * @throws MalformedURLException if the URL string could not be resolved
158     */

159    public XMLMetaData(String JavaDoc mmbClassName, String JavaDoc resourceClassName,
160                       String JavaDoc url, Map JavaDoc properties) throws MalformedURLException JavaDoc
161    {
162       this(mmbClassName, resourceClassName, new URL JavaDoc(url), properties);
163    }
164
165    /**
166     * Creates a new <code>XMLMetaData</code> instance using an explicit DOM element
167     * as the configuration source, and requiring an explicit version indicator.
168     * The version should be the PublicID for the dtd or (worse) the dtd url.
169     *
170     * @param mmbClassName a <code>String</code> value
171     * @param resourceClassName a <code>String</code> value
172     * @param element an <code>org.w3c.dom.Element</code> value
173     * @param version a <code>String</code> value
174     */

175    public XMLMetaData(String JavaDoc mmbClassName, String JavaDoc resourceClassName, Element element, String JavaDoc version)
176    {
177       super();
178       this.mmbClassName = mmbClassName;
179       this.resourceClassName = resourceClassName;
180       this.element = element;
181       versionString = version;
182    }
183
184
185    // MetaDataBuilder implementation --------------------------------
186
/**
187     * Constructs the Model MBean metadata. This implementation reads the
188     * document type definition from the beginning of the XML file and picks
189     * a corresponding XML builder based on the schema name. In case no
190     * document type is defined the latest schema builder for this JBossMX
191     * release is used. <p>
192     *
193     * The SAX parser implementation is selected by default based on JAXP
194     * configuration. If you want to use JAXP to select the parser, you can
195     * set the system property <tt>"javax.xml.parsers.SAXParserFactory"</tt>.
196     * For example, to use Xerces you might define: <br><pre>
197     *
198     * java -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl ...
199     *
200     * </pre>
201     *
202     * In case you can't or don't want to use JAXP to configure the SAX parser
203     * implementation you can override the SAX parser implementation by setting
204     * an MBean descriptor field {@link XMBeanConstants#SAX_PARSER} to the
205     * parser class string value.
206     *
207     * @return initialized MBean info
208     * @throws NotCompliantMBeanException if there were errors building the
209     * MBean info from the given XML file.
210     */

211    public MBeanInfo JavaDoc build() throws NotCompliantMBeanException JavaDoc
212    {
213       try
214       {
215          int version = NO_VERSION;
216
217          if (versionString == null)
218          {
219             // by default, let JAXP pick the SAX parser
220
SAXReader reader = new SAXReader();
221
222             // check if user wants to override the SAX parser property
223
if (properties.get(SAX_PARSER) != null)
224             {
225                try
226                {
227                   reader.setXMLReaderClassName(getStringProperty(SAX_PARSER));
228                }
229
230                catch (SAXException JavaDoc e)
231                {
232                   //Should log and ignore, I guess
233
} // end of try-catch
234
}
235             // by default we validate
236
reader.setValidation(true);
237
238             // the user can override the validation by setting the VALIDATE property
239
try
240             {
241                boolean validate = getBooleanProperty(XML_VALIDATION);
242                reader.setValidation(validate);
243             }
244             catch (IllegalPropertyException e)
245             {
246                // FIXME: log the exception (warning)
247

248                // fall through, use the default value
249
}
250
251             //supply it with our dtd locally.
252
reader.setEntityResolver(new JBossEntityResolver());
253
254             // get the element and start parsing...
255
Document doc = reader.read(url);
256             element = doc.getRootElement();
257             DocumentType type = doc.getDocType();
258
259             version = validateVersionString(type.getPublicID());
260             if (version == NO_VERSION)
261             {
262                version = validateVersionString(type.getSystemID());
263             } // end of if ()
264

265          }
266          else
267          {
268             version = validateVersionString(versionString);
269          } // end of else
270

271          if (element == null)
272          {
273             throw new IllegalStateException JavaDoc("No element supplied with explict version!");
274          }
275          // These are the known schemas for us. Pick the correct one based on
276
// schema or default to the latest.docURL.endsWith(JBOSSMX_XMBEAN_DTD_1_0)
277
if (version == JBOSS_XMBEAN_1_0 ||
278              version == JBOSS_XMBEAN_1_1 ||
279              version == JBOSS_XMBEAN_1_2)
280          {
281             // jboss_xmbean_1_0.dtd is the only implemented useful xmbean
282
return new JBossXMBean10(mmbClassName, resourceClassName, element, properties).build();
283          }
284          else
285          {
286             throw new NotCompliantMBeanException JavaDoc("Unknown xmbean type " + versionString);
287          } // end of else
288

289       }
290       catch (DocumentException e)
291       {
292          throw new JBossNotCompliantMBeanException("Error parsing the XML file, from XMLMetaData: ", e);
293       }
294    }
295
296    private int validateVersionString(String JavaDoc versionString)
297    {
298       if (PUBLIC_JBOSSMX_XMBEAN_DTD_1_0.equals(versionString))
299       {
300          return JBOSS_XMBEAN_1_0;
301       } // end of if ()
302
if (versionString != null && versionString.endsWith(JBOSSMX_XMBEAN_DTD_1_0))
303       {
304          return JBOSS_XMBEAN_1_0;
305       } // end of if ()
306
if (PUBLIC_JBOSSMX_XMBEAN_DTD_1_1.equals(versionString))
307       {
308          return JBOSS_XMBEAN_1_1;
309       } // end of if ()
310
if (versionString != null && versionString.endsWith(JBOSSMX_XMBEAN_DTD_1_1))
311       {
312          return JBOSS_XMBEAN_1_1;
313       } // end of if ()
314
if (PUBLIC_JBOSSMX_XMBEAN_DTD_1_2.equals(versionString))
315       {
316          return JBOSS_XMBEAN_1_2;
317       } // end of if ()
318
if (versionString != null && versionString.endsWith(JBOSSMX_XMBEAN_DTD_1_2))
319       {
320          return JBOSS_XMBEAN_1_2;
321       } // end of if ()
322

323       //There is nothing defined for jboss xmbean 1.2, so we can't recognize it.
324
return NO_VERSION;
325    }
326
327 }
328
Popular Tags