Search in sources :

Example 6 with Recipe

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

the class BlueprintRepository method push.

public void push(Recipe recipe) {
    LinkedList<Recipe> list = stack.get();
    if (list != null && list.contains(recipe)) {
        ArrayList<Recipe> circularity = new ArrayList<Recipe>(list.subList(list.indexOf(recipe), list.size()));
        // remove anonymous nodes from circularity list
        for (Iterator<Recipe> iterator = circularity.iterator(); iterator.hasNext(); ) {
            Recipe item = iterator.next();
            if (item != recipe && item.getName() == null) {
                iterator.remove();
            }
        }
        // add ending node to list so a full circuit is shown
        circularity.add(recipe);
        throw new CircularDependencyException(circularity);
    }
    if (list == null) {
        list = new LinkedList<Recipe>();
        stack.set(list);
    }
    list.add(recipe);
}
Also used : 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) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException)

Example 7 with Recipe

use of org.apache.aries.blueprint.di.Recipe 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 8 with Recipe

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

the class BeanRecipe method getDependencies.

public List<Recipe> getDependencies() {
    List<Recipe> recipes = new ArrayList<Recipe>();
    for (Object o : properties.values()) {
        if (o instanceof Recipe) {
            Recipe recipe = (Recipe) o;
            recipes.add(recipe);
        }
    }
    recipes.addAll(getConstructorDependencies());
    return recipes;
}
Also used : Recipe(org.apache.aries.blueprint.di.Recipe) AbstractRecipe(org.apache.aries.blueprint.di.AbstractRecipe) ArrayList(java.util.ArrayList)

Example 9 with Recipe

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

the class WiringTest method testWiring.

public void testWiring() throws Exception {
    ComponentDefinitionRegistryImpl registry = parse("/test-wiring.xml");
    Repository repository = new TestBlueprintContainer(registry).getRepository();
    Object obj1 = repository.create("pojoA");
    assertNotNull(obj1);
    assertTrue(obj1 instanceof PojoA);
    PojoA pojoa = (PojoA) obj1;
    // test singleton scope
    assertTrue(obj1 == repository.create("pojoA"));
    Object obj2 = repository.create("pojoB");
    assertNotNull(obj2);
    assertTrue(obj2 instanceof PojoB);
    PojoB pojob = (PojoB) obj2;
    assertNotNull(pojoa.getPojob());
    assertNotNull(pojoa.getPojob().getUri());
    assertNotNull(pojoa.getList());
    assertEquals("list value", pojoa.getList().get(0));
    assertEquals(new Integer(55), pojoa.getList().get(2));
    assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getList().get(3));
    Object c0 = pojoa.getList().get(1);
    Object c1 = pojoa.getList().get(4);
    assertNotNull(c0);
    assertNotNull(c1);
    assertEquals(PojoB.class, c0.getClass());
    assertEquals(PojoB.class, c1.getClass());
    assertNotSame(c0, c1);
    assertNotNull(pojoa.getArray());
    assertEquals("list value", pojoa.getArray()[0]);
    assertEquals(pojob, pojoa.getArray()[1]);
    assertEquals(new Integer(55), pojoa.getArray()[2]);
    assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getArray()[3]);
    assertNotNull(pojoa.getSet());
    assertTrue(pojoa.getSet().contains("set value"));
    assertTrue(pojoa.getSet().contains(pojob.getUri()));
    assertTrue(pojoa.getSet().contains(URI.create("http://geronimo.apache.org")));
    assertNotNull(pojoa.getMap());
    assertEquals("val", pojoa.getMap().get("key"));
    assertEquals(pojob, pojoa.getMap().get(pojob));
    assertEquals(URI.create("http://geronimo.apache.org"), pojoa.getMap().get(new Integer(5)));
    assertNotNull(pojoa.getProps());
    assertEquals("value1", pojoa.getProps().get("key1"));
    assertEquals("value2", pojoa.getProps().get("2"));
    assertEquals("bar", pojoa.getProps().get("foo"));
    assertNotNull(pojoa.getNumber());
    assertEquals(new BigInteger("10"), pojoa.getNumber());
    assertNotNull(pojoa.getIntArray());
    assertEquals(3, pojoa.getIntArray().length);
    assertEquals(1, pojoa.getIntArray()[0]);
    assertEquals(50, pojoa.getIntArray()[1]);
    assertEquals(100, pojoa.getIntArray()[2]);
    assertNotNull(pojoa.getNumberArray());
    assertEquals(4, pojoa.getNumberArray().length);
    assertEquals(new Integer(1), pojoa.getNumberArray()[0]);
    assertEquals(new BigInteger("50"), pojoa.getNumberArray()[1]);
    assertEquals(new Long(100), pojoa.getNumberArray()[2]);
    assertEquals(new Integer(200), pojoa.getNumberArray()[3]);
    // test init-method
    assertEquals(true, pojob.getInitCalled());
    // test service
    Object obj3 = repository.create("service1");
    assertNotNull(obj3);
    assertTrue(obj3 instanceof ServiceRegistration);
    ExecutionContext.Holder.setContext((ExecutionContext) repository);
    for (Recipe r : ((ServiceRecipe) repository.getRecipe("service1")).getDependencies()) {
        if (r instanceof MapRecipe) {
            Map m = (Map) r.create();
            assertEquals("value1", m.get("key1"));
            assertEquals("value2", m.get("key2"));
            assertTrue(m.get("key3") instanceof List);
        }
    }
    ExecutionContext.Holder.setContext(null);
    // tests 'prototype' scope
    Object obj4 = repository.create("pojoC");
    assertNotNull(obj4);
    assertTrue(obj4 != repository.create("pojoC"));
    repository.destroy();
    // test destroy-method
    assertEquals(true, pojob.getDestroyCalled());
}
Also used : PojoB(org.apache.aries.blueprint.pojos.PojoB) ServiceRecipe(org.apache.aries.blueprint.container.ServiceRecipe) MapRecipe(org.apache.aries.blueprint.di.MapRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) PojoA(org.apache.aries.blueprint.pojos.PojoA) MapRecipe(org.apache.aries.blueprint.di.MapRecipe) ServiceRecipe(org.apache.aries.blueprint.container.ServiceRecipe) BigInteger(java.math.BigInteger) Repository(org.apache.aries.blueprint.di.Repository) BlueprintRepository(org.apache.aries.blueprint.container.BlueprintRepository) BigInteger(java.math.BigInteger) MyObject(org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject) ArrayList(java.util.ArrayList) List(java.util.List) ComponentDefinitionRegistryImpl(org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl) HashMap(java.util.HashMap) Map(java.util.Map) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 10 with Recipe

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

