1 22 package org.jboss.test.jmx.loading; 23 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import javax.xml.parsers.SAXParser ; 27 import javax.xml.parsers.SAXParserFactory ; 28 29 import org.jboss.logging.Logger; 30 import org.jboss.system.ServiceMBeanSupport; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.helpers.DefaultHandler ; 34 35 40 public class ResourceTsts extends ServiceMBeanSupport implements ResourceTstsMBean 41 { 42 private String namespace = null; 43 44 public ResourceTsts() 45 { 46 log.debug("ResourceTsts.ctor call stack", new Throwable ("CallStack")); 47 } 48 49 public String getName() 50 { 51 return "ResourceTst"; 52 } 53 54 public void setNamespace(String namespace) 55 { 56 this.namespace = namespace; 57 } 58 59 protected void startService() throws Exception 60 { 61 String serviceName = super.getServiceName().toString(); 62 log.debug("startService("+serviceName+")"); 63 log.debug("startService call stack", new Throwable ("CallStack")); 64 ClassLoader serviceLoader = getClass().getClassLoader(); 65 ClassLoader 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 URL url1 = tcl.getResource("META-INF/config.xml"); 72 log.debug("META-INF/config.xml via TCL: "+url1); 73 URL url2 = tcl.getResource("/META-INF/config.xml"); 74 log.debug("/META-INF/config.xml via TCL: "+url2); 75 URL url3 = tcl.getResource("file:/META-INF/config.xml"); 76 log.debug("file:/META-INF/config.xml via TCL: "+url3); 77 URL url4 = tcl.getResource("META-INF/config.xml"); 78 log.debug("META-INF/config.xml via serviceLoader: "+url4); 79 80 if( url1 == null ) 82 throw new IllegalStateException ("No META-INF/config.xml available via TCL"); 83 InputStream is = url1.openStream(); 84 SAXParserFactory factory = SAXParserFactory.newInstance(); 85 SAXParser parser = factory.newSAXParser(); 86 ConfigHandler handler = new ConfigHandler(namespace); 87 parser.parse(is, handler); 88 log.debug("Successfully parsed url1"); 89 is.close(); 90 String optionValue = handler.value.toString(); 92 if( optionValue.equals(serviceName) ) 93 throw new IllegalStateException (optionValue+" != "+serviceName); 94 log.debug("Config.option1 matches service name"); 95 } 96 97 static class ConfigHandler extends DefaultHandler 98 { 99 static Logger log = Logger.getLogger(ConfigHandler.class); 100 boolean optionTag; 101 StringBuffer value = new StringBuffer (); 102 String namespace; 103 104 ConfigHandler(String namespace) 105 { 106 this.namespace = namespace; 107 } 108 public void startElement(String uri, String localName, String qName, Attributes attributes) 109 throws SAXException 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 119 { 120 if( optionTag ) 121 value.append(str, start, length); 122 } 123 public void endElement(String uri, String localName, String qName) 124 throws SAXException 125 { 126 log.debug("endElement, uri="+uri+"localName="+localName+", qName="+qName); 127 } 128 } 129 } 130 | Popular Tags |