Search in sources :

Example 91 with BundleException

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

the class Sling method startup.

// ---------- BundleActivator ----------------------------------------------
/**
     * Called when the OSGi framework is being started. This implementation
     * registers as a service listener for the
     * <code>javax.servlet.Servlet</code> class and calls the
     * {@link #doStartBundle()} method for implementations to execute more
     * startup tasks. Additionally the <code>context</code> URL protocol
     * handler is registered.
     *
     * @param bundleContext The <code>BundleContext</code> of the system
     *            bundle of the OSGi framework.
     * @throws BundleException May be thrown if the {@link #doStartBundle()} throws.
     */
private final void startup(BundleContext bundleContext) {
    // register the context URL handler
    Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put(URLConstants.URL_HANDLER_PROTOCOL, new String[] { "context" });
    ContextProtocolHandler contextHandler = new ContextProtocolHandler(this.resourceProvider);
    bundleContext.registerService(URLStreamHandlerService.class.getName(), contextHandler, props);
    // register the platform MBeanServer
    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    Hashtable<String, Object> mbeanProps = new Hashtable<String, Object>();
    try {
        ObjectName beanName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
        AttributeList attrs = platformMBeanServer.getAttributes(beanName, new String[] { "MBeanServerId", "SpecificationName", "SpecificationVersion", "SpecificationVendor", "ImplementationName", "ImplementationVersion", "ImplementationVendor" });
        for (Object object : attrs) {
            Attribute attr = (Attribute) object;
            if (attr.getValue() != null) {
                mbeanProps.put(attr.getName(), attr.getValue().toString());
            }
        }
    } catch (Exception je) {
        logger.log(Logger.LOG_INFO, "start: Cannot set service properties of Platform MBeanServer service, registering without", je);
    }
    bundleContext.registerService(MBeanServer.class.getName(), platformMBeanServer, mbeanProps);
    bundleContext.registerService(LaunchpadContentProvider.class.getName(), resourceProvider, null);
}
Also used : URLStreamHandlerService(org.osgi.service.url.URLStreamHandlerService) LaunchpadContentProvider(org.apache.sling.launchpad.api.LaunchpadContentProvider) Attribute(javax.management.Attribute) Hashtable(java.util.Hashtable) AttributeList(javax.management.AttributeList) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 92 with BundleException

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

the class BundleInstallUpgradeExceptionRetryTest method testUpdateFailsWithMoreThanMaxRetrys.

