KickJava   Java API By Example, From Geeks To Geeks.

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


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

11
12 package org.eclipse.osgi.framework.adaptor.core;
13
14 import java.io.*;
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.security.ProtectionDomain JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import org.eclipse.osgi.framework.adaptor.BundleClassLoader;
20 import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate;
21 import org.eclipse.osgi.framework.debug.Debug;
22 import org.eclipse.osgi.framework.internal.core.Constants;
23 import org.eclipse.osgi.framework.util.Headers;
24 import org.eclipse.osgi.service.resolver.Version;
25 import org.osgi.framework.BundleException;
26
27 public class SystemBundleData extends AbstractBundleData {
28     public static final String JavaDoc OSGI_FRAMEWORK = "osgi.framework"; //$NON-NLS-1$
29
private BundleFile baseBundleFile;
30
31     public SystemBundleData(AbstractFrameworkAdaptor adaptor) throws BundleException {
32         super(adaptor, 0);
33         File JavaDoc osgiBase = getOsgiBase();
34         manifest = createManifest(osgiBase);
35         createBundleFile(osgiBase);
36         setMetaData();
37     }
38
39     private File JavaDoc getOsgiBase() {
40         String JavaDoc frameworkLocation = System.getProperty(OSGI_FRAMEWORK);
41         if (frameworkLocation != null)
42             // TODO assumes the location is a file URL
43
return new File JavaDoc(frameworkLocation.substring(5));
44         frameworkLocation = System.getProperty("user.dir"); //$NON-NLS-1$
45
if (frameworkLocation != null)
46             return new File JavaDoc(frameworkLocation);
47         return null;
48     }
49
50     private Headers createManifest(File JavaDoc osgiBase) throws BundleException {
51         InputStream in = null;
52
53         if (osgiBase != null && osgiBase.exists()) {
54             try {
55                 in = new FileInputStream(new File JavaDoc(osgiBase, Constants.OSGI_BUNDLE_MANIFEST));
56             } catch (FileNotFoundException e) {
57                 // do nothing here. in == null
58
}
59         }
60
61         // If we cannot find the Manifest file then use the old SYSTEMBUNDLE.MF file.
62
// This allows an adaptor to package the SYSTEMBUNDLE.MF file in a jar.
63
if (in == null) {
64             in = getClass().getResourceAsStream(Constants.OSGI_SYSTEMBUNDLE_MANIFEST);
65         }
66         if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
67             if (in == null) {
68                 Debug.println("Unable to find system bundle manifest " + Constants.OSGI_SYSTEMBUNDLE_MANIFEST); //$NON-NLS-1$
69
}
70         }
71
72         if (in == null)
73             throw new BundleException(AdaptorMsg.formatter.getString("SYSTEMBUNDLE_MISSING_MANIFEST")); //$NON-NLS-1$
74

75         Headers systemManifest = Headers.parseManifest(in);
76         // check the OSGi system package property
77
String JavaDoc systemExportProp = System.getProperty(Constants.OSGI_SYSTEMPACKAGES);
78         if (systemExportProp != null)
79             appendManifestValue(systemManifest, Constants.EXPORT_PACKAGE, systemExportProp);
80         // now get any extra packages and services that the adaptor wants
81
// to export and merge this into the system bundle's manifest
82
String JavaDoc exportPackages = adaptor.getExportPackages();
83         String JavaDoc exportServices = adaptor.getExportServices();
84         String JavaDoc providePackages = adaptor.getProvidePackages();
85         if (exportPackages != null)
86             appendManifestValue(systemManifest, Constants.EXPORT_PACKAGE, exportPackages);
87         if (exportServices != null)
88             appendManifestValue(systemManifest, Constants.EXPORT_SERVICE, exportServices);
89         if (providePackages != null)
90             appendManifestValue(systemManifest, Constants.PROVIDE_PACKAGE, providePackages);
91         return systemManifest;
92     }
93
94     private void appendManifestValue(Headers systemManifest, String JavaDoc header, String JavaDoc append) {
95         String JavaDoc newValue = (String JavaDoc) systemManifest.get(header);
96         if (newValue == null) {
97             newValue = append;
98         } else {
99             newValue += "," + append; //$NON-NLS-1$
100
}
101         systemManifest.set(header, null);
102         systemManifest.set(header, newValue);
103     }
104
105     private void createBundleFile(File JavaDoc osgiBase) {
106         if (osgiBase != null)
107             try {
108                 baseBundleFile = adaptor.createBundleFile(osgiBase, this);
109             } catch (IOException JavaDoc e) {
110                 // should not happen
111
}
112         else
113             baseBundleFile = new BundleFile(osgiBase) {
114                 public File JavaDoc getFile(String JavaDoc path) {
115                     return null;
116                 }
117
118                 public BundleEntry getEntry(String JavaDoc path) {
119                     return null;
120                 }
121
122                 public Enumeration JavaDoc getEntryPaths(String JavaDoc path) {
123                     return null;
124                 }
125
126                 public void close() throws IOException JavaDoc {
127                 }
128
129                 public void open() throws IOException JavaDoc {
130                 }
131
132                 public boolean containsDir(String JavaDoc dir) {
133                     return false;
134                 }
135             };
136     }
137
138     private void setMetaData() {
139         setActivator((String JavaDoc) manifest.get(Constants.BUNDLE_ACTIVATOR));
140         setClassPath((String JavaDoc) manifest.get(Constants.BUNDLE_CLASSPATH));
141         setDynamicImports((String JavaDoc) manifest.get(Constants.DYNAMICIMPORT_PACKAGE));
142         setExecutionEnvironment((String JavaDoc) manifest.get(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT));
143         setLocation(Constants.SYSTEM_BUNDLE_LOCATION);
144         setSymbolicName(AbstractBundleData.parseSymbolicName(manifest));
145         String JavaDoc sVersion = (String JavaDoc) manifest.get(Constants.BUNDLE_VERSION);
146         if (sVersion != null)
147             setVersion(new Version(sVersion));
148     }
149
150     public BundleClassLoader createClassLoader(ClassLoaderDelegate delegate, ProtectionDomain JavaDoc domain, String JavaDoc[] bundleclasspath) {
151         return null;
152     }
153
154     public File JavaDoc createGenerationDir() {
155         return null;
156     }
157
158     public BundleFile getBaseBundleFile() {
159         return baseBundleFile;
160     }
161
162     public String JavaDoc findLibrary(String JavaDoc libname) {
163         return null;
164     }
165
166     public void installNativeCode(String JavaDoc[] nativepaths) throws BundleException {
167     }
168
169     public File JavaDoc getDataFile(String JavaDoc path) {
170         return null;
171     }
172
173     public int getStartLevel() {
174         return 0;
175     }
176
177     public int getStatus() {
178         return 0;
179     }
180
181     public void close() throws IOException JavaDoc {
182     }
183
184     public void open() throws IOException JavaDoc {
185     }
186
187     public void save() throws IOException JavaDoc {
188     }
189 }
Popular Tags