1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*******************************************************************************
* Copyright (c) 2008, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.internal.composite;
import java.io.*;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import org.eclipse.osgi.framework.adaptor.BundleClassLoader;
import org.eclipse.osgi.framework.adaptor.BundleData;
import org.eclipse.osgi.framework.internal.core.Constants;
import org.eclipse.osgi.internal.loader.BundleLoader;
import org.eclipse.osgi.internal.loader.BundleLoaderProxy;
import org.eclipse.osgi.launch.Equinox;
import org.eclipse.osgi.service.internal.composite.CompositeModule;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.osgi.framework.*;
import org.osgi.framework.launch.Framework;
import org.osgi.service.framework.*;
public class CompositeImpl extends CompositeBase implements CompositeBundle {
private static String COMPOSITE_STORAGE = "store"; //$NON-NLS-1$
public static String COMPOSITE_CONFIGURATION = "compositeConfig.properties"; //$NON-NLS-1$
private final ServiceTrackerManager trackerManager = new ServiceTrackerManager();
public CompositeImpl(BundleData bundledata, org.eclipse.osgi.framework.internal.core.Framework framework) throws BundleException {
super(bundledata, framework);
}
protected Framework findCompanionFramework(org.eclipse.osgi.framework.internal.core.Framework thisFramework, BundleData thisData) throws BundleException {
// allocate storage area for the composite framework
File compositeStorage = thisData.getDataFile(COMPOSITE_STORAGE);
boolean firstTime = false;
if (!compositeStorage.exists())
// the child storage area has not been allocated; this is the first time
firstTime = true;
// find the configuration properties
URL childConfig = bundledata.getEntry(COMPOSITE_CONFIGURATION);
Properties props = new Properties();
try {
props.load(childConfig.openStream());
} catch (IOException e) {
throw new BundleException("Could not load child configuration", e); //$NON-NLS-1$
}
props.put(Constants.FRAMEWORK_STORAGE, compositeStorage.getAbsolutePath());
// save the parent framework so the parent companion bundle can find it
props.put(PROP_PARENTFRAMEWORK, thisFramework.getSystemBundleContext().getBundle());
// TODO leaks "this" out of the constructor
props.put(PROP_COMPOSITE, this);
Equinox equinox = new Equinox((Map) props);
if (!firstTime)
// if not the first time then we are done
return equinox;
equinox.init();
installSurrogate(equinox.getBundleContext(), thisData);
return equinox;
}
private void installSurrogate(BundleContext companionContext, BundleData thisData) throws BundleException {
Bundle surrogate;
try {
InputStream surrogateContent = CompositeHelper.getSurrogateInput(thisData.getManifest(), null, null);
surrogate = companionContext.installBundle(thisData.getLocation(), surrogateContent);
} catch (IOException e) {
throw new BundleException("Error installing parent companion composite bundle", e); //$NON-NLS-1$
}
// disable the surrogate initially since we know we have not resolved the composite yet.
CompositeHelper.setDisabled(true, surrogate, companionContext);
// set the permissions of the surrogate bundle
CompositeHelper.setCompositePermissions(thisData.getLocation(), companionContext);
}
private boolean updateSurrogate(BundleData thisData, BundleDescription child, ExportPackageDescription[] matchingExports) throws BundleException {
// update the surrogate content with the matching exports provided by the composite
InputStream surrogateContent;
try {
surrogateContent = CompositeHelper.getSurrogateInput(thisData.getManifest(), child, matchingExports);
} catch (IOException e) {
throw new BundleException("Error updating surrogate bundle.", e); //$NON-NLS-1$
}
CompositeModule surrogateComposite = (CompositeModule) getSurrogateBundle();
surrogateComposite.updateContent(surrogateContent);
// enable/disable the surrogate composite based on if we have exports handed to us
boolean disable = matchingExports == null ? true : false;
CompositeHelper.setDisabled(disable, getSurrogateBundle(), getCompositeFramework().getBundleContext());
// return true if we can resolve the surrogate bundle
return disable ? false : surrogateComposite.resolveContent();
}
public Framework getCompositeFramework() {
if ((getState() & Bundle.UNINSTALLED) == 0) {
if ((companionFramework.getState() & (Bundle.INSTALLED | Bundle.RESOLVED)) != 0)
try {
companionFramework.init();
} catch (BundleException e) {
throw new RuntimeException("Cannot initialize child framework.", e);
}
}
return companionFramework;
}
public SurrogateBundle getSurrogateBundle() {
return (SurrogateBundle) getCompanionBundle();
}
protected Bundle getCompanionBundle() {
return getCompositeFramework().getBundleContext().getBundle(1);
}
public void update(Map compositeManifest) throws BundleException {
// validate the composite manifest
CompositeHelper.validateCompositeManifest(compositeManifest);
if (isResolved()) {
// force the class loader creation before updating the surrogate to cache the current state
// this is to allow for lazy updates of composite bundles
BundleLoader loader = getBundleLoader();
if (loader != null)
loader.createClassLoader();
}
try {
Map frameworkConfig = getFrameworkConfig();
// first update the parent companion and disable it
updateSurrogate(getBundleData(), null, null);
// update the content with the new manifest
updateContent(CompositeHelper.getCompositeInput(frameworkConfig, compositeManifest));
} catch (IOException e) {
throw new BundleException("Error updating composite.", e); //$NON-NLS-1$
}
}
private Map getFrameworkConfig() throws IOException {
Properties result = new Properties();
URL config = getEntry(COMPOSITE_CONFIGURATION);
result.load(config.openStream());
return result;
}
public void uninstall() throws BundleException {
// ensure class loader is created if needed
checkClassLoader();
// stop first before stopping the child to let the service listener clean up
stop(Bundle.STOP_TRANSIENT);
stopChildFramework();
super.uninstall();
}
private void checkClassLoader() {
BundleLoaderProxy proxy = getLoaderProxy();
if (proxy != null && proxy.inUse() && proxy.getBundleLoader() != null) {
BundleClassLoader loader = proxy.getBundleLoader().createClassLoader();
loader.getResource("dummy"); //$NON-NLS-1$
}
}
protected void startHook() throws BundleException {
// always start the child framework
companionFramework.start();
trackerManager.startedComposite();
}
protected void stopHook() throws BundleException {
// bug 363561; need to make sure the class loader is created
// before stopping the composite framework
try {
checkClassLoader();
} catch (Throwable t) {
framework.publishFrameworkEvent(FrameworkEvent.ERROR, this, t);
}
trackerManager.stoppedComposite();
// do not stop the framework unless we are persistently stopped
if ((bundledata.getStatus() & Constants.BUNDLE_STARTED) == 0)
stopChildFramework();
}
public void started(CompositeModule surrogate) {
if (surrogate == getSurrogateBundle())
trackerManager.startedSurrogate();
}
public void stopped(CompositeModule surrogate) {
if (surrogate == getSurrogateBundle())
trackerManager.stoppedSurrogate();
}
private void stopChildFramework() throws BundleException {
companionFramework.stop();
try {
FrameworkEvent stopped = companionFramework.waitForStop(30000);
switch (stopped.getType()) {
case FrameworkEvent.ERROR :
throw new BundleException("Error stopping the child framework.", stopped.getThrowable()); //$NON-NLS-1$
case FrameworkEvent.WAIT_TIMEDOUT :
throw new BundleException("Timed out waiting for the child framework to stop."); //$NON-NLS-1$
case FrameworkEvent.STOPPED :
// normal stop, just return
return;
default :
throw new BundleException("Unexpected code returned when stopping the child framework:" + stopped.getType()); //$NON-NLS-1$
}
} catch (InterruptedException e) {
throw new BundleException("Error stopping child framework", e); //$NON-NLS-1$
}
}
public boolean giveExports(ExportPackageDescription[] matchingExports) {
if (matchingExports == null) {
SurrogateBundle surrogate = getSurrogateBundle();
// disable the surrogate
CompositeHelper.setDisabled(true, getSurrogateBundle(), getCompositeFramework().getBundleContext());
// refresh the surrogate synchronously
((CompositeModule) surrogate).refreshContent();
return true;
}
try {
return updateSurrogate(getBundleData(), getBundleDescription(), matchingExports);
} catch (BundleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/*
* Listens to source and target bundles and source and target framework changes
*/
class ServiceTrackerManager {
static final int COMPOSITE_ACTIVE = 0x01;
static final int SURROGATE_ACTIVE = 0x02;
// @GuardedBy(this)
private int bundlesActive = 0;
// @GuardedBy(this)
private CompositeServiceTracker shareToChildServices;
// @GuardedBy(this)
private CompositeServiceTracker shareToParentServices;
void startedComposite() throws BundleException {
open(COMPOSITE_ACTIVE);
getSurrogateBundle().start(Bundle.START_TRANSIENT);
}
void startedSurrogate() {
open(SURROGATE_ACTIVE);
}
void stoppedComposite() {
try {
getSurrogateBundle().stop(Bundle.STOP_TRANSIENT);
} catch (BundleException e) {
// nothing
} catch (IllegalStateException e) {
// child framework must have been stoped
}
close(COMPOSITE_ACTIVE);
}
void stoppedSurrogate() {
close(SURROGATE_ACTIVE);
}
private synchronized void open(int bundleActive) {
bundlesActive |= bundleActive;
if ((bundlesActive & (COMPOSITE_ACTIVE | SURROGATE_ACTIVE)) != (COMPOSITE_ACTIVE | SURROGATE_ACTIVE))
return;
// create a service tracker to track and share services from the parent framework
shareToChildServices = new CompositeServiceTracker(getBundleContext(), getSurrogateBundle().getBundleContext(), (String) getBundleContext().getBundle().getHeaders("").get(CompositeBundleFactory.COMPOSITE_SERVICE_FILTER_IMPORT)); //$NON-NLS-1$
shareToChildServices.open();
// create a service tracker to track and share services from the child framework
shareToParentServices = new CompositeServiceTracker(getSurrogateBundle().getBundleContext(), getBundleContext(), (String) getBundleContext().getBundle().getHeaders("").get(CompositeBundleFactory.COMPOSITE_SERVICE_FILTER_EXPORT)); //$NON-NLS-1$
shareToParentServices.open();
}
private synchronized void close(int bundleStopped) {
bundlesActive ^= bundleStopped;
// close the service tracker to stop tracking and sharing services
if (shareToChildServices != null)
shareToChildServices.close();
if (shareToParentServices != null)
shareToParentServices.close();
}
}
}
|