下面列出了org.osgi.framework.BundleEvent#STARTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
try {
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
if (event.getType() == BundleEvent.STARTED) {
addUIPermissionFromBundle(bundle);
}
} catch (Exception e) {
log.error("Error occured when processing component xml in bundle " +
bundle.getSymbolicName(), e);
}
}
/**
* Process bundle components when it starts.
*/
public void bundleChanged(BundleEvent event) {
postponeCheckin();
try {
final Bundle bundle = event.getBundle();
switch (event.getType()) {
case BundleEvent.LAZY_ACTIVATION:
lazy.add(bundle);
processBundle(bundle);
break;
case BundleEvent.STARTED:
if (!lazy.remove(bundle)) {
processBundle(bundle);
}
break;
case BundleEvent.STOPPING:
lazy.remove(bundle);
removeBundle(bundle, ComponentConstants.DEACTIVATION_REASON_BUNDLE_STOPPED);
break;
}
} finally {
postponeCheckout();
}
}
public synchronized void startBundle() throws BundleException {
if (this.state == BundleEvent.INSTALLED) {
throw new IllegalStateException("Cannot start uninstalled bundle "
+ toString());
} else if (this.state != BundleEvent.RESOLVED) {
if (this.state == BundleEvent.STARTED) {
resolveBundle(true);
}
this.state = BundleEvent.UPDATED;
try {
isValid = true;
this.state = BundleEvent.RESOLVED;
Framework.notifyBundleListeners(BundleEvent.STARTED, this);
if (Framework.DEBUG_BUNDLES && log.isInfoEnabled()) {
log.info("Framework: Bundle " + toString() + " started.");
}
} catch (Throwable th) {
Framework.clearBundleTrace(this);
this.state = BundleEvent.STOPPED;
String msg = "Error starting bundle " + toString();
log.error(msg,th);
}
}
}
@Override
public void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
try {
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
if (event.getType() == BundleEvent.STARTED) {
addUIPermissionFromBundle(bundle);
}
} catch (Exception e) {
log.error("Error occured when processing component xml in bundle " +
bundle.getSymbolicName(), e);
}
}
public void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
if (event.getType() == BundleEvent.RESOLVED) {
checkBundle(bundle);
} else if (event.getType() == BundleEvent.STARTED) {
checkBundleScriptEngines(bundle);
}
}
@Override
public void bundleChanged(BundleEvent bundleEvent) {
// Parse and createObject metadata when installed
Bundle bundle = bundleEvent.getBundle();
if (bundleEvent.getType() == BundleEvent.STARTED) {
checkAndLoadBundle(bundle);
} else if (bundleEvent.getType() == BundleEvent.STOPPED) {
checkAndUnloadBundle(bundle);
}
}
@Override
public void bundleChanged(BundleEvent bundleEvent) {
if (bundleEvent.getType() == BundleEvent.STARTED) {
addExtensions(bundleEvent.getBundle());
} else {
removeExtensions(bundleEvent.getBundle());
}
}
public void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
if (event.getType() == BundleEvent.RESOLVED) {
checkBundle(bundle);
} else if (event.getType() == BundleEvent.STARTED) {
checkBundleScriptEngines(bundle);
}
}
public void bundleChanged(BundleEvent bundleEvent) {
Bundle bundle = bundleEvent.getBundle();
try {
if (bundleEvent.getType() == BundleEvent.STARTED) {
addJrXmlToRegistry(bundle);
}
} catch (Exception e) {
log.error("Error occurred when putting jrXml file to registry in bundle "
+ bundle.getSymbolicName(), e);
}
}
/**
* The bundle event callback method inserts all bundle events into the
* log. Events are all assigned the log level info.
*
* @param be
* the bundle event that has occured.
*/
public void bundleChanged(BundleEvent be) {
String msg = null;
switch (be.getType()) {
case BundleEvent.INSTALLED:
msg = "BundleEvent INSTALLED";
break;
case BundleEvent.STARTED:
msg = "BundleEvent STARTED";
break;
case BundleEvent.STOPPED:
msg = "BundleEvent STOPPED";
break;
case BundleEvent.UNINSTALLED:
msg = "BundleEvent UNINSTALLED";
break;
case BundleEvent.UPDATED:
msg = "BundleEvent UPDATED";
break;
case BundleEvent.RESOLVED:
msg = "BundleEvent RESOLVED";
break;
case BundleEvent.UNRESOLVED:
msg = "BundleEvent UNRESOLVED";
break;
/* case BundleEvent.STARTING:
msg = "BundleEvent STARTING";
break;
case BundleEvent.STOPPING:
msg = "BundleEvent STOPPING";
break;
*/
}
lrsf.log(new LogEntryImpl(be.getBundle(), LogService.LOG_INFO, msg));
}
/**
* Invoked when an other module has been installed or un-installed.
* This method notifies the Apache SIS library that the classpath may have changed.
*
* @param event the event that describe the life-cycle change.
*/
@Override
public void bundleChanged(final BundleEvent event) {
switch (event.getType()) {
case BundleEvent.STARTED: {
SystemListener.fireClasspathChanged();
break;
}
case BundleEvent.STOPPED: {
SystemListener.fireClasspathChanged();
SystemListener.removeModule(event.getBundle().getSymbolicName());
break;
}
}
}
public static String bundleEventName(int type)
{
switch (type) {
case BundleEvent.INSTALLED:
return "installed";
case BundleEvent.STARTED:
return "started";
case BundleEvent.STOPPED:
return "stopped";
case BundleEvent.UNINSTALLED:
return "uninstalled";
case BundleEvent.UPDATED:
return "updated";
case BundleEvent.RESOLVED:
return "resolved";
case BundleEvent.UNRESOLVED:
return "unresolved";
case BundleEvent.STARTING:
return "starting";
case BundleEvent.STOPPING:
return "stopping";
case BundleEvent.LAZY_ACTIVATION:
return "lazyActivation";
default:
return "<" + type + ">";
}
}
private BundleEvent initializeEvent(Bundle bundle) {
switch (bundle.getState()) {
case Bundle.INSTALLED:
return new BundleEvent(BundleEvent.INSTALLED, bundle);
case Bundle.RESOLVED:
return new BundleEvent(BundleEvent.RESOLVED, bundle);
default:
return new BundleEvent(BundleEvent.STARTED, bundle);
}
}
@Override
public void setBundleStartLevel(Bundle bundle, int level) {
if (bundle == this) {
throw new IllegalArgumentException("Cannot set the start level for the system bundle.");
}
BundleImpl bundleImpl = (BundleImpl) bundle;
if (bundleImpl.state == BundleEvent.INSTALLED) {
throw new IllegalArgumentException("Bundle " + bundle + " has been uninstalled");
} else if (level <= 0) {
throw new IllegalArgumentException("Start level " + level + " is not Component valid level");
} else {
bundleImpl.currentStartlevel = level;
bundleImpl.updateMetadata();
if (level <= Framework.startlevel && bundle.getState() != BundleEvent.RESOLVED && bundleImpl.persistently) {
try {
bundleImpl.startBundle();
} catch (Throwable e) {
e.printStackTrace();
Framework.notifyFrameworkListeners(BundleEvent.STARTED, bundle, e);
}
} else if (level <= Framework.startlevel) {
} else {
if (bundle.getState() != BundleEvent.STOPPED || bundle.getState() != BundleEvent.STARTED) {
try {
bundleImpl.stopBundle();
} catch (Throwable e2) {
Framework.notifyFrameworkListeners(BundleEvent.STARTED, bundle, e2);
}
}
}
}
}
public void start(final BundleContext bundleContext) throws Exception {
/*
* Check for the services that should be deployed before this if there
* are any.
* if there are such services we wait for such bundles to deploy and
* gets the notification
* through a service listener.
*/
// first check whether there are such services and they have already
// deployed.
String pendingBundleName = null;
final List<Bundle> pendingBundles = new ArrayList<Bundle>();
for (Bundle bundle : bundleContext.getBundles()) {
pendingBundleName = (String) bundle.getHeaders().get(DEPLOY_BEFORE);
if ((pendingBundleName != null) && (pendingBundleName.equals(getName())) &&
(bundle.getState() != Bundle.ACTIVE)) {
// i.e this bundle should be started before the user manager but
// yet has not started
pendingBundles.add(bundle);
}
}
if (pendingBundles.isEmpty()) {
startDeploy(bundleContext);
} else {
BundleListener bundleListener = new BundleListener() {
public void bundleChanged(BundleEvent bundleEvent) {
synchronized (pendingBundles) {
if (bundleEvent.getType() == BundleEvent.STARTED) {
pendingBundles.remove(bundleEvent.getBundle());
if (pendingBundles.isEmpty()) {
// now start the user manager deployment
bundleContext.removeBundleListener(this);
try {
startDeploy(bundleContext);
} catch (Exception e) {
log.error("Can not start the bundle ", e);
}
}
}
}
}
};
bundleContext.addBundleListener(bundleListener);
}
}
final void notifyBundleChange(final String symbolicName, final Version version, final int action) {
final Exception stack = Netigso.LOG.isLoggable(Level.FINER) ? new Exception("StackTrace") : null;
final Runnable doLog = new Runnable() {
@Override
public void run() {
if (isEnabled(symbolicName)) {
return;
}
final Mutex mutex = Main.getModuleSystem().getManager().mutex();
if (!mutex.isReadAccess()) {
mutex.postReadRequest(this);
return;
}
String type = "" + action;
Level notify = Level.INFO;
switch (action) {
case BundleEvent.INSTALLED:
return; // no message for installed
case BundleEvent.RESOLVED:
type = "resolved";
break;
case BundleEvent.STARTED:
type = "started";
break;
case BundleEvent.STOPPED:
type = "stopped";
break;
case BundleEvent.UNINSTALLED:
return; // nothing for uninstalled
case BundleEvent.LAZY_ACTIVATION:
type = "lazy";
notify = Level.FINEST;
break;
case BundleEvent.STARTING:
type = "starting";
notify = Level.FINEST;
break;
}
Netigso.LOG.log(notify, "bundle {0}@{2} {1}", new Object[]{
symbolicName, type, version
});
if (stack != null) {
Netigso.LOG.log(Level.FINER, null, stack);
}
}
};
RP.post(doLog);
}
/**
* Scan classpaths for given package name (and sub packages) to scan for and
* register all classes with @Model annotation.
*/
public static void addModelsForPackage(BundleContext bundleContext, Class... classes) {
Bundle bundle = new ModelsPackageBundle(classes, Bundle.ACTIVE, bundleContext);
BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
MockOsgi.sendBundleEvent(bundleContext, event);
}
/**
* A listener for events sent by bundles
* @param bundleEvent The event sent by the bundle
* @author Johnny Baveras
*/
public void bundleChanged(BundleEvent bundleEvent) {
String topic = null;
boolean knownMessageType = true;
switch (bundleEvent.getType()) {
case BundleEvent.INSTALLED:
topic = INSTALLED_TOPIC;
break;
case BundleEvent.STARTED:
topic = STARTED_TOPIC;
break;
case BundleEvent.STOPPED:
topic = STOPPED_TOPIC;
break;
case BundleEvent.UPDATED:
topic = UPDATED_TOPIC;
break;
case BundleEvent.UNINSTALLED:
topic = UNINSTALLED_TOPIC;
break;
case BundleEvent.RESOLVED:
topic = RESOLVED_TOPIC;
break;
case BundleEvent.UNRESOLVED:
topic = UNRESOLVED_TOPIC;
break;
default:
/* Setting the boolean to false if an unknown event arrives */
knownMessageType = false;
break;
}
/* Stores the properties of the event in the dictionary, if the event is known */
if (knownMessageType) {
if(!Activator.handlerTracker.anyHandlersMatching(topic)) {
return;
}
Map<String,Object> props = new HashMap<String,Object>();
Bundle bundle = bundleEvent.getBundle();
putProp(props, EventConstants.EVENT, bundleEvent);
putProp(props, "bundle.id", new Long(bundle.getBundleId()));
putProp(props, EventConstants.BUNDLE_SYMBOLICNAME, bundle.getSymbolicName());
putProp(props, "bundle", bundle);
/* Tries posting the event once the properties are set */
try {
Activator.eventAdmin.postEvent(new Event(topic, props));
} catch (Exception e) {
Activator.log.error("EXCEPTION in bundleChanged()", e);
}
} else {
/* Logs an error if the event, which arrived, were of an unknown type */
Activator.log.error("Recieved unknown bundle event message (type="
+bundleEvent.getType() +"), discarding");
}
// }
}
@Override
@SuppressLint({"NewApi"})
public void bundleChanged(BundleEvent bundleEvent) {
switch (bundleEvent.getType()) {
case BundleEvent.LOADED:
loaded(bundleEvent.getBundle());
break;
case BundleEvent.INSTALLED:
installed(bundleEvent.getBundle());
break;
case BundleEvent.STARTED:
if (isLewaOS()) {
if (Looper.myLooper() == null) {
Looper.prepare();
}
started(bundleEvent.getBundle());
} else if (Framework.isFrameworkStartupShutdown()) {
BundleStartTask bundleStartTask = new BundleStartTask();
if (VERSION.SDK_INT > 11) {
bundleStartTask.executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR,
bundleEvent.getBundle());
return;
}
bundleStartTask
.execute(bundleEvent.getBundle());
} else {
started(bundleEvent.getBundle());
}
break;
case BundleEvent.STOPPED:
stopped(bundleEvent.getBundle());
break;
case BundleEvent.UPDATED:
updated(bundleEvent.getBundle());
break;
case BundleEvent.UNINSTALLED:
{
uninstalled(bundleEvent.getBundle());
break;
}
default:
}
}
BundleImpl(File bundleDir, String location,
InputStream archiveInputStream, File archiveFile, boolean isInstall)
throws BundleException, IOException {
this.persistently = false;
this.domain = null;
this.registeredFrameworkListeners = null;
this.registeredBundleListeners = null;
long currentTimeMillis = System.currentTimeMillis();
this.location = location;
this.currentStartlevel = Framework.startlevel;
this.bundleDir = bundleDir;
if (archiveInputStream != null) {
// try {
this.archive = new BundleArchive(location, bundleDir, archiveInputStream);
// } catch (Throwable e) {
// Framework.deleteDirectory(bundleDir);
// throw new BundleException("Could not install bundle " + location, e);
// }
} else if (archiveFile != null) {
try {
this.archive = new BundleArchive(location, bundleDir, archiveFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.state = BundleEvent.STARTED;
updateMetadata();
if (isInstall) {
Framework.bundles.put(location, this);
resolveBundle(false);
Framework.notifyBundleListeners(BundleEvent.INSTALLED, this);
}
if (Framework.DEBUG_BUNDLES && log.isInfoEnabled()) {
log.info("Framework: Bundle " + toString() + " created. "
+ (System.currentTimeMillis() - currentTimeMillis) + " ms");
}
}