Search in sources :

Example 1 with ExtendedReferenceMetadata

use of org.apache.aries.blueprint.ExtendedReferenceMetadata in project aries by apache.

the class RecipeBuilder method createReferenceListRecipe.

private Recipe createReferenceListRecipe(ReferenceListMetadata metadata) {
    ValueRecipe filterRecipe = null;
    if (metadata instanceof ExtendedReferenceMetadata) {
        ValueMetadata filterMetadata = ((ExtendedServiceReferenceMetadata) metadata).getExtendedFilter();
        if (filterMetadata != null) {
            filterRecipe = (ValueRecipe) getValue(filterMetadata, null);
        }
    }
    CollectionRecipe listenersRecipe = null;
    if (metadata.getReferenceListeners() != null) {
        listenersRecipe = new CollectionRecipe(getName(null), ArrayList.class, Object.class.getName());
        for (ReferenceListener listener : metadata.getReferenceListeners()) {
            listenersRecipe.add(createRecipe(listener));
        }
    }
    ReferenceListRecipe recipe = new ReferenceListRecipe(getName(metadata.getId()), blueprintContainer, metadata, filterRecipe, listenersRecipe, getDependencies(metadata));
    return recipe;
}
Also used : ExtendedReferenceMetadata(org.apache.aries.blueprint.ExtendedReferenceMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ArrayList(java.util.ArrayList) ReferenceListener(org.osgi.service.blueprint.reflect.ReferenceListener) ExtendedServiceReferenceMetadata(org.apache.aries.blueprint.ExtendedServiceReferenceMetadata) ValueRecipe(org.apache.aries.blueprint.di.ValueRecipe) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe)

Example 2 with ExtendedReferenceMetadata

use of org.apache.aries.blueprint.ExtendedReferenceMetadata in project aries by apache.

the class ReferenceRecipe method internalCreate.

