1 11 12 package org.eclipse.core.runtime.internal.adaptor; 13 14 import java.io.File ; 15 import java.security.ProtectionDomain ; 16 import java.util.ArrayList ; 17 import java.util.jar.Attributes ; 18 import java.util.jar.Manifest ; 19 import org.eclipse.osgi.baseadaptor.*; 20 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry; 21 import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile; 22 import org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook; 23 import org.eclipse.osgi.baseadaptor.loader.*; 24 import org.eclipse.osgi.framework.adaptor.BundleProtectionDomain; 25 import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate; 26 import org.eclipse.osgi.internal.baseadaptor.BaseClassLoadingHook; 27 import org.eclipse.osgi.internal.baseadaptor.BaseStorageHook; 28 29 public class EclipseClassLoadingHook implements ClassLoadingHook, HookConfigurator { 30 private static String [] NL_JAR_VARIANTS = buildNLJarVariants(EclipseEnvironmentInfo.getDefault().getNL()); 31 private static boolean DEFINE_PACKAGES; 32 private static String [] LIB_VARIANTS = buildLibraryVariants(); 33 34 static { 35 try { 36 Class.forName("java.lang.Package"); DEFINE_PACKAGES = true; 38 } catch (ClassNotFoundException e) { 39 DEFINE_PACKAGES = false; 40 } 41 } 42 43 private static String [] buildLibraryVariants() { 44 ArrayList result = new ArrayList (); 45 EclipseEnvironmentInfo info = EclipseEnvironmentInfo.getDefault(); 46 result.add("ws/" + info.getWS() + "/"); result.add("os/" + info.getOS() + "/" + info.getOSArch() + "/"); result.add("os/" + info.getOS() + "/"); String nl = info.getNL(); 50 nl = nl.replace('_', '/'); 51 while (nl.length() > 0) { 52 result.add("nl/" + nl + "/"); int i = nl.lastIndexOf('/'); 54 nl = (i < 0) ? "" : nl.substring(0, i); } 56 result.add(""); return (String []) result.toArray(new String [result.size()]); 58 } 59 60 public byte[] processClass(String name, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) { 61 if (!DEFINE_PACKAGES) 62 return null; 63 int lastIndex = name.lastIndexOf('.'); 65 if (lastIndex < 0) 66 return null; 67 String packageName = name.substring(0, lastIndex); 68 Object pkg = manager.getBaseClassLoader().publicGetPackage(packageName); 69 if (pkg != null) 70 return null; 71 72 String specTitle = null, specVersion = null, specVendor = null, implTitle = null, implVersion = null, implVendor = null; 74 ClasspathManifest cpm = (ClasspathManifest) classpathEntry.getUserObject(ClasspathManifest.KEY); 75 if (cpm == null) { 76 cpm = new ClasspathManifest(); 77 classpathEntry.addUserObject(cpm); 78 } 79 Manifest mf = cpm.getManifest(classpathEntry, manager); 80 if (mf != null) { 81 Attributes mainAttributes = mf.getMainAttributes(); 82 String dirName = packageName.replace('.', '/') + '/'; 83 Attributes packageAttributes = mf.getAttributes(dirName); 84 boolean noEntry = false; 85 if (packageAttributes == null) { 86 noEntry = true; 87 packageAttributes = mainAttributes; 88 } 89 specTitle = packageAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE); 90 if (specTitle == null && !noEntry) 91 specTitle = mainAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE); 92 specVersion = packageAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION); 93 if (specVersion == null && !noEntry) 94 specVersion = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION); 95 specVendor = packageAttributes.getValue(Attributes.Name.SPECIFICATION_VENDOR); 96 if (specVendor == null && !noEntry) 97 specVendor = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VENDOR); 98 implTitle = packageAttributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE); 99 if (implTitle == null && !noEntry) 100 implTitle = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE); 101 implVersion = packageAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION); 102 if (implVersion == null && !noEntry) 103 implVersion = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION); 104 implVendor = packageAttributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR); 105 if (implVendor == null && !noEntry) 106 implVendor = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR); 107 } 108 manager.getBaseClassLoader().publicDefinePackage(packageName, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, null); 111 return null; 113 } 114 115 public boolean addClassPathEntry(ArrayList cpEntries, String cp, ClasspathManager hostmanager, BaseData sourcedata, ProtectionDomain sourcedomain) { 116 String var = hasPrefix(cp); 117 if (var != null) 118 return addInternalClassPath(var, cpEntries, cp, hostmanager, sourcedata, sourcedomain); 120 if (cp.startsWith(BaseStorageHook.EXTERNAL_LIB_PREFIX)) { 121 cp = cp.substring(BaseStorageHook.EXTERNAL_LIB_PREFIX.length()); 122 ClasspathEntry cpEntry = hostmanager.getExternalClassPath(BaseStorageHook.substituteVars(cp), sourcedata, sourcedomain); 124 if (cpEntry != null) { 125 cpEntries.add(cpEntry); 126 return true; 127 } 128 } 129 return false; 130 } 131 132 private boolean addInternalClassPath(String var, ArrayList cpEntries, String cp, ClasspathManager hostloader, BaseData sourcedata, ProtectionDomain sourcedomain) { 133 if (var.equals("ws")) return ClasspathManager.addClassPathEntry(cpEntries, "ws/" + EclipseEnvironmentInfo.getDefault().getWS() + cp.substring(4), hostloader, sourcedata, sourcedomain); if (var.equals("os")) return ClasspathManager.addClassPathEntry(cpEntries, "os/" + EclipseEnvironmentInfo.getDefault().getOS() + cp.substring(4), hostloader, sourcedata, sourcedomain); if (var.equals("nl")) { cp = cp.substring(4); 139 for (int i = 0; i < NL_JAR_VARIANTS.length; i++) 140 if (ClasspathManager.addClassPathEntry(cpEntries, "nl/" + NL_JAR_VARIANTS[i] + cp, hostloader, sourcedata, sourcedomain)) return true; 142 } 143 return false; 144 } 145 146 private static String hasPrefix(String libPath) { 148 if (libPath.startsWith("$ws$")) return "ws"; if (libPath.startsWith("$os$")) return "os"; if (libPath.startsWith("$nl$")) return "nl"; return null; 155 } 156 157 private static String [] buildNLJarVariants(String nl) { 158 ArrayList result = new ArrayList (); 159 nl = nl.replace('_', '/'); 160 while (nl.length() > 0) { 161 result.add("nl/" + nl + "/"); int i = nl.lastIndexOf('/'); 163 nl = (i < 0) ? "" : nl.substring(0, i); } 165 result.add(""); return (String []) result.toArray(new String [result.size()]); 167 } 168 169 public void recordClassDefine(String name, Class clazz, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) { 170 } 172 173 public String findLibrary(BaseData data, String libName) { 174 if (libName.length() == 0) 175 return null; 176 if (libName.charAt(0) == '/' || libName.charAt(0) == '\\') 177 libName = libName.substring(1); 178 String mappedLibName = System.mapLibraryName(libName); 179 String result = searchVariants(data, mappedLibName); 180 if (result != null) 181 return result; 182 String [] mappedLibNames = BaseClassLoadingHook.mapLibraryNames(mappedLibName); 183 for (int i = 0; i < mappedLibNames.length && result == null; i++) 184 result = searchVariants(data, mappedLibNames[i]); 185 return result; 186 } 187 188 private String searchVariants(BaseData bundledata, String path) { 189 for (int i = 0; i < LIB_VARIANTS.length; i++) { 190 BundleFile baseBundleFile = bundledata.getBundleFile(); 191 BundleEntry libEntry = baseBundleFile.getEntry(LIB_VARIANTS[i] + path); 192 if (libEntry != null) { 193 File libFile = baseBundleFile.getFile(LIB_VARIANTS[i] + path, true); 194 if (libFile == null) 195 return null; 196 if (org.eclipse.osgi.service.environment.Constants.OS_HPUX.equals(EclipseEnvironmentInfo.getDefault().getOS())) { 198 try { 199 Runtime.getRuntime().exec(new String [] {"chmod", "755", libFile.getAbsolutePath()}).waitFor(); } catch (Exception e) { 202 e.printStackTrace(); 203 } 204 } 205 return libFile.getAbsolutePath(); 206 } 207 } 208 return null; 209 } 210 211 public ClassLoader getBundleClassLoaderParent() { 212 return null; } 214 215 public void addHooks(HookRegistry hookRegistry) { 216 hookRegistry.addClassLoadingHook(this); 217 } 218 219 public BaseClassLoader createClassLoader(ClassLoader parent, ClassLoaderDelegate delegate, BundleProtectionDomain domain, BaseData data, String [] bundleclasspath) { 220 return null; 222 } 223 224 public void initializedClassLoader(BaseClassLoader baseClassLoader, BaseData data) { 225 227 } 228 } 229
| Popular Tags
|