KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > BundleImpl


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26
27 package com.opensugar.cube;
28
29 import com.opensugar.cube.java2.ManifestHelper;
30 import com.opensugar.cube.packageAdmin.ExportedPackageImpl;
31 import com.opensugar.cube.packageAdmin.PackageImport;
32
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleException;
35 import org.osgi.framework.ServiceReference;
36 import org.osgi.framework.BundleEvent;
37 import org.osgi.framework.FrameworkEvent;
38 import org.osgi.framework.BundleActivator;
39 import org.osgi.framework.PackagePermission;
40 import org.osgi.framework.ServicePermission;
41 import org.osgi.framework.Constants;
42
43 import java.io.InputStream JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.FileInputStream JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.util.Dictionary JavaDoc;
49 import java.util.Hashtable JavaDoc;
50 import java.net.URL JavaDoc;
51 import java.net.MalformedURLException JavaDoc;
52 import java.util.zip.ZipFile JavaDoc;
53 import java.util.Vector JavaDoc;
54
55 public class BundleImpl implements Bundle {
56
57    public int getState() {
58       return state;
59    }
60
61    public void start() throws BundleException {
62       getCube().checkAdminPermission();
63
64       // If the bundle in uninstalled, an illegal state exception is thrown
65
if ( getState() == UNINSTALLED ) {
66          throw new IllegalStateException JavaDoc( "Cannot start a bundle after it has been uninstalled" );
67       }
68
69       // If the bundle is starting or stopping, wait for the bundle to change state
70
// before continuing. If this does not occur in a reasonable time, throw a
71
// bundle exception
72
if ( getState() == STARTING || getState() == STOPPING ) {
73          if ( !waitUntilStartedOrStopped() ) {
74             throw new BundleException( "Cannot stop a bundle that is stuck in starting or stopping mode" );
75          }
76       }
77
78       // If the bundle is active return immediately
79
if ( getState() == ACTIVE ) {
80          return;
81       }
82
83       // If the bundle is not resolved, attempt to resolve it. If it cannot be
84
// resolved, throw a bundle exception
85
if ( getState() != RESOLVED ) {
86          // This should have no effect because packages are processed every time
87
// a bundle is installed or updated.
88
// Do it anyway since the spec says so.
89
getCube().getPackageAdmin().resolveBundles();
90          if ( getState() != RESOLVED ) {
91             PackageImport[] unsatisfiedImports = getCube().getPackageAdmin().getUnsatisfiedImports( this );
92
93             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
94             String JavaDoc version;
95             for ( int i = 0; i < unsatisfiedImports.length; i++ ) {
96                sb.append( unsatisfiedImports[ i ].getName() );
97                version = unsatisfiedImports[ i ].getSpecificationVersion();
98                if ( version != null ) {
99                   sb.append( " " + version );
100                }
101                if ( i < unsatisfiedImports.length - 1 ) {
102                   sb.append( ", " );
103                }
104             }
105             throw new BundleException( "Cannot start a bundle that is not resolved. Missing packages: " + sb.toString() );
106          }
107       }
108
109       // Set the state to STARTING
110
state = STARTING;
111
112       // If a bundle activator is specified call its start method
113
// If an error occurs, set the state back to RESOLVED, remove bundle listeners,
114
// unregister services registered, release services used, and throw a bundle
115
// exception
116
bundleContext = new BundleContextImpl( cube, this );
117       try {
118          String JavaDoc bundleActivatorName = (String JavaDoc)headers.get( "Bundle-Activator" );
119          if ( bundleActivatorName != null ) {
120             Class JavaDoc bundleActivatorClass = classLoader.loadClass( bundleActivatorName.trim() );
121             bundleActivator = (BundleActivator)bundleActivatorClass.newInstance();
122             bundleActivator.start( bundleContext );
123          }
124       }
125       catch( Throwable JavaDoc e ) {
126          state = RESOLVED;
127          bundleActivator = null;
128          // Remove bundle listeners, unregister services registered, release services used
129
cleanUp();
130          throw new BundleException( "Exception occured in the bundle activator's start method", e );
131       }
132
133       // Record that this bundle has been started, so it gets restarted automatically
134
// next time the framework is started.
135
try {
136          getCube().registerBundleAsStarted( getBundleId() );
137       }
138       catch( IOException JavaDoc e ) {
139          getCube().fireFrameworkEvent( FrameworkEvent.ERROR, this, e );
140       }
141
142       // Set the state to ACTIVE
143
state = ACTIVE;
144
145       // Broadcast bundle started event
146
getCube().fireBundleEvent( BundleEvent.STARTED, this );
147    }
148
149    public void stop() throws BundleException {
150       stop( false );
151    }
152
153    public void update() throws BundleException, SecurityException JavaDoc {
154       update( null );
155    }
156
157    public void update( InputStream JavaDoc in ) throws BundleException, SecurityException JavaDoc {
158       // in can be null
159

160       getCube().checkAdminPermission();
161
162       // If the bundle is uninstalled, an illegal state exception is thrown
163
if ( getState() == UNINSTALLED ) {
164          throw new IllegalStateException JavaDoc( "Cannot update a bundle after it has been uninstalled" );
165       }
166
167       // Record whether or not this bundle is started, so we know if we need to
168
// restart it after it has been updated
169
boolean restart = false;
170       if ( getState() == ACTIVE ) {
171          restart = true;
172       }
173
174       // If the bundle is active or starting, it is stopped using the stop() method
175
// If the stop() method throws an exception, the exception is rethrown and this
176
// method exits
177
if ( getState() == ACTIVE || getState() == STARTING ) {
178          stop();
179       }
180
181       // Determine location of the version of bundle
182
String JavaDoc tempLocation = (String JavaDoc)headers.get( "Bundle-UpdateLocation" );
183       if ( tempLocation == null || tempLocation.trim().length() == 0 ) {
184          tempLocation = location;
185       }
186
187       // Install new version of bundle
188
// If an exception occurs while installing new version, restore original version
189
Throwable JavaDoc exceptionWhileInstalling = null;
190       String JavaDoc oldLocation = location;
191       File JavaDoc oldBundleJarFile = bundleJarFile;
192       File JavaDoc newBundleJarFile = getCube().getNewBundleJarFile( id );
193       try {
194          // Retreive new jar file and store in bundle directory
195
// Close input stream even if exception is thrown
196
if ( in == null ) {
197             URL JavaDoc url = new URL JavaDoc( tempLocation );
198             in = url.openStream();
199          }
200
201          Util.transferData( in, new FileOutputStream JavaDoc( newBundleJarFile ) );
202
203          // Uninitialize bundle first so that class loader can be closed
204
uninitializeBundle();
205
206          // Re-initialize bundle
207
initializeBundle( tempLocation, getCube().getCurrentBundleJarFile( id ) );
208       }
209       catch( Throwable JavaDoc e ) {
210          exceptionWhileInstalling = e;
211          // Restore original version (bundle has not been uninitialized, so old values still work)
212
if ( getCube().deleteBundleJarFile( newBundleJarFile ) ) {
213             try {
214                initializeBundle( oldLocation, oldBundleJarFile );
215             }
216             catch( Throwable JavaDoc e2 ) {
217                throw new BundleException( "Fatal bundle error: failure to restore original bundle version after unsuccessful bundle update attempt", e );
218             }
219          }
220          else {
221             throw new BundleException( "Fatal bundle error: failure to delete jar file that was created during unsuccessful bundle update attempt", e );
222          }
223       }
224
225       // Set the state to INSTALLED
226
state = INSTALLED;
227
228       // If the new version was successfully installed, broadcast a bundle updated
229
// event
230
if ( exceptionWhileInstalling == null ) {
231          getCube().fireBundleEvent( BundleEvent.UPDATED, this );
232       }
233
234       getCube().getPackageAdmin().resolveBundles();
235
236       // If the bundle was originally active, start the updated bundle using the
237
// start() method (the bundle will be resolved if it can in the start method)
238
// If the start method throws an exception, broadcast a framework error event
239
if ( restart ) {
240          try {
241             start();
242          }
243          catch( Throwable JavaDoc e ) {
244             getCube().fireFrameworkErrorEvent( this, e );
245          }
246       }
247
248 // getCube().unresolveBundles();
249
// getCube().resolveBundles();
250

251       // Throw a bundle exception if an error occured while attempting to install
252
// new version of bundle
253
if ( exceptionWhileInstalling != null ) {
254          throw new BundleException( "Exception occured while attempting to install new version of bundle. Original version was restored.", exceptionWhileInstalling );
255       }
256    }
257
258    public void uninstall() throws BundleException {
259       getCube().checkAdminPermission();
260
261       // If the bundle is uninstalled, an illegal state exception is thrown
262
if ( getState() == UNINSTALLED ) {
263          throw new IllegalStateException JavaDoc( "Cannot uninstall bundle that is already uninstalled" );
264       }
265
266       // If the bundle is active or starting, stop it using the stop() method
267
// If the stop() method throws an exception, broadcast framework error event
268
if ( getState() == ACTIVE || getState() == STARTING ) {
269          try {
270             stop();
271          }
272          catch( Throwable JavaDoc e ) {
273             getCube().fireFrameworkErrorEvent( this, e );
274          }
275       }
276
277       // Uninitialize bundle
278
try {
279          uninitializeBundle();
280       }
281       catch( IOException JavaDoc e ) {
282          getCube().fireFrameworkEvent( FrameworkEvent.ERROR, this, e );
283       }
284
285       try {
286          getCube().setBundleUninstalled( id );
287       }
288       catch ( IOException JavaDoc e ) {
289          getCube().fireFrameworkEvent( FrameworkEvent.ERROR, this, e );
290       }
291       getCube().bundleUninstalled( id );
292
293       // Broadcast bundle uninstalled event
294
getCube().fireBundleEvent( BundleEvent.UNINSTALLED, this );
295
296       // Set the state to UNINSTALLED
297
state = UNINSTALLED;
298    }
299
300    public Dictionary JavaDoc getHeaders() {
301       getCube().checkAdminPermission();
302       return headers;
303    }
304
305    public long getBundleId() {
306       return id;
307    }
308
309    public String JavaDoc getLocation() {
310       getCube().checkAdminPermission();
311       return getLocationWithoutAdminPermissionCheck();
312    }
313
314    // used only for cube to do debug logging, so that bundle location can be included
315
// in operation logs, even if the code being executed does not have AdminPermission,
316
// which is normally required to get the bundle location;
317
public String JavaDoc getLocationWithoutAdminPermissionCheck() {
318       return location.trim();
319    }
320
321    public ServiceReference[] getRegisteredServices() {
322       if ( getState() == UNINSTALLED ) {
323          throw new IllegalStateException JavaDoc( "Cannot get services registered by a bundle after it has been uninstalled" );
324       }
325
326       ServiceRegistrationImpl[] serviceRegistrations = getCube().getServiceRegistry().getServicesRegisteredBy( this );
327       if ( serviceRegistrations == null || serviceRegistrations.length == 0 ) {
328          return null;
329       }
330
331       Vector JavaDoc tmp = new Vector JavaDoc();
332       ServiceReference serviceReference;
333       for ( int i = 0; i < serviceRegistrations.length; i++ ) {
334          serviceReference = serviceRegistrations[ i ].getReference();
335          try {
336             getCube().checkServicePermissionForAtLeastOne( (String JavaDoc[])serviceReference.getProperty( "objectClass" ), ServicePermission.GET );
337             tmp.addElement( serviceReference );
338          }
339          catch( SecurityException JavaDoc ignore ) {}
340       }
341
342       if ( tmp.size() == 0 ) {
343          return null;
344       }
345       ServiceReference[] ret = new ServiceReference[ tmp.size() ];
346       tmp.copyInto( ret );
347       return ret;
348    }
349
350    public ServiceReference[] getServicesInUse() {
351       if ( getState() == UNINSTALLED ) {
352          throw new IllegalStateException JavaDoc( "Cannot get services used by a bundle after it has been uninstalled" );
353       }
354
355       ServiceReference[] serviceReferences = getCube().getServiceRegistry().getServicesUsedBy( this );
356       if ( serviceReferences == null || serviceReferences.length == 0 ) {
357          return null;
358       }
359
360       Vector JavaDoc tmp = new Vector JavaDoc();
361       for ( int i = 0; i < serviceReferences.length; i++ ) {
362          try {
363             getCube().checkServicePermissionForAtLeastOne( (String JavaDoc[])serviceReferences[ i ].getProperty( "objectClass" ), ServicePermission.GET );
364             tmp.addElement( serviceReferences[ i ] );
365          }
366          catch( SecurityException JavaDoc ignore ) {}
367       }
368
369       if ( tmp.size() == 0 ) {
370          return null;
371       }
372       ServiceReference[] ret = new ServiceReference[ tmp.size() ];
373       tmp.copyInto( ret );
374       return ret;
375    }
376
377    public boolean hasPermission( Object JavaDoc permission ) {
378       Util.checkNullParameter( permission, getClass().getName(), "hasPermission", "permission" );
379
380       if ( getState() == UNINSTALLED ) {
381          throw new IllegalStateException JavaDoc( "Cannot check a bundle's permissions after it has been uninstalled" );
382       }
383
384       return getCube().hasPermission( this, permission );
385    }
386
387    public int compareTo( Object JavaDoc obj ) {
388       Util.checkNullParameter( obj, getClass().getName(), "compareTo", "obj" );
389
390       if ( obj instanceof Bundle ) {
391          Bundle bundleToCompareTo = (Bundle)obj;
392          return (int)( getBundleId() - bundleToCompareTo.getBundleId() );
393       }
394       throw new IllegalArgumentException JavaDoc( "Object to compare to must be of type org.osgi.framework.Bundle" );
395    }
396
397    public URL JavaDoc getResource( String JavaDoc name ) throws IllegalStateException JavaDoc {
398       if ( getState() == UNINSTALLED ) {
399          throw new IllegalStateException JavaDoc( "Cannot get resource after bundle has been uninstalled" );
400       }
401
402       try {
403          getCube().checkAdminPermission();
404       }
405       catch( SecurityException JavaDoc e ) {
406          return null;
407       }
408
409 /*
410 // ???
411 // look for resource using current class loader
412 // if not found using current class loader, try getting resource using old class loaders
413 URL url = classLoader.getResource( name );
414 if ( oldClassLoaders != null ) {
415    int i = 0;
416    while( url == null && i < oldClassLoaders.size() ) {
417       url = ( (BundleClassLoader)oldClassLoaders.elementAt( i ) ).getResource( name );
418    }
419 }
420 return url;
421 */

422
423       // shared packages should only be searched if this bundle is resolved
424
// this is ok because when classloader tries to search shared packages, package admin
425
// will always return null if the bundle is not resolved
426
return classLoader.getResource( name );
427    }
428
429    public NamedPropertySet[] getExportedPackages() {
430       return exportedPackages;
431    }
432
433    public NamedPropertySet[] getExplicitlyImportedPackages() {
434       return explicitlyImportedPackages;
435    }
436
437 // *****************************************************************************
438

439    // Independant of bundle updates
440
private AbstractCube cube;
441    private long id;
442
443    // Change when bundle is updated
444
private String JavaDoc location;
445    private File JavaDoc bundleJarFile;
446    private Dictionary JavaDoc headers;
447    private NamedPropertySet[] exportedPackages;
448    private NamedPropertySet[] explicitlyImportedPackages;
449    private BundleClassLoader classLoader;
450
451    // changes when the bundle is stopped
452
private BundleContextImpl bundleContext;
453
454    private int state;
455    private BundleActivator bundleActivator;
456
457    protected BundleImpl( AbstractCube cube, long id, String JavaDoc location, File JavaDoc bundleJarFile ) throws IOException JavaDoc, ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
458       this.cube = cube;
459       this.id = id;
460
461       // initialize bundle context (so that things can be done before the bundle
462
// is started - getDataFile() for instance)
463
bundleContext = new BundleContextImpl( cube, this );
464
465       initializeBundle( location, bundleJarFile );
466    }
467
468    public BundleContextImpl getBundleContext() {
469       return bundleContext;
470    }
471
472    // Called by package admin
473
public void setResolved() {
474       if ( getState() != INSTALLED ) {
475          throw new RuntimeException JavaDoc( "Cannot resolve a bundle that is not in the installed state" );
476       }
477
478       state = RESOLVED;
479    }
480
481    // Called by package admin
482
public void setInstalled() {
483       if ( getState() != RESOLVED ) {
484          throw new RuntimeException JavaDoc( "Cannot unresolve a bundle that is not in the resolved state" );
485       }
486       state = INSTALLED;
487
488       // Broadcast bundle installed event
489
getCube().fireBundleEvent( BundleEvent.INSTALLED, this );
490    }
491
492    public BundleClassLoader getClassLoader() {
493       return classLoader;
494    }
495
496    public AbstractCube getCube() {
497       return cube;
498    }
499
500    public void stop( boolean keepRegisteredAsStarted ) throws BundleException {
501       getCube().checkAdminPermission();
502
503       // If the bundle is uninstalled, an illegal state exception is thrown
504
if ( getState() == UNINSTALLED ) {
505          throw new IllegalStateException JavaDoc( "Cannot stop a bundle after it has been uninstalled" );
506       }
507
508       // If the bundle is starting or stopping, wait for the bundle to change state
509
// before continuing. If this does not occur in a reasonable time, throw a
510
// bundle exception
511
if ( getState() == STARTING || getState() == STOPPING ) {
512          if ( !waitUntilStartedOrStopped() ) {
513             throw new BundleException( "Cannot stop a bundle that is stuck in starting or stopping mode" );
514          }
515       }
516
517       // If the bundle is not active return immediately
518
if ( getState() != ACTIVE ) {
519          return;
520       }
521
522       // Set the state to STOPPING
523
state = STOPPING;
524
525       // If keepRegisteredAsStarted flag is false, record that this bundle has been
526
// stopped, so it does not restart automatically next time the framework is started.
527
if ( !keepRegisteredAsStarted ) {
528          if ( !getCube().unregisterBundleAsStarted( this ) ) {
529             getCube().fireFrameworkErrorEvent( this, new Exception JavaDoc( "Bundle could not unregister as started bundle" ) );
530          }
531       }
532
533       // If a bundle activator is specified call its stop method
534
// If an error occurs, continue this method and throw a bundle exception
535
// after performing the rest of this method
536
Throwable JavaDoc exceptionWhileStopping = null;
537       try {
538          if ( bundleActivator != null ) {
539             bundleActivator.stop( bundleContext );
540          }
541       }
542       catch( Throwable JavaDoc e ) {
543          exceptionWhileStopping = e;
544       }
545
546       // Unregister service/bundle/framework listeners,
547
// unregister services registered
548
// and release services used
549
cleanUp();
550
551       // Set the state to RESOLVED
552
state = RESOLVED;
553
554       // Broadcast bundle stopped event
555
getCube().fireBundleEvent( BundleEvent.STOPPED, this );
556
557       // Disable bundle context (so that anyone who has kept a reference on it will
558
// get an IllegalStateException when calling methods)
559
bundleContext.setStopped();
560
561       // Throw a bundle exception if an error occured in the stop method of the bundle
562
// activator
563
if ( exceptionWhileStopping != null ) {
564          throw new BundleException( "Exception occured in the bundle activator's stop method", exceptionWhileStopping );
565       }
566    }
567
568 // *****************************************************************************
569

