KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import org.eclipse.osgi.framework.adaptor.BundleData;
18 import org.eclipse.osgi.framework.debug.Debug;
19 import org.eclipse.osgi.util.NLS;
20 import org.osgi.framework.*;
21
22 public class BundleFragment extends AbstractBundle {
23
24     /** The resolved host that this fragment is attached to */
25     protected BundleLoaderProxy[] hosts;
26
27     /**
28      * @param bundledata
29      * @param framework
30      * @throws BundleException
31      */

32     public BundleFragment(BundleData bundledata, Framework framework) throws BundleException {
33         super(bundledata, framework);
34         hosts = null;
35     }
36
37     /**
38      * Load the bundle.
39      */

40     protected void load() {
41         if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
42             if ((state & (INSTALLED)) == 0) {
43                 Debug.println("Bundle.load called when state != INSTALLED: " + this); //$NON-NLS-1$
44
Debug.printStackTrace(new Exception JavaDoc("Stack trace")); //$NON-NLS-1$
45
}
46         }
47
48         if (framework.isActive()) {
49             SecurityManager JavaDoc sm = System.getSecurityManager();
50
51             if (sm != null && framework.permissionAdmin != null) {
52                 domain = framework.permissionAdmin.createProtectionDomain(this);
53             }
54         }
55     }
56
57     /**
58      * Reload from a new bundle.
59      * This method must be called while holding the bundles lock.
60      *
61      * @param newBundle Dummy Bundle which contains new data.
62      * @return true if an exported package is "in use". i.e. it has been imported by a bundle
63      */

64     protected boolean reload(AbstractBundle newBundle) {
65         if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
66             if ((state & (INSTALLED | RESOLVED)) == 0) {
67                 Debug.println("Bundle.reload called when state != INSTALLED | RESOLVED: " + this); //$NON-NLS-1$
68
Debug.printStackTrace(new Exception JavaDoc("Stack trace")); //$NON-NLS-1$
69
}
70         }
71
72         boolean exporting = false;
73         if (framework.isActive()) {
74             if (hosts != null) {
75                 if (state == RESOLVED) {
76                     exporting = true; // if we have a host we cannot be removed until the host is refreshed
77
hosts = null;
78                     state = INSTALLED;
79                 }
80             }
81         } else {
82             /* close the outgoing jarfile */
83             try {
84                 this.bundledata.close();
85             } catch (IOException JavaDoc e) {
86                 // Do Nothing
87
}
88         }
89         if (!exporting) {
90             /* close the outgoing jarfile */
91             try {
92                 this.bundledata.close();
93             } catch (IOException JavaDoc e) {
94                 // Do Nothing
95
}
96         }
97
98         this.bundledata = newBundle.bundledata;
99         this.bundledata.setBundle(this);
100         // create a new domain for the bundle because its signers/symbolic-name may have changed
101
if (framework.isActive() && System.getSecurityManager() != null && framework.permissionAdmin != null)
102             domain = framework.permissionAdmin.createProtectionDomain(this);
103         return (exporting);
104     }
105
106     /**
107      * Refresh the bundle. This is called by Framework.refreshPackages.
108      * This method must be called while holding the bundles lock.
109      * this.loader.unimportPackages must have already been called before calling
110      * this method!
111      */

112     protected void refresh() {
113         if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
114             if ((state & (UNINSTALLED | INSTALLED | RESOLVED)) == 0) {
115                 Debug.println("Bundle.refresh called when state != UNINSTALLED | INSTALLED | RESOLVED: " + this); //$NON-NLS-1$
116
Debug.printStackTrace(new Exception JavaDoc("Stack trace")); //$NON-NLS-1$
117
}
118         }
119
120         if (state == RESOLVED) {
121             hosts = null;
122             state = INSTALLED;
123             // Do not publish UNRESOLVED event here. This is done by caller
124
// to resolve if appropriate.
125
}
126         manifestLocalization = null;
127     }
128
129     /**
130      * Unload the bundle.
131      * This method must be called while holding the bundles lock.
132      *
133      * @return true if an exported package is "in use". i.e. it has been imported by a bundle
134      */

135     protected boolean unload() {
136         if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
137             if ((state & (UNINSTALLED | INSTALLED | RESOLVED)) == 0) {
138                 Debug.println("Bundle.unload called when state != UNINSTALLED | INSTALLED | RESOLVED: " + this); //$NON-NLS-1$
139
Debug.printStackTrace(new Exception JavaDoc("Stack trace")); //$NON-NLS-1$
140
}
141         }
142
143         boolean exporting = false;
144         if (framework.isActive()) {
145             if (hosts != null) {
146                 if (state == RESOLVED) {
147                     exporting = true; // if we have a host we cannot be removed until the host is refreshed
148
hosts = null;
149                     state = INSTALLED;
150                 }
151                 domain = null;
152             }
153         }
154         if (!exporting) {
155             try {
156                 this.bundledata.close();
157             } catch (IOException JavaDoc e) { // Do Nothing.
158
}
159         }
160
161         return (exporting);
162     }
163
164     /**
165      * This method loads a class from the bundle.
166      *
167      * @param name the name of the desired Class.
168      * @param checkPermission indicates whether a permission check should be done.
169      * @return the resulting Class
170      * @exception java.lang.ClassNotFoundException if the class definition was not found.
171      */

