Search in sources :

Example 1 with UnwrapperedBeanHolder

use of org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder in project aries by apache.

the class AggregateConverter method convert.

public Object convert(Object fromValue, final ReifiedType type) throws Exception {
    // Discard null values
    if (fromValue == null) {
        return null;
    }
    // First convert service proxies
    if (fromValue instanceof Convertible) {
        return ((Convertible) fromValue).convert(type);
    } else if (fromValue instanceof UnwrapperedBeanHolder) {
        UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) fromValue;
        if (isAssignable(holder.unwrapperedBean, type)) {
            return BeanRecipe.wrap(holder, type.getRawClass());
        } else {
            fromValue = BeanRecipe.wrap(holder, Object.class);
        }
    } else if (isAssignable(fromValue, type)) {
        // If the object is an instance of the type, just return it
        return fromValue;
    }
    final Object finalFromValue = fromValue;
    ConversionResult result = null;
    AccessControlContext acc = blueprintContainer.getAccessControlContext();
    if (acc == null) {
        result = convertWithConverters(fromValue, type);
    } else {
        result = AccessController.doPrivileged(new PrivilegedExceptionAction<ConversionResult>() {

            public ConversionResult run() throws Exception {
                return convertWithConverters(finalFromValue, type);
            }
        }, acc);
    }
    if (result == null) {
        if (fromValue instanceof Number && Number.class.isAssignableFrom(unwrap(toClass(type)))) {
            return convertToNumber((Number) fromValue, toClass(type));
        } else if (fromValue instanceof String) {
            return convertFromString((String) fromValue, toClass(type), blueprintContainer);
        } else if (toClass(type).isArray() && (fromValue instanceof Collection || fromValue.getClass().isArray())) {
            return convertToArray(fromValue, type);
        } else if (Map.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Map || fromValue instanceof Dictionary)) {
            return convertToMap(fromValue, type);
        } else if (Dictionary.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Map || fromValue instanceof Dictionary)) {
            return convertToDictionary(fromValue, type);
        } else if (Collection.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Collection || fromValue.getClass().isArray())) {
            return convertToCollection(fromValue, type);
        } else {
            throw new Exception("Unable to convert value " + fromValue + " to type " + type);
        }
    }
    return result.value;
}
Also used : Dictionary(java.util.Dictionary) AccessControlContext(java.security.AccessControlContext) Collection(java.util.Collection) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) HashMap(java.util.HashMap) Map(java.util.Map) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Example 2 with UnwrapperedBeanHolder

use of org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder in project aries by apache.

the class AggregateConverter method canConvert.

