Search in sources :

Example 66 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class InfoAction method getOsgiFramework.

String getOsgiFramework() {
    try {
        Callable<String> call = new Callable<String>() {

            @Override
            public String call() throws Exception {
                BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
                Bundle sysBundle = context.getBundle(0);
                return sysBundle.getSymbolicName() + "-" + sysBundle.getVersion();
            }
        };
        return call.call();
    } catch (Throwable t) {
        // We're not in OSGi, just safely return null
        return null;
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Callable(java.util.concurrent.Callable) BundleContext(org.osgi.framework.BundleContext)

Example 67 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class GuardingFindHookTest method mockConfigAdminBundleContext.

@SuppressWarnings({ "rawtypes", "unchecked" })
private BundleContext mockConfigAdminBundleContext(Dictionary<String, Object>... configs) throws IOException, InvalidSyntaxException {
    Configuration[] configurations = new Configuration[configs.length];
    for (int i = 0; i < configs.length; i++) {
        Configuration conf = EasyMock.createMock(Configuration.class);
        EasyMock.expect(conf.getProperties()).andReturn(configs[i]).anyTimes();
        EasyMock.expect(conf.getPid()).andReturn((String) configs[i].get(Constants.SERVICE_PID)).anyTimes();
        EasyMock.replay(conf);
        configurations[i] = conf;
    }
    ConfigurationAdmin ca = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(ca.listConfigurations("(&(service.pid=org.apache.karaf.service.acl.*)(service.guard=*))")).andReturn(configurations).anyTimes();
    EasyMock.replay(ca);
    final ServiceReference caSR = EasyMock.createMock(ServiceReference.class);
    EasyMock.replay(caSR);
    Bundle b = EasyMock.createMock(Bundle.class);
    EasyMock.expect(b.getBundleId()).andReturn(877342449L).anyTimes();
    EasyMock.replay(b);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
    EasyMock.expect(bc.createFilter(EasyMock.isA(String.class))).andAnswer(() -> FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0])).anyTimes();
    String cmFilter = "(&(objectClass=" + ConfigurationAdmin.class.getName() + ")" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
    bc.addServiceListener(EasyMock.isA(ServiceListener.class), EasyMock.eq(cmFilter));
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(bc.getServiceReferences(EasyMock.anyObject(String.class), EasyMock.eq(cmFilter))).andReturn(new ServiceReference<?>[] { caSR }).anyTimes();
    EasyMock.expect(bc.getService(caSR)).andReturn(ca).anyTimes();
    EasyMock.replay(bc);
    return bc;
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference) BundleContext(org.osgi.framework.BundleContext)

Example 68 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class GuardProxyCatalogTest method testGuardProxyCatalog.

@Test
public void testGuardProxyCatalog() throws Exception {
    Bundle b = EasyMock.createMock(Bundle.class);
    EasyMock.expect(b.getBundleId()).andReturn(9823L).anyTimes();
    EasyMock.replay(b);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
    bc.addServiceListener(EasyMock.isA(ServiceListener.class));
    EasyMock.expectLastCall().once();
    String caFilter = "(&(objectClass=org.osgi.service.cm.ConfigurationAdmin)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
    EasyMock.expect(bc.createFilter(caFilter)).andReturn(FrameworkUtil.createFilter(caFilter)).anyTimes();
    String pmFilter = "(&(objectClass=org.apache.aries.proxy.ProxyManager)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
    EasyMock.expect(bc.createFilter(pmFilter)).andReturn(FrameworkUtil.createFilter(pmFilter)).anyTimes();
    EasyMock.replay(bc);
    GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
    assertTrue("Service Tracker for ConfigAdmin should be opened", gpc.configAdminTracker.getTrackingCount() != -1);
    assertTrue("Service Tracker for ProxyManager should be opened", gpc.proxyManagerTracker.getTrackingCount() != -1);
    EasyMock.verify(bc);
    // Add some more behaviour checks to the bundle context
    EasyMock.reset(bc);
    bc.removeServiceListener(EasyMock.isA(ServiceListener.class));
    EasyMock.expectLastCall().once();
    EasyMock.replay(bc);
    gpc.close();
    assertEquals("Service Tracker for ConfigAdmin should be closed", -1, gpc.configAdminTracker.getTrackingCount());
    assertEquals("Service Tracker for ProxyManager should be closed", -1, gpc.proxyManagerTracker.getTrackingCount());
    EasyMock.verify(bc);
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 69 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class WebContainerServiceImpl method stop.

public void stop(List<Long> bundleIds) throws Exception {
    if (bundleIds != null && !bundleIds.isEmpty()) {
        for (long bundleId : bundleIds) {
            if (webEventHandler.getBundleEvents().containsKey(bundleId)) {
                WebEvent webEvent = webEventHandler.getBundleEvents().get(bundleId);
                Bundle bundle = webEvent.getBundle();
                if (bundle != null) {
                    // deploy
                    warManager.stop(bundleId);
                } else {
                    System.out.println("Bundle ID " + bundleId + " is invalid");
                    LOGGER.warn("Bundle ID {} is invalid", bundleId);
                }
            }
        }
    }
}
Also used : WebBundle(org.apache.karaf.web.WebBundle) Bundle(org.osgi.framework.Bundle) WebEvent(org.ops4j.pax.web.service.spi.WebEvent)

Example 70 with Bundle

use of org.osgi.framework.Bundle in project karaf by apache.

the class WebContainerServiceImpl method list.

public List<WebBundle> list() throws Exception {
    Bundle[] bundles = bundleContext.getBundles();
    Map<Long, WebEvent> bundleEvents = webEventHandler.getBundleEvents();
    List<WebBundle> webBundles = new ArrayList<>();
    if (bundles != null) {
        for (Bundle bundle : bundles) {
            // first check if the bundle is a web bundle
            String contextPath = bundle.getHeaders().get("Web-ContextPath");
            if (contextPath == null) {
                // this one used by pax-web but is deprecated
                contextPath = bundle.getHeaders().get("Webapp-Context");
            }
            if (contextPath == null) {
                // the bundle is not a web bundle
                continue;
            }
            WebBundle webBundle = new WebBundle();
            contextPath = contextPath.trim();
            // get the bundle name
            String name = bundle.getHeaders().get(Constants.BUNDLE_NAME);
            // if there is no name, then default to symbolic name
            name = (name == null) ? bundle.getSymbolicName() : name;
            // if there is no symbolic name, resort to location
            name = (name == null) ? bundle.getLocation() : name;
            // get the bundle version
            String version = bundle.getHeaders().get(Constants.BUNDLE_VERSION);
            name = ((version != null)) ? name + " (" + version + ")" : name;
            long bundleId = bundle.getBundleId();
            int level = bundle.adapt(BundleStartLevel.class).getStartLevel();
            if (!contextPath.startsWith("/")) {
                contextPath = "/" + contextPath;
            }
            webBundle.setBundleId(bundleId);
            webBundle.setName(name);
            webBundle.setContextPath(contextPath);
            webBundle.setLevel(level);
            webBundle.setState(getStateString(bundle));
            webBundle.setWebState(state(bundle.getBundleId()));
            webBundles.add(webBundle);
        }
    }
    return webBundles;
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) WebBundle(org.apache.karaf.web.WebBundle) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) WebBundle(org.apache.karaf.web.WebBundle) WebEvent(org.ops4j.pax.web.service.spi.WebEvent)

Aggregations

Bundle (org.osgi.framework.Bundle)2490 Test (org.junit.Test)659 URL (java.net.URL)388 BundleContext (org.osgi.framework.BundleContext)311 File (java.io.File)298 ArrayList (java.util.ArrayList)292 IOException (java.io.IOException)278 BundleException (org.osgi.framework.BundleException)270 HashMap (java.util.HashMap)149 ServiceReference (org.osgi.framework.ServiceReference)145 Path (org.eclipse.core.runtime.Path)126 Hashtable (java.util.Hashtable)124 HashSet (java.util.HashSet)95 InputStream (java.io.InputStream)94 IStatus (org.eclipse.core.runtime.IStatus)86 Status (org.eclipse.core.runtime.Status)82 List (java.util.List)80 Map (java.util.Map)74 BundleWiring (org.osgi.framework.wiring.BundleWiring)74 Version (org.osgi.framework.Version)73