Search in sources :

Example 76 with BundleContext

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

the class JdbcLoginModuleTest method setUp.

@Before
public void setUp() throws Exception {
    System.setProperty("derby.stream.error.file", "target/derby.log");
    // Create datasource
    dataSource = new EmbeddedDataSource40();
    dataSource.setDatabaseName("memory:db");
    dataSource.setCreateDatabase("create");
    // Delete tables
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(true);
        try {
            try (Statement statement = connection.createStatement()) {
                statement.execute("drop table USERS");
            }
        } catch (SQLException e) {
        // Ignore
        }
        try {
            try (Statement statement = connection.createStatement()) {
                statement.execute("drop table ROLES");
            }
        } catch (SQLException e) {
        // Ignore
        }
        connection.commit();
    }
    // Create tables
    try (Connection connection = dataSource.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            statement.execute("create table USERS (USERNAME VARCHAR(32) PRIMARY KEY, PASSWORD VARCHAR(32))");
        }
        try (Statement statement = connection.createStatement()) {
            statement.execute("create table ROLES (USERNAME VARCHAR(32), ROLE VARCHAR(1024))");
        }
        connection.commit();
    }
    // Mocks
    BundleContext context = EasyMock.createMock(BundleContext.class);
    ServiceReference reference = EasyMock.createMock(ServiceReference.class);
    // Create options
    options = new HashMap<>();
    options.put(JDBCUtils.DATASOURCE, "osgi:" + DataSource.class.getName());
    options.put(BundleContext.class.getName(), context);
    expect(context.getServiceReferences(DataSource.class.getName(), null)).andReturn(new ServiceReference[] { reference });
    expect((DataSource) context.getService(reference)).andReturn(dataSource);
    expect(context.ungetService(reference)).andReturn(true);
    EasyMock.replay(context);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) EmbeddedDataSource40(org.apache.derby.jdbc.EmbeddedDataSource40) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) DataSource(javax.sql.DataSource) Before(org.junit.Before)

Example 77 with BundleContext

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

the class BlueprintCommand method createNewAction.

public Action createNewAction() {
    Action action = (Action) blueprintContainer.getComponentInstance(actionId);
    if (action instanceof BlueprintContainerAware) {
        ((BlueprintContainerAware) action).setBlueprintContainer(blueprintContainer);
    }
    if (action instanceof BundleContextAware) {
        BundleContext context = (BundleContext) blueprintContainer.getComponentInstance("blueprintBundleContext");
        ((BundleContextAware) action).setBundleContext(context);
    }
    return action;
}
Also used : Action(org.apache.felix.gogo.commands.Action) BundleContextAware(org.apache.karaf.shell.console.BundleContextAware) BlueprintContainerAware(org.apache.karaf.shell.console.BlueprintContainerAware) BundleContext(org.osgi.framework.BundleContext)

Example 78 with BundleContext

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

the class TestBundleFactory method createBundleContext.

public BundleContext createBundleContext() {
    BundleContext bundleContext = createMock(BundleContext.class);
    Bundle[] bundles = createBundles();
    expect(bundleContext.getProperty("karaf.systemBundlesStartLevel")).andReturn(Integer.toString(50)).anyTimes();
    expect(bundleContext.getBundles()).andReturn(bundles).anyTimes();
    expect(bundleContext.getBundle(0)).andReturn(null).anyTimes();
    expect(bundleContext.getBundle(1)).andReturn(bundles[0]).anyTimes();
    expect(bundleContext.getBundle(2)).andReturn(bundles[1]).anyTimes();
    replay(bundleContext);
    return bundleContext;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

Example 79 with BundleContext

use of org.osgi.framework.BundleContext 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 80 with BundleContext

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

the class GuardProxyCatalogTest method testIsProxy.

@Test
public void testIsProxy() throws Exception {
    BundleContext bc = mockBundleContext();
    GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
    Dictionary<String, Object> props = new Hashtable<>();
    props.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
    assertTrue(gpc.isProxy(mockServiceReference(props)));
    assertFalse(gpc.isProxy(mockServiceReference(new Hashtable<>())));
}
Also used : Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

BundleContext (org.osgi.framework.BundleContext)433 Test (org.junit.Test)188 ServiceReference (org.osgi.framework.ServiceReference)124 Bundle (org.osgi.framework.Bundle)119 Hashtable (java.util.Hashtable)72 ArrayList (java.util.ArrayList)39 ServiceRegistration (org.osgi.framework.ServiceRegistration)38 Dictionary (java.util.Dictionary)29 HashMap (java.util.HashMap)28 Matchers.anyString (org.mockito.Matchers.anyString)28 File (java.io.File)23 Before (org.junit.Before)23 URL (java.net.URL)21 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)20 IOException (java.io.IOException)19 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)15 Map (java.util.Map)15 Configuration (org.osgi.service.cm.Configuration)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13