public boolean canConvert(Object fromValue, final ReifiedType toType) {
    if (fromValue == null) {
        return true;
    } else if (fromValue instanceof UnwrapperedBeanHolder) {
        fromValue = ((UnwrapperedBeanHolder) fromValue).unwrapperedBean;
    }
    if (isAssignable(fromValue, toType)) {
        return true;
    }
    final Object toTest = fromValue;
    boolean canConvert = false;
    AccessControlContext acc = blueprintContainer.getAccessControlContext();
    if (acc == null) {
        canConvert = canConvertWithConverters(toTest, toType);
    } else {
        canConvert = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

            public Boolean run() {
                return canConvertWithConverters(toTest, toType);
            }
        }, acc);
    }
    if (canConvert) {
        return true;
    }
    // TODO implement better logic ?!
    try {
        convert(toTest, toType);
        return true;
    } catch (Exception e) {
        return false;
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Example 3 with UnwrapperedBeanHolder

use of org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder in project aries by apache.

the class ServiceRecipe method createService.

private void createService() {
    try {
        if (service == null) {
            LOGGER.debug("Creating service instance");
            //We can't use the BlueprintRepository because we don't know what interfaces
            //to use yet! We have to be a bit smarter.
            ExecutionContext old = ExecutionContext.Holder.setContext(blueprintContainer.getRepository());
            try {
                Object o = serviceRecipe.create();
                if (o instanceof Convertible) {
                    o = blueprintContainer.getRepository().convert(o, new ReifiedType(Object.class));
                    validateClasses(o);
                } else if (o instanceof UnwrapperedBeanHolder) {
                    UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) o;
                    if (holder.unwrapperedBean instanceof ServiceFactory) {
                        //If a service factory is used, make sure the proxy classes implement this
                        //interface so that later on, internalGetService will create the real
                        //service from it.
                        LOGGER.debug("{} implements ServiceFactory, creating proxy that also implements this", holder.unwrapperedBean);
                        Collection<Class<?>> cls = getClassesForProxying(holder.unwrapperedBean);
                        cls.add(blueprintContainer.loadClass("org.osgi.framework.ServiceFactory"));
                        o = BeanRecipe.wrap(holder, cls);
                    } else {
                        validateClasses(holder.unwrapperedBean);
                        o = BeanRecipe.wrap(holder, getClassesForProxying(holder.unwrapperedBean));
                    }
                } else if (!(o instanceof ServiceFactory)) {
                    validateClasses(o);
                }
                service = o;
            } catch (Exception e) {
                LOGGER.error("Error retrieving service from " + this, e);
                throw new ComponentDefinitionException(e);
            } finally {
                ExecutionContext.Holder.setContext(old);
            }
            LOGGER.debug("Service created: {}", service);
        }
        // When the service is first requested, we need to create listeners and call them
        if (!initialServiceRegistration && listeners == null) {
            LOGGER.debug("Creating listeners");
            if (listenersRecipe != null) {
                listeners = (List) createRecipe(listenersRecipe);
            } else {
                listeners = Collections.emptyList();
            }
            LOGGER.debug("Listeners created: {}", listeners);
            if (registration.get() != null) {
                LOGGER.debug("Calling listeners for initial service registration");
                for (ServiceListener listener : listeners) {
                    listener.register(service, registrationProperties);
                }
            } else {
                LOGGER.debug("Calling listeners for initial service unregistration");
                for (ServiceListener listener : listeners) {
                    listener.unregister(service, registrationProperties);
                }
            }
        }
    } catch (RuntimeException e) {
        LOGGER.error("Error retrieving service from " + this, e);
        throw e;
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) Convertible(org.apache.aries.blueprint.container.AggregateConverter.Convertible) ServiceListener(org.apache.aries.blueprint.utils.ServiceListener) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ServiceFactory(org.osgi.framework.ServiceFactory) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Collection(java.util.Collection) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Example 4 with UnwrapperedBeanHolder

use of org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder in project aries by apache.

the class BlueprintRepository method createAll.

public Map<String, Object> createAll(Collection<String> names, Collection<Class<?>> proxyInterfaces) throws ComponentDefinitionException {
    ExecutionContext oldContext = ExecutionContext.Holder.setContext(this);
    try {
        Map<String, Object> instances = createInstances(names);
        for (String name : instances.keySet()) {
            Object obj = instances.get(name);
            if (obj instanceof UnwrapperedBeanHolder)
                obj = BeanRecipe.wrap((UnwrapperedBeanHolder) obj, proxyInterfaces);
            instances.put(name, convert(name, obj));
        }
        return instances;
    } finally {
        ExecutionContext.Holder.setContext(oldContext);
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Aggregations

UnwrapperedBeanHolder (org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)4 AccessControlContext (java.security.AccessControlContext)2 Collection (java.util.Collection)2 ExecutionContext (org.apache.aries.blueprint.di.ExecutionContext)2 PrivilegedAction (java.security.PrivilegedAction)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Convertible (org.apache.aries.blueprint.container.AggregateConverter.Convertible)1 ServiceListener (org.apache.aries.blueprint.utils.ServiceListener)1 ServiceFactory (org.osgi.framework.ServiceFactory)1 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)1 ReifiedType (org.osgi.service.blueprint.container.ReifiedType)1