下面列出了org.osgi.framework.Bundle#RESOLVED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String makeState ( 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";
default:
return Integer.toString ( state );
}
}
/**
* 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();
}
public void registerExistingBundles(BundleContext context) throws IOException {
for (Bundle bundle : context.getBundles()) {
if ((bundle.getState() == Bundle.RESOLVED
|| bundle.getState() == Bundle.STARTING
|| bundle.getState() == Bundle.ACTIVE
|| bundle.getState() == Bundle.STOPPING)
&& bundle.getBundleId() != context.getBundle().getBundleId()) {
register(bundle);
}
}
}
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public final synchronized void open() {
final OpenState openState = withLock(lockOpenState.writeLock(), () -> {
if (this.openState == OpenState.CREATED) {
this.openState = OpenState.OPENED;
}
return this.openState;
});
if (openState != OpenState.OPENED) {
logger.warn("Open XML document bundle tracker forbidden (state: {})", openState);
return;
}
relevantBundlesTracker = new BundleTracker(context,
Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE, null) {
@Override
public @Nullable Object addingBundle(@NonNullByDefault({}) Bundle bundle,
@NonNullByDefault({}) BundleEvent event) {
return withLock(lockOpenState.readLock(),
() -> openState == OpenState.OPENED && isBundleRelevant(bundle) ? bundle : null);
}
};
relevantBundlesTracker.open();
super.open();
}
public @Override boolean isEnabled() {
switch (b.getState()) {
case Bundle.RESOLVED:
case Bundle.ACTIVE:
case Bundle.STARTING:
case Bundle.STOPPING:
return true;
default:
return false;
}
}
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);
}
}
}
}
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);
}
}
static private void ensureBundleStarted(String name) throws BundleException {
Bundle bundle = getBundle(name);
if (bundle != null) {
if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING) {
bundle.start(Bundle.START_TRANSIENT);
}
}
}
public TagLibTracker ( final BundleContext context, String mappedPrefix )
{
this.systemTlds.add ( "org.apache.taglibs.standard-impl" );
if ( mappedPrefix == null )
{
mappedPrefix = "/WEB-INF/";
}
this.mappedPrefix = mappedPrefix;
this.bundleTracker = new BundleTracker<> ( context, Bundle.RESOLVED | Bundle.ACTIVE, this.customizer );
this.bundleTracker.open ();
}
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+"]";
}
/**
* 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);
}
}
@Override
public Representation put(final Representation value,
final Variant variant) {
try {
final Bundle bundle = getBundleFromKeys(RestService.BUNDLE_ID_KEY);
if (bundle == null) {
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return null;
}
final BundleStatePojo targetState = fromRepresentation(value,
value.getMediaType());
if (bundle.getState() == Bundle.UNINSTALLED) {
return ERROR(Status.CLIENT_ERROR_PRECONDITION_FAILED, "target state "
+ targetState.getState() + " not reachable from the current state");
} else if (targetState.getState() == Bundle.ACTIVE) {
bundle.start(targetState.getOptions());
return getRepresentation(
new BundleStatePojo(bundle.getState()), variant);
} else if (targetState.getState() == Bundle.RESOLVED) {
bundle.stop(targetState.getOptions());
return getRepresentation(
new BundleStatePojo(bundle.getState()), variant);
} else {
return ERROR(Status.CLIENT_ERROR_BAD_REQUEST, "target state "
+ targetState.getState() + " not supported");
}
} catch (final Exception e) {
return ERROR(e, variant);
}
}
/**
* Sweeps the OSGi container to find the first {@link Bundle} that can load the class.
*
* @param name The classname.
* @return The loaded class.
*/
protected Class<?> sweepContainer(String name) {
Class<?> cls = null;
Bundle[] bundles = bundle.getBundleContext().getBundles();
int bundleIdx = 0;
for (; bundleIdx < bundles.length; bundleIdx++) {
Bundle b = bundles[bundleIdx];
// Skip bundles that haven't reached RESOLVED state; skip fragments.
if (b.getState() <= Bundle.RESOLVED || b.getHeaders().get(Constants.FRAGMENT_HOST) != null)
continue;
try {
cls = b.loadClass(name);
break;
}
catch (ClassNotFoundException ignored) {
// No-op.
}
}
if (cls == null)
nonResolvable.add(name);
else
resolved.put(name, bundles[bundleIdx]);
return cls;
}
void triggerActivation() {
try {
activate0();
} catch (final BundleException be) {
// see spec 4.4.6.2 lazy activation policy
state = Bundle.STOPPING;
framework.notifyBundleListeners(BundleEvent.STOPPING, this);
state = Bundle.RESOLVED;
framework.notifyBundleListeners(BundleEvent.STOPPED, this);
}
}
/**
* This method start a thread that stop this Framework, stopping all started
* bundles.
*
* <p>
* If the framework is not started, this method does nothing. If the framework
* is started, this method will:
* <ol>
* <li>Set the state of the FrameworkContext to <i>inactive</i>.</li>
* <li>Suspended all started bundles as described in the
* {@link Bundle#stop(int)} method except that the persistent state of the
* bundle will continue to be started. Reports any exceptions that occur
* during stopping using <code>FrameworkErrorEvents</code>.</li>
* <li>Disable event handling.</li>
* </ol>
* </p>
*
*/
void shutdown(final boolean restart) {
synchronized (lock) {
boolean wasActive = false;
switch (state) {
case Bundle.INSTALLED:
case Bundle.RESOLVED:
shutdownDone(false);
break;
case Bundle.ACTIVE:
wasActive = true;
// Fall through
case Bundle.STARTING:
if (shutdownThread == null) {
try {
final boolean wa = wasActive;
shutdownThread = new Thread(fwCtx.threadGroup, "Framework shutdown") {
@Override
public void run() {
shutdown0(restart, wa);
}
};
shutdownThread.setDaemon(false);
shutdownThread.start();
} catch (final Exception e) {
systemShuttingdownDone(new FrameworkEvent(FrameworkEvent.ERROR, this, e));
}
}
break;
case Bundle.STOPPING:
// Shutdown already inprogress
break;
}
}
}
@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>();
}
/**
* Start the installed bundle.
*/
private void startBundle(final Bundle bundle) {
// ignore if bundle is already in the starting state or later
if ((bundle.getState() & (Bundle.INSTALLED | Bundle.RESOLVED)) != 0) {
try {
log.debug("Starting {}/{}", bundle.getSymbolicName(), bundle.getVersion());
bundle.start();
log.debug("Started {}/{}", bundle.getSymbolicName(), bundle.getVersion());
}
catch (BundleException e) {
log.warn("Problem starting {}", bundle.getLocation(), e);
}
}
}
public Extender(BundleContext context) {
Extender.context = context;
this.engineServiceTracker = new ServiceTracker(context, ProcessEngine.class.getName(), this);
this.bundleTracker = new BundleTracker(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE, this);
}
/**
* set the current startlevel but does not update the metadata.
*
* @param targetLevel
* the startlevel.
*
*/
protected void setLevel(final Bundle[] bundleArray, final int targetLevel,
final boolean all) {
if (startlevel == targetLevel) {
return;
}
final boolean up = targetLevel > startlevel;
final int levels = up ? targetLevel - startlevel
: startlevel - targetLevel;
final MultiMap<Integer, AbstractBundle> startLevels = new MultiMap<Integer, AbstractBundle>(
0);
// prepare startlevels
for (int i = 0; i < bundleArray.length; i++) {
final AbstractBundle bundle = (AbstractBundle) bundleArray[i];
if (bundle == Concierge.this || bundle.state == Bundle.UNINSTALLED
|| up && bundle.autostart == AUTOSTART_STOPPED
|| !up && bundle.state == Bundle.RESOLVED) {
continue;
}
final int offset;
if (up) {
offset = bundle.startlevel - startlevel - 1;
} else {
offset = startlevel - bundle.startlevel;
}
if (offset >= 0 && offset < levels) {
startLevels.insert(new Integer(offset), bundle);
}
}
for (int i = 0; i < levels; i++) {
if (up) {
startlevel++;
} else {
startlevel--;
}
final List<AbstractBundle> list = startLevels.get(new Integer(i));
if (list == null) {
continue;
}
final BundleImpl[] toProcess = list
.toArray(new BundleImpl[list.size()]);
for (int j = 0; j < toProcess.length; j++) {
try {
if (up) {
// transient is implicit
toProcess[j]
.activate(toProcess[j].isActivationPolicyUsed()
? Bundle.START_ACTIVATION_POLICY : 0);
} else {
if (toProcess[toProcess.length - j - 1]
.getState() == Bundle.UNINSTALLED) {
continue;
}
// transient is implicit
toProcess[toProcess.length - j - 1].stopBundle();
}
} catch (final BundleException be) {
if (be.getNestedException() != null) {
be.getNestedException().printStackTrace();
}
be.printStackTrace();
notifyFrameworkListeners(FrameworkEvent.ERROR,
up ? toProcess[j]
: toProcess[toProcess.length - j - 1],
be);
} catch (final Throwable t) {
t.printStackTrace();
notifyFrameworkListeners(FrameworkEvent.ERROR,
up ? toProcess[j]
: toProcess[toProcess.length - j - 1],
t);
}
}
}
startlevel = targetLevel;
}
/** Checks about Bundle RESOLVED or ACTIVE state. */
protected boolean isBundleResolved(final Bundle bundle) {
return ((bundle.getState() == Bundle.RESOLVED)
|| (bundle.getState() == Bundle.ACTIVE));
}