KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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 package org.eclipse.core.runtime.internal.adaptor;
12
13 import java.util.Hashtable JavaDoc;
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 /**
26  * Implementation for the runtime shutdown hook that provides
27  * support for legacy bundles. All legacy bundles are stopped
28  * in the proper order.
29  *
30  * <p>Internal class.</p>
31  */

32 public class BundleStopper {
33     /* must be a synchronized object */
34     private Hashtable JavaDoc 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 JavaDoc[][] cycles) {
46         if (!Debug.DEBUG || !Debug.DEBUG_ENABLED)
47             return;
48
49         // log cycles
50
if (cycles.length > 0) {
51             StringBuffer JavaDoc cycleText = new StringBuffer JavaDoc("["); //$NON-NLS-1$
52
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 JavaDoc 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 JavaDoc[][] cycles = stateHelper.sortBundles(allToStop);
71         logCycles(cycles);
72         stoppedBundles = new Hashtable JavaDoc(allToStop.length);
73         basicStopBundles();
74     }
75
76     private void basicStopBundles() {
77         stopping = true;
78         // stop all active bundles in the reverse order of Require-Bundle
79
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 a lazy started bundle was explicitly started,
88
// then make sure it keeps the persistenly started bit
89
if (!storageHook.isActivatedOnClassLoad())
90                             bundledata.setStatus(Constants.BUNDLE_STARTED);
91                     }
92                 } catch (Exception JavaDoc e) {
93                     String JavaDoc 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