KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > OSGiURLPolicy


1 /*
2  * Oscar - An implementation of the OSGi framework.
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.oscar;
37
38 import java.net.MalformedURLException JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.security.AccessController JavaDoc;
41 import java.security.PrivilegedExceptionAction JavaDoc;
42
43 import org.ungoverned.moduleloader.Module;
44 import org.ungoverned.moduleloader.ModuleManager;
45 import org.ungoverned.moduleloader.URLPolicy;
46 import org.ungoverned.oscar.util.OscarConstants;
47
48 public class OSGiURLPolicy implements URLPolicy
49 {
50     private Oscar m_oscar = null;
51     private BundleURLStreamHandler m_handler = null;
52     private FakeURLStreamHandler m_fakeHandler = null;
53
54     public OSGiURLPolicy(Oscar oscar)
55     {
56         m_oscar = oscar;
57     }
58
59     public URL JavaDoc createCodeSourceURL(ModuleManager mgr, Module module)
60     {
61         URL JavaDoc url = null;
62         BundleImpl bundle = null;
63         try
64         {
65             bundle = (BundleImpl)
66                 m_oscar.getBundle(BundleInfo.getBundleIdFromModuleId(module.getId()));
67             if (bundle != null)
68             {
69                 url = new URL JavaDoc(bundle.getInfo().getLocation());
70             }
71         }
72         catch (NumberFormatException JavaDoc ex)
73         {
74             url = null;
75         }
76         catch (MalformedURLException JavaDoc ex)
77         {
78             if (m_fakeHandler == null)
79             {
80                 m_fakeHandler = new FakeURLStreamHandler();
81             }
82             try
83             {
84                 url = new URL JavaDoc(null,
85                     OscarConstants.FAKE_URL_PROTOCOL_VALUE
86                     + "//" + bundle.getLocation(), m_fakeHandler);
87             }
88             catch (Exception JavaDoc ex2)
89             {
90                 url = null;
91             }
92         }
93         return url;
94     }
95
96     public URL JavaDoc createResourceURL(ModuleManager mgr, Module module, int rsIdx, String JavaDoc name)
97     {
98         if (m_handler == null)
99         {
100             m_handler = new BundleURLStreamHandler(mgr);
101         }
102
103         // Add a slash if there is one already, otherwise
104
// the is no slash separating the host from the file
105
// in the resulting URL.
106
if (!name.startsWith("/"))
107         {
108             name = "/" + name;
109         }
110
111         try
112         {
113             if (System.getSecurityManager() != null)
114             {
115                 return (URL JavaDoc) AccessController.doPrivileged(
116                     new CreateURLPrivileged(module.getId(), rsIdx, name));
117             }
118             else
119             {
120                 return new URL JavaDoc(OscarConstants.BUNDLE_URL_PROTOCOL,
121                     module.getId(), -1, "/" + rsIdx + name, m_handler);
122             }
123         }
124         catch (Exception JavaDoc ex)
125         {
126             System.err.println("OSGiURLPolicy: " + ex);
127             return null;
128         }
129     }
130
131     /**
132      * This simple class is used to perform the privileged action of
133      * creating a URL using the "bundle:" protocol stream handler.
134     **/

135     private class CreateURLPrivileged implements PrivilegedExceptionAction JavaDoc
136     {
137         private String JavaDoc m_id = null;
138         private int m_rsIdx = 0;
139         private String JavaDoc m_name = null;
140
141         public CreateURLPrivileged(String JavaDoc id, int rsIdx, String JavaDoc name)
142         {
143             m_id = id;
144             m_rsIdx = rsIdx;
145             m_name = name;
146         }
147
148         public Object JavaDoc run() throws Exception JavaDoc
149         {
150             return new URL JavaDoc("bundle", m_id, -1, "/" + m_rsIdx + m_name, m_handler);
151         }
152     }
153 }
Popular Tags