172     protected Class JavaDoc loadClass(String JavaDoc name, boolean checkPermission) throws ClassNotFoundException JavaDoc {
173         if (checkPermission) {
174             try {
175                 framework.checkAdminPermission(this, AdminPermission.CLASS);
176             } catch (SecurityException JavaDoc e) {
177                 throw new ClassNotFoundException JavaDoc();
178             }
179             checkValid();
180         }
181         // cannot load a class from a fragment because there is no classloader
182
// associated with fragments.
183
throw new ClassNotFoundException JavaDoc(NLS.bind(Msg.BUNDLE_FRAGMENT_CNFE, name));
184     }
185
186     /**
187      * Find the specified resource in this bundle.
188      *
189      * This bundle's class loader is called to search for the named resource.
190      * If this bundle's state is <tt>INSTALLED</tt>, then only this bundle will
191      * be searched for the specified resource. Imported packages cannot be searched
192      * when a bundle has not been resolved.
193      *
194      * @param name The name of the resource.
195      * See <tt>java.lang.ClassLoader.getResource</tt> for a description of
196      * the format of a resource name.
197      * @return a URL to the named resource, or <tt>null</tt> if the resource could
198      * not be found or if the caller does not have
199      * the <tt>AdminPermission</tt>, and the Java Runtime Environment supports permissions.
200      *
201      * @exception java.lang.IllegalStateException If this bundle has been uninstalled.
202      */

203     public URL JavaDoc getResource(String JavaDoc name) {
204         checkValid();
205         // cannot get a resource for a fragment because there is no classloader
206
// associated with fragments.
207
return (null);
208
209     }
210
211     public Enumeration JavaDoc getResources(String JavaDoc name) {
212         checkValid();
213         // cannot get a resource for a fragment because there is no classloader
214
// associated with fragments.
215
return null;
216     }
217
218     /**
219      * Internal worker to start a bundle.
220      *
221      * @param options
222      */

223     protected void startWorker(int options) throws BundleException {
224         throw new BundleException(NLS.bind(Msg.BUNDLE_FRAGMENT_START, this));
225     }
226
227     /**
228      * Internal worker to stop a bundle.
229      *
230      * @param options
231      */

232     protected void stopWorker(int options) throws BundleException {
233         throw new BundleException(NLS.bind(Msg.BUNDLE_FRAGMENT_STOP, this));
234     }
235
236     /**
237      * Provides a list of {@link ServiceReference}s for the services
238      * registered by this bundle
239      * or <code>null</code> if the bundle has no registered
240      * services.
241      *
242      * <p>The list is valid at the time
243      * of the call to this method, but the framework is a very dynamic
244      * environment and services can be modified or unregistered at anytime.
245      *
246      * @return An array of {@link ServiceReference} or <code>null</code>.
247      * @exception java.lang.IllegalStateException If the
248      * bundle has been uninstalled.
249      * @see ServiceRegistrationImpl
250      * @see ServiceReference
251      */

252     public ServiceReference[] getRegisteredServices() {
253         checkValid();
254         // Fragments cannot have a BundleContext and therefore
255
// cannot have any services registered.
256
return null;
257     }
258
259     /**
260      * Provides a list of {@link ServiceReference}s for the
261      * services this bundle is using,
262      * or <code>null</code> if the bundle is not using any services.
263      * A bundle is considered to be using a service if the bundle's
264      * use count for the service is greater than zero.
265      *
266      * <p>The list is valid at the time
267      * of the call to this method, but the framework is a very dynamic
268      * environment and services can be modified or unregistered at anytime.
269      *
270      * @return An array of {@link ServiceReference} or <code>null</code>.
271      * @exception java.lang.IllegalStateException If the
272      * bundle has been uninstalled.
273      * @see ServiceReference
274      */

275     public ServiceReference[] getServicesInUse() {
276         checkValid();
277         // Fragments cannot have a BundleContext and therefore
278
// cannot have any services in use.
279
return null;
280     }
281
282     protected BundleLoaderProxy[] getHosts() {
283         return hosts;
284     }
285
286     protected boolean isFragment() {
287         return true;
288     }
289
290     /**
291      * Adds a host bundle for this fragment.
292      * @param value the BundleHost to add to the set of host bundles
293      */

294     protected boolean addHost(BundleLoaderProxy host) {
295         if (host == null)
296             return false;
297         try {
298             ((BundleHost) host.getBundleHost()).attachFragment(this);
299         } catch (BundleException be) {
300             framework.publishFrameworkEvent(FrameworkEvent.ERROR, host.getBundleHost(), be);
301             return false;
302         }
303         if (hosts == null) {
304             hosts = new BundleLoaderProxy[] {host};
305             return true;
306         }
307         for (int i = 0; i < hosts.length; i++) {
308             if (host.getBundleHost() == hosts[i].getBundleHost())
309                 return true; // already a host
310
}
311         BundleLoaderProxy[] newHosts = new BundleLoaderProxy[hosts.length + 1];
312         System.arraycopy(hosts, 0, newHosts, 0, hosts.length);
313         newHosts[newHosts.length - 1] = host;
314         hosts = newHosts;
315         return true;
316     }
317
318     protected BundleLoader getBundleLoader() {
319         // Fragments cannot have a BundleLoader.
320
return null;
321     }
322
323     /**
324      * Return the current context for this bundle.
325      *
326      * @return BundleContext for this bundle.
327      */

328     protected BundleContextImpl getContext() {
329         // Fragments cannot have a BundleContext.
330
return null;
331     }
332 }
333
Popular Tags