KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > system > metadata > test > MetaDataTestDelegate


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.test.system.metadata.test;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30
31 import org.jboss.system.metadata.ServiceMetaData;
32 import org.jboss.system.metadata.ServiceMetaDataParser;
33 import org.jboss.test.AbstractTestDelegate;
34 import org.jboss.util.xml.JBossEntityResolver;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38
39 /**
40  * MetaDataTestDelegate.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @version $Revision: 1.1 $
44  */

45 public class MetaDataTestDelegate extends AbstractTestDelegate
46 {
47    /** The parser */
48    private DocumentBuilder JavaDoc parser;
49    
50    public MetaDataTestDelegate(Class JavaDoc clazz)
51    {
52       super(clazz);
53    }
54
55    public void setUp() throws Exception JavaDoc
56    {
57       super.setUp();
58       DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
59       parser = factory.newDocumentBuilder();
60    }
61
62    /**
63     * Unmarshal an object
64     *
65     * @param url the url
66     * @return the list of services
67     * @throws Exception for any error
68     */

69    public List JavaDoc<ServiceMetaData> unmarshal(URL JavaDoc url) throws Exception JavaDoc
70    {
71       long start = System.currentTimeMillis();
72
73       Element JavaDoc element = null;
74
75       try
76       {
77          InputStream JavaDoc stream = url.openStream();
78          try
79          {
80             InputSource JavaDoc is = new InputSource JavaDoc(stream);
81             is.setSystemId(url.toString());
82             parser.setEntityResolver(new JBossEntityResolver());
83             log.debug("Initialized parsing in " + (System.currentTimeMillis() - start) + "ms");
84
85             Document JavaDoc document = parser.parse(is);
86             element = document.getDocumentElement();
87          }
88          finally
89          {
90             stream.close();
91          }
92
93          ServiceMetaDataParser parser = new ServiceMetaDataParser(element);
94          List JavaDoc<ServiceMetaData> result = parser.parse();
95          log.debug("Total parse for " + url + " took " + (System.currentTimeMillis() - start) + "ms");
96          return result;
97       }
98       catch (Exception JavaDoc e)
99       {
100          log.debug("Error during parsing: " + url + ": " + e);
101          throw e;
102       }
103    }
104 }
105
Popular Tags