@Override
protected Object internalCreate() throws ComponentDefinitionException {
    try {
        if (explicitDependencies != null) {
            for (Recipe recipe : explicitDependencies) {
                recipe.create();
            }
        }
        // Create the proxy
        Set<Class<?>> interfaces = new HashSet<Class<?>>();
        Class<?> clz = getInterfaceClass();
        if (clz != null)
            interfaces.add(clz);
        if (metadata instanceof ExtendedReferenceMetadata) {
            interfaces.addAll(loadAllClasses(((ExtendedReferenceMetadata) metadata).getExtraInterfaces()));
        }
        Object result;
        if (isStaticLifecycle()) {
            result = getService();
        } else {
            proxy = createProxy(new ServiceDispatcher(), interfaces);
            // Add partially created proxy to the context
            result = new ServiceProxyWrapper();
        }
        addPartialObject(result);
        // Handle initial references
        createListeners();
        updateListeners();
        return result;
    } catch (ComponentDefinitionException e) {
        throw e;
    } catch (Throwable t) {
        throw new ComponentDefinitionException(t);
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe) ValueRecipe(org.apache.aries.blueprint.di.ValueRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) ExtendedReferenceMetadata(org.apache.aries.blueprint.ExtendedReferenceMetadata) HashSet(java.util.HashSet)

Example 3 with ExtendedReferenceMetadata

use of org.apache.aries.blueprint.ExtendedReferenceMetadata in project aries by apache.

the class AbstractServiceReferenceRecipe method createOsgiFilter.

/**
 * Create the OSGi filter corresponding to the ServiceReferenceMetadata constraints
 *
 * @param metadata the service reference metadata
 * @return the OSGi filter
 */
private static String createOsgiFilter(ServiceReferenceMetadata metadata, String extendedFilter) {
    List<String> members = new ArrayList<String>();
    // Handle filter
    String flt = metadata.getFilter();
    if (flt != null && flt.length() > 0) {
        if (!flt.startsWith("(")) {
            flt = "(" + flt + ")";
        }
        members.add(flt);
    }
    // Handle extended filter
    if (extendedFilter != null && extendedFilter.length() > 0) {
        if (!extendedFilter.startsWith("(")) {
            extendedFilter = "(" + extendedFilter + ")";
        }
        members.add(extendedFilter);
    }
    // Handle interfaces
    String interfaceName = metadata.getInterface();
    Class runtimeClass = getRuntimeClass(metadata);
    if (runtimeClass != null) {
        interfaceName = runtimeClass.getName();
    }
    if (interfaceName != null && interfaceName.length() > 0) {
        if (metadata instanceof ExtendedReferenceMetadata) {
            ExtendedReferenceMetadata erm = (ExtendedReferenceMetadata) metadata;
            if (!erm.getExtraInterfaces().isEmpty()) {
                StringBuilder sb = new StringBuilder("(&");
                sb.append("(" + Constants.OBJECTCLASS + "=" + interfaceName + ")");
                for (String s : erm.getExtraInterfaces()) {
                    sb.append("(" + Constants.OBJECTCLASS + "=" + s + ")");
                }
                sb.append(")");
                members.add(sb.toString());
            } else {
                members.add("(" + Constants.OBJECTCLASS + "=" + interfaceName + ")");
            }
        } else {
            members.add("(" + Constants.OBJECTCLASS + "=" + interfaceName + ")");
        }
    }
    // Handle component name
    String componentName = metadata.getComponentName();
    if (componentName != null && componentName.length() > 0) {
        members.add("(" + BlueprintConstants.COMPONENT_NAME_PROPERTY + "=" + componentName + ")");
    }
    // Create filter
    if (members.isEmpty()) {
        throw new IllegalStateException("No constraints were specified on the service reference");
    }
    if (members.size() == 1) {
        return members.get(0);
    }
    StringBuilder sb = new StringBuilder("(&");
    for (String member : members) {
        sb.append(member);
    }
    sb.append(")");
    return sb.toString();
}
Also used : ExtendedReferenceMetadata(org.apache.aries.blueprint.ExtendedReferenceMetadata) ArrayList(java.util.ArrayList)

Example 4 with ExtendedReferenceMetadata

use of org.apache.aries.blueprint.ExtendedReferenceMetadata in project aries by apache.

the class RecipeBuilder method createReferenceRecipe.

private ReferenceRecipe createReferenceRecipe(ReferenceMetadata metadata) {
    ValueRecipe filterRecipe = null;
    if (metadata instanceof ExtendedReferenceMetadata) {
        ValueMetadata filterMetadata = ((ExtendedServiceReferenceMetadata) metadata).getExtendedFilter();
        if (filterMetadata != null) {
            filterRecipe = (ValueRecipe) getValue(filterMetadata, null);
        }
    }
    CollectionRecipe listenersRecipe = null;
    if (metadata.getReferenceListeners() != null) {
        listenersRecipe = new CollectionRecipe(getName(null), ArrayList.class, Object.class.getName());
        for (ReferenceListener listener : metadata.getReferenceListeners()) {
            listenersRecipe.add(createRecipe(listener));
        }
    }
    ReferenceRecipe recipe = new ReferenceRecipe(getName(metadata.getId()), blueprintContainer, metadata, filterRecipe, listenersRecipe, getDependencies(metadata));
    return recipe;
}
Also used : ExtendedReferenceMetadata(org.apache.aries.blueprint.ExtendedReferenceMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ArrayList(java.util.ArrayList) ReferenceListener(org.osgi.service.blueprint.reflect.ReferenceListener) ExtendedServiceReferenceMetadata(org.apache.aries.blueprint.ExtendedServiceReferenceMetadata) ValueRecipe(org.apache.aries.blueprint.di.ValueRecipe) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe)

Example 5 with ExtendedReferenceMetadata

use of org.apache.aries.blueprint.ExtendedReferenceMetadata in project aries by apache.

the class ReferenceRecipe method getService.

private Object getService() throws InterruptedException {
    synchronized (monitor) {
        if (isStarted() && trackedServiceReference == null && metadata.getTimeout() > 0 && metadata.getAvailability() == ServiceReferenceMetadata.AVAILABILITY_MANDATORY) {
            // Here we want to get the blueprint bundle itself, so don't use #getBundleContextForServiceLookup()
            blueprintContainer.getEventDispatcher().blueprintEvent(createWaitingevent());
            monitor.wait(metadata.getTimeout());
        }
        Object result = null;
        if (trackedServiceReference == null) {
            if (isStarted()) {
                boolean failed = true;
                if (metadata.getAvailability() == ReferenceMetadata.AVAILABILITY_OPTIONAL && metadata instanceof ExtendedReferenceMetadata) {
                    if (defaultBean == null) {
                        String defaultBeanId = ((ExtendedReferenceMetadata) metadata).getDefaultBean();
                        if (defaultBeanId != null) {
                            defaultBean = blueprintContainer.getComponentInstance(defaultBeanId);
                            failed = false;
                        }
                    } else {
                        failed = false;
                    }
                    BlueprintRepository repository = ((BlueprintContainerImpl) blueprintContainer).getRepository();
                    ExecutionContext oldContext = null;
                    try {
                        oldContext = ExecutionContext.Holder.setContext(repository);
                        result = convert(defaultBean, new GenericType(getInterfaceClass()));
                    } catch (Exception e) {
                        throw new IllegalStateException("Wrong type for defaultBean", e);
                    } finally {
                        ExecutionContext.Holder.setContext(oldContext);
                    }
                }
                if (failed) {
                    if (metadata.getAvailability() == ServiceReferenceMetadata.AVAILABILITY_MANDATORY) {
                        LOGGER.info("Timeout expired when waiting for mandatory OSGi service reference {}", getOsgiFilter());
                        throw new ServiceUnavailableException("Timeout expired when waiting for mandatory OSGi service reference: " + getOsgiFilter(), getOsgiFilter());
                    } else {
                        LOGGER.info("No matching service for optional OSGi service reference {}", getOsgiFilter());
                        throw new ServiceUnavailableException("No matching service for optional OSGi service reference: " + getOsgiFilter(), getOsgiFilter());
                    }
                }
            } else {
                throw new ServiceUnavailableException("The Blueprint container is being or has been destroyed: " + getOsgiFilter(), getOsgiFilter());
            }
        } else {
            if (trackedService == null) {
                trackedService = getServiceSecurely(trackedServiceReference);
            }
            if (trackedService == null) {
                throw new IllegalStateException("getService() returned null for " + trackedServiceReference);
            }
            result = trackedService;
        }
        return result;
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) ExtendedReferenceMetadata(org.apache.aries.blueprint.ExtendedReferenceMetadata) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException)

Aggregations

ExtendedReferenceMetadata (org.apache.aries.blueprint.ExtendedReferenceMetadata)5 ArrayList (java.util.ArrayList)3 CollectionRecipe (org.apache.aries.blueprint.di.CollectionRecipe)3 ValueRecipe (org.apache.aries.blueprint.di.ValueRecipe)3 ExtendedServiceReferenceMetadata (org.apache.aries.blueprint.ExtendedServiceReferenceMetadata)2 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)2 ReferenceListener (org.osgi.service.blueprint.reflect.ReferenceListener)2 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)2 HashSet (java.util.HashSet)1 ExecutionContext (org.apache.aries.blueprint.di.ExecutionContext)1 Recipe (org.apache.aries.blueprint.di.Recipe)1 ServiceUnavailableException (org.osgi.service.blueprint.container.ServiceUnavailableException)1