Search in sources :

Example 11 with Reference

use of org.osgi.service.component.annotations.Reference in project sling by apache.

the class LegacyResourceProviderWhiteboard method bindResourceProvider.

@Reference(service = ResourceProvider.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void bindResourceProvider(final ServiceReference<ResourceProvider> ref) {
    final BundleContext bundleContext = ref.getBundle().getBundleContext();
    final ResourceProvider provider = bundleContext.getService(ref);
    if (provider != 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<>();
                newProps.put(PROPERTY_AUTHENTICATE, AuthType.no.toString());
                newProps.put(PROPERTY_MODIFIABLE, provider instanceof ModifyingResourceProvider);
                newProps.put(PROPERTY_ADAPTABLE, provider instanceof Adaptable);
                newProps.put(PROPERTY_ATTRIBUTABLE, provider instanceof AttributableResourceProvider);
                newProps.put(PROPERTY_REFRESHABLE, provider instanceof RefreshableResourceProvider);
                newProps.put(PROPERTY_NAME, provider.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 LegacyResourceProviderAdapter(provider, languages, ownsRoot), newProps);
                newServices.add(reg);
            }
        }
        registrations.put(provider, newServices);
    }
}
Also used : RefreshableResourceProvider(org.apache.sling.api.resource.RefreshableResourceProvider) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) ModifyingResourceProvider(org.apache.sling.api.resource.ModifyingResourceProvider) 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) Adaptable(org.apache.sling.api.adapter.Adaptable) AttributableResourceProvider(org.apache.sling.api.resource.AttributableResourceProvider) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference) Reference(org.osgi.service.component.annotations.Reference)

Example 12 with Reference

use of org.osgi.service.component.annotations.Reference in project bnd by bndtools.

the class AnnotationReader method doComponent.

/**
	 * @param annotation
	 * @throws Exception
	 */
@SuppressWarnings("deprecation")
protected void doComponent(Component comp, Annotation annotation) throws Exception {
    if (!mismatchedAnnotations.isEmpty()) {
        String componentName = comp.name();
        componentName = (componentName == null) ? className.getFQN() : componentName;
        for (Entry<String, List<DeclarativeServicesAnnotationError>> e : mismatchedAnnotations.entrySet()) {
            for (DeclarativeServicesAnnotationError errorDetails : e.getValue()) {
                if (errorDetails.fieldName != null) {
                    analyzer.error("The DS component %s uses standard annotations to declare it as a component, but also uses the bnd DS annotation: %s on field %s. It is an error to mix these two types of annotations", componentName, e.getKey(), errorDetails.fieldName).details(errorDetails);
                } else if (errorDetails.methodName != null) {
                    analyzer.error("The DS component %s uses standard annotations to declare it as a component, but also uses the bnd DS annotation: %s on method %s with signature %s. It is an error to mix these two types of annotations", componentName, e.getKey(), errorDetails.methodName, errorDetails.methodSignature).details(errorDetails);
                } else {
                    analyzer.error("The DS component %s uses standard annotations to declare it as a component, but also uses the bnd DS annotation: %s. It is an error to mix these two types of annotations", componentName, e.getKey()).details(errorDetails);
                }
            }
        }
        return;
    }
    // Check if we are doing a super class
    if (component.implementation != null)
        return;
    component.implementation = clazz.getClassName();
    component.name = comp.name();
    component.factory = comp.factory();
    component.configurationPolicy = comp.configurationPolicy();
    if (annotation.get("enabled") != null)
        component.enabled = comp.enabled();
    if (annotation.get("factory") != null)
        component.factory = comp.factory();
    if (annotation.get("immediate") != null)
        component.immediate = comp.immediate();
    if (annotation.get("servicefactory") != null)
        component.scope = comp.servicefactory() ? ServiceScope.BUNDLE : ServiceScope.SINGLETON;
    if (annotation.get("scope") != null && comp.scope() != ServiceScope.DEFAULT) {
        component.scope = comp.scope();
        if (comp.scope() == ServiceScope.PROTOTYPE) {
            component.updateVersion(V1_3);
        }
    }
    if (annotation.get("configurationPid") != null) {
        component.configurationPid = comp.configurationPid();
        if (component.configurationPid.length > 1) {
            component.updateVersion(V1_3);
        } else {
            component.updateVersion(V1_2);
        }
    }
    if (annotation.get("xmlns") != null)
        component.xmlns = comp.xmlns();
    String[] properties = comp.properties();
    if (properties != null)
        for (String entry : properties) {
            if (entry.contains("=")) {
                analyzer.error("Found an = sign in an OSGi DS Component annotation on %s. In the bnd annotation " + "this is an actual property but in the OSGi, this element must refer to a path with Java properties. " + "However, found a path with an '=' sign which looks like a mixup (%s) with the 'property' element.", clazz, entry).details(new DeclarativeServicesAnnotationError(className.getFQN(), null, null, ErrorType.COMPONENT_PROPERTIES_ERROR));
            }
            component.properties.add(entry);
        }
    doProperty(comp.property());
    Object[] x = annotation.get("service");
    if (x == null) {
        // fqn.
        if (interfaces != null) {
            List<TypeRef> result = new ArrayList<TypeRef>();
            for (int i = 0; i < interfaces.length; i++) {
                if (!interfaces[i].equals(analyzer.getTypeRef("scala/ScalaObject")))
                    result.add(interfaces[i]);
            }
            component.service = result.toArray(EMPTY);
        }
    } else {
        // We have explicit interfaces set
        component.service = new TypeRef[x.length];
        for (int i = 0; i < x.length; i++) {
            TypeRef typeRef = (TypeRef) x[i];
            Clazz service = analyzer.findClass(typeRef);
            if (!analyzer.assignable(clazz, service)) {
                analyzer.error("Class %s is not assignable to specified service %s", clazz.getFQN(), typeRef.getFQN()).details(new DeclarativeServicesAnnotationError(className.getFQN(), null, null, ErrorType.INCOMPATIBLE_SERVICE));
            }
            component.service[i] = typeRef;
        }
    }
    // make sure reference processing knows this is a Reference in Component
    member = null;
    Object[] refAnnotations = annotation.get("reference");
    if (refAnnotations != null) {
        for (Object o : refAnnotations) {
            Annotation refAnnotation = (Annotation) o;
            Reference ref = refAnnotation.getAnnotation();
            doReference(ref, refAnnotation);
        }
    }
}
Also used : DeclarativeServicesAnnotationError(aQute.bnd.component.error.DeclarativeServicesAnnotationError) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Reference(org.osgi.service.component.annotations.Reference) ArrayList(java.util.ArrayList) Annotation(aQute.bnd.osgi.Annotation) ArrayList(java.util.ArrayList) List(java.util.List) Clazz(aQute.bnd.osgi.Clazz)

