1 22 package org.jboss.test.classloader.resource; 23 24 import java.io.BufferedReader ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.net.URL ; 28 import java.util.Enumeration ; 29 30 import org.jboss.system.ServiceMBeanSupport; 31 32 38 public class ResourceTest 39 extends ServiceMBeanSupport 40 implements ResourceTestMBean, Runnable 41 { 42 private Exception threadEx; 43 private boolean running; 44 private String dtdName; 45 46 public String getDtdName() 47 { 48 return dtdName; 49 } 50 public void setDtdName(String dtdName) 51 { 52 this.dtdName = dtdName; 53 } 54 55 protected void startService() 56 throws Exception 57 { 58 Thread t = new Thread (this, "RsrcLoader"); 60 synchronized( ResourceTest.class ) 61 { 62 t.start(); 63 ResourceTest.class.wait(); 64 } 65 66 loadLocalResource(); 67 loadGlobalResource(); 68 findResources(); 69 running = false; 70 t.join(); 71 if( threadEx != null ) 72 throw threadEx; 73 } 74 75 protected void stopService() 76 throws Exception 77 { 78 running = false; 79 } 80 81 84 public void loadLocalResource() 85 throws Exception 86 { 87 log.info("Looking for resource: META-INF/jboss-service.xml"); 88 ClassLoader cl = getClass().getClassLoader(); 89 URL serviceXML = cl.getResource("META-INF/jboss-service.xml"); 90 if (serviceXML == null) 91 throw new Exception ("Cannot find META-INF/jboss-service.xml"); 92 log.info("Found META-INF/jboss-service.xml: "+serviceXML); 93 InputStream is = serviceXML.openStream(); 94 BufferedReader reader = new BufferedReader (new InputStreamReader (is)); 95 String line = reader.readLine(); 96 boolean foundService = false; 97 while (line != null && foundService == false ) 98 { 99 if (line.indexOf("org.jboss.test.classloader.resource.ResourceTest") != -1) 100 foundService = true; 101 line = reader.readLine(); 102 } 103 is.close(); 104 if( foundService == false ) 105 throw new Exception ("Wrong META-INF/jboss-service.xml"); 106 107 log.info("Looking for resource: "+dtdName); 109 URL dtd = cl.getResource(dtdName); 110 if( dtd == null ) 111 throw new Exception ("Failed to find "+dtdName); 112 log.info("Found "+dtdName+": "+dtd); 113 } 114 115 118 public void loadGlobalResource() 119 throws Exception 120 { 121 ClassLoader loader = getClass().getClassLoader(); 122 log.info("loadGlobalResource, loader="+loader); 123 URL resURL = loader.getResource("standardjboss.xml"); 124 if (resURL == null) 125 throw new Exception ("Cannot find standardjboss.xml"); 126 resURL = loader.getResource("log4j.xml"); 127 if (resURL == null) 128 throw new Exception ("Cannot find log4j.xml"); 129 resURL = loader.getResource("jndi.properties"); 130 if (resURL == null) 131 throw new Exception ("Cannot find jndi.properties"); 132 } 133 134 137 public void findResources() 138 throws Exception 139 { 140 ClassLoader loader = getClass().getClassLoader(); 141 log.info("findResources, loader="+loader); 142 Enumeration resURLs = loader.getResources("META-INF/MANIFEST.MF"); 143 if ( resURLs == null || resURLs.hasMoreElements() == false ) 144 throw new Exception ("Cannot find META-INF/MANIFEST.MF"); 145 int count = 0; 146 log.debug("Begin META-INF/MANIFESTs"); 147 while( resURLs.hasMoreElements() ) 148 { 149 URL url = (URL ) resURLs.nextElement(); 150 count ++; 151 log.debug(url); 152 } 153 log.debug("End META-INF/MANIFESTs, count="+count); 154 if ( count <= 0 ) 155 throw new Exception ("Did not find multiple META-INF/MANIFEST.MFs"); 156 } 157 158 161 public void run() 162 { 163 ClassLoader loader = getClass().getClassLoader(); 164 do 165 { 166 synchronized( ResourceTest.class ) 167 { 168 ResourceTest.class.notify(); 169 log.info("Notified start thread"); 170 } 171 try 173 { 174 javax.mail.Session.getInstance(System.getProperties()); 175 176 Class sessionClass = loader.loadClass("javax.mail.Session"); 177 log.info("Loading JavaMail resources using: "+sessionClass.getClassLoader()); 178 URL resURL = sessionClass.getResource("/META-INF/javamail.default.address.map"); 179 if( resURL == null ) 180 throw new Exception ("Failed to find javamail.default.address.map"); 181 resURL = sessionClass.getResource("/META-INF/javamail.default.providers"); 182 if( resURL == null ) 183 throw new Exception ("Failed to find javamail.default.providers"); 184 resURL = sessionClass.getResource("/META-INF/javamail.charset.map"); 185 if( resURL == null ) 186 throw new Exception ("Failed to find javamail.charset.map"); 187 resURL = sessionClass.getResource("/META-INF/mailcap"); 188 if( resURL == null ) 189 throw new Exception ("Failed to find mailcap"); 190 log.info("Found all JavaMail resources"); 191 resURL = sessionClass.getResource("nowhere-to-be-found.xml"); 193 } 194 catch(Exception e) 195 { 196 threadEx = e; 197 log.error("Failed to load resource", e); 198 break; 199 } 200 } while( running ); 201 } 202 } 203 | Popular Tags |