1 36 package org.ungoverned.oscar; 37 38 import java.net.URL ; 39 40 import org.ungoverned.moduleloader.Module; 41 import org.ungoverned.moduleloader.ResourceNotFoundException; 42 import org.ungoverned.moduleloader.search.ImportSearchPolicy; 43 44 53 public class OSGiImportSearchPolicy extends ImportSearchPolicy 54 { 55 61 public static final String DYNAMIC_IMPORTS_ATTR = "dynamic-imports"; 62 63 private Oscar m_oscar = null; 64 65 public OSGiImportSearchPolicy(Oscar oscar) 66 { 67 super(new OSGiCompatibilityPolicy(oscar), new OSGiSelectionPolicy(oscar)); 68 m_oscar = oscar; 69 } 70 71 public Class findClass(Module module, String name) 72 throws ClassNotFoundException  73 { 74 Class clazz = super.findClass(module, name); 75 76 if (clazz == null) 77 { 78 clazz = findClassDynamic(module, name); 79 } 80 81 return clazz; 82 } 83 84 public URL findResource(Module module, String name) 85 throws ResourceNotFoundException 86 { 87 URL url = super.findResource(module, name); 88 89 if (url == null) 90 { 91 url = findResourceDynamic(module, name); 92 } 93 94 return url; 95 } 96 97 protected Class findClassDynamic(Module module, String name) 98 { 99 103 try 104 { 105 BundleImpl bundle = 106 (BundleImpl) m_oscar.getBundle( 107 BundleInfo.getBundleIdFromModuleId(module.getId())); 108 BundleInfo info = bundle.getInfo(); 109 110 int idx = name.lastIndexOf('.'); 113 if (idx < 0) 114 { 115 return null; 117 } 118 String pkgTarget = name.substring(0, idx); 119 120 String [] dynImports = OSGiImportSearchPolicy.getDynamicImports(module); 123 boolean matches = false; 124 for (int i = 0; !matches && (i < dynImports.length); i++) 125 { 126 if (dynImports[i].equals("*")) 128 { 129 matches = true; 130 } 131 else if (dynImports[i].endsWith(".*")) 133 { 134 matches = pkgTarget.regionMatches( 135 0, dynImports[i], 0, dynImports[i].length() - 2); 136 } 137 else 139 { 140 matches = pkgTarget.equals(dynImports[i]); 141 } 142 } 143 144 if (!matches || ImportSearchPolicy.doesImport(module, pkgTarget)) 149 { 150 return null; 151 } 152 153 int[] version = { 0, 0, 0 }; 156 if (m_oscar.addImport(module, pkgTarget, version, false)) 157 { 158 Module resolvingModule = 166 ImportSearchPolicy.getImportResolvingModule(module, pkgTarget); 167 if (resolvingModule != null) 168 { 169 return resolvingModule.getClassLoader().loadClass(name); 171 } 172 } 173 } 174 catch (Exception ex) 175 { 176 Oscar.error("Unable to dynamically import package.", ex); 177 } 178 179 return null; 180 } 181 182 protected URL findResourceDynamic(Module module, String name) 183 { 184 188 try 189 { 190 BundleImpl bundle = 191 (BundleImpl) m_oscar.getBundle( 192 BundleInfo.getBundleIdFromModuleId(module.getId())); 193 BundleInfo info = bundle.getInfo(); 194 195 int idx = name.lastIndexOf('/'); 198 if (idx < 0) 199 { 200 return null; 202 } 203 String pkgTarget = name.substring(0, idx); 204 pkgTarget = pkgTarget.replace('/', '.'); 205 206 String [] dynImports = OSGiImportSearchPolicy.getDynamicImports(module); 209 boolean matches = false; 210 for (int i = 0; !matches && (i < dynImports.length); i++) 211 { 212 if (dynImports[i].equals("*")) 214 { 215 matches = true; 216 } 217 else if (dynImports[i].endsWith(".*")) 219 { 220 matches = pkgTarget.regionMatches( 221 0, dynImports[i], 0, dynImports[i].length() - 2); 222 } 223 else 225 { 226 matches = pkgTarget.equals(dynImports[i]); 227 } 228 } 229 230 if (!matches || ImportSearchPolicy.doesImport(module, pkgTarget)) 235 { 236 return null; 237 } 238 239 int[] version = { 0, 0, 0 }; 242 if (m_oscar.addImport(module, pkgTarget, version, false)) 243 { 244 Module resolvingModule = 252 ImportSearchPolicy.getImportResolvingModule(module, pkgTarget); 253 if (resolvingModule != null) 254 { 255 return resolvingModule.getClassLoader().getResource(name); 257 } 258 } 259 } 260 catch (Exception ex) 261 { 262 Oscar.error("Unable to dynamically import package.", ex); 263 } 264 265 return null; 266 } 267 268 275 public static String [] getDynamicImports(Module module) 276 { 277 Object value = module.getAttribute(OSGiImportSearchPolicy.DYNAMIC_IMPORTS_ATTR); 278 if (value != null) 279 { 280 return (String []) value; 281 } 282 return new String [0]; 283 } 284 }
| Popular Tags
|