| 1 36 package org.ungoverned.oscar; 37 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 import java.security.AccessController ; 41 import java.security.PrivilegedExceptionAction ; 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 createCodeSourceURL(ModuleManager mgr, Module module) 60 { 61 URL 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 (bundle.getInfo().getLocation()); 70 } 71 } 72 catch (NumberFormatException ex) 73 { 74 url = null; 75 } 76 catch (MalformedURLException ex) 77 { 78 if (m_fakeHandler == null) 79 { 80 m_fakeHandler = new FakeURLStreamHandler(); 81 } 82 try 83 { 84 url = new URL (null, 85 OscarConstants.FAKE_URL_PROTOCOL_VALUE 86 + "//" + bundle.getLocation(), m_fakeHandler); 87 } 88 catch (Exception ex2) 89 { 90 url = null; 91 } 92 } 93 return url; 94 } 95 96 public URL createResourceURL(ModuleManager mgr, Module module, int rsIdx, String name) 97 { 98 if (m_handler == null) 99 { 100 m_handler = new BundleURLStreamHandler(mgr); 101 } 102 103 if (!name.startsWith("/")) 107 { 108 name = "/" + name; 109 } 110 111 try 112 { 113 if (System.getSecurityManager() != null) 114 { 115 return (URL ) AccessController.doPrivileged( 116 new CreateURLPrivileged(module.getId(), rsIdx, name)); 117 } 118 else 119 { 120 return new URL (OscarConstants.BUNDLE_URL_PROTOCOL, 121 module.getId(), -1, "/" + rsIdx + name, m_handler); 122 } 123 } 124 catch (Exception ex) 125 { 126 System.err.println("OSGiURLPolicy: " + ex); 127 return null; 128 } 129 } 130 131 135 private class CreateURLPrivileged implements PrivilegedExceptionAction  136 { 137 private String m_id = null; 138 private int m_rsIdx = 0; 139 private String m_name = null; 140 141 public CreateURLPrivileged(String id, int rsIdx, String name) 142 { 143 m_id = id; 144 m_rsIdx = rsIdx; 145 m_name = name; 146 } 147 148 public Object run() throws Exception  149 { 150 return new URL ("bundle", m_id, -1, "/" + m_rsIdx + m_name, m_handler); 151 } 152 } 153 } | Popular Tags |