KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > corba > util > urlhandler > resource > Handler


1
2
3 /*
4 * Copyright (C) The Community OpenORB Project. All rights reserved.
5 *
6 * This software is published under the terms of The OpenORB Community Software
7 * License version 1.0, a copy of which has been included with this distribution
8 * in the LICENSE.txt file.
9 */

10 package org.objectweb.openccm.corba.util.urlhandler.resource;
11
12 import java.io.FileNotFoundException JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLConnection JavaDoc;
18 import java.net.URLStreamHandler JavaDoc;
19
20 /**
21  * This class allows to find resources in jar files using the OpenCCM
22  * launcher. This class is an extension of the URLStreamHandler
23  * class. It overrides the method openConnection and adds a level of
24  * indirection to the way resources are loaded from a URL.
25  *
26  */

27 public class Handler
28     extends URLStreamHandler JavaDoc
29 {
30     /**
31      * Opens a connection to the specified URL.
32      *
33      * @param url A URL to open a connection to.
34      * @return The established connection.
35      * @throws IOException When the file specified by the
36      * URL could not be found.
37      */

38     protected URLConnection JavaDoc openConnection( final URL JavaDoc url )
39         throws IOException JavaDoc
40     {
41         String JavaDoc cln = url.getHost();
42         String JavaDoc resrce = url.getFile().substring( 1 );
43
44         URL JavaDoc realURL;
45
46         if ( cln != null && cln.length() != 0 )
47         {
48             Class JavaDoc clz;
49
50             try
51             {
52                 clz = Thread.currentThread().getContextClassLoader().
53                         loadClass( cln );
54             }
55             catch ( final ClassNotFoundException JavaDoc ex )
56             {
57                 throw new IOException JavaDoc("Class " + cln + " cannot be found (" + ex + ")");
58             }
59
60             realURL = clz.getResource( resrce );
61
62             if ( realURL == null )
63                 throw new FileNotFoundException JavaDoc(
64                       "Class resource " + resrce + " of class "
65                       + cln + " cannot be found" );
66
67         }
68         else
69         {
70             // works with the openccm launcher
71
realURL = Thread.currentThread().getContextClassLoader().getResource( resrce );
72
73             // Trying best effort
74
if (realURL == null)
75                 realURL = ClassLoader.getSystemResource( resrce );
76
77             if ( realURL == null )
78                 throw new FileNotFoundException JavaDoc( "System resource "
79                       + resrce + " cannot be found" );
80         }
81         return realURL.openConnection();
82     }
83 }
84
85
Popular Tags