1 11 package org.eclipse.core.runtime.internal.adaptor; 12 13 import java.util.Hashtable ; 14 import org.eclipse.osgi.baseadaptor.BaseData; 15 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 16 import org.eclipse.osgi.framework.debug.Debug; 17 import org.eclipse.osgi.framework.internal.core.*; 18 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 19 import org.eclipse.osgi.service.resolver.BundleDescription; 20 import org.eclipse.osgi.service.resolver.StateHelper; 21 import org.eclipse.osgi.util.NLS; 22 import org.osgi.framework.Bundle; 23 import org.osgi.framework.BundleContext; 24 25 32 public class BundleStopper { 33 34 private Hashtable stoppedBundles; 35 private BundleDescription[] allToStop = null; 36 private BundleContext context; 37 private FrameworkAdaptor adaptor; 38 private boolean stopping; 39 40 public BundleStopper(BundleContext context, FrameworkAdaptor adaptor) { 41 this.context = context; 42 this.adaptor = adaptor; 43 } 44 45 private void logCycles(Object [][] cycles) { 46 if (!Debug.DEBUG || !Debug.DEBUG_ENABLED) 47 return; 48 49 if (cycles.length > 0) { 51 StringBuffer cycleText = new StringBuffer ("["); for (int i = 0; i < cycles.length; i++) { 53 cycleText.append('['); 54 for (int j = 0; j < cycles[i].length; j++) { 55 cycleText.append(((BundleDescription) cycles[i][j]).getSymbolicName()); 56 cycleText.append(','); 57 } 58 cycleText.insert(cycleText.length() - 1, ']'); 59 } 60 cycleText.setCharAt(cycleText.length() - 1, ']'); 61 String message = NLS.bind(EclipseAdaptorMsg.ECLIPSE_BUNDLESTOPPER_CYCLES_FOUND, cycleText); 62 FrameworkLogEntry entry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, message, 0, null, null); 63 adaptor.getFrameworkLog().log(entry); 64 } 65 } 66 67 public void stopBundles() { 68 allToStop = adaptor.getState().getResolvedBundles(); 69 StateHelper stateHelper = adaptor.getPlatformAdmin().getStateHelper(); 70 Object [][] cycles = stateHelper.sortBundles(allToStop); 71 logCycles(cycles); 72 stoppedBundles = new Hashtable (allToStop.length); 73 basicStopBundles(); 74 } 75 76 private void basicStopBundles() { 77 stopping = true; 78 for (int stoppingIndex = allToStop.length - 1; stoppingIndex >= 0; stoppingIndex--) { 80 AbstractBundle toStop = (AbstractBundle) context.getBundle(allToStop[stoppingIndex].getBundleId()); 81 BaseData bundledata = (BaseData) toStop.getBundleData(); 82 EclipseStorageHook storageHook = (EclipseStorageHook) bundledata.getStorageHook(EclipseStorageHook.KEY); 83 if (toStop.getBundleId() != 0 && storageHook != null && storageHook.isAutoStartable()) { 84 try { 85 if ((toStop.getState() == Bundle.ACTIVE) && (toStop instanceof BundleHost)) { 86 toStop.stop(); 87 if (!storageHook.isActivatedOnClassLoad()) 90 bundledata.setStatus(Constants.BUNDLE_STARTED); 91 } 92 } catch (Exception e) { 93 String message = NLS.bind(EclipseAdaptorMsg.ECLIPSE_BUNDLESTOPPER_ERROR_STOPPING_BUNDLE, allToStop[stoppingIndex].toString()); 94 FrameworkLogEntry entry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, message, 0, e, null); 95 adaptor.getFrameworkLog().log(entry); 96 } finally { 97 stoppedBundles.put(toStop, toStop); 98 } 99 } 100 } 101 stopping = false; 102 } 103 104 public boolean isStopped(Bundle bundle) { 105 if (stoppedBundles == null) 106 return false; 107 return stoppedBundles.get(bundle) != null; 108 } 109 110 public boolean isStopping() { 111 return stopping; 112 } 113 } 114 | Popular Tags |