Search in sources :

Example 81 with BundleContext

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

the class ProxyLoginModuleInitializer method init.

public void init() {
    BundleContext context = bundleContext.getBundle(0).getBundleContext();
    ProxyLoginModule.init(context);
}
Also used : BundleContext(org.osgi.framework.BundleContext)

Example 82 with BundleContext

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

the class BundleHelpProvider method getHelp.

@Override
public String getHelp(Session session, String path) {
    if (path.indexOf('|') > 0) {
        if (path.startsWith("bundle|")) {
            path = path.substring("bundle|".length());
        } else {
            return null;
        }
    }
    if (path.matches("[0-9]*")) {
        long id = Long.parseLong(path);
        BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
        Bundle bundle = bundleContext.getBundle(id);
        if (bundle != null) {
            String title = ShellUtil.getBundleName(bundle);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            ps.println("\n" + title);
            ps.println(ShellUtil.getUnderlineString(title));
            URL bundleInfo = bundle.getEntry("OSGI-INF/bundle.info");
            if (bundleInfo != null) {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(bundleInfo.openStream()))) {
                    int maxSize = 80;
                    Terminal terminal = session.getTerminal();
                    if (terminal != null) {
                        maxSize = terminal.getWidth();
                    }
                    WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
                    WikiParser parser = new WikiParser(visitor);
                    parser.parse(reader);
                } catch (Exception e) {
                // ignore
                }
            }
            ps.close();
            return baos.toString();
        }
    }
    return null;
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) WikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WikiParser(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser) Terminal(org.apache.karaf.shell.api.console.Terminal) URL(java.net.URL) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) BufferedReader(java.io.BufferedReader) BundleContext(org.osgi.framework.BundleContext)

Example 83 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 84 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 85 with BundleContext

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

the class LegacyResourceProviderWhiteboard method bindResourceProviderFactory.

@Reference(service = ResourceProviderFactory.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void bindResourceProviderFactory(final ServiceReference<ResourceProviderFactory> ref) {
    final BundleContext bundleContext = ref.getBundle().getBundleContext();
    final ResourceProviderFactory factory = bundleContext.getService(ref);
    if (factory != null) {
        final String[] propertyNames = ref.getPropertyKeys();
        final boolean ownsRoot = toBoolean(ref.getProperty(OWNS_ROOTS), false);
        final List<ServiceRegistration> newServices = new ArrayList<>();
        for (final String path : PropertiesUtil.toStringArray(ref.getProperty(ROOTS), new String[0])) {
            if (path != null && !path.isEmpty()) {
                final Dictionary<String, Object> newProps = new Hashtable<>();
                if (PropertiesUtil.toBoolean(ref.getProperty(PROPERTY_REQUIRED), false)) {
                    newProps.put(PROPERTY_AUTHENTICATE, AuthType.required.toString());
                } else {
                    newProps.put(PROPERTY_AUTHENTICATE, AuthType.lazy.toString());
                }
                newProps.put(PROPERTY_MODIFIABLE, true);
                newProps.put(PROPERTY_ADAPTABLE, true);
                newProps.put(PROPERTY_ATTRIBUTABLE, true);
                newProps.put(PROPERTY_REFRESHABLE, true);
                newProps.put(PROPERTY_NAME, factory.getClass().getName());
                newProps.put(PROPERTY_ROOT, normalizePath(path));
                if (ArrayUtils.contains(propertyNames, SERVICE_PID)) {
                    newProps.put(ORIGINAL_SERVICE_PID, ref.getProperty(SERVICE_PID));
                }
                if (ArrayUtils.contains(propertyNames, USE_RESOURCE_ACCESS_SECURITY)) {
                    newProps.put(PROPERTY_USE_RESOURCE_ACCESS_SECURITY, ref.getProperty(USE_RESOURCE_ACCESS_SECURITY));
                }
                if (ArrayUtils.contains(propertyNames, SERVICE_RANKING)) {
                    newProps.put(SERVICE_RANKING, ref.getProperty(SERVICE_RANKING));
                }
                String[] languages = PropertiesUtil.toStringArray(ref.getProperty(LANGUAGES), new String[0]);
                ServiceRegistration reg = bundleContext.registerService(org.apache.sling.spi.resource.provider.ResourceProvider.class.getName(), new LegacyResourceProviderFactoryAdapter(factory, languages, ownsRoot), newProps);
                newServices.add(reg);
            }
        }
        registrations.put(factory, newServices);
    }
}
Also used : ResourceProviderFactory(org.apache.sling.api.resource.ResourceProviderFactory) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) RefreshableResourceProvider(org.apache.sling.api.resource.RefreshableResourceProvider) AttributableResourceProvider(org.apache.sling.api.resource.AttributableResourceProvider) ModifyingResourceProvider(org.apache.sling.api.resource.ModifyingResourceProvider) ResourceProvider(org.apache.sling.api.resource.ResourceProvider) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference) Reference(org.osgi.service.component.annotations.Reference)

Aggregations

BundleContext (org.osgi.framework.BundleContext)524 Test (org.junit.Test)186 Bundle (org.osgi.framework.Bundle)175 ServiceReference (org.osgi.framework.ServiceReference)126 File (java.io.File)82 Hashtable (java.util.Hashtable)75 HashMap (java.util.HashMap)70 Equinox (org.eclipse.osgi.launch.Equinox)51 BundleException (org.osgi.framework.BundleException)51 ArrayList (java.util.ArrayList)50 LinkedHashMap (java.util.LinkedHashMap)45 ServiceRegistration (org.osgi.framework.ServiceRegistration)41 IOException (java.io.IOException)40 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)38 URL (java.net.URL)33 Dictionary (java.util.Dictionary)32 Matchers.anyString (org.mockito.Matchers.anyString)28 Before (org.junit.Before)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)26 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18