KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > SystemBundleData


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.framework.internal.core;
13
14 import java.io.*;
15 import java.net.URL JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import org.eclipse.osgi.framework.adaptor.*;
18 import org.eclipse.osgi.framework.adaptor.core.*;
19 import org.eclipse.osgi.framework.debug.Debug;
20 import org.eclipse.osgi.framework.internal.core.Constants;
21 import org.eclipse.osgi.framework.util.Headers;
22 import org.osgi.framework.BundleException;
23
24 public class SystemBundleData extends AbstractBundleData {
25     public static final String JavaDoc OSGI_FRAMEWORK = "osgi.framework"; //$NON-NLS-1$
26

27     public SystemBundleData(AbstractFrameworkAdaptor adaptor) throws BundleException {
28         super(adaptor, 0);
29         File osgiBase = getOsgiBase();
30         createBundleFile(osgiBase);
31         manifest = createManifest(osgiBase);
32         setMetaData();
33         setLastModified(System.currentTimeMillis()); // just set the lastModified to the current time
34
}
35
36     private File getOsgiBase() {
37         String JavaDoc frameworkLocation = System.getProperty(OSGI_FRAMEWORK);
38         if (frameworkLocation != null)
39             // TODO assumes the location is a file URL
40
return new File(frameworkLocation.substring(5));
41         try {
42             URL JavaDoc url = getClass().getProtectionDomain().getCodeSource().getLocation();
43             // assumes file URL
44
return new File(url.getPath());
45         } catch (Throwable JavaDoc e) {
46             // do nothing
47
}
48         frameworkLocation = System.getProperty("user.dir"); //$NON-NLS-1$
49
if (frameworkLocation != null)
50             return new File(frameworkLocation);
51         return null;
52     }
53
54     private Headers createManifest(File osgiBase) throws BundleException {
55         InputStream in = null;
56
57         if (osgiBase != null && osgiBase.exists()) {
58             try {
59                 BundleEntry entry = baseBundleFile.getEntry(Constants.OSGI_BUNDLE_MANIFEST);
60                 if (entry != null)
61                     in = entry.getInputStream();
62             } catch (IOException e) {
63                 // do nothing here. in == null
64
}
65         }
66
67         // If we cannot find the Manifest file from the baseBundleFile then
68
// search for the manifest as a classloader resource
69
// This allows an adaptor to package the SYSTEMBUNDLE.MF file in a jar.
70
if (in == null) {
71             in = getManifestAsResource();
72         }
73         if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
74             if (in == null) {
75                 Debug.println("Unable to find system bundle manifest " + Constants.OSGI_BUNDLE_MANIFEST); //$NON-NLS-1$
76
}
77         }
78
79         if (in == null)
80             throw new BundleException(AdaptorMsg.SYSTEMBUNDLE_MISSING_MANIFEST);
81         Headers systemManifest = Headers.parseManifest(in);
82         // now get any extra packages and services that the adaptor wants
83
// to export and merge this into the system bundle's manifest
84
String JavaDoc exportPackages = adaptor.getExportPackages();
85         String JavaDoc exportServices = adaptor.getExportServices();
86         String JavaDoc providePackages = adaptor.getProvidePackages();
87         if (exportPackages != null)
88             appendManifestValue(systemManifest, Constants.EXPORT_PACKAGE, exportPackages);
89         if (exportServices != null)
90             appendManifestValue(systemManifest, Constants.EXPORT_SERVICE, exportServices);
91         if (providePackages != null)
92             appendManifestValue(systemManifest, Constants.PROVIDE_PACKAGE, providePackages);
93         return systemManifest;
94     }
95
96     private InputStream getManifestAsResource() {
97         ClassLoader JavaDoc cl = getClass().getClassLoader();
98         try {
99             // get all manifests in your classloader delegation
100
Enumeration JavaDoc manifests = cl != null ? cl.getResources(Constants.OSGI_BUNDLE_MANIFEST) : ClassLoader.getSystemResources(Constants.OSGI_BUNDLE_MANIFEST);
101             while (manifests.hasMoreElements()) {
102                 URL JavaDoc url = (URL JavaDoc) manifests.nextElement();
103                 try {
104                     // check each manifest until we find one with the Eclipse-SystemBundle: true header
105
Headers headers = Headers.parseManifest(url.openStream());
106                     if ("true".equals(headers.get(Constants.ECLIPSE_SYSTEMBUNDLE))) //$NON-NLS-1$
107
return url.openStream();
108                 } catch (BundleException e) {
109                     // ignore and continue to next URL
110
}
111             }
112         } catch (IOException e) {
113             // ignore and return null
114
}
115         return null;
116     }
117
118     private void appendManifestValue(Headers systemManifest, String JavaDoc header, String JavaDoc append) {
119         String JavaDoc newValue = (String JavaDoc) systemManifest.get(header);
120         if (newValue == null) {
121             newValue = append;
122         } else {
123             newValue += "," + append; //$NON-NLS-1$
124
}
125         systemManifest.set(header, null);
126         systemManifest.set(header, newValue);
127     }
128
129     private void createBundleFile(File osgiBase) {
130         if (osgiBase != null)
131             try {
132                 baseBundleFile = adaptor.createBundleFile(osgiBase, this);
133             } catch (IOException e) {
134                 // should not happen
135
}
136         else
137             baseBundleFile = new BundleFile(osgiBase) {
138                 public File getFile(String JavaDoc path) {
139                     return null;
140                 }
141
142                 public BundleEntry getEntry(String JavaDoc path) {
143                     return null;
144                 }
145
146                 public Enumeration JavaDoc getEntryPaths(String JavaDoc path) {
147                     return null;
148                 }
149
150                 public void close() {
151                     // do nothing
152
}
153
154                 public void open() {
155                     // do nothing
156
}
157
158                 public boolean containsDir(String JavaDoc dir) {
159                     return false;
160                 }
161             };
162     }
163
164     private void setMetaData() throws BundleException {
165         setLocation(Constants.SYSTEM_BUNDLE_LOCATION);
166         loadFromManifest();
167     }
168
169     public BundleClassLoader createClassLoader(ClassLoaderDelegate delegate, BundleProtectionDomain domain, String JavaDoc[] bundleclasspath) {
170         return null;
171     }
172
173     public File createGenerationDir() {
174         return null;
175     }
176
177     public String JavaDoc findLibrary(String JavaDoc libname) {
178         return null;
179     }
180
181     public void installNativeCode(String JavaDoc[] nativepaths) throws BundleException {
182         // do nothing
183
}
184
185     public File getDataFile(String JavaDoc path) {
186         return null;
187     }
188
189     public int getStartLevel() {
190         return 0;
191     }
192
193     public int getStatus() {
194         return 0;
195     }
196
197     public void save() {
198         // do nothing
199
}
200
201     public String JavaDoc[] getBundleSigners() {
202         return null; // system bundle cannot be signed
203
}
204 }
205
Popular Tags