Search in sources :

Example 51 with Bundle

use of org.osgi.framework.Bundle in project opennms by OpenNMS.

the class Activator method createAndRegisterVaadinResourceServlet.

private void createAndRegisterVaadinResourceServlet() {
    Bundle vaadin = null;
    for (Bundle bundle : bundleContext.getBundles()) {
        if ("com.vaadin.client-compiled".equals(bundle.getSymbolicName())) {
            vaadin = bundle;
            break;
        }
    }
    Dictionary<String, String> props;
    props = new Hashtable<String, String>();
    props.put("alias", VaadinResourceServlet.VAADIN);
    HttpServlet vaadinResourceServlet = new VaadinResourceServlet(vaadin);
    resourceService = bundleContext.registerService(Servlet.class.getName(), vaadinResourceServlet, props);
    bundleContext.registerService(VaadinResourceService.class.getName(), vaadinResourceServlet, null);
}
Also used : Bundle(org.osgi.framework.Bundle) HttpServlet(javax.servlet.http.HttpServlet) VaadinResourceServlet(org.opennms.vaadin.extender.internal.servlet.VaadinResourceServlet) VaadinResourceService(org.opennms.vaadin.extender.VaadinResourceService)

Example 52 with Bundle

use of org.osgi.framework.Bundle in project azure-tools-for-java by Microsoft.

the class SDKJarsFilter method getClasspathEntriesOfAzureLibabries.

