1 /***************************************
2 * *
3 * JBoss: The OpenSource J2EE WebOS *
4 * *
5 * Distributable under LGPL license. *
6 * See terms of license at gnu.org. *
7 * *
8 ***************************************/
9
10 package org.jboss.net.protocol.resource;
11
12 import java.io.IOException;
13 import java.io.FileNotFoundException;
14
15 import java.net.URL;
16 import java.net.MalformedURLException;
17
18 import org.jboss.net.protocol.DelegatingURLConnection;
19
20 import org.jboss.logging.Logger;
21
22 /**
23 * Provides access to system resources as a URLConnection.
24 *
25 * @version <tt>$Revision: 1.3.2.1 $</tt>
26 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
27 * @author Scott.Stark@jboss.org
28 */
29 public class ResourceURLConnection
30 extends DelegatingURLConnection
31 {
32 private static final Logger log = Logger.getLogger(ResourceURLConnection.class);
33
34 public ResourceURLConnection(final URL url)
35 throws MalformedURLException, IOException
36 {
37 super(url);
38 }
39
40 protected URL makeDelegateUrl(final URL url)
41 throws MalformedURLException, IOException
42 {
43 String name = url.getHost();
44 String file = url.getFile();
45 if (file != null && !file.equals(""))
46 {
47 name += file;
48 }
49
50 // first try TCL and then SCL
51
52 ClassLoader cl = Thread.currentThread().getContextClassLoader();
53 URL target = cl.getResource(name);
54
55 if (target == null)
56 {
57 cl = ClassLoader.getSystemClassLoader();
58 target = cl.getResource(name);
59 }
60
61 if (target == null)
62 throw new FileNotFoundException("Could not locate resource: " + name);
63
64 if (log.isTraceEnabled())
65 {
66 log.trace("Target resource URL: " + target);
67 try
68 {
69 log.trace("Target resource URL connection: " + target.openConnection());
70 }
71 catch (Exception ignore)
72 {
73 }
74 }
75
76 // Return a new URL as the cl version does not use the JB stream factory
77 return new URL(target.toExternalForm());
78 }
79 }