KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > SystemBundle


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar;
37
38 import java.io.InputStream JavaDoc;
39 import java.security.AccessController JavaDoc;
40 import java.security.PrivilegedAction JavaDoc;
41 import java.util.*;
42
43 import org.osgi.framework.*;
44 import org.ungoverned.moduleloader.LibrarySource;
45 import org.ungoverned.moduleloader.ResourceSource;
46 import org.ungoverned.moduleloader.SystemResourceSource;
47 import org.ungoverned.moduleloader.search.ImportSearchPolicy;
48 import org.ungoverned.oscar.util.CaseInsensitiveMap;
49 import org.ungoverned.oscar.util.OscarConstants;
50
51 class SystemBundle extends BundleImpl
52 {
53     private List m_activatorList = null;
54     private BundleActivator m_activator = null;
55     private Thread JavaDoc m_shutdownThread = null;
56     private Object JavaDoc[][] m_attributes = null;
57     private ResourceSource[] m_resSources = null;
58     private LibrarySource[] m_libSources = null;
59
60     protected SystemBundle(Oscar oscar, BundleInfo info, List activatorList)
61         throws BundleException
62     {
63         super(oscar, info);
64
65         // Create an activator list if necessary.
66
if (activatorList == null)
67         {
68             activatorList = new ArrayList();
69         }
70
71         // Add the bundle activator for the package admin service.
72
activatorList.add(new PackageAdminActivator(oscar));
73
74         // Add the bundle activator for the start level service.
75
activatorList.add(new StartLevelActivator(oscar));
76
77         m_activatorList = activatorList;
78
79         int[] version120 = { 1, 2, 0 };
80         int[] version100 = { 1, 0, 0 };
81         Object JavaDoc[][] exports = new Object JavaDoc[][] {
82             { "org.osgi.framework", version120, null },
83             { "org.osgi.service.packageadmin", version120, null },
84             { "org.osgi.service.startlevel", version100, null }
85         };
86         m_attributes = new Object JavaDoc[][] {
87             new Object JavaDoc[] { "exports", exports },
88             new Object JavaDoc[] { "imports", new Object JavaDoc[0][0] },
89             new Object JavaDoc[] { "propagates", new Object JavaDoc[0] }
90         };
91         m_resSources = new ResourceSource[] {
92             new SystemResourceSource()
93         };
94         m_libSources = null;
95
96         String JavaDoc exportString = "";
97         for (int i = 0; i < exports.length; i++)
98         {
99             if (i < (exports.length - 1))
100             {
101                 exportString = exportString +
102                     exports[i][ImportSearchPolicy.IDENTIFIER_IDX]
103                     + "; specification-version=\""
104                     + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[0] + "."
105                     + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[1] + "."
106                     + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[2] + "\", ";
107             }
108             else
109             {
110                 exportString = exportString +
111                     exports[i][ImportSearchPolicy.IDENTIFIER_IDX]
112                     + "; specification-version=\""
113                     + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[0] + "."
114                     + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[1] + "."
115                     + ((int[])exports[i][ImportSearchPolicy.VERSION_IDX])[2] + "\"";
116             }
117         }
118
119         // Initialize header map as a case insensitive map.
120
Map map = new CaseInsensitiveMap();
121         map.put(OscarConstants.BUNDLE_VERSION, OscarConstants.OSCAR_VERSION_VALUE);
122         map.put(OscarConstants.BUNDLE_NAME, "System Bundle");
123         map.put(OscarConstants.BUNDLE_DESCRIPTION,
124             "This bundle is system specific; it implements various system services.");
125         map.put(OscarConstants.EXPORT_PACKAGE, exportString);
126         ((SystemBundleArchive) getInfo().getArchive()).setManifestHeader(map);
127     }
128
129     public Object JavaDoc[][] getAttributes()
130     {
131         return m_attributes;
132     }
133
134     public ResourceSource[] getResourceSources()
135     {
136         return m_resSources;
137     }
138
139     public LibrarySource[] getLibrarySources()
140     {
141         return m_libSources;
142     }
143
144     public synchronized void start() throws BundleException
145     {
146         Oscar.debug("SystemBundle.start()");
147
148         // The system bundle is only started once and it
149
// is started by Oscar.
150
if (getState() == Bundle.ACTIVE)
151         {
152             throw new BundleException("Cannot start the system bundle.");
153         }
154
155         getInfo().setState(Bundle.STARTING);
156
157         try {
158             getInfo().setContext(new BundleContextImpl(getOscar(), this));
159             getActivator().start(getInfo().getContext());
160         } catch (Throwable JavaDoc throwable) {
161 throwable.printStackTrace();
162             throw new BundleException(
163                 "Unable to start system bundle.", throwable);
164         }
165
166         // Do NOT set the system bundle state to active yet, this
167
// must be done after all other bundles have been restarted.
168
// This will be done in the Oscar.initialize() method.
169
}
170
171     public synchronized void stop() throws BundleException
172     {
173         Oscar.debug("SystemBundle.stop()");
174
175         if (System.getSecurityManager() != null)
176         {
177             AccessController.checkPermission(new AdminPermission());
178         }
179
180         // Spec says stop() on SystemBundle should return immediately and
181
// shutdown framework on another thread.
182
if (getOscar().getFrameworkStatus() == Oscar.RUNNING_STATUS)
183         {
184             // Initial call of stop, so kick off shutdown.
185
m_shutdownThread = new Thread JavaDoc("OscarShutdown") {
186                 public void run()
187                 {
188                     try {
189                         getOscar().shutdown();
190                     } catch (Exception JavaDoc ex) {
191                         Oscar.error("SystemBundle: Error while shutting down.");
192                         Oscar.error("SystemBundle: " + ex);
193                     }
194
195                     // Only shutdown the JVM if Oscar is running stand-alone.
196
String JavaDoc embedded = getOscar().getConfigProperty(
197                         OscarConstants.EMBEDDED_EXECUTION_PROP);
198                     boolean isEmbedded = (embedded == null)
199                         ? false : embedded.equals("true");
200                     if (!isEmbedded)
201                     {
202                         if (System.getSecurityManager() != null)
203                         {
204                             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
205                                 public Object JavaDoc run()
206                                 {
207                                     System.exit(0);
208                                     return null;
209                                 }
210                             });
211                         }
212                         else
213                         {
214                             System.exit(0);
215                         }
216                     }
217                 }
218             };
219             getInfo().setState(Bundle.STOPPING);
220             m_shutdownThread.start();
221         }
222         else if ((getOscar().getFrameworkStatus() == Oscar.STOPPING_STATUS) &&
223                  (Thread.currentThread() == m_shutdownThread))
224         {
225             // Callback from shutdown thread, so do our own stop.
226
try {
227                 getActivator().stop(getInfo().getContext());
228             } catch (Throwable JavaDoc throwable) {
229                 throw new BundleException(
230                         "Unable to stop system bundle.", throwable);
231             }
232         }
233     }
234
235     public synchronized void uninstall() throws BundleException
236     {
237         Oscar.debug("SystemBundle.uninstall()");
238         throw new BundleException("Cannot uninstall the system bundle.");
239     }
240
241     public synchronized void update() throws BundleException
242     {
243         update(null);
244     }
245
246     public synchronized void update(InputStream JavaDoc is) throws BundleException
247     {
248         Oscar.debug("SystemBundle.update()");
249
250         if (System.getSecurityManager() != null)
251         {
252             AccessController.checkPermission(new AdminPermission());
253         }
254
255         // TODO: This is supposed to stop and then restart the framework.
256
throw new BundleException("System bundle update not implemented yet.");
257     }
258
259     protected BundleActivator getActivator()
260         throws Exception JavaDoc
261     {
262         if (m_activator == null)
263         {
264             m_activator = new SystemBundleActivator(getOscar(), m_activatorList);
265         }
266         return m_activator;
267     }
268 }
269
Popular Tags