下面列出了org.osgi.framework.Bundle#INSTALLED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @see org.osgi.service.packageadmin.PackageAdmin#getRequiredBundles(java.lang.String)
*/
public RequiredBundle[] getRequiredBundles(final String symbolicName) {
final Bundle[] bundles = context.getBundles();
final ArrayList<RequiredBundle> result = new ArrayList<RequiredBundle>();
for (final Bundle bundle : bundles) {
if (bundle.getState() == Bundle.INSTALLED || bundle.getState() == Bundle.UNINSTALLED) {
continue;
}
final BundleRevision rev = bundle.adapt(BundleRevision.class);
if (isFragment(rev)) {
continue;
}
if (symbolicName == null || symbolicName.equals(rev.getSymbolicName())) {
result.add(new RequiredBundleImpl(rev));
}
}
return toArrayOrNull(result, RequiredBundle.class);
}
/**
* 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;
}
/**
* @see org.osgi.framework.Bundle#loadClass(java.lang.String)
* @category Bundle
*/
public Class<?> loadClass(final String name) throws ClassNotFoundException {
if (state == Bundle.UNINSTALLED) {
throw new IllegalStateException("Bundle is uninstalled");
}
// is this actually a fragment?
if (currentRevision.isFragment()) {
throw new ClassNotFoundException(
"This bundle is a fragment and cannot load any classes.");
}
if (state == Bundle.INSTALLED) {
try {
currentRevision.resolve(true);
} catch (final BundleException be) {
framework.notifyFrameworkListeners(FrameworkEvent.ERROR, this,
be);
throw new ClassNotFoundException(name, be);
}
}
return currentRevision.classloader.findClass(name);
}
/**
* @see org.osgi.framework.Bundle#findEntries(String, String, boolean)
* @category Bundle
*/
public Enumeration<URL> findEntries(final String path,
final String filePattern, final boolean recurse) {
// try to resolve bundle if state is installed
if (state == Bundle.INSTALLED) {
try {
currentRevision.resolve(false);
} catch (final BundleException ex) {
// ignore and search only in this bundles jar file
}
}
final Revision revision = currentRevision == null
? (Revision) revisions.get(0) : currentRevision;
return revision.findEntries(path, filePattern, recurse);
}
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 );
}
}
/** Returns bundle state as readable string. */
protected String getBundleStateAsString(final int state) {
switch (state) {
case Bundle.INSTALLED:
return "INSTALLED";
case Bundle.RESOLVED:
return "RESOLVED";
case Bundle.ACTIVE:
return "ACTIVE";
case Bundle.STARTING:
return "STARTING";
case Bundle.STOPPING:
return "STOPPING";
case Bundle.UNINSTALLED:
return "UNINSTALLED";
default:
return "UNKNOWN state: " + state;
}
}
public void start () throws ServletException, NamespaceException
{
final BundleContext bundleContext = FrameworkUtil.getBundle ( DispatcherServletInitializer.class ).getBundleContext ();
this.context = Dispatcher.createContext ( bundleContext );
this.errorHandler = new ErrorHandlerTracker ();
Dictionary<String, String> initparams = new Hashtable<> ();
final MultipartConfigElement multipart = Servlets.createMultiPartConfiguration ( PROP_PREFIX_MP );
final DispatcherServlet servlet = new DispatcherServlet ();
servlet.setErrorHandler ( this.errorHandler );
this.webContainer.registerServlet ( servlet, "dispatcher", new String[] { "/" }, initparams, 1, false, multipart, this.context );
this.proxyFilter = new FilterTracker ( bundleContext );
this.webContainer.registerFilter ( this.proxyFilter, new String[] { "/*" }, null, null, this.context );
initparams = new Hashtable<> ();
initparams.put ( "filter-mapping-dispatcher", "request" );
this.webContainer.registerFilter ( new BundleFilter (), new String[] { "/bundle/*" }, null, initparams, this.context );
this.jspTracker = new BundleTracker<> ( bundleContext, Bundle.INSTALLED | Bundle.ACTIVE, new JspBundleCustomizer ( this.webContainer, this.context ) );
this.jspTracker.open ();
}
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);
}
}
/**
* Get bundle state as a constant length string. Show state left adjusted as
* 12 character string.
*
* @param bundle
* the bundle
* @return The bundles state
*/
public static String showState(Bundle bundle)
{
switch (bundle.getState()) {
case Bundle.INSTALLED:
return "installed ";
case Bundle.RESOLVED:
return "resolved ";
case Bundle.STARTING:
return "starting ";
case Bundle.ACTIVE:
return "active ";
case Bundle.STOPPING:
return "stopping ";
case Bundle.UNINSTALLED:
return "uninstalled ";
default:
return "ILLEGAL <" + bundle.getState() + "> ";
}
}
/**
* 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();
}
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);
}
}
static Bundle getBundle(String symbolicName) {
PackageAdmin packageAdmin = packageAdminTracker.getService();
if (packageAdmin == null)
return null;
Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
if (bundles == null)
return null;
// Return the first bundle that is not installed or uninstalled
for (int i = 0; i < bundles.length; i++) {
if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundles[i];
}
}
return null;
}
static private Bundle getBundle(String symbolicName) {
PackageAdmin packageAdmin = packageAdminTracker.getService();
if (packageAdmin == null)
return null;
Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
if (bundles == null)
return null;
// Return the first bundle that is not installed or uninstalled
for (int i = 0; i < bundles.length; i++) {
if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundles[i];
}
}
return null;
}
private boolean startBundle(String bundleId)
{
PackageAdmin packageAdmin = (PackageAdmin) getService(getContext(), PackageAdmin.class.getName());
if (packageAdmin == null)
return false;
Bundle[] bundles = packageAdmin.getBundles(bundleId, null);
if (bundles != null && bundles.length > 0)
{
for (int i = 0; i < bundles.length; i++)
{
try
{
if ((bundles[0].getState() & Bundle.INSTALLED) == 0)
{
bundles[0].start();
return true;
}
}
catch (BundleException e)
{
// failed, try next bundle
}
}
}
return false;
}
/** Checks about Bundle INSTALLED state. */
protected void assertBundleInstalled(final Bundle bundle) {
if (bundle.getState() == Bundle.INSTALLED) {
// all fine
} else {
Assert.assertThat(
"Bundle " + bundle.getSymbolicName()
+ " needs to be INSTALLED",
getBundleStateAsString(bundle.getState()),
is(getBundleStateAsString(Bundle.INSTALLED)));
}
}
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);
}
}
private void install() throws BundleException {
// register bundle with framework:
synchronized (framework) {
framework.bundles.add(this);
framework.bundleID_bundles.put(new Long(getBundleId()), this);
framework.symbolicName_bundles
.insert(currentRevision.getSymbolicName(), this);
framework.location_bundles.put(location, this);
}
this.state = Bundle.INSTALLED;
// resolve if it is a framework extension
if (currentRevision.isExtensionBundle()) {
if(currentRevision.resolve(false)){
// call start for extension activator before notifying framework of resolve
if(currentRevision.extActivatorClassName != null){
try {
@SuppressWarnings("unchecked")
final Class<BundleActivator> activatorClass = (Class<BundleActivator>) framework.systemBundleClassLoader.loadClass(currentRevision.extActivatorClassName);
if (activatorClass == null) {
throw new ClassNotFoundException(currentRevision.extActivatorClassName);
}
currentRevision.extActivatorInstance = activatorClass.newInstance();
currentRevision.extActivatorInstance.start(framework.context);
} catch(Throwable err){
currentRevision.extActivatorInstance = null;
framework.notifyFrameworkListeners(FrameworkEvent.ERROR, BundleImpl.this,
new BundleException("Error calling extension bundle start(): " + this.toString(),
BundleException.ACTIVATOR_ERROR, err));
}
}
}
}
// we are just installing the bundle, if it is
// possible, resolve it, if not, wait until the
// exports are really needed (i.e., they become critical)
// if (!currentRevision.isFragment()) {
// currentRevision.resolve(false);
// }
}
public StringBuffer bundleInfo(Bundle[] targets)
{
final StringBuffer sb = new StringBuffer();
startFont(sb);
final Set<Bundle> closure =
new TreeSet<Bundle>(Util.bundleIdComparator);
for (final Bundle target : targets) {
int state = target.getState();
if (state != Bundle.INSTALLED && state != Bundle.UNINSTALLED ) {
closure.addAll(Util.getClosure(target, null));
} else {
sb.append("Bundle #");
sb.append(target.getBundleId());
sb.append(" is in ");
sb.append(state == Bundle.INSTALLED ? "INSTALLED" : "UNINSTALLED");
sb.append(" state, closure not available");
return sb;
}
}
boolean containsUninstalled = false;
if (closure.size() == 0) {
sb.append("No dependencies");
} else {
sb.append("<b>Dependencies via capabilities and services</b><br>");
for (final Bundle depB : closure) {
sb.append(" ");
if (depB.getState() != Bundle.UNINSTALLED) {
Util.bundleLink(sb, depB);
} else {
sb.append("<b>UNINSTALLED</b>, ");
sb.append(Util.getBundleName(depB));
containsUninstalled = true;
}
sb.append("<br>");
}
}
sb.append("<br>");
// Add xarsg info if we seem to be running knopflerfish
if (targets.length > 0
&& (-1 != targets[0].getClass().getName().indexOf("knopflerfish"))) {
if (!containsUninstalled) {
final String xargs =
Util.getXARGS(null, closure).toString();
sb.append("<hr>");
startFont(sb);
sb.append("<b>Suggested startup .xargs file</b><br>\n");
sb.append("</font>");
sb.append("<pre>");
sb.append("<font size=\"-2\">");
// sb.append(Text.replace(xargs, "\n", "<br>"));
sb.append(xargs);
sb.append("</font>");
sb.append("</pre>");
} else {
sb.append("<hr>");
startFont(sb);
sb.append("<b>Suggested startup .xargs not available when closure contains uninstalled bundles</b><br>\n");
sb.append("</font>");
}
}
sb.append("</font>");
return sb;
}
@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);
}
}
/**
* Determine if the given bundle can and needs to be resolved.
*
* <ol>
* <li>A bundle in state installed can be resolved.
* </ol>
*
* @param bundle
* the bundle to check.
* @return {@code true} if the bundle needs to be resolved.
*/
boolean resolveBundlePossible(final Bundle bundle)
{
final int state = bundle.getState();
return (state & Bundle.INSTALLED) != 0;
}