Search in sources :

Example 26 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 27 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 28 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 29 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)

Example 30 with BundleContext

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

the class SightlyCompiledScriptTest method testEvalSlingBindings.

/**
     * Tests that SlingBindings are correctly handled by compiled scripts, by setting them from the script context to the request
     * attributes.
     * @throws ScriptException
     */
@Test
public void testEvalSlingBindings() throws ScriptException {
    ScriptEngine scriptEngine = mock(ScriptEngine.class);
    final RenderUnit renderUnit = mock(RenderUnit.class);
    Whitebox.setInternalState(renderUnit, "subTemplates", new HashMap<String, Object>());
    final BundleContext bundleContext = MockOsgi.newBundleContext();
    bundleContext.registerService(ExtensionRegistryService.class.getName(), mock(ExtensionRegistryService.class), new Hashtable<String, Object>());
    ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
    final MockSlingHttpServletRequest request = spy(new MockSlingHttpServletRequest(resourceResolver, bundleContext));
    SightlyCompiledScript compiledScript = spy(new SightlyCompiledScript(scriptEngine, renderUnit));
    ScriptContext scriptContext = mock(ScriptContext.class);
    StringWriter writer = new StringWriter();
    when(scriptContext.getWriter()).thenReturn(writer);
    Bindings scriptContextBindings = new SimpleBindings() {

        {
            put("test", "testValue");
            put(SlingBindings.REQUEST, request);
            put(SlingBindings.SLING, MockSling.newSlingScriptHelper(bundleContext));
        }
    };
    SlingBindings oldBindings = new SlingBindings();
    oldBindings.put("old", "oldValue");
    request.setAttribute(SlingBindings.class.getName(), oldBindings);
    when(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)).thenReturn(scriptContextBindings);
    compiledScript.eval(scriptContext);
    ArgumentCaptor<SlingBindings> slingBindingsArgumentCaptor = ArgumentCaptor.forClass(SlingBindings.class);
    ArgumentCaptor<String> attributeNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
    // request.setAttribute should have been invoked 3 times: once here, twice in the compiled script
    verify(request, times(3)).setAttribute(attributeNameArgumentCaptor.capture(), slingBindingsArgumentCaptor.capture());
    List<SlingBindings> slingBindingsValues = slingBindingsArgumentCaptor.getAllValues();
    int invocation = 1;
    for (SlingBindings bindings : slingBindingsValues) {
        switch(invocation) {
            case 1:
                assertEquals(oldBindings, bindings);
                break;
            case 2:
                assertEquals(3, bindings.size());
                for (Map.Entry<String, Object> entry : scriptContextBindings.entrySet()) {
                    assertEquals(entry.getValue(), bindings.get(entry.getKey()));
                }
                break;
            case 3:
                assertEquals(oldBindings, bindings);
        }
        invocation++;
    }
    for (String key : attributeNameArgumentCaptor.getAllValues()) {
        assertEquals(SlingBindings.class.getName(), key);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SimpleBindings(javax.script.SimpleBindings) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) HashMap(java.util.HashMap) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

BundleContext (org.osgi.framework.BundleContext)1052 Test (org.junit.Test)361 Bundle (org.osgi.framework.Bundle)298 ServiceReference (org.osgi.framework.ServiceReference)229 File (java.io.File)119 Hashtable (java.util.Hashtable)114 HashMap (java.util.HashMap)98 ArrayList (java.util.ArrayList)97 IOException (java.io.IOException)75 ServiceRegistration (org.osgi.framework.ServiceRegistration)68 BundleException (org.osgi.framework.BundleException)60 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)59 Equinox (org.eclipse.osgi.launch.Equinox)51 ComponentContext (org.osgi.service.component.ComponentContext)50 Dictionary (java.util.Dictionary)48 Before (org.junit.Before)48 Properties (java.util.Properties)47 LinkedHashMap (java.util.LinkedHashMap)46 URL (java.net.URL)43 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)40