Search in sources :

Example 86 with BundleException

use of org.osgi.framework.BundleException in project sling by apache.

the class LogSupport method frameworkEvent.

// ---------- FrameworkListener --------------------------------------------
/**
     * Listens for Framework events and logs the respective events according to
     * the Log Service specification.
     * <p>
     * In the case of a Framework ERROR which is a ClassNotFoundException for an
     * unresolved bundle, the message is logged at INFO level instead of ERROR
     * level as prescribed by the spec. This is because such a situation should
     * not really result in a Framework ERROR but the Apache Felix framework has
     * no means of controlling this at the moment (framework 1.0.4 release).
     */
public void frameworkEvent(FrameworkEvent event) {
    int level = LogService.LOG_INFO;
    String message;
    Throwable exception = event.getThrowable();
    switch(event.getType()) {
        case FrameworkEvent.STARTED:
            message = "FrameworkEvent STARTED";
            break;
        case FrameworkEvent.ERROR:
            message = "FrameworkEvent ERROR";
            // resolve
            if (exception instanceof BundleException) {
                StackTraceElement[] ste = exception.getStackTrace();
                if (ste != null && ste.length > 0 && "loadBundleClass".equals(ste[0].getMethodName())) {
                    message += ": Class " + exception.getMessage() + " not found";
                    if (event.getBundle() != null) {
                        message += " in bundle " + event.getBundle().getSymbolicName() + " (" + event.getBundle().getBundleId() + ")";
                    }
                    level = LogService.LOG_INFO;
                    // don't care for a stack trace here
                    exception = null;
                    break;
                }
            }
            level = LogService.LOG_ERROR;
            break;
        case FrameworkEvent.PACKAGES_REFRESHED:
            message = "FrameworkEvent PACKAGES REFRESHED";
            break;
        case FrameworkEvent.STARTLEVEL_CHANGED:
            message = "FrameworkEvent STARTLEVEL CHANGED to " + this.startLevelService.getStartLevel();
            break;
        case FrameworkEvent.WARNING:
            message = "FrameworkEvent WARNING";
            break;
        case FrameworkEvent.INFO:
            message = "FrameworkEvent INFO";
            break;
        default:
            message = "FrameworkEvent " + event.getType();
    }
    final LogEntry entry = new LogEntryImpl(event.getBundle(), null, level, message, exception);
    fireLogEvent(entry);
}
Also used : BundleException(org.osgi.framework.BundleException) LogEntry(org.osgi.service.log.LogEntry)

Example 87 with BundleException

use of org.osgi.framework.BundleException in project sling by apache.

the class AbstractJobHandlingTest method setup.

public void setup() throws IOException {
    // set load delay to 3 sec
    final org.osgi.service.cm.Configuration c2 = this.configAdmin.getConfiguration("org.apache.sling.event.impl.jobs.jcr.PersistenceHandler", null);
    Dictionary<String, Object> p2 = new Hashtable<>();
    p2.put(JobManagerConfiguration.PROPERTY_BACKGROUND_LOAD_DELAY, 3L);
    // and startup.delay to 1sec - otherwise default of 30sec breaks tests!
    p2.put("startup.delay", 1L);
    c2.update(p2);
    // with the 'startup.delay' set to 1sec - so that ITs actually succeed
    try {
        Bundle[] bundles = bc.getBundles();
        for (Bundle bundle : bundles) {
            if (bundle.getSymbolicName().contains("sling.event")) {
                // assuming we only have 1 bundle that contains 'sling.event'
                LoggerFactory.getLogger(getClass()).info("starting bundle... " + bundle);
                bundle.start();
                break;
            }
        }
    } catch (BundleException e) {
        LoggerFactory.getLogger(getClass()).error("could not start sling.event bundle: " + e, e);
        throw new RuntimeException(e);
    }
}
Also used : Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) BundleException(org.osgi.framework.BundleException)

Example 88 with BundleException

use of org.osgi.framework.BundleException in project sling by apache.

the class BundleStartTask method execute.

/**
     * @see org.apache.sling.installer.api.tasks.InstallTask#execute(org.apache.sling.installer.api.tasks.InstallationContext)
     */
