KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > adaptor > BundleStopper


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.runtime.adaptor;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
16 import org.eclipse.osgi.framework.internal.core.AbstractBundle;
17 import org.eclipse.osgi.framework.internal.core.BundleHost;
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.osgi.framework.Bundle;
22 import org.osgi.framework.BundleContext;
23
24 /**
25  * Implementation for the runtime shutdown hook that provides
26  * support for legacy bundles. All legacy bundles are stopped
27  * in the proper order.
28  *
29  * <p>Internal class.</p>
30  */

31 public class BundleStopper {
32     /* must be a synchronized object */
33     private Hashtable JavaDoc stoppedBundles;
34
35     private BundleDescription[] allToStop = null;
36
37     private void logCycles(Object JavaDoc[][] cycles) {
38         // log cycles
39
if (cycles.length > 0) {
40             StringBuffer JavaDoc cycleText = new StringBuffer JavaDoc("["); //$NON-NLS-1$
41
for (int i = 0; i < cycles.length; i++) {
42                 cycleText.append('[');
43                 for (int j = 0; j < cycles[i].length; j++) {
44                     cycleText.append(((BundleDescription) cycles[i][j]).getSymbolicName());
45                     cycleText.append(',');
46                 }
47                 cycleText.insert(cycleText.length() - 1, ']');
48             }
49             cycleText.setCharAt(cycleText.length() - 1, ']');
50             String JavaDoc message = EclipseAdaptorMsg.formatter.getString("ECLIPSE_BUNDLESTOPPER_CYCLES_FOUND", cycleText); //$NON-NLS-1$
51
FrameworkLogEntry entry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, message, 0, null, null);
52             EclipseAdaptor.getDefault().getFrameworkLog().log(entry);
53         }
54     }
55
56     public void stopBundles() {
57         allToStop = EclipseAdaptor.getDefault().getState().getResolvedBundles();
58         StateHelper stateHelper = EclipseAdaptor.getDefault().getPlatformAdmin().getStateHelper();
59         Object JavaDoc[][] cycles = stateHelper.sortBundles(allToStop);
60         logCycles(cycles);
61         stoppedBundles = new Hashtable JavaDoc(allToStop.length);
62         basicStopBundles();
63     }
64
65     private void basicStopBundles() {
66         BundleContext context = EclipseAdaptor.getDefault().getContext();
67         // stop all active bundles in the reverse order of Require-Bundle
68
for (int stoppingIndex = allToStop.length - 1; stoppingIndex >= 0; stoppingIndex--) {
69             AbstractBundle toStop = (AbstractBundle) context.getBundle(allToStop[stoppingIndex].getBundleId());
70             try {
71                 if (toStop.getState() != Bundle.ACTIVE || !(toStop instanceof BundleHost) || toStop.getBundleId() == 0)
72                     continue;
73                 if (!((EclipseBundleData) toStop.getBundleData()).isAutoStartable())
74                     continue;
75                 toStop.stop();
76             } catch (Exception JavaDoc e) {
77                 String JavaDoc message = EclipseAdaptorMsg.formatter.getString("ECLIPSE_BUNDLESTOPPER_ERROR_STOPPING_BUNDLE", allToStop[stoppingIndex].toString()); //$NON-NLS-1$
78
FrameworkLogEntry entry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, message, 0, e, null);
79                 EclipseAdaptor.getDefault().getFrameworkLog().log(entry);
80             } finally {
81                 stoppedBundles.put(toStop, toStop);
82             }
83         }
84     }
85
86     public boolean isStopped(Bundle bundle) {
87         if (stoppedBundles == null)
88             return false;
89         return stoppedBundles.get(bundle) != null;
90     }
91 }
Popular Tags