KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > baseadaptor > BaseClassLoadingHook


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.internal.baseadaptor;
13
14 import java.io.File JavaDoc;
15 import java.security.ProtectionDomain JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import org.eclipse.osgi.baseadaptor.BaseData;
18 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
19 import org.eclipse.osgi.baseadaptor.hooks.ClassLoadingHook;
20 import org.eclipse.osgi.baseadaptor.loader.*;
21 import org.eclipse.osgi.framework.adaptor.BundleProtectionDomain;
22 import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate;
23 import org.eclipse.osgi.framework.debug.Debug;
24 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
25 import org.eclipse.osgi.util.ManifestElement;
26
27 public class BaseClassLoadingHook implements ClassLoadingHook {
28     private static final String JavaDoc[] LIB_EXTENSIONS = ManifestElement.getArrayFromList(FrameworkProperties.getProperty("osgi.framework.library.extensions"), ","); //$NON-NLS-1$ //$NON-NLS-2$
29
private static final String JavaDoc[] EMPTY_STRINGS = new String JavaDoc[0];
30
31     /*
32      * Maps an already mapped library name to additional library file extensions.
33      * This is needed on platforms like AIX where .a and .so can be used as library file
34      * extensions, but System.mapLibraryName only returns a single string.
35      */

36     public static String JavaDoc[] mapLibraryNames(String JavaDoc mappedLibName) {
37         int extIndex = mappedLibName.lastIndexOf('.');
38         if (LIB_EXTENSIONS.length == 0 || extIndex < 0)
39             return EMPTY_STRINGS;
40         String JavaDoc libNameBase = mappedLibName.substring(0, extIndex);
41         String JavaDoc[] results = new String JavaDoc[LIB_EXTENSIONS.length];
42         for (int i = 0; i < results.length; i++)
43             results[i] = libNameBase + LIB_EXTENSIONS[i];
44         return results;
45     }
46
47     public String JavaDoc findLibrary(BaseData data, String JavaDoc libName) {
48         String JavaDoc mappedName = System.mapLibraryName(libName);
49         String JavaDoc path = null;
50         if (Debug.DEBUG && Debug.DEBUG_LOADER)
51             Debug.println(" mapped library name: " + mappedName); //$NON-NLS-1$
52
path = findNativePath(data, mappedName);
53         if (path == null) {
54             String JavaDoc[] mappedNames = mapLibraryNames(mappedName);
55             for (int i = 0; i < mappedNames.length && path == null; i++)
56                 path = findNativePath(data, mappedNames[i]);
57         }
58         if (path == null) {
59             if (Debug.DEBUG && Debug.DEBUG_LOADER)
60                 Debug.println(" library does not exist: " + mappedName); //$NON-NLS-1$
61
path = findNativePath(data, libName);
62         }
63         if (Debug.DEBUG && Debug.DEBUG_LOADER)
64             Debug.println(" returning library: " + path); //$NON-NLS-1$
65
return path;
66     }
67
68     private String JavaDoc findNativePath(BaseData bundledata, String JavaDoc libname) {
69         int slash = libname.lastIndexOf('/');
70         if (slash >= 0)
71             libname = libname.substring(slash + 1);
72         String JavaDoc[] nativepaths = getNativePaths(bundledata);
73         if (nativepaths == null)
74             return null;
75         for (int i = 0; i < nativepaths.length; i++) {
76             slash = nativepaths[i].lastIndexOf('/');
77             String JavaDoc path = slash < 0 ? nativepaths[i] : nativepaths[i].substring(slash + 1);
78             if (path.equals(libname)) {
79                 if (nativepaths[i].startsWith(BaseStorageHook.EXTERNAL_LIB_PREFIX)) {
80                     // references an external library; do variable substitution
81
String JavaDoc externalPath = BaseStorageHook.substituteVars(nativepaths[i].substring(BaseStorageHook.EXTERNAL_LIB_PREFIX.length()));
82                     File JavaDoc nativeFile = new File JavaDoc(externalPath);
83                     return nativeFile.getAbsolutePath();
84                 }
85                 // this is a normal library contained within the bundle
86
File JavaDoc nativeFile = bundledata.getBundleFile().getFile(nativepaths[i], true);
87                 if (nativeFile != null)
88                     return nativeFile.getAbsolutePath();
89             }
90         }
91         return null;
92     }
93
94     private String JavaDoc[] getNativePaths(BaseData bundledata) {
95         BaseStorageHook storageHook = (BaseStorageHook) bundledata.getStorageHook(BaseStorageHook.KEY);
96         return storageHook != null ? storageHook.getNativePaths() : null;
97     }
98
99     public boolean addClassPathEntry(ArrayList JavaDoc cpEntries, String JavaDoc cp, ClasspathManager hostmanager, BaseData sourcedata, ProtectionDomain JavaDoc sourcedomain) {
100         // do nothing
101
return false;
102     }
103
104     public ClassLoader JavaDoc getBundleClassLoaderParent() {
105         // do nothing
106
return null;
107     }
108
109     public byte[] processClass(String JavaDoc name, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) {
110         // do nothing
111
return null;
112     }
113
114     public BaseClassLoader createClassLoader(ClassLoader JavaDoc parent, ClassLoaderDelegate delegate, BundleProtectionDomain domain, BaseData data, String JavaDoc[] bundleclasspath) {
115         // do nothing
116
return null;
117     }
118
119     public void initializedClassLoader(BaseClassLoader baseClassLoader, BaseData data) {
120         // do nothing
121
}
122
123 }
124
Popular Tags