Example 13 with Reference

use of org.osgi.service.component.annotations.Reference in project sling by apache.

the class UseRuntimeExtension method bindUseProvider.

// OSGi ################################################################################################################################
@Reference(policy = ReferencePolicy.DYNAMIC, service = UseProvider.class, cardinality = ReferenceCardinality.MULTIPLE)
private void bindUseProvider(ServiceReference serviceReference) {
    BundleContext bundleContext = serviceReference.getBundle().getBundleContext();
    providersMap.put(serviceReference, (UseProvider) bundleContext.getService(serviceReference));
}
Also used : BundleContext(org.osgi.framework.BundleContext) Reference(org.osgi.service.component.annotations.Reference) ServiceReference(org.osgi.framework.ServiceReference)

Example 14 with Reference

use of org.osgi.service.component.annotations.Reference in project jackrabbit-oak by apache.

the class CacheStatsMetrics method setMetricRegistry.

@Reference(target = "(name=oak)")
synchronized void setMetricRegistry(MetricRegistry registry) {
    LOG.debug("setMetricRegistry({})", registry);
    for (CacheStatsMBean stats : cacheStatsMBeans.values()) {
        unregisterCacheStatsMBean(this.registry, stats);
        registerCacheStatsMBean(registry, stats);
    }
    this.registry = registry;
}
Also used : CacheStatsMBean(org.apache.jackrabbit.oak.api.jmx.CacheStatsMBean) Reference(org.osgi.service.component.annotations.Reference)

Example 15 with Reference

use of org.osgi.service.component.annotations.Reference in project bndtools by bndtools.

the class HeadlessBuildManagerImpl method addPlugin.

@Reference(cardinality = ReferenceCardinality.AT_LEAST_ONE, policy = ReferencePolicy.DYNAMIC)
void addPlugin(HeadlessBuildPlugin plugin) {
    if (plugin == null) {
        return;
    }
    NamedPlugin pluginInformation = plugin.getInformation();
    String name = pluginInformation.getName();
    synchronized (plugins) {
        plugins.put(name, plugin);
        pluginsInformation.put(name, pluginInformation);
    }
}
Also used : NamedPlugin(org.bndtools.api.NamedPlugin) Reference(org.osgi.service.component.annotations.Reference)

Aggregations

Reference (org.osgi.service.component.annotations.Reference)18 ArrayList (java.util.ArrayList)4 BundleContext (org.osgi.framework.BundleContext)4 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2 List (java.util.List)2 ModuleInformation (org.eclipse.smarthome.automation.module.core.provider.ModuleInformation)2 ModuleType (org.eclipse.smarthome.automation.type.ModuleType)2 ServiceReference (org.osgi.framework.ServiceReference)2 DeclarativeServicesAnnotationError (aQute.bnd.component.error.DeclarativeServicesAnnotationError)1 Annotation (aQute.bnd.osgi.Annotation)1 Clazz (aQute.bnd.osgi.Clazz)1 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)1 LocalizationUtils (com.adobe.cq.wcm.core.components.internal.LocalizationUtils)1 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)1 PageListItemImpl (com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl)1 SearchImpl (com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl)1 PN_FRAGMENT_VARIATION_PATH (com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH)1 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)1 Search (com.adobe.cq.wcm.core.components.models.Search)1