KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > resource > ResourceTest


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.classloader.resource;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 import org.jboss.system.ServiceMBeanSupport;
31
32 /** A simple service to test resource loading.
33
34  @author Adrian.Brock@HappeningTimes.com
35  @author Scott.Stark@jboss.org
36  @version $Revision: 37406 $
37  */

38 public class ResourceTest
39    extends ServiceMBeanSupport
40    implements ResourceTestMBean, Runnable JavaDoc
41 {
42    private Exception JavaDoc threadEx;
43    private boolean running;
44    private String JavaDoc dtdName;
45
46    public String JavaDoc getDtdName()
47    {
48       return dtdName;
49    }
50    public void setDtdName(String JavaDoc dtdName)
51    {
52       this.dtdName = dtdName;
53    }
54
55    protected void startService()
56       throws Exception JavaDoc
57    {
58       // Run a thread in the background looking for rsrc using a different loader
59
Thread JavaDoc t = new Thread JavaDoc(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 JavaDoc
77    {
78       running = false;
79    }
80
81    /**
82     * Checks we can find a local resource in our deployment unit
83     */

84    public void loadLocalResource()
85       throws Exception JavaDoc
86    {
87       log.info("Looking for resource: META-INF/jboss-service.xml");
88       ClassLoader JavaDoc cl = getClass().getClassLoader();
89       URL JavaDoc serviceXML = cl.getResource("META-INF/jboss-service.xml");
90       if (serviceXML == null)
91          throw new Exception JavaDoc("Cannot find META-INF/jboss-service.xml");
92       log.info("Found META-INF/jboss-service.xml: "+serviceXML);
93       InputStream JavaDoc is = serviceXML.openStream();
94       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
95       String JavaDoc 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 JavaDoc("Wrong META-INF/jboss-service.xml");
106
107       // Look for the dtds/sample.dtd
108
log.info("Looking for resource: "+dtdName);
109       URL JavaDoc dtd = cl.getResource(dtdName);
110       if( dtd == null )
111          throw new Exception JavaDoc("Failed to find "+dtdName);
112       log.info("Found "+dtdName+": "+dtd);
113    }
114
115    /**
116     * Checks we can find a global resource located in the conf dir
117     */

118    public void loadGlobalResource()
119       throws Exception JavaDoc
120    {
121       ClassLoader JavaDoc loader = getClass().getClassLoader();
122       log.info("loadGlobalResource, loader="+loader);
123       URL JavaDoc resURL = loader.getResource("standardjboss.xml");
124       if (resURL == null)
125          throw new Exception JavaDoc("Cannot find standardjboss.xml");
126       resURL = loader.getResource("log4j.xml");
127       if (resURL == null)
128          throw new Exception JavaDoc("Cannot find log4j.xml");
129       resURL = loader.getResource("jndi.properties");
130       if (resURL == null)
131          throw new Exception JavaDoc("Cannot find jndi.properties");
132    }
133
134    /** Check that the URLClassLoader.getResources locates the resource
135     * across the repository class loader.
136     */

137    public void findResources()
138       throws Exception JavaDoc
139    {
140       ClassLoader JavaDoc loader = getClass().getClassLoader();
141       log.info("findResources, loader="+loader);
142       Enumeration JavaDoc resURLs = loader.getResources("META-INF/MANIFEST.MF");
143       if ( resURLs == null || resURLs.hasMoreElements() == false )
144          throw new Exception JavaDoc("Cannot find META-INF/MANIFEST.MF");
145       int count = 0;
146       log.debug("Begin META-INF/MANIFESTs");
147       while( resURLs.hasMoreElements() )
148       {
149          URL JavaDoc url = (URL JavaDoc) 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 JavaDoc("Did not find multiple META-INF/MANIFEST.MFs");
156    }
157
158    /** Load resources in the background to test MT access to the repository
159     * during resource lookup
160     */

161    public void run()
162    {
163       ClassLoader JavaDoc loader = getClass().getClassLoader();
164       do
165       {
166          synchronized( ResourceTest.class )
167          {
168             ResourceTest.class.notify();
169             log.info("Notified start thread");
170          }
171          // Load some resouces located from the JavaMail mail.jar
172
try
173          {
174             javax.mail.Session.getInstance(System.getProperties());
175
176             Class JavaDoc sessionClass = loader.loadClass("javax.mail.Session");
177             log.info("Loading JavaMail resources using: "+sessionClass.getClassLoader());
178             URL JavaDoc resURL = sessionClass.getResource("/META-INF/javamail.default.address.map");
179             if( resURL == null )
180                throw new Exception JavaDoc("Failed to find javamail.default.address.map");
181             resURL = sessionClass.getResource("/META-INF/javamail.default.providers");
182             if( resURL == null )
183                throw new Exception JavaDoc("Failed to find javamail.default.providers");
184             resURL = sessionClass.getResource("/META-INF/javamail.charset.map");
185             if( resURL == null )
186                throw new Exception JavaDoc("Failed to find javamail.charset.map");
187             resURL = sessionClass.getResource("/META-INF/mailcap");
188             if( resURL == null )
189                throw new Exception JavaDoc("Failed to find mailcap");
190             log.info("Found all JavaMail resources");
191             // Look for a resource that does not exist
192
resURL = sessionClass.getResource("nowhere-to-be-found.xml");
193          }
194          catch(Exception JavaDoc e)
195          {
196             threadEx = e;
197             log.error("Failed to load resource", e);
198             break;
199          }
200       } while( running );
201    }
202 }
203
Popular Tags