KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > loading > ResourceTsts


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.test.jmx.loading;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import javax.xml.parsers.SAXParser JavaDoc;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.system.ServiceMBeanSupport;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34
35 /** This service access xml files as config resources from via the TCL
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 58115 $
39  */

40 public class ResourceTsts extends ServiceMBeanSupport implements ResourceTstsMBean
41 {
42    private String JavaDoc namespace = null;
43
44    public ResourceTsts()
45    {
46       log.debug("ResourceTsts.ctor call stack", new Throwable JavaDoc("CallStack"));
47    }
48
49    public String JavaDoc getName()
50    {
51       return "ResourceTst";
52    }
53
54    public void setNamespace(String JavaDoc namespace)
55    {
56       this.namespace = namespace;
57    }
58
59    protected void startService() throws Exception JavaDoc
60    {
61       String JavaDoc serviceName = super.getServiceName().toString();
62       log.debug("startService("+serviceName+")");
63       log.debug("startService call stack", new Throwable JavaDoc("CallStack"));
64       ClassLoader JavaDoc serviceLoader = getClass().getClassLoader();
65       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
66       log.debug("ResourceTsts.CodeSource:"+getClass().getProtectionDomain().getCodeSource());
67       log.debug("ResourceTsts.ClassLoader:"+serviceLoader);
68       log.debug("ResourceTsts.startService() TCL:"+tcl);
69
70       // Try some other resource names against the TCL
71
URL JavaDoc url1 = tcl.getResource("META-INF/config.xml");
72       log.debug("META-INF/config.xml via TCL: "+url1);
73       URL JavaDoc url2 = tcl.getResource("/META-INF/config.xml");
74       log.debug("/META-INF/config.xml via TCL: "+url2);
75       URL JavaDoc url3 = tcl.getResource("file:/META-INF/config.xml");
76       log.debug("file:/META-INF/config.xml via TCL: "+url3);
77       URL JavaDoc url4 = tcl.getResource("META-INF/config.xml");
78       log.debug("META-INF/config.xml via serviceLoader: "+url4);
79
80       // Try loading via the TCL resource
81
if( url1 == null )
82          throw new IllegalStateException JavaDoc("No META-INF/config.xml available via TCL");
83       InputStream JavaDoc is = url1.openStream();
84       SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
85       SAXParser JavaDoc parser = factory.newSAXParser();
86       ConfigHandler handler = new ConfigHandler(namespace);
87       parser.parse(is, handler);
88       log.debug("Successfully parsed url1");
89       is.close();
90       // Validate that the option matches our service name
91
String JavaDoc optionValue = handler.value.toString();
92       if( optionValue.equals(serviceName) )
93          throw new IllegalStateException JavaDoc(optionValue+" != "+serviceName);
94       log.debug("Config.option1 matches service name");
95    }
96
97    static class ConfigHandler extends DefaultHandler JavaDoc
98    {
99       static Logger log = Logger.getLogger(ConfigHandler.class);
100       boolean optionTag;
101       StringBuffer JavaDoc value = new StringBuffer JavaDoc();
102       String JavaDoc namespace;
103
104       ConfigHandler(String JavaDoc namespace)
105       {
106          this.namespace = namespace;
107       }
108       public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes)
109          throws SAXException JavaDoc
110       {
111          log.debug("startElement, uri="+uri+"localName="+localName+", qName="+qName);
112          if( namespace == null )
113             optionTag = qName.equals("option1");
114          else
115             optionTag = qName.equals(namespace+"option1");
116       }
117       public void characters(char[] str, int start, int length)
118          throws SAXException JavaDoc
119       {
120          if( optionTag )
121             value.append(str, start, length);
122       }
123       public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
124          throws SAXException JavaDoc
125       {
126          log.debug("endElement, uri="+uri+"localName="+localName+", qName="+qName);
127       }
128    }
129 }
130
Popular Tags