Search in sources :

Example 1 with ExecutionContext

use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.

the class BlueprintRepository method getAllRecipes.

public Set<Recipe> getAllRecipes(String... names) {
    ExecutionContext oldContext = ExecutionContext.Holder.setContext(this);
    try {
        Set<Recipe> allRecipes = new HashSet<Recipe>();
        Collection<String> topLevel = names != null && names.length > 0 ? Arrays.asList(names) : recipes.keySet();
        for (String name : topLevel) {
            internalGetAllRecipes(allRecipes, getRecipe(name));
        }
        return allRecipes;
    } finally {
        ExecutionContext.Holder.setContext(oldContext);
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) IdRefRecipe(org.apache.aries.blueprint.di.IdRefRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) CollectionRecipe(org.apache.aries.blueprint.di.CollectionRecipe) RefRecipe(org.apache.aries.blueprint.di.RefRecipe) HashSet(java.util.HashSet)

Example 2 with ExecutionContext

use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.

the class BlueprintContainerImpl method injectBeanInstance.

public void injectBeanInstance(BeanMetadata bmd, Object o) throws IllegalArgumentException, ComponentDefinitionException {
    ExecutionContext origContext = ExecutionContext.Holder.setContext((ExecutionContext) getRepository());
    try {
        ComponentMetadata cmd = componentDefinitionRegistry.getComponentDefinition(bmd.getId());
        if (cmd == null || cmd != bmd) {
            throw new IllegalArgumentException(bmd.getId() + " not found in blueprint container");
        }
        Recipe r = this.getRepository().getRecipe(bmd.getId());
        if (r instanceof BeanRecipe) {
            BeanRecipe br = (BeanRecipe) r;
            if (!br.getType().isInstance(o)) {
                throw new IllegalArgumentException("Instance class " + o.getClass().getName() + " is not an instance of " + br.getClass());
            }
            br.setProperties(o);
        } else {
            throw new IllegalArgumentException(bmd.getId() + " does not refer to a BeanRecipe");
        }
    } finally {
        ExecutionContext.Holder.setContext(origContext);
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) Recipe(org.apache.aries.blueprint.di.Recipe) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata)

Example 3 with ExecutionContext

use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.

the class AbstractServiceReferenceRecipe method getExtendedOsgiFilter.

private String getExtendedOsgiFilter() {
    if (filterRecipe != null) {
        Object object;
        BlueprintRepository repository = ((BlueprintContainerImpl) blueprintContainer).getRepository();
        ExecutionContext oldContext = null;
        try {
            oldContext = ExecutionContext.Holder.setContext(repository);
            object = filterRecipe.create();
        } finally {
            ExecutionContext.Holder.setContext(oldContext);
        }
        if (object != null) {
            String flt = object.toString();
            if (flt != null && flt.length() > 0) {
                if (!flt.startsWith("(")) {
                    flt = "(" + flt + ")";
                }
                return flt;
            }
        }
    }
    return null;
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext)

Example 4 with ExecutionContext

use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.

the class ReflectionUtilsTest method before.

@BeforeClass
public static void before() throws ClassNotFoundException {
    mockBlueprint = EasyMock.createNiceMock(ExtendedBlueprintContainer.class);
    final Capture<String> nameCapture = new Capture<String>();
    EasyMock.expect(mockBlueprint.loadClass(EasyMock.capture(nameCapture))).andAnswer(new IAnswer<Class<?>>() {

        public Class<?> answer() throws Throwable {
            return Thread.currentThread().getContextClassLoader().loadClass(nameCapture.getValue());
        }
    });
    EasyMock.replay(mockBlueprint);
    ExecutionContext.Holder.setContext(new ExecutionContext() {

        public void addPartialObject(String name, Object object) {
        }

        public boolean containsObject(String name) {
            return false;
        }

        public Object convert(Object value, ReifiedType type) throws Exception {
            if (type.getRawClass().equals(Inconvertible.class))
                throw new Exception();
            else if (type.getRawClass().equals(String.class))
                return String.valueOf(value);
            else if (type.getRawClass().equals(List.class)) {
                if (value == null)
                    return null;
                else if (value instanceof Collection)
                    return new ArrayList((Collection) value);
                else
                    throw new Exception();
            } else if (value == null)
                return null;
            else if (type.getRawClass().isInstance(value))
                return value;
            else
                throw new Exception();
        }

        public boolean canConvert(Object value, ReifiedType type) {
            if (value instanceof Inconvertible)
                return false;
            else if (type.getRawClass().equals(String.class))
                return true;
            else if (type.getRawClass().equals(List.class) && (value == null || value instanceof Collection))
                return true;
            else
                return false;
        }

        public Object getObject(String name) {
            return null;
        }

        public Object getPartialObject(String name) {
            return null;
        }

        public Recipe getRecipe(String name) {
            return null;
        }

        public Class loadClass(String className) throws ClassNotFoundException {
            return null;
        }

        public Recipe pop() {
            return null;
        }

        public void push(Recipe recipe) throws CircularDependencyException {
        }

        public void removePartialObject(String name) {
        }

        public Future<Object> addFullObject(String name, Future<Object> object) {
            return null;
        }
    });
}
Also used : Recipe(org.apache.aries.blueprint.di.Recipe) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) ArrayList(java.util.ArrayList) Capture(org.easymock.Capture) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) Collection(java.util.Collection) Future(java.util.concurrent.Future) BeforeClass(org.junit.BeforeClass) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExtendedBlueprintContainer(org.apache.aries.blueprint.services.ExtendedBlueprintContainer) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException) BeforeClass(org.junit.BeforeClass)

Example 5 with ExecutionContext

use of org.apache.aries.blueprint.di.ExecutionContext 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)

Aggregations

ExecutionContext (org.apache.aries.blueprint.di.ExecutionContext)7 Recipe (org.apache.aries.blueprint.di.Recipe)3 Collection (java.util.Collection)2 UnwrapperedBeanHolder (org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)2 ExtendedBlueprintContainer (org.apache.aries.blueprint.services.ExtendedBlueprintContainer)2 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)2 ReifiedType (org.osgi.service.blueprint.container.ReifiedType)2 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Future (java.util.concurrent.Future)1 Convertible (org.apache.aries.blueprint.container.AggregateConverter.Convertible)1 CircularDependencyException (org.apache.aries.blueprint.di.CircularDependencyException)1 CollectionRecipe (org.apache.aries.blueprint.di.CollectionRecipe)1 IdRefRecipe (org.apache.aries.blueprint.di.IdRefRecipe)1 RefRecipe (org.apache.aries.blueprint.di.RefRecipe)1 ServiceListener (org.apache.aries.blueprint.utils.ServiceListener)1