001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.classloader.resource;
023:
024: import java.io.BufferedReader;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.net.URL;
028: import java.util.Enumeration;
029:
030: import org.jboss.system.ServiceMBeanSupport;
031:
032: /** A simple service to test resource loading.
033:
034: @author Adrian.Brock@HappeningTimes.com
035: @author Scott.Stark@jboss.org
036: @version $Revision: 59680 $
037: */
038: public class ResourceTest extends ServiceMBeanSupport implements
039: ResourceTestMBean, Runnable {
040: private Exception threadEx;
041: private boolean running;
042: private String dtdName;
043:
044: public String getDtdName() {
045: return dtdName;
046: }
047:
048: public void setDtdName(String dtdName) {
049: this .dtdName = dtdName;
050: }
051:
052: protected void startService() throws Exception {
053: // Run a thread in the background looking for rsrc using a different loader
054: Thread t = new Thread(this , "RsrcLoader");
055: synchronized (ResourceTest.class) {
056: t.start();
057: ResourceTest.class.wait();
058: }
059:
060: loadLocalResource();
061: loadGlobalResource();
062: findResources();
063: running = false;
064: t.join();
065: if (threadEx != null)
066: throw threadEx;
067: }
068:
069: protected void stopService() throws Exception {
070: running = false;
071: }
072:
073: /**
074: * Checks we can find a local resource in our deployment unit
075: */
076: public void loadLocalResource() throws Exception {
077: log.info("Looking for resource: META-INF/jboss-service.xml");
078: ClassLoader cl = getClass().getClassLoader();
079: URL serviceXML = cl.getResource("META-INF/jboss-service.xml");
080: if (serviceXML == null)
081: throw new Exception(
082: "Cannot find META-INF/jboss-service.xml");
083: log.info("Found META-INF/jboss-service.xml: " + serviceXML);
084: InputStream is = serviceXML.openStream();
085: BufferedReader reader = new BufferedReader(
086: new InputStreamReader(is));
087: String line = reader.readLine();
088: boolean foundService = false;
089: while (line != null && foundService == false) {
090: if (line
091: .indexOf("org.jboss.test.classloader.resource.ResourceTest") != -1)
092: foundService = true;
093: line = reader.readLine();
094: }
095: is.close();
096: if (foundService == false)
097: throw new Exception("Wrong META-INF/jboss-service.xml");
098:
099: // Look for the dtds/sample.dtd
100: log.info("Looking for resource: " + dtdName);
101: URL dtd = cl.getResource(dtdName);
102: if (dtd == null)
103: throw new Exception("Failed to find " + dtdName);
104: log.info("Found " + dtdName + ": " + dtd);
105: }
106:
107: /**
108: * Checks we can find a global resource located in the conf dir
109: */
110: public void loadGlobalResource() throws Exception {
111: ClassLoader loader = getClass().getClassLoader();
112: log.info("loadGlobalResource, loader=" + loader);
113: URL resURL = loader.getResource("standardjboss.xml");
114: if (resURL == null)
115: throw new Exception("Cannot find standardjboss.xml");
116: resURL = loader.getResource("jboss-log4j.xml");
117: if (resURL == null)
118: throw new Exception("Cannot find jboss-log4j.xml");
119: resURL = loader.getResource("jndi.properties");
120: if (resURL == null)
121: throw new Exception("Cannot find jndi.properties");
122: }
123:
124: /** Check that the URLClassLoader.getResources locates the resource
125: * across the repository class loader.
126: */
127: public void findResources() throws Exception {
128: ClassLoader loader = getClass().getClassLoader();
129: log.info("findResources, loader=" + loader);
130: Enumeration resURLs = loader
131: .getResources("META-INF/MANIFEST.MF");
132: if (resURLs == null || resURLs.hasMoreElements() == false)
133: throw new Exception("Cannot find META-INF/MANIFEST.MF");
134: int count = 0;
135: log.debug("Begin META-INF/MANIFESTs");
136: while (resURLs.hasMoreElements()) {
137: URL url = (URL) resURLs.nextElement();
138: count++;
139: log.debug(url);
140: }
141: log.debug("End META-INF/MANIFESTs, count=" + count);
142: if (count <= 0)
143: throw new Exception(
144: "Did not find multiple META-INF/MANIFEST.MFs");
145: }
146:
147: /** Load resources in the background to test MT access to the repository
148: * during resource lookup
149: */
150: public void run() {
151: ClassLoader loader = getClass().getClassLoader();
152: do {
153: synchronized (ResourceTest.class) {
154: ResourceTest.class.notify();
155: log.info("Notified start thread");
156: }
157: // Load some resouces located from the JavaMail mail.jar
158: try {
159: javax.mail.Session.getInstance(System.getProperties());
160:
161: Class sessionClass = loader
162: .loadClass("javax.mail.Session");
163: log.info("Loading JavaMail resources using: "
164: + sessionClass.getClassLoader());
165: URL resURL = sessionClass
166: .getResource("/META-INF/javamail.default.address.map");
167: if (resURL == null)
168: throw new Exception(
169: "Failed to find javamail.default.address.map");
170: resURL = sessionClass
171: .getResource("/META-INF/javamail.default.providers");
172: if (resURL == null)
173: throw new Exception(
174: "Failed to find javamail.default.providers");
175: resURL = sessionClass
176: .getResource("/META-INF/javamail.charset.map");
177: if (resURL == null)
178: throw new Exception(
179: "Failed to find javamail.charset.map");
180: resURL = sessionClass.getResource("/META-INF/mailcap");
181: if (resURL == null)
182: throw new Exception("Failed to find mailcap");
183: log.info("Found all JavaMail resources");
184: // Look for a resource that does not exist
185: resURL = sessionClass
186: .getResource("nowhere-to-be-found.xml");
187: } catch (Exception e) {
188: threadEx = e;
189: log.error("Failed to load resource", e);
190: break;
191: }
192: } while (running);
193: }
194: }
|