@Test
public void testUpdateFailsWithMoreThanMaxRetrys() throws Exception {
    // install version 1.1 and fail activation with exception all attempts
    final Object listener = this.startObservingBundleEvents();
    final AtomicReference<ServiceRegistration<AtomicInteger>> ref = new AtomicReference<ServiceRegistration<AtomicInteger>>(null);
    final AtomicInteger counter = new AtomicInteger(5);
    bundleContext.addBundleListener(new SynchronousBundleListener() {

        @Override
        public void bundleChanged(org.osgi.framework.BundleEvent event) {
            if (event.getType() == org.osgi.framework.BundleEvent.STOPPED && event.getBundle().getSymbolicName().equals(symbolicName) && event.getBundle().getVersion().equals(new Version("1.0.0"))) {
                System.out.println(event.getSource() + " " + event.getType());
                Thread.dumpStack();
                if (ref.get() == null) {
                    try {
                        event.getBundle().start();
                        ref.set(bundleContext.registerService(AtomicInteger.class, counter, null));
                    } catch (BundleException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    ref.getAndSet(null).unregister();
                    if (counter.get() == 0) {
                        bundleContext.removeBundleListener(this);
                    }
                }
            }
        }
    });
    installer.updateResources(URL_SCHEME, getInstallableResource(createTestBundle(new Version("2.0.0"))), null);
    try {
        long time = 0;
        while (counter.get() >= 0 && time < 1000) {
            sleep(100);
            time += 100;
        }
        assertBundle("After installing", symbolicName, "1.0.0", Bundle.ACTIVE);
    } finally {
        if (ref.get() != null) {
            ref.get().unregister();
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(org.osgi.framework.Version) BundleException(org.osgi.framework.BundleException) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 93 with BundleException

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

the class AbstractLaunchpadStartingMojo method executeWithArtifacts.

/**
     * {@inheritDoc}
     */
@Override
protected void executeWithArtifacts() throws MojoExecutionException {
    try {
        final Map<String, String> props = new HashMap<String, String>();
        props.put(SharedConstants.SLING_HOME, slingHome);
        // ensure launchpad is set
        props.put(SharedConstants.SLING_LAUNCHPAD, slingHome);
        if (forceBundleLoad) {
            props.put(SharedConstants.FORCE_PACKAGE_BUNDLE_LOADING, "true");
        }
        // set up and configure Felix Logger
        int logLevelNum;
        if (logLevel == null) {
            logLevelNum = DEFAULT_LOG_LEVEL;
        } else {
            logLevelNum = toLogLevelInt(logLevel, DEFAULT_LOG_LEVEL);
        }
        props.put(LOG_LEVEL_PROP, String.valueOf(logLevelNum));
        // Display port number on console, in case HttpService doesn't
        getLog().info("HTTP server port: " + httpPort);
        props.put(PROP_PORT, String.valueOf(httpPort));
        // prevent tons of needless WARN from the framework
        Logger logger = new Logger();
        logger.setLogLevel(Logger.LOG_ERROR);
        if (propertiesFile.exists()) {
            File tmp = null;
            try {
                tmp = File.createTempFile("sling", "props");
                mavenFileFilter.copyFile(propertiesFile, tmp, true, project, Collections.EMPTY_LIST, true, System.getProperty("file.encoding"), mavenSession);
                Properties loadedProps = PropertyUtils.loadPropertyFile(tmp, null);
                for (Object key : loadedProps.keySet()) {
                    props.put((String) key, (String) loadedProps.get(key));
                }
            } catch (IOException e) {
                throw new MojoExecutionException("Unable to create filtered properties file", e);
            } catch (MavenFilteringException e) {
                throw new MojoExecutionException("Unable to create filtered properties file", e);
            } finally {
                if (tmp != null) {
                    tmp.delete();
                }
            }
        }
        sling = startSling(resourceProvider, props, logger);
    } catch (BundleException be) {
        getLog().error("Failed to Start OSGi framework", be);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) Logger(org.apache.felix.framework.Logger) Properties(java.util.Properties) File(java.io.File)

Example 94 with BundleException

use of org.osgi.framework.BundleException in project bnd by bndtools.

the class MiniFramework method installBundle.

@Override
public Bundle installBundle(String location, InputStream in) throws BundleException {
    Context c;
    try {
        in.close();
        try {
            @SuppressWarnings("unused") URL url = new URL(location);
        } catch (MalformedURLException e) {
            throw new BundleException("For the mini framework, the location must be a proper URL even though this is not required by the specification " + location, e);
        }
        c = new Context(this, last, ++ID, location);
        bundles.put(Long.valueOf(c.id), c);
        last = c;
        return c;
    } catch (Exception e) {
        throw new BundleException("Can't install " + location, e);
    }
}
Also used : BundleContext(org.osgi.framework.BundleContext) MalformedURLException(java.net.MalformedURLException) BundleException(org.osgi.framework.BundleException) URL(java.net.URL) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 95 with BundleException

use of org.osgi.framework.BundleException in project bndtools by bndtools.

the class OSGiJUnitLaunchDelegate method getLaunch.

// A couple of hacks to make sure the JUnit plugin is active and notices our
// launch.
@Override
public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
    // start the JUnit plugin
    try {
        Bundle jdtJUnitBundle = BundleUtils.findBundle(Plugin.getDefault().getBundleContext(), JDT_JUNIT_BSN, null);
        if (jdtJUnitBundle == null)
            throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format("Bundle \"{0}\" was not found. Cannot report JUnit results via the Workbench.", JDT_JUNIT_BSN), null));
        jdtJUnitBundle.start();
    } catch (BundleException e) {
        throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format("Error starting bundle \"{0}\". Cannot report JUnit results via the Workbench.", JDT_JUNIT_BSN), null));
    }
    // JUnit plugin ignores the launch unless attribute
    // "org.eclipse.jdt.launching.PROJECT_ATTR" is set.
    ILaunchConfigurationWorkingCopy modifiedConfig = configuration.getWorkingCopy();
    IResource launchResource = LaunchUtils.getTargetResource(configuration);
    if (launchResource != null) {
        String launchProjectName = LaunchUtils.getLaunchProjectName(launchResource);
        IProject launchProject = ResourcesPlugin.getWorkspace().getRoot().getProject(launchProjectName);
        if (!launchProject.exists()) {
            launchProjectName = launchResource.getProject().getName();
        }
        modifiedConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, launchProjectName);
    }
    return super.getLaunch(modifiedConfig.doSave(), mode);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

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