下面列出了org.osgi.framework.Bundle#STARTING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String toStateString(final int state) {
switch (state) {
case Bundle.UNINSTALLED:
return "UNINSTALLED";
case Bundle.INSTALLED:
return "INSTALLED";
case Bundle.RESOLVED:
return "RESOLVED";
case Bundle.STARTING:
return "STARTING";
case Bundle.STOPPING:
return "STOPPING";
case Bundle.ACTIVE:
return "ACTIVE";
default:
return Integer.toString(state);
}
}
public static Color getColor(int state) {
switch(state) {
case Bundle.INSTALLED:
return installedColor;
case Bundle.ACTIVE:
return activeColor;
case Bundle.RESOLVED:
return resolvedColor;
case Bundle.UNINSTALLED:
return uninstalledColor;
case Bundle.STARTING:
return startingColor;
case Bundle.STOPPING:
return stoppingColor;
}
return Color.black;
}
/**
* Gets the OSGi state from the string form.
*
* @param state the state as String
* @return the OSGi bundle state
*/
public static int from(String state) {
switch (state.toUpperCase()) {
case ACTIVE:
return Bundle.ACTIVE;
case INSTALLED:
return Bundle.INSTALLED;
case RESOLVED:
return Bundle.RESOLVED;
case STARTING:
return Bundle.STARTING;
case STOPPING:
return Bundle.STOPPING;
case UNINSTALLED:
return Bundle.UNINSTALLED;
default:
return -1; // Unknown.
}
}
/**
* Determine if the given bundle can and needs to be started.
*
* <ol>
* <li>A fragment bundle must never be started.
* <li>If no start-level support is present in the framework then any bundle
* in state installed, resolved or starting can be started.
* <li>A bundle that is not persistently started can be started.
* <li>A bundle that is persistently started and in any of the states
* installed, resolved, starting and assigned to a start level that is lower
* or equal to the current start level can be started.
* </ol>
*
* @param bundle
* the bundle to check.
* @return {@code true} if the bundle needs to be started.
*/
boolean startBundlePossible(final Bundle bundle)
{
final BundleRevision bRevCur = bundle.adapt(BundleRevision.class);
final boolean isFragment =
bRevCur.getTypes() == BundleRevision.TYPE_FRAGMENT;
if (isFragment) {
return false;
}
final int state = bundle.getState();
final boolean startable =
(state & (Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING)) != 0;
final BundleStartLevel bsl = bundle.adapt(BundleStartLevel.class);
if (bsl == null) {
return startable;
}
if (!bsl.isPersistentlyStarted()) {
return true;
}
return startable && bsl.getStartLevel() <= getCurrentStartLevel();
}
/**
* get the status.
*
* @param state
* the state code.
* @return the status message.
*/
private String status(final int state) {
switch (state) {
case Bundle.ACTIVE:
return "(active)";
case Bundle.INSTALLED:
return "(installed)";
case Bundle.RESOLVED:
return ("(resolved)");
case Bundle.STARTING:
return ("(starting) ");
case Bundle.STOPPING:
return ("(stopping)");
case Bundle.UNINSTALLED:
return ("(uninstalled)");
}
return null;
}
/**
* Gets the String form of the given OSGi bundle state.
*
* @param state the state
* @return the string form.
*/
public static String from(int state) {
switch (state) {
case Bundle.ACTIVE:
return ACTIVE;
case Bundle.INSTALLED:
return INSTALLED;
case Bundle.RESOLVED:
return RESOLVED;
case Bundle.STARTING:
return STARTING;
case Bundle.STOPPING:
return STOPPING;
case Bundle.UNINSTALLED:
return UNINSTALLED;
default:
return "UNKNOWN (" + Integer.toString(state) + ")";
}
}
/**
* Start SCR.
*
*/
void start() {
cmHandler.start();
bc.addBundleListener(this);
postponeCheckin();
try {
final Bundle [] bundles = bc.getBundles();
for (final Bundle bundle : bundles) {
if ((bundle.getState() & (Bundle.ACTIVE | Bundle.STARTING)) != 0) {
processBundle(bundle);
}
}
} finally {
postponeCheckout();
}
bc.registerService(ServiceComponentRuntime.class.getName(),
new ServiceComponentRuntimeImpl(this), null);
bc.registerService(org.apache.felix.scr.ScrService.class.getName(),
new ScrServiceImpl(this), null);
}
/**
* Get all bundles currently in bundle state ACTIVE.
*
* @return A List of BundleImpl.
*/
List<BundleImpl> getActiveBundles() {
checkIllegalState();
final ArrayList<BundleImpl> slist = new ArrayList<BundleImpl>();
synchronized (bundles) {
for (final Enumeration<BundleImpl> e = bundles.elements(); e.hasMoreElements();) {
final BundleImpl b = e.nextElement();
final int s = b.getState();
if (s == Bundle.ACTIVE || s == Bundle.STARTING) {
slist.add(b);
}
}
}
return slist;
}
/**
* this method checks the initial bundle that are installed/active before bundle tracker is opened.
*
* @param b
* the bundle to check
*/
private void checkInitialBundle(Bundle b) {
// If the bundle is active, check it
if (b.getState() == Bundle.RESOLVED || b.getState() == Bundle.STARTING || b.getState() == Bundle.ACTIVE) {
checkBundle(b);
}
}
void syncStartLevel(BundleImpl bs) {
try {
if (fwCtx.debug.startlevel) {
fwCtx.debug.println("syncstartlevel: " + bs);
}
synchronized (lock) {
synchronized (fwCtx.resolver) {
if (bs.getStartLevel() <= currentLevel) {
final BundleGeneration current = bs.current();
if ((bs.getState() & (Bundle.INSTALLED|Bundle.RESOLVED|Bundle.STOPPING)) != 0
&& current.archive.getAutostartSetting()!=-1) {
if (fwCtx.debug.startlevel) {
fwCtx.debug.println("startlevel: start " + bs);
}
int startOptions = Bundle.START_TRANSIENT;
if (isBundleActivationPolicyUsed(current.archive)) {
startOptions |= Bundle.START_ACTIVATION_POLICY;
}
bs.start(startOptions);
}
} else {
if ((bs.getState() & (Bundle.ACTIVE|Bundle.STARTING)) != 0) {
if (fwCtx.debug.startlevel) {
fwCtx.debug.println("startlevel: stop " + bs);
}
bs.stop(Bundle.STOP_TRANSIENT);
}
}
}
}
} catch (final Throwable t) {
fwCtx.frameworkError(bs, t);
}
}
public @Override void start(final BundleContext context) throws Exception {
if (System.getProperty("netbeans.home") != null) {
throw new IllegalStateException("Should not be run from inside regular NetBeans module system");
}
String storage = context.getProperty(Constants.FRAMEWORK_STORAGE);
if (storage != null) {
System.setProperty("netbeans.user", storage);
}
System.setProperty("TopSecurityManager.disable", "true");
NbBundle.setBranding(System.getProperty("branding.token"));
OSGiMainLookup.initialize(context);
queue = new DependencyQueue<String,Bundle>();
this.context = context;
framework = ((Framework) context.getBundle(0));
if (framework.getState() == Bundle.STARTING) {
LOG.fine("framework still starting");
final AtomicReference<FrameworkListener> frameworkListener = new AtomicReference<FrameworkListener>();
frameworkListener.set(new FrameworkListener() {
public @Override void frameworkEvent(FrameworkEvent event) {
if (event.getType() == FrameworkEvent.STARTED) {
// System.err.println("framework started");
context.removeFrameworkListener(frameworkListener.get());
context.addBundleListener(Activator.this);
processLoadedBundles();
}
}
});
context.addFrameworkListener(frameworkListener.get());
} else {
LOG.fine("framework already started");
context.addBundleListener(this);
processLoadedBundles();
}
}
private void ensureBundleStarted(String symbolicName) {
Bundle bundle = getBundle(symbolicName);
if (bundle != null) {
if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING) {
try {
bundle.start(Bundle.START_TRANSIENT);
} catch (BundleException e) {
Logger logger = LoggerFactory.getLogger("org.eclipse.orion.server.core"); //$NON-NLS-1$
logger.error("Could not start bundle " + symbolicName, e);
}
}
}
}
private String getBundleInfo (Bundle bundle) {
String state = null;
switch (bundle.getState()) {
case Bundle.UNINSTALLED: state = "UNINSTALLED"; break;
case Bundle.INSTALLED: state = "INSTALLED"; break;
case Bundle.RESOLVED: state = "RESOLVED"; break;
case Bundle.STARTING: state = "STARTING"; break;
case Bundle.STOPPING: state = "STOPPING"; break;
case Bundle.ACTIVE: state = "ACTIVE"; break;
}
return bundle.getSymbolicName() + " " + bundle.getVersion() + " ["+state+"]";
}
boolean triggersActivationCls(String name) {
if (Bundle.STOPPING != fwCtx.systemBundle.getState() && state == Bundle.STARTING
&& operation != ACTIVATING) {
String pkg = "";
final int pos = name.lastIndexOf('.');
if (pos != -1) {
pkg = name.substring(0, pos);
}
return current().isPkgActivationTrigger(pkg);
}
return false;
}
@SuppressWarnings("unchecked")
public ResourceBundleTracker(BundleContext bundleContext, LocaleProvider localeProvider) {
super(bundleContext, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE, null);
this.localeProvider = localeProvider;
pkgAdmin = (PackageAdmin) bundleContext
.getService(bundleContext.getServiceReference(PackageAdmin.class.getName()));
this.bundleLanguageResourceMap = new LinkedHashMap<Bundle, LanguageResourceBundleManager>();
}
protected boolean isBundleResolved(String bundleId) {
Bundle[] bundles = Activator.getInstance().getBundle().getBundleContext().getBundles();
Optional<Bundle> bundle = Arrays.stream(bundles).filter(b -> bundleId.equals(b.getSymbolicName())).findFirst();
return bundle.isPresent() && (bundle.get().getState() & (Bundle.RESOLVED | Bundle.STARTING) | Bundle.ACTIVE) != 0;
}
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();
}
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
updateIcon();
super.paintIcon(c, g, x, y);
Icon overlay = null;
switch (bundle.getState()) {
case Bundle.ACTIVE:
overlay = OVERLAY_ACTIVE;
break;
case Bundle.INSTALLED:
overlay = OVERLAY_INSTALLED;
break;
case Bundle.RESOLVED:
overlay = OVERLAY_RESOLVED;
break;
case Bundle.STARTING:
overlay = OVERLAY_STARTING;
break;
case Bundle.STOPPING:
overlay = OVERLAY_STOPPING;
break;
case Bundle.UNINSTALLED:
overlay = OVERLAY_UNINSTALLED;
break;
default:
}
if (overlay != null) {
final int x1 = x + (getIconWidth() - overlay.getIconWidth());
final int y1 = y + (getIconHeight() - overlay.getIconHeight());
final int w = overlay.getIconWidth();
final int h = overlay.getIconHeight();
g.setColor(Color.white);
g.fill3DRect(x1 - 1, y1 - 1, w + 2, h + 2, true);
overlay.paintIcon(c, g, x1, y1);
}
}
/**
* 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();
}
/**
* update the bundle from an input stream.
*
* @param stream
* the stream.
* @throws BundleException
* if something goes wrong.
* @see org.osgi.framework.Bundle#update(java.io.InputStream)
* @category Bundle
*/
public synchronized void update(final InputStream stream)
throws BundleException {
lastModified = System.currentTimeMillis();
try {
if (framework.SECURITY_ENABLED) {
// TODO: check AdminPermission(this, LIFECYCLE)
}
if (state == UNINSTALLED) {
throw new IllegalStateException(
"Cannot update uninstalled bundle " + toString());
}
final Revision updatedRevision = readAndProcessInputStream(stream);
boolean wasActive = false;
if (state == ACTIVE) {
// so we have to restart it after update
wasActive = true;
stop();
}
if (currentRevision.isFragment()) {
state = INSTALLED;
framework.removeFragment(currentRevision);
framework.notifyBundleListeners(BundleEvent.UPDATED, this);
} else {
updateLastModified();
if (currentRevision != null) {
// do not close here, e.g. old wirings should still be
// available
currentRevision.cleanup(false);
}
}
framework.checkForCollision(CollisionHook.UPDATING, this,
updatedRevision);
framework.symbolicName_bundles
.remove(currentRevision.getSymbolicName(), this);
currentRevision = updatedRevision;
symbolicName = currentRevision.getSymbolicName();
version = currentRevision.getVersion();
revisions.add(0, updatedRevision);
framework.symbolicName_bundles
.insert(currentRevision.getSymbolicName(), this);
if (!currentRevision.isFragment()) {
currentRevision.resolve(false);
}
framework.notifyBundleListeners(BundleEvent.UPDATED, this);
if (wasActive) {
try {
start();
} catch (final BundleException be) {
// TODO: to log
}
}
if ((framework.state != Bundle.STARTING || !framework.restart)
&& framework.state != Bundle.STOPPING) {
updateMetadata();
}
} finally {
try {
stream.close();
} catch (final IOException e) {
// ignore
}
}
}