KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > url > resource > ResourceURLConnection


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.system.url.resource;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import org.apache.geronimo.system.url.DelegatingURLConnection;
26
27 /**
28  * Provides access to system resources as a URLConnection.
29  *
30  * @version $Rev: 157019 $ $Date: 2005-03-10 17:01:40 -0800 (Thu, 10 Mar 2005) $
31  */

32 public class ResourceURLConnection extends DelegatingURLConnection {
33     public ResourceURLConnection(final URL JavaDoc url) throws MalformedURLException JavaDoc, IOException JavaDoc {
34         super(url);
35     }
36
37     protected URL JavaDoc makeDelegateUrl(final URL JavaDoc url) throws MalformedURLException JavaDoc, IOException JavaDoc {
38         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
39         String JavaDoc name = url.getPath();
40         URL JavaDoc target = null;
41
42         // If there is a ref, load from the ref class
43
String JavaDoc ref = url.getRef();
44         if (ref != null) {
45             try {
46                 Class JavaDoc type = classLoader.loadClass(ref);
47                 target = type.getResource(name);
48             } catch (ClassNotFoundException JavaDoc ignore) {
49                 // ignore... somewhat expected, and we are handling this below
50
}
51         }
52         // initial / doesn't work with classLoader.getResource, so if it's present remove it.
53
// this is a hack but somehow openorb is rewriting our url so the path starts with a /
54
if (name.startsWith("/")) {
55             name = name.substring(1);
56         }
57         if (target == null) {
58             // Then try TCL and then SCL
59
target = classLoader.getResource(name);
60
61             if (target == null) {
62                 target = ClassLoader.getSystemClassLoader().getResource(name);
63             }
64         }
65
66         if (target == null) {
67             throw new FileNotFoundException JavaDoc("Could not locate resource: " + name);
68         }
69
70         return target;
71     }
72 }
73
Popular Tags