下面列出了org.osgi.framework.BundleException#STATECHANGE_ERROR 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Adds an bundle as an extension.
* TODO, check that bootclasspath extensions really are on the
* bootclasspath.
*/
void attachExtension(BundleGeneration extension) throws BundleException {
if (extension.isBootClassPathExtension()) {
// if we attach during startup, we assume that bundle is in BCP.
if (getClassLoader() == null) {
current().attachFragment(extension, false);
} else {
throw new BundleException("Bootclasspath extension can not be dynamicly activated", BundleException.UNSUPPORTED_OPERATION);
}
} else {
if (extensions.containsKey(extension.bundle)) {
throw new BundleException("Framework extension updates can not be resolved", BundleException.STATECHANGE_ERROR);
}
try {
addClassPathURL(new URL("file:" + extension.archive.getJarLocation()));
} catch (final Exception e) {
throw new BundleException("Framework extension could not be dynamicly activated", BundleException.UNSUPPORTED_OPERATION, e);
}
current().attachFragment(extension, false);
extensions.put(extension.bundle, handleExtensionActivator(extension));
}
}
public BundleImpl(final Concierge framework,
final BundleContext installingContext, final String location,
final long bundleId, final InputStream stream)
throws BundleException {
this.framework = framework;
this.location = location;
this.bundleId = bundleId;
this.startlevel = framework.initStartlevel;
this.lastModified = System.currentTimeMillis();
if (framework.SECURITY_ENABLED) {
try {
final PermissionCollection permissions = new Permissions();
permissions.add(new FilePermission(
framework.STORAGE_LOCATION + bundleId,
"read,write,execute,delete"));
domain = new ProtectionDomain(
new CodeSource(
new URL("file:" + framework.STORAGE_LOCATION
+ bundleId),
(java.security.cert.Certificate[]) null),
permissions);
} catch (final Exception e) {
e.printStackTrace();
throw new BundleException("Exception while installing bundle",
BundleException.STATECHANGE_ERROR, e);
}
}
this.storageLocation = framework.STORAGE_LOCATION + bundleId
+ File.separatorChar;
currentRevision = readAndProcessInputStream(stream);
symbolicName = currentRevision.getSymbolicName();
version = currentRevision.getVersion();
revisions.add(0, currentRevision);
// check is same version is already installed
framework.checkForCollision(CollisionHook.INSTALLING,
installingContext.getBundle(), currentRevision);
install();
// if we are not during startup (in case of restart) or shutdown, update the metadata
if ((framework.state != Bundle.STARTING || !framework.restart)
&& framework.state != Bundle.STOPPING) {
updateMetadata();
}
}
/**
* start the bundle with options.
*
* @throws BundleException
* if the bundle cannot be resolved or the Activator throws an
* exception.
* @see org.osgi.framework.Bundle#start(int)
* @category Bundle
*/
public void start(final int options) throws BundleException {
if (framework.SECURITY_ENABLED) {
// TODO: check AdminPermission(this, EXECUTE)
}
if (state == UNINSTALLED) {
throw new IllegalStateException(
"Cannot start uninstalled bundle " + toString());
}
if (currentRevision.isFragment()) {
throw new BundleException(
"The fragment bundle " + toString() + " cannot be started",
BundleException.INVALID_OPERATION);
}
if (!lazyActivation
&& (state == Bundle.STARTING || state == Bundle.STOPPING)) {
try {
synchronized (this) {
wait(TIMEOUT);
}
} catch (final InterruptedException ie) {
// ignore and proceed
}
if (state == Bundle.STARTING || state == Bundle.STOPPING) {
// hit the timeout
throw new BundleException(
"Timeout occurred. Bundle was unable to start.",
BundleException.STATECHANGE_ERROR);
}
}
if ((options & Bundle.START_TRANSIENT) > 0) {
if (startlevel > framework.startlevel) {
throw new BundleException(
"Bundle (with start level " + startlevel
+ ") cannot be started due to the framework's current start level of "
+ framework.startlevel,
BundleException.START_TRANSIENT_ERROR);
}
} else {
if ((options & Bundle.START_TRANSIENT) == 0) {
autostart = options == 0 ? AUTOSTART_STARTED_WITH_EAGER
: AUTOSTART_STARTED_WITH_DECLARED;
}
updateMetadata();
}
if (startlevel <= framework.startlevel) {
activate(options);
}
}
/**
* stop the bundle.
*
* @throws BundleException
* if the bundle has been uninstalled before.
*
* @param options
* for stopping the bundle.
* @see org.osgi.framework.Bundle#stop(int)
* @category Bundle
*/
public void stop(final int options) throws BundleException {
if (framework.SECURITY_ENABLED) {
// TODO: check AdminPermission(this, EXECUTE);
}
if (state == UNINSTALLED) {
throw new IllegalStateException(
"Cannot stop uninstalled bundle " + toString());
}
if (currentRevision.isFragment()) {
throw new BundleException(
"The fragment bundle " + toString() + " cannot be stopped",
BundleException.INVALID_OPERATION);
}
if (state == Bundle.STARTING || state == Bundle.STOPPING) {
try {
synchronized (this) {
wait(TIMEOUT);
}
} catch (final InterruptedException ie) {
// ignore
}
if (state == UNINSTALLED) {
throw new IllegalStateException(
"Cannot stop uninstalled bundle " + toString());
}
if (state == Bundle.STARTING || state == Bundle.STOPPING) {
// timeout occurred!
throw new BundleException(
"Timeout occurred. Bundle was unable to stop!",
BundleException.STATECHANGE_ERROR);
}
}
if (options != Bundle.STOP_TRANSIENT) {
// change persistent autostart configuration
autostart = AUTOSTART_STOPPED;
updateMetadata();
}
if (state != ACTIVE && state != STARTING) {
return;
}
stopBundle();
}
/**
* the actual starting happens here. This method does not modify the
* persistent meta data.
*
* @throws BundleException
* if the bundle has been uninstalled before.
*/
synchronized void stopBundle() throws BundleException {
if (state == INSTALLED) {
return;
}
final int oldState = state;
// step 5
state = STOPPING;
// step 6
framework.notifyBundleListeners(BundleEvent.STOPPING, this);
try {
if (oldState == ACTIVE) {
if (currentRevision.activatorInstance != null) {
currentRevision.activatorInstance.stop(context);
}
if (state == UNINSTALLED) {
throw new BundleException(
"Activator.stop() uninstalled this bundle!",
BundleException.ACTIVATOR_ERROR);
}
}
} catch (final Throwable t) {
throw new BundleException("Error stopping bundle " + toString(),
BundleException.STATECHANGE_ERROR, t);
} finally {
if (currentRevision != null
&& currentRevision.activatorInstance != null) {
currentRevision.activatorInstance = null;
}
framework.clearBundleTrace(this);
state = RESOLVED;
framework.notifyBundleListeners(BundleEvent.STOPPED, this);
if (framework.DEBUG_BUNDLES) {
framework.logger.log(LogService.LOG_INFO,
"framework: Bundle " + toString() + " stopped.");
}
if (context != null) {
context.isValid = false;
}
context = null;
synchronized (this) {
notify();
}
}
}
/**
* Wait for an ongoing operation to finish.
*
* @param lock Object used for locking.
* @param src Caller to include in exception message.
* @param longWait True, if we should wait extra long before aborting.
* @throws BundleException if the ongoing (de-)activation does not finish
* within reasonable time.
*/
void waitOnOperation(Object lock, final String src, boolean longWait) throws BundleException {
if (operation != IDLE) {
long left = longWait ? 20000 : 500;
final long waitUntil = Util.timeMillis() + left;
do {
try {
lock.wait(left);
if (operation == IDLE) {
return;
}
} catch (final InterruptedException _ie) {
}
left = waitUntil - Util.timeMillis();
} while (left > 0);
String op;
switch (operation) {
case IDLE:
// Should not happen!
return;
case ACTIVATING:
op = "start";
break;
case DEACTIVATING:
op = "stop";
break;
case RESOLVING:
op = "resolve";
break;
case UNINSTALLING:
op = "uninstall";
break;
case UNRESOLVING:
op = "unresolve";
break;
case UPDATING:
op = "update";
break;
default:
op = "unknown operation";
break;
}
throw new BundleException(src + " called during " + op + " of Bundle#"
+ id, BundleException.STATECHANGE_ERROR);
}
}