Search in sources :

Example 36 with ServiceReference

use of org.osgi.framework.ServiceReference 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 37 with ServiceReference

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

the class BindingsValuesProvidersByContextImpl method activate.

@Activate
public void activate(ComponentContext ctx) {
    bundleContext = ctx.getBundleContext();
    synchronized (pendingRefs) {
        for (ServiceReference ref : pendingRefs) {
            addingService(ref);
        }
        pendingRefs.clear();
    }
    bvpTracker = new ServiceTracker(bundleContext, BindingsValuesProvider.class.getName(), this);
    bvpTracker.open();
    // Map services can also be registered to provide bindings
    mapsTracker = new ServiceTracker(bundleContext, Map.class.getName(), this);
    mapsTracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.osgi.service.component.annotations.Activate)

Example 38 with ServiceReference

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

the class ScriptHelper method getServices.

/**
     * @see org.apache.sling.api.scripting.SlingScriptHelper#getServices(java.lang.Class, java.lang.String)
     */
@SuppressWarnings("unchecked")
public <ServiceType> ServiceType[] getServices(Class<ServiceType> serviceType, String filter) throws InvalidServiceFilterSyntaxException {
    try {
        final ServiceReference[] refs = this.bundleContext.getServiceReferences(serviceType.getName(), filter);
        ServiceType[] result = null;
        if (refs != null) {
            // sort by service ranking (lowest first) (see ServiceReference#compareTo(Object))
            List<ServiceReference> references = Arrays.asList(refs);
            Collections.sort(references);
            // get the highest ranking first
            Collections.reverse(references);
            final List<ServiceType> objects = new ArrayList<ServiceType>();
            for (ServiceReference reference : references) {
                final ServiceType service = (ServiceType) this.bundleContext.getService(reference);
                if (service != null) {
                    if (this.references == null) {
                        this.references = new ArrayList<ServiceReference>();
                    }
                    this.references.add(reference);
                    objects.add(service);
                }
            }
            if (objects.size() > 0) {
                ServiceType[] srv = (ServiceType[]) Array.newInstance(serviceType, objects.size());
                result = objects.toArray(srv);
            }
        }
        return result;
    } catch (InvalidSyntaxException ise) {
        throw new InvalidServiceFilterSyntaxException(filter, "Invalid filter syntax", ise);
    }
}
Also used : ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvalidServiceFilterSyntaxException(org.apache.sling.api.scripting.InvalidServiceFilterSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 39 with ServiceReference

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

the class ScriptHelper method cleanup.

/**
     * Clean up this instance.
     */
public void cleanup() {
    if (this.references != null) {
        final Iterator<ServiceReference> i = this.references.iterator();
        while (i.hasNext()) {
            final ServiceReference ref = i.next();
            this.bundleContext.ungetService(ref);
        }
        this.references.clear();
    }
    if (this.services != null) {
        this.services.clear();
    }
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 40 with ServiceReference

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

the class ServiceCache method getService.

/**
     * Return a service for the given service class.
     * @param <ServiceType> The service class / interface
     * @param type The requested service
     * @return The service or <code>null</code>
     */
@SuppressWarnings("unchecked")
public <ServiceType> ServiceType getService(Class<ServiceType> type) {
    final String key = type.getName();
    Reference reference = this.cache.get(key);
    if (reference == null) {
        // get the service
        ServiceReference ref = this.bundleContext.getServiceReference(key);
        if (ref != null) {
            final Object service = this.bundleContext.getService(ref);
            if (service != null) {
                reference = new Reference();
                reference.service = service;
                reference.reference = ref;
            } else {
                ref = null;
            }
        }
        // assume missing service
        if (reference == null) {
            reference = NULL_REFERENCE;
        }
        // check to see whether another thread has not done the same thing
        synchronized (this) {
            Reference existing = this.cache.get(key);
            if (existing == null) {
                this.cache.put(key, reference);
                ref = null;
            } else {
                reference = existing;
            }
        }
        // unget the service if another thread was faster
        if (ref != null) {
            this.bundleContext.ungetService(ref);
        }
    }
    // return whatever we got (which may be null)
    return (ServiceType) reference.service;
}
Also used : ServiceReference(org.osgi.framework.ServiceReference) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1687 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)229 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)182 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65