01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.util.urlhandler.resource;
17:
18: import java.net.URL;
19: import java.net.URLConnection;
20:
21: public class Handler extends java.net.URLStreamHandler {
22:
23: protected URLConnection openConnection(URL url)
24: throws java.io.IOException {
25: String cln = url.getHost();
26:
27: String resrce = url.getFile().substring(1);
28:
29: URL realURL;
30:
31: if (cln != null && cln.length() != 0) {
32: Class clz;
33: ClassLoader cl = getContextClassLoader();
34:
35: try {
36:
37: clz = Class.forName(cln, true, cl);
38: } catch (ClassNotFoundException ex) {
39: throw (java.io.IOException) new java.net.MalformedURLException(
40: "Class " + cln + " cannot be found (" + ex
41: + ")").initCause(ex);
42: }
43:
44: realURL = cl.getResource(resrce);
45:
46: if (realURL == null)
47: throw new java.io.FileNotFoundException(
48: "Class resource " + resrce + " of class " + cln
49: + " cannot be found");
50: } else {
51: ClassLoader cl = getContextClassLoader();
52: realURL = cl.getResource(resrce);
53:
54: if (realURL == null)
55: throw new java.io.FileNotFoundException(
56: "System resource " + resrce
57: + " cannot be found");
58: }
59:
60: return realURL.openConnection();
61: }
62:
63: public static ClassLoader getContextClassLoader() {
64: return (ClassLoader) java.security.AccessController
65: .doPrivileged(new java.security.PrivilegedAction() {
66: public Object run() {
67: return Thread.currentThread()
68: .getContextClassLoader();
69: }
70: });
71: }
72:
73: }
|