KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > moduleloader > DefaultURLPolicy


1 /*
2  * ModuleLoader - A generic, policy-driven class loader.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.moduleloader;
37
38 import java.net.URL JavaDoc;
39
40 /**
41  * <p>
42  * This class implements a simple <tt>URLPolicy</tt> that the <tt>ModuleManager</tt>
43  * uses if the application does not specify one. This implementation always returns
44  * <tt>null</tt> for <tt>CodeSource</tt> <tt>URL</tt>s, which means that security
45  * is simply ignored. For resource <tt>URL</tt>s, it returns an <tt>URL</tt> in the
46  * form of:
47  * </p>
48  * <pre>
49  * module://&lt;module-id&gt;/&lt;resource-path&gt;
50  * </pre>
51  * <p>
52  * In order to properly handle the "<tt>module:</tt>" protocol, this policy
53  * also defines a custom <tt>java.net.URLStreamHandler</tt> that it assigns
54  * to each <tt>URL</tt> as it is created. This custom handler is used to
55  * return a custom <tt>java.net.URLConnection</tt> that will correctly parse
56  * the above <tt>URL</tt> and retrieve the associated resource bytes using
57  * methods from <tt>ModuleManager</tt> and <tt>Module</tt>.
58  * </p>
59  * @see org.ungoverned.moduleloader.ModuleManager
60  * @see org.ungoverned.moduleloader.Module
61  * @see org.ungoverned.moduleloader.URLPolicy
62 **/

63 public class DefaultURLPolicy implements URLPolicy
64 {
65     private ModuleURLStreamHandler m_handler = null;
66
67     /**
68      * <p>
69      * This method is a stub and always returns <tt>null</tt>.
70      * </p>
71      * @param mgr the <tt>ModuleManager</tt> of the module.
72      * @param module the module for which the <tt>URL</tt> is to be created.
73      * @return <tt>null</tt>.
74     **/

75     public URL JavaDoc createCodeSourceURL(ModuleManager mgr, Module module)
76     {
77         return null;
78     }
79
80     /**
81      * <p>
82      * This method returns a <tt>URL</tt> that is suitable
83      * for accessing the bytes of the specified resource.
84      * </p>
85      * @param mgr the <tt>ModuleManager</tt> of the module.
86      * @param module the module for which the resource is being loaded.
87      * @param rsIdx the index of the <tt>ResourceSource</tt> containing the resource.
88      * @param name the name of the resource being loaded.
89      * @return an <tt>URL</tt> for retrieving the resource.
90     **/

91     public URL JavaDoc createResourceURL(ModuleManager mgr, Module module, int rsIdx, String JavaDoc name)
92     {
93         if (m_handler == null)
94         {
95             m_handler = new ModuleURLStreamHandler(mgr);
96         }
97
98         // Add a slash if there is one already, otherwise
99
// the is no slash separating the host from the file
100
// in the resulting URL.
101
if (!name.startsWith("/"))
102         {
103             name = "/" + name;
104         }
105
106         try
107         {
108             return new URL JavaDoc("module", module.getId(), -1, "/" + rsIdx + name, m_handler);
109         }
110         catch (Exception JavaDoc ex)
111         {
112             System.err.println("DefaultResourceURLPolicy: " + ex);
113             return null;
114         }
115     }
116 }
Popular Tags