public void execute(final InstallationContext ctx) {
    // this is just a sanity check which should never be reached
    if (bundleId == 0) {
        String message = "Bundle 0 is the framework bundle, ignoring request to start it";
        this.getLogger().debug(message);
        if (this.getResource() != null) {
            this.setFinishedState(ResourceState.INSTALLED, null, message);
        }
        return;
    }
    // and another sanity check
    final Bundle b = this.getBundleContext().getBundle(bundleId);
    if (b == null) {
        String message = MessageFormat.format("Cannot start bundle, id not found: {0}", bundleId);
        this.getLogger().debug(message);
        this.setFinishedState(ResourceState.IGNORED, null, message);
        return;
    }
    if (BundleUtil.isBundleActive(b)) {
        String message = MessageFormat.format("Bundle already started, no action taken: {0}", bundleId);
        this.getLogger().debug(message);
        this.setFinishedState(ResourceState.INSTALLED, null, message);
    } else {
        // Try to start bundle, and if that doesn't work we'll need to retry
        try {
            b.start();
            this.setFinishedState(ResourceState.INSTALLED);
            ctx.log("Started bundle {}", b);
        } catch (final BundleException e) {
            this.getLogger().info("Could not start bundle {}. Reason: {}. Will retry.", new Object[] { b, e });
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException)

Example 89 with BundleException

use of org.osgi.framework.BundleException in project sling by apache.

the class SystemBundleUpdateTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final Bundle systemBundle = this.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
    // sanity check
    if (systemBundle == null) {
        this.setFinishedState(ResourceState.IGNORED, null, "Cannot update system bundle!");
        ctx.asyncTaskFailed(this);
        return;
    }
    // restart system bundle
    if (this.getResource() == null) {
        ctx.log("Refreshing system bundle.");
        this.getBundleRefresher().refreshBundles(ctx, Collections.singletonList(systemBundle), false);
    } else {
        InputStream is = null;
        try {
            is = getResource().getInputStream();
            if (is == null) {
                String message = MessageFormat.format("RegisteredResource provides null InputStream, cannot update system bundle: {0}", getResource());
                getLogger().warn(message);
                this.setFinishedState(ResourceState.IGNORED, null, message);
                ctx.asyncTaskFailed(this);
            } else {
                try {
                    systemBundle.update(is);
                } catch (final BundleException e) {
                    String message = MessageFormat.format("Updating system bundle failed due to {0} - unable to retry: {1}", e.getLocalizedMessage(), this);
                    getLogger().warn(message, e);
                    this.setFinishedState(ResourceState.IGNORED, null, message);
                    ctx.asyncTaskFailed(this);
                }
            }
        } catch (final IOException e) {
            String message = MessageFormat.format("Removing failing task due to  due to {0} - unable to retry: {1}", e.getLocalizedMessage(), this);
            this.getLogger().warn(message, e);
            this.setFinishedState(ResourceState.IGNORED, null, message);
            ctx.asyncTaskFailed(this);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (final IOException ignore) {
                }
            }
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException)

Example 90 with BundleException

use of org.osgi.framework.BundleException in project sling by apache.

the class Sling method destroy.

/**
     * Destroys this servlet by shutting down the OSGi framework and hence the
     * delegatee servlet if one is set at all.
     */
public final void destroy() {
    if (framework != null) {
        // get a private copy of the reference and remove the class ref
        Framework myFramework;
        synchronized (this) {
            myFramework = framework;
            framework = null;
        }
        // shutdown the Felix container
        if (myFramework != null) {
            logger.log(Logger.LOG_INFO, "Shutting down Apache Sling");
            try {
                myFramework.stop();
                myFramework.waitForStop(0);
            } catch (BundleException be) {
                // may be thrown by stop, log but continue
                logger.log(Logger.LOG_ERROR, "Failure initiating Framework Shutdown", be);
            } catch (InterruptedException ie) {
                // may be thrown by waitForStop, log but continue
                logger.log(Logger.LOG_ERROR, "Interrupted while waiting for the Framework Termination", ie);
            }
            logger.log(Logger.LOG_INFO, "Apache Sling stopped");
        }
    }
}
Also used : BundleException(org.osgi.framework.BundleException) Framework(org.osgi.framework.launch.Framework)

Aggregations

BundleException (org.osgi.framework.BundleException)99 Bundle (org.osgi.framework.Bundle)54 IOException (java.io.IOException)31 Test (org.junit.Test)19 File (java.io.File)15 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 FileInputStream (java.io.FileInputStream)9 BundleContext (org.osgi.framework.BundleContext)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Hashtable (java.util.Hashtable)5 Manifest (java.util.jar.Manifest)5 ServiceReference (org.osgi.framework.ServiceReference)5 Version (org.osgi.framework.Version)5 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)5 LowDiskException (android.taobao.atlas.runtime.LowDiskException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4