570    private boolean waitUntilStartedOrStopped() {
571       int reasonableTime = getCube().getReasonableTimeToWait();
572       long stopTime = System.currentTimeMillis() + reasonableTime;
573       int sleepTime = reasonableTime / 10;
574       while ( ( getState() == STARTING || getState() == STOPPING ) && System.currentTimeMillis() < stopTime ) {
575          try {
576             Thread.sleep( sleepTime );
577          }
578          catch ( InterruptedException JavaDoc ignore ) {}
579       }
580       if ( getState() != STARTING && getState() != STOPPING ) {
581          return true;
582       }
583       else {
584          return false;
585       }
586    }
587
588    // Called when the bundle is created or updated
589
private void initializeBundle( String JavaDoc location, File JavaDoc bundleJarFile ) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
590       this.location = location;
591       this.bundleJarFile = bundleJarFile;
592
593       // Initialize headers
594
ZipFile JavaDoc archive = new ZipFile JavaDoc( bundleJarFile );
595       ManifestHelper manifest = new ManifestHelper( archive );
596       headers = new BundleHeaders( manifest.getMainAttributes() );
597       archive.close();
598
599       // Cannot create instance of bundle activator or even load bundle activator class
600
// here because that might require classes that are not available yet if the bundle
601
// is not resolved
602
// That is done in in the start method, when we are sure the bundle is resolved
603
bundleActivator = null;
604
605       // Set state to "installed"
606
state = INSTALLED;
607
608       // Resolve classpath and native code dependencies if any
609
NamedPropertySet[] classPath = NamedPropertySet.createNamedPropertySets( (String JavaDoc)headers.get( Constants.BUNDLE_CLASSPATH ) );
610       NamedPropertySet[] nativeLibraries = NamedPropertySet.createNamedPropertySets( (String JavaDoc)headers.get( Constants.BUNDLE_NATIVECODE ) );
611       classLoader = getCube().createClassLoaderForBundle( this, bundleJarFile, nativeLibraries, classPath );
612
613       // Register package imports and exports if any
614
exportedPackages = NamedPropertySet.createNamedPropertySets( (String JavaDoc)headers.get( Constants.EXPORT_PACKAGE ) );
615       if ( exportedPackages != null ) {
616          getCube().checkPackagePermission( exportedPackages, PackagePermission.EXPORT, classLoader.getCodeSource() );
617       }
618       else {
619          exportedPackages = new NamedPropertySet[ 0 ];
620       }
621       explicitlyImportedPackages = NamedPropertySet.createNamedPropertySets( (String JavaDoc)headers.get( Constants.IMPORT_PACKAGE ) );
622       if ( explicitlyImportedPackages != null ) {
623          getCube().checkPackagePermission( explicitlyImportedPackages, PackagePermission.IMPORT, classLoader.getCodeSource() );
624       }
625       else {
626          explicitlyImportedPackages = new NamedPropertySet[ 0 ];
627       }
628
629       // exported packages are the implicitly imported packages
630
// and the explicitly exported packages
631
//
632
// the implicitly imported packages are the exported packages,
633
// except when an exported package is imported in a different version than exported
634
// (it can make sense to import a lower version that exported, I don't know if it
635
// makes sense to import a higher version than exported)
636
Vector JavaDoc vec = new Vector JavaDoc();
637       for ( int i = 0; i < explicitlyImportedPackages.length; i++ ) {
638          vec.addElement( explicitlyImportedPackages[ i ] );
639       }
640       for ( int i = 0; i < exportedPackages.length; i++ ) {
641          boolean explicitlyExported = false;
642          for ( int j = 0; j < explicitlyImportedPackages.length; j++ ) {
643             if ( exportedPackages[ i ].getName().equals( explicitlyImportedPackages[ j ].getName() ) ) {
644                explicitlyExported = true;
645             }
646          }
647          if ( !explicitlyExported ) {
648             vec.addElement( exportedPackages[ i ] );
649          }
650       }
651       NamedPropertySet[] importedPackages = new NamedPropertySet[ vec.size() ];
652       vec.copyInto( importedPackages );
653
654       for ( int i = 0; i < exportedPackages.length; i++ ) {
655          getCube().getPackageAdmin().exportPackage( this, exportedPackages[ i ].getName(), (String JavaDoc)exportedPackages[ i ].getProperty( Constants.PACKAGE_SPECIFICATION_VERSION ), classLoader );
656       }
657       for ( int i = 0; i < importedPackages.length; i++ ) {
658          getCube().getPackageAdmin().importPackage( this, importedPackages[ i ].getName(), (String JavaDoc)importedPackages[ i ].getProperty( Constants.PACKAGE_SPECIFICATION_VERSION ) );
659       }
660    }
661
662    // Called when the bundle is uninstalled or in first phase of bundle update
663
private void uninitializeBundle() throws IOException JavaDoc {
664       // no need to call cleanUp() because stop() must have been called before we get here.
665

666       bundleJarFile = null;
667       bundleActivator = null;
668
669       getCube().getPackageAdmin().bundleUpdatedOrUninstalled( this );
670    }
671
672    // Called when the bundle stops
673
private void cleanUp() {
674       // remove bundle listeners
675
getCube().removeServiceListenersRegisteredBy( this );
676       getCube().removeBundleListenersRegisteredBy( this );
677       getCube().removeFrameworkListenersRegisteredBy( this );
678
679       // unregister services registered
680
ServiceRegistrationImpl[] serviceRegistrations = getCube().getServiceRegistry().getServicesRegisteredBy( this );
681       if ( serviceRegistrations != null ) {
682          for ( int i = 0; i < serviceRegistrations.length; i++ ) {
683             serviceRegistrations[ i ].unregister();
684          }
685       }
686
687       // release services used
688
// could call getCube().getServiceRegistry().ungetAllServices( bundle )
689
ServiceReferenceImpl[] serviceReferences = getCube().getServiceRegistry().getServicesUsedBy( this );
690       if ( serviceReferences != null ) {
691          for ( int i = 0; i < serviceReferences.length; i++ ) {
692             getCube().getServiceRegistry().ungetService( serviceReferences[ i ], this );
693          }
694       }
695    }
696 /*
697    protected void eagerlyRemove() throws IOException {
698       // unregister exported packages
699       getCube().getPackageRegistry().unregisterExportedPackages( this );
700       // see if any bundles need to be unresolved since we removed exported packages
701       getCube().unresolveBundles();
702
703       // clean up class loader
704       classLoader.cleanUp();
705       classLoader = null;
706
707       // clean up old class loaders
708       if ( oldClassLoaders != null ) {
709          for ( int i = 1; i < oldClassLoaders.size(); i++ ) {
710             ( (BundleClassLoader)oldClassLoaders.elementAt( i ) ).cleanUp();
711          }
712          oldClassLoaders = null;
713       }
714    }
715 */

716 }
Popular Tags