KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > urlhandler > resource > Handler


1 /**
2 * Redistribution and use of this software and associated
3 * documentation ("Software"), with or without modification, are
4 * permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright statements
7 * and notices. Redistributions must also contain a copy of this
8 * document.
9 *
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio Inc. For written permission, please
18 * contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio Inc. Exolab is a registered trademark of
23 * Intalio Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO OR
32 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * Copyright 2000 (C) Intalio Inc. All Rights Reserved.
42 *
43 * $Id: Handler.java 1096 2004-03-26 21:41:16Z dblevins $
44 *
45 * Date Author Changes
46 * 22/Nov/2000 Chris Wood Created
47 *
48 */

49
50 package org.openejb.util.urlhandler.resource;
51
52 import java.net.URL JavaDoc;
53 import java.net.URLConnection JavaDoc;
54
55 /**
56  *
57  * @author Chris Wood <a HREF="mailto:wood@intalio.com">&lt;wood@intalio.com&gt;</a>
58  * @version $Revision: 1096 $ $Date: 2004-03-26 13:41:16 -0800 (Fri, 26 Mar 2004) $
59  */

60 public class Handler extends java.net.URLStreamHandler JavaDoc {
61     
62     protected URLConnection JavaDoc openConnection( URL JavaDoc url ) throws java.io.IOException JavaDoc {
63         String JavaDoc cln = url.getHost();
64
65         String JavaDoc resrce = url.getFile().substring( 1 );
66
67         URL JavaDoc realURL;
68
69         if ( cln != null && cln.length() != 0 ) {
70             Class JavaDoc clz;
71             ClassLoader JavaDoc cl = getContextClassLoader();
72
73             try {
74                 //clz = Class.forName( cln );
75
clz = Class.forName( cln, true, cl );
76             } catch ( ClassNotFoundException JavaDoc ex ) {
77                 throw new java.net.MalformedURLException JavaDoc( "Class " + cln + " cannot be found (" + ex + ")" );
78             }
79
80             realURL = cl.getResource( resrce );
81
82             if ( realURL == null )
83                 throw new java.io.FileNotFoundException JavaDoc( "Class resource " + resrce + " of class " + cln + " cannot be found" );
84         } else {
85             ClassLoader JavaDoc cl = getContextClassLoader();
86             realURL = cl.getResource( resrce );
87
88             if ( realURL == null )
89                 throw new java.io.FileNotFoundException JavaDoc( "System resource " + resrce + " cannot be found" );
90         }
91
92         return realURL.openConnection();
93     }
94
95     public static ClassLoader JavaDoc getContextClassLoader() {
96         return (ClassLoader JavaDoc) java.security.AccessController.doPrivileged(
97             new java.security.PrivilegedAction JavaDoc() {
98                 public Object JavaDoc run() {
99                     return Thread.currentThread().getContextClassLoader();
100                 }
101             }
102         );
103     }
104
105 }
106
Popular Tags