the class BlueprintContainerImpl method updateUninstantiatedRecipes.

private void updateUninstantiatedRecipes() {
    Repository tmpRepo = new NoOsgiRecipeBuilder(this, tempRecipeIdSpace).createRepository();
    LOGGER.debug("Updating blueprint repository");
    for (String name : repository.getNames()) {
        if (repository.getInstance(name) == null) {
            LOGGER.debug("Removing uninstantiated recipe {}", new Object[] { name });
            repository.removeRecipe(name);
        } else {
            LOGGER.debug("Recipe {} is already instantiated", new Object[] { name });
        }
    }
    for (String name : tmpRepo.getNames()) {
        if (repository.getInstance(name) == null) {
            LOGGER.debug("Adding new recipe {}", new Object[] { name });
            Recipe r = tmpRepo.getRecipe(name);
            if (r != null) {
                repository.putRecipe(name, r);
            }
        } else {
            LOGGER.debug("Recipe {} is already instantiated and cannot be updated", new Object[] { name });
        }
    }
}
Also used : Repository(org.apache.aries.blueprint.di.Repository) Recipe(org.apache.aries.blueprint.di.Recipe)

Aggregations

Recipe (org.apache.aries.blueprint.di.Recipe)21 RefRecipe (org.apache.aries.blueprint.di.RefRecipe)12 CollectionRecipe (org.apache.aries.blueprint.di.CollectionRecipe)11 ArrayList (java.util.ArrayList)10 IdRefRecipe (org.apache.aries.blueprint.di.IdRefRecipe)9 ValueRecipe (org.apache.aries.blueprint.di.ValueRecipe)6 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)6 MapRecipe (org.apache.aries.blueprint.di.MapRecipe)5 ArrayRecipe (org.apache.aries.blueprint.di.ArrayRecipe)4 ComponentFactoryRecipe (org.apache.aries.blueprint.di.ComponentFactoryRecipe)4 DependentComponentFactoryRecipe (org.apache.aries.blueprint.di.DependentComponentFactoryRecipe)4 PassThroughRecipe (org.apache.aries.blueprint.di.PassThroughRecipe)4 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Map (java.util.Map)3 AbstractRecipe (org.apache.aries.blueprint.di.AbstractRecipe)3 CircularDependencyException (org.apache.aries.blueprint.di.CircularDependencyException)3