KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.security.Permission JavaDoc;
18 import java.security.ProtectionDomain JavaDoc;
19 import org.eclipse.osgi.framework.debug.Debug;
20 import org.osgi.framework.*;
21
22 /**
23  * This class subclasses Bundle to provide a system Bundle
24  * so that the framework can be represented as a bundle and
25  * can access the services provided by other bundles.
26  */

27
28 public class SystemBundle extends BundleHost {
29
30     ProtectionDomain JavaDoc systemDomain;
31
32     /**
33      * Private SystemBundle object constructor.
34      * This method creates the SystemBundle and its BundleContext.
35      * The SystemBundle's state is set to STARTING.
36      * This method is called when the framework is constructed.
37      *
38      * @param framework Framework this bundle is running in
39      */

40     protected SystemBundle(Framework framework) throws BundleException {
41         super(framework.adaptor.createSystemBundleData(), framework); // startlevel=0 means framework stopped
42
Constants.setInternalSymbolicName(bundledata.getSymbolicName());
43         state = Bundle.RESOLVED;
44         context = createContext();
45     }
46
47     /**
48      * Load the bundle.
49      * This methods overrides the Bundle method and does nothing.
50      *
51      */

52     protected void load() {
53         SecurityManager JavaDoc sm = System.getSecurityManager();
54
55         if (sm != null) {
56             systemDomain = getClass().getProtectionDomain();
57         }
58     }
59
60     /**
61      * Reload from a new bundle.
62      * This methods overrides the Bundle method and does nothing.
63      *
64      * @param newBundle
65      * @return false
66      */

67     protected boolean reload(AbstractBundle newBundle) {
68         return (false);
69     }
70
71     /**
72      * Refresh the bundle.
73      * This methods overrides the Bundle method and does nothing.
74      *
75      */

76     protected void refresh() {
77         // do nothing
78
}
79
80     /**
81      * Unload the bundle.
82      * This methods overrides the Bundle method and does nothing.
83      *
84      * @return false
85      */

86     protected boolean unload() {
87         return (false);
88     }
89
90     /**
91      * Close the the Bundle's file.
92      * This method closes the BundleContext for the SystemBundle
93      * and sets the SystemBundle's state to UNINSTALLED.
94      *
95      */

96     protected void close() {
97         context.close();
98         context = null;
99
100         state = UNINSTALLED;
101     }
102
103     /**
104      * This method loads a class from the bundle.
105      *
106      * @param name the name of the desired Class.
107      * @param checkPermission indicates whether a permission check should be done.
108      * @return the resulting Class
109      * @exception java.lang.ClassNotFoundException if the class definition was not found.
110      */

111     protected Class JavaDoc loadClass(String JavaDoc name, boolean checkPermission) throws ClassNotFoundException JavaDoc {
112         if (checkPermission) {
113             framework.checkAdminPermission(this, AdminPermission.CLASS);
114             checkValid();
115         }
116         return (Class.forName(name));
117     }
118
119     /**
120      * Find the specified resource in this bundle.
121      * This methods returns null for the system bundle.
122      */

123     public URL JavaDoc getResource(String JavaDoc name) {
124         return (null);
125     }
126
127     /**
128      * Indicate SystemBundle is resolved.
129      *
130      */

131     protected boolean isUnresolved() {
132         return (false);
133     }
134
135     /**
136      * Start this bundle.
137      * This methods overrides the Bundle method and does nothing.
138      *
139      */

140     public void start() {
141         framework.checkAdminPermission(this, AdminPermission.EXECUTE);
142     }
143
144     /**
145      * Start the SystemBundle.
146      * This method launches the framework.
147      *
148      */

149     protected void resume() {
150         /* initialize the startlevel service */
151         framework.startLevelManager.initialize();
152
153         framework.startLevelManager.launch(framework.startLevelManager.getFrameworkStartLevel());
154
155     }
156
157     /**
158      * Stop the framework.
159      * This method spawns a thread which will call framework.shutdown.
160      *
161      */

162     public void stop() {
163         framework.checkAdminPermission(this, AdminPermission.EXECUTE);
164
165         if (state == ACTIVE) {
166             Thread JavaDoc shutdown = framework.secureAction.createThread(new Runnable JavaDoc() {
167                 public void run() {
168                     try {
169                         framework.shutdown();
170                     } catch (Throwable JavaDoc t) {
171                         // allow the adaptor to handle this unexpected error
172
framework.adaptor.handleRuntimeError(t);
173                     }
174                 }
175             }, "System Bundle Shutdown"); //$NON-NLS-1$
176

177             shutdown.start();
178         }
179     }
180
181     /**
182      * Stop the SystemBundle.
183      * This method shuts down the framework.
184      *
185      */

186     protected void suspend() {
187
188         framework.startLevelManager.shutdown();
189         framework.startLevelManager.cleanup();
190
191         /* clean up the exporting loaders */
192         framework.packageAdmin.cleanup();
193
194         if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
195             Debug.println("->Framework shutdown"); //$NON-NLS-1$
196
}
197         // fire the STOPPED event here.
198
// All bundles have been unloaded, but there may be a boot strap listener that is interested (bug 182742)
199
framework.publishBundleEvent(BundleEvent.STOPPED, this);
200     }
201
202     protected void suspend(boolean lock) {
203         // do nothing
204
}
205
206     /**
207      * Update this bundle.
208      * This method spawns a thread which will call framework.shutdown
209      * followed by framework.launch.
210      *
211      */

212     public void update() {
213         framework.checkAdminPermission(this, AdminPermission.LIFECYCLE);
214
215         if (state == ACTIVE) {
216             Thread JavaDoc restart = framework.secureAction.createThread(new Runnable JavaDoc() {
217                 public void run() {
218                     framework.shutdown();
219
220                     framework.launch();
221                 }
222             }, "System Bundle Update"); //$NON-NLS-1$
223

224             restart.start();
225         }
226     }
227
228     /**
229      * Update this bundle from an InputStream.
230      * This methods overrides the Bundle method and does nothing.
231      *
232      * @param in The InputStream from which to read the new bundle.
233      */

234     public void update(InputStream JavaDoc in) {
235         update();
236
237         try {
238             in.close();
239         } catch (IOException JavaDoc e) {
240             // do nothing
241
}
242     }
243
244     /**
245      * Uninstall this bundle.
246      * This methods overrides the Bundle method and throws an exception.
247      *
248      */

249     public void uninstall() throws BundleException {
250         framework.checkAdminPermission(this, AdminPermission.LIFECYCLE);
251
252         throw new BundleException(Msg.BUNDLE_SYSTEMBUNDLE_UNINSTALL_EXCEPTION);
253     }
254
255     /**
256      * Determine whether the bundle has the requested
257      * permission.
258      * This methods overrides the Bundle method and returns <code>true</code>.
259      *
260      * @param permission The requested permission.
261      * @return <code>true</code>
262      */

263     public boolean hasPermission(Object JavaDoc permission) {
264         if (systemDomain != null) {
265             if (permission instanceof Permission JavaDoc) {
266                 return systemDomain.implies((Permission JavaDoc) permission);
267             }
268
269             return false;
270         }
271
272         return true;
273     }
274
275     /**
276      * No work to do for the SystemBundle.
277      *
278      * @param refreshedBundles
279      * A list of bundles which have been refreshed as a result
280      * of a packageRefresh
281      */

282     protected void unresolvePermissions(AbstractBundle[] refreshedBundles) {
283         // Do nothing
284
}
285 }
286
Popular Tags