Search in sources :

Example 91 with Bundle

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

the class Converters method convertBundle.

private Object convertBundle(Object in) {
    String s = in.toString();
    try {
        long id = Long.parseLong(s);
        return context.getBundle(id);
    } catch (NumberFormatException nfe) {
    // Ignore
    }
    Bundle[] bundles = context.getBundles();
    for (Bundle b : bundles) {
        if (b.getLocation().equals(s)) {
            return b;
        }
        if (b.getSymbolicName().equals(s)) {
            return b;
        }
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle)

Example 92 with Bundle

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

the class CommonResourceResolverFactoryImpl method getServiceUserAuthenticationInfo.

@Override
public Map<String, Object> getServiceUserAuthenticationInfo(final String subServiceName) throws LoginException {
    // get an administrative resource resolver
    // Ensure a mapped user name: If no user is defined for a bundle
    // acting as a service, the user may be null. We can decide whether
    // this should yield guest access or no access at all. For now
    // no access is granted if there is no service user defined for
    // the bundle.
    final Bundle bundle = this.activator.getBundleContext().getBundle();
    final String userName = this.activator.getServiceUserMapper().getServiceUserID(bundle, subServiceName);
    if (userName == null) {
        throw new LoginException("Cannot derive user name for bundle " + bundle + " and sub service " + subServiceName);
    }
    final Map<String, Object> authenticationInfo = new HashMap<>();
    // ensure proper user name and service bundle
    authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, subServiceName);
    authenticationInfo.put(ResourceResolverFactory.USER, userName);
    authenticationInfo.put(ResourceProvider.AUTH_SERVICE_BUNDLE, bundle);
    return authenticationInfo;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Bundle(org.osgi.framework.Bundle) LoginException(org.apache.sling.api.resource.LoginException)

Example 93 with Bundle

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

the class ResourceResolverControl method getResourceTypeResourceResolver.

private ResourceResolver getResourceTypeResourceResolver(final ResourceResolverFactory factory, final ResourceResolver resolver) {
    if (this.isAdmin) {
        return resolver;
    } else {
        if (this.resourceTypeResourceResolver == null) {
            try {
                // make sure we're getting the resourceTypeResourceResolver on behalf of
                // the resourceresolver bundle
                final Bundle bundle = FrameworkUtil.getBundle(ResourceResolverControl.class);
                final Map<String, Object> authenticationInfo = new HashMap<>();
                authenticationInfo.put(ResourceProvider.AUTH_SERVICE_BUNDLE, bundle);
                authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, "hierarchy");
                this.resourceTypeResourceResolver = factory.getServiceResourceResolver(authenticationInfo);
            } catch (final LoginException e) {
                throw new IllegalStateException("Failed to create resource-type ResourceResolver", e);
            }
        }
        return this.resourceTypeResourceResolver;
    }
}
Also used : HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) Bundle(org.osgi.framework.Bundle) LoginException(org.apache.sling.api.resource.LoginException)

Example 94 with Bundle

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

the class ScriptEngineManagerFactory method refreshScriptEngineManager.

/**
     * Refresh the script engine manager.
     */
private void refreshScriptEngineManager() {
    // create (empty) script engine manager
    final ClassLoader loader = getClass().getClassLoader();
    final SlingScriptEngineManager tmp = new SlingScriptEngineManager(loader);
    // register script engines from bundles
    synchronized (this.engineSpiBundles) {
        for (final Bundle bundle : this.engineSpiBundles) {
            registerFactories(tmp, bundle);
        }
    }
    // register script engines from registered services
    synchronized (this.engineSpiServices) {
        for (final Map.Entry<ScriptEngineFactory, Map<Object, Object>> factory : this.engineSpiServices.entrySet()) {
            registerFactory(tmp, factory.getKey(), factory.getValue());
        }
    }
    scriptEngineManager.setDelegatee(tmp);
    final List<ScriptEngineFactory> factories = tmp.getEngineFactories();
    for (final ScriptEngineFactory factory : factories) {
        log.info("ScriptEngine {}/{} is now handling {}, {}, {}.", new Object[] { factory.getEngineName(), factory.getEngineVersion(), factory.getExtensions(), factory.getMimeTypes(), factory.getNames() });
    }
}
Also used : SlingScriptEngineManager(org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager) Bundle(org.osgi.framework.Bundle) ScriptEngineFactory(javax.script.ScriptEngineFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 95 with Bundle

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

the class ScriptEngineManagerFactoryTest method checkAddingScriptBundle.

@Test
public void checkAddingScriptBundle() throws Exception {
    context.checking(new Expectations() {

        {
            exactly(1).of(bundleCtx).registerService(with(equal(new String[] { "javax.script.ScriptEngineManager", "org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager" })), with(any(Object.class)), with(any(Dictionary.class)));
            will(returnValue(new MockServiceRegistration()));
        }
    });
    ScriptEngineManagerFactory factory = new ScriptEngineManagerFactory();
    factory.activate(componentCtx);
    ScriptEngineManager first = factory.getScriptEngineManager();
    assertNull(first.getEngineByName("dummy"));
    final Bundle bundle = context.mock(Bundle.class);
    final File factoryFile = createFactoryFile();
    context.checking(new Expectations() {

        {
            atLeast(1).of(bundle).getEntry("META-INF/services/javax.script.ScriptEngineFactory");
            will(returnValue(factoryFile.toURI().toURL()));
            atLeast(1).of(bundle).loadClass(SCRIPT_ENGINE_FACTORY.getName());
            will(returnValue(SCRIPT_ENGINE_FACTORY));
        }
    });
    factory.bundleChanged(new BundleEvent(BundleEvent.STARTED, bundle));
    ScriptEngineManager second = factory.getScriptEngineManager();
    assertNotNull(second.getEngineByName("dummy"));
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) Bundle(org.osgi.framework.Bundle) ScriptEngineManager(javax.script.ScriptEngineManager) BundleEvent(org.osgi.framework.BundleEvent) File(java.io.File) Test(org.junit.Test)

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