public IClasspathEntry[] getClasspathEntriesOfAzureLibabries(IPath containerPath) {
    String sdkID = "com.microsoft.azuretools.sdk";
    Bundle bundle = Platform.getBundle(sdkID);
    // Search the available SDKs
    Bundle[] bundles = Platform.getBundles(sdkID, null);
    List<IClasspathEntry> listEntries = new ArrayList<IClasspathEntry>();
    if (bundles != null) {
        for (Bundle bundle2 : bundles) {
            if (bundle2.getVersion().toString().startsWith(containerPath.segment(1))) {
                bundle = bundle2;
                break;
            }
        }
        // Get the SDK jar.
        URL sdkJar = FileLocator.find(bundle, new Path("azure-1.0.0.jar"), null);
        URL resSdkJar = null;
        IClasspathAttribute[] attr = null;
        try {
            if (sdkJar != null) {
                resSdkJar = FileLocator.resolve(sdkJar);
            // create classpath attribute for java doc, if present
            }
            if (resSdkJar == null) {
                /*
					 * if sdk jar is not present then create an place holder for
					 * sdk jar so that it would be shown as missing file
					 */
                URL bundleLoc = new URL(bundle.getLocation());
                StringBuffer strBfr = new StringBuffer(bundleLoc.getPath());
                strBfr.append(File.separator).append("azure-1.0.0.jar");
                URL jarLoc = new URL(strBfr.toString());
                IPath jarPath = new Path(FileLocator.resolve(jarLoc).getPath());
                File jarFile = jarPath.toFile();
                listEntries.add(JavaCore.newLibraryEntry(new Path(jarFile.getAbsolutePath()), null, null, null, attr, true));
            } else {
                File directory = new File(resSdkJar.getPath());
                // create the library entry for sdk jar
                listEntries.add(JavaCore.newLibraryEntry(new Path(directory.getAbsolutePath()), null, null, null, attr, true));
                FilenameFilter sdkJarsFilter = new SDKJarsFilter();
                File[] jars = new File(String.format("%s%s%s", directory.getParent(), File.separator, Messages.depLocation)).listFiles(sdkJarsFilter);
                for (int i = 0; i < jars.length; i++) {
                    listEntries.add(JavaCore.newLibraryEntry(new Path(jars[i].getAbsolutePath()), null, null, null, attr, true));
                }
            }
        } catch (Exception e) {
            listEntries = new ArrayList<IClasspathEntry>();
            Activator.getDefault().log(Messages.excp, e);
        }
    }
    IClasspathEntry[] entries = new IClasspathEntry[listEntries.size()];
    // Return the classpath entries.
    return listEntries.toArray(entries);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URL(java.net.URL) CoreException(org.eclipse.core.runtime.CoreException) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Example 53 with Bundle

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

the class Activator method requiresCdiExtender.

private boolean requiresCdiExtender(Bundle bundle) {
    BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
    List<BundleWire> requiredBundleWires = bundleWiring.getRequiredWires(EXTENDER_NAMESPACE);
    for (BundleWire bundleWire : requiredBundleWires) {
        Map<String, Object> attributes = bundleWire.getCapability().getAttributes();
        if (attributes.containsKey(EXTENDER_NAMESPACE) && attributes.get(EXTENDER_NAMESPACE).equals(CDI_CAPABILITY_NAME)) {
            Bundle providerWiringBundle = bundleWire.getProviderWiring().getBundle();
            if (providerWiringBundle.equals(_bundleContext.getBundle())) {
                return true;
            }
        }
    }
    return false;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 54 with Bundle

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

the class ConfigurationTests method testConfiguration.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testConfiguration() throws Exception {
    Bundle tb3Bundle = installBundle("tb3.jar");
    Configuration configurationA = null, configurationB = null;
    try {
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + "))");
        ServiceTracker<CdiContainer, CdiContainer> containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceReference<CdiContainer> serviceReference = containerTracker.getServiceReference();
        assertNotNull(serviceReference);
        assertEquals(CdiEvent.Type.WAITING_FOR_CONFIGURATIONS, serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE));
        configurationA = configurationAdmin.getConfiguration("configA", "?");
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("ports", new int[] { 12, 4567 });
        configurationA.update(properties);
        configurationB = configurationAdmin.getConfiguration("configB", "?");
        properties = new Hashtable<>();
        properties.put("color", "green");
        properties.put("ports", new int[] { 80 });
        configurationB.update(properties);
        containerTracker.close();
        filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceTracker<BeanService, BeanService> stA = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=A))"), null);
        stA.open(true);
        BeanService<Callable<int[]>> beanService = stA.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("blue", beanService.doSomething());
        assertArrayEquals(new int[] { 12, 4567 }, beanService.get().call());
        ServiceTracker<BeanService, BeanService> stB = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=B))"), null);
        stB.open(true);
        beanService = stB.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("green", beanService.doSomething());
        assertArrayEquals(new int[] { 80 }, beanService.get().call());
    } finally {
        if (configurationA != null) {
            try {
                configurationA.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        if (configurationB != null) {
            try {
                configurationB.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        tb3Bundle.uninstall();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Callable(java.util.concurrent.Callable) Filter(org.osgi.framework.Filter) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 55 with Bundle

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

the class XAJPAEMFLocator method setupTransactionManager.

private void setupTransactionManager(BundleContext context, Map<String, Object> props, Map<String, Object> providerProps, ThreadLocal<TransactionControl> t, ServiceReference<EntityManagerFactoryBuilder> reference) {
    String provider = (String) reference.getProperty(JPA_UNIT_PROVIDER);
    ServiceReference<PersistenceProvider> providerRef = getPersistenceProvider(provider, context);
    if (providerRef == null) {
        // TODO log a warning and give up
        return;
    }
    Bundle providerBundle = providerRef.getBundle();
    Bundle txControlProviderBundle = context.getBundle();
    try {
        if ("org.hibernate.jpa.HibernatePersistenceProvider".equals(provider)) {
            try {
                providerBundle.loadClass("org.hibernate.resource.transaction.TransactionCoordinatorBuilder");
            } catch (Exception e) {
                BundleWiring wiring = providerBundle.adapt(BundleWiring.class);
                providerBundle = wiring.getRequiredWires("osgi.wiring.package").stream().filter(bw -> "org.hibernate".equals(bw.getCapability().getAttributes().get("osgi.wiring.package"))).map(BundleWire::getProviderWiring).map(BundleWiring::getBundle).findFirst().get();
            }
            ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
            Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.hibernate.impl.HibernateTxControlPlatform");
            Object plugin = pluginClazz.getConstructor(ThreadLocal.class).newInstance(t);
            props.put("hibernate.transaction.coordinator_class", plugin);
        } else if ("org.apache.openjpa.persistence.PersistenceProviderImpl".equals(provider)) {
            ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
            Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.openjpa.impl.OpenJPATxControlPlatform");
            Object plugin = pluginClazz.getConstructor(ThreadLocal.class).newInstance(t);
            props.put("openjpa.ManagedRuntime", plugin);
        } else if ("org.eclipse.persistence.jpa.PersistenceProvider".equals(provider)) {
            ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
            Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.eclipse.impl.EclipseTxControlPlatform");
            pluginClazz.getMethod("setTransactionControl", ThreadLocal.class).invoke(null, t);
            props.put("eclipselink.target-server", pluginClazz.getName());
            props.put("org.apache.aries.jpa.eclipselink.plugin.types", pluginClazz);
            // transactions without blowing up.
            if (!props.containsKey("eclipselink.jdbc.sequence-connection-pool")) {
                props.put("eclipselink.jdbc.sequence-connection-pool", "true");
            }
        } else {
            // TODO log a warning and give up
            return;
        }
    } catch (Exception e) {
        //TODO log a warning and give up
        e.printStackTrace();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) PersistenceProvider(javax.persistence.spi.PersistenceProvider) BundleWiring(org.osgi.framework.wiring.BundleWiring) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

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