KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.*;
15 import java.net.URL JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import org.eclipse.osgi.baseadaptor.*;
18 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
19 import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile;
20 import org.eclipse.osgi.baseadaptor.hooks.StorageHook;
21 import org.eclipse.osgi.framework.adaptor.*;
22 import org.eclipse.osgi.framework.debug.Debug;
23 import org.eclipse.osgi.framework.internal.core.Constants;
24 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
25 import org.eclipse.osgi.framework.util.Headers;
26 import org.osgi.framework.BundleException;
27
28 public class SystemBundleData extends BaseData {
29     private static final String JavaDoc OSGI_FRAMEWORK = "osgi.framework"; //$NON-NLS-1$
30

31     public SystemBundleData(BaseAdaptor adaptor) throws BundleException {
32         super(0, adaptor);
33         File osgiBase = getOsgiBase();
34         createBundleFile(osgiBase);
35         manifest = createManifest(osgiBase);
36         setMetaData();
37         setLastModified(System.currentTimeMillis()); // just set the lastModified to the current time
38
StorageHook[] hooks = adaptor.getHookRegistry().getStorageHooks();
39         StorageHook[] instanceHooks = new StorageHook[hooks.length];
40         for (int i = 0; i < hooks.length; i++)
41             instanceHooks[i] = hooks[i].create(this);
42         setStorageHooks(instanceHooks);
43     }
44
45     private File getOsgiBase() {
46         String JavaDoc frameworkLocation = FrameworkProperties.getProperty(SystemBundleData.OSGI_FRAMEWORK);
47         if (frameworkLocation != null)
48             // TODO assumes the location is a file URL
49
return new File(frameworkLocation.substring(5));
50         try {
51             URL JavaDoc url = getClass().getProtectionDomain().getCodeSource().getLocation();
52             // assumes file URL
53
return new File(url.getPath());
54         } catch (Throwable JavaDoc e) {
55             // do nothing
56
}
57         frameworkLocation = FrameworkProperties.getProperty("user.dir"); //$NON-NLS-1$
58
if (frameworkLocation != null)
59             return new File(frameworkLocation);
60         return null;
61     }
62
63     private Headers createManifest(File osgiBase) throws BundleException {
64         InputStream in = null;
65
66         if (osgiBase != null && osgiBase.exists())
67             try {
68                 BundleEntry entry = getBundleFile().getEntry(Constants.OSGI_BUNDLE_MANIFEST);
69                 if (entry != null)
70                     in = entry.getInputStream();
71             } catch (IOException e) {
72                 // do nothing here. in == null
73
}
74
75         // If we cannot find the Manifest file from the baseBundleFile then
76
// search for the manifest as a classloader resource
77
// This allows an adaptor to package the SYSTEMBUNDLE.MF file in a jar.
78
if (in == null)
79             in = getManifestAsResource();
80         if (Debug.DEBUG && Debug.DEBUG_GENERAL)
81             if (in == null)
82                 Debug.println("Unable to find system bundle manifest " + Constants.OSGI_BUNDLE_MANIFEST); //$NON-NLS-1$
83

84         if (in == null)
85             throw new BundleException(AdaptorMsg.SYSTEMBUNDLE_MISSING_MANIFEST);
86         return Headers.parseManifest(in);
87     }
88
89     private InputStream getManifestAsResource() {
90         ClassLoader JavaDoc cl = getClass().getClassLoader();
91         try {
92             // get all manifests in your classloader delegation
93
Enumeration JavaDoc manifests = cl != null ? cl.getResources(Constants.OSGI_BUNDLE_MANIFEST) : ClassLoader.getSystemResources(Constants.OSGI_BUNDLE_MANIFEST);
94             while (manifests.hasMoreElements()) {
95                 URL JavaDoc url = (URL JavaDoc) manifests.nextElement();
96                 try {
97                     // check each manifest until we find one with the Eclipse-SystemBundle: true header
98
Headers headers = Headers.parseManifest(url.openStream());
99                     if ("true".equals(headers.get(Constants.ECLIPSE_SYSTEMBUNDLE))) //$NON-NLS-1$
100
return url.openStream();
101                 } catch (BundleException e) {
102                     // ignore and continue to next URL
103
}
104             }
105         } catch (IOException e) {
106             // ignore and return null
107
}
108         return null;
109     }
110
111     private void createBundleFile(File osgiBase) {
112         if (osgiBase != null)
113             try {
114                 bundleFile = getAdaptor().createBundleFile(osgiBase, this);
115             } catch (IOException e) {
116                 // should not happen
117
}
118         else
119             bundleFile = new BundleFile(osgiBase) {
120                 public File getFile(String JavaDoc path, boolean nativeCode) {
121                     return null;
122                 }
123
124                 public BundleEntry getEntry(String JavaDoc path) {
125                     return null;
126                 }
127
128                 public Enumeration JavaDoc getEntryPaths(String JavaDoc path) {
129                     return null;
130                 }
131
132                 public void close() {
133                     // do nothing
134
}
135
136                 public void open() {
137                     // do nothing
138
}
139
140                 public boolean containsDir(String JavaDoc dir) {
141                     return false;
142                 }
143             };
144     }
145
146     private void setMetaData() throws BundleException {
147         setLocation(Constants.SYSTEM_BUNDLE_LOCATION);
148         BaseStorageHook.loadManifest(this, manifest);
149     }
150
151     public BundleClassLoader createClassLoader(ClassLoaderDelegate delegate, BundleProtectionDomain domain, String JavaDoc[] bundleclasspath) {
152         return null;
153     }
154
155     public File createGenerationDir() {
156         return null;
157     }
158
159     public String JavaDoc findLibrary(String JavaDoc libname) {
160         return null;
161     }
162
163     public void installNativeCode(String JavaDoc[] nativepaths) throws BundleException {
164         // do nothing
165
}
166
167     public File getDataFile(String JavaDoc path) {
168         return null;
169     }
170
171     public int getStartLevel() {
172         return 0;
173     }
174
175     public int getStatus() {
176         return Constants.BUNDLE_STARTED;
177     }
178
179     public void save() {
180         // do nothing
181
}
182
183 }
184
Popular Tags