Search in sources :

Example 1 with CircularDependencyException

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

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

the class WiringTest method testCircularPrototype.

public void testCircularPrototype() throws Exception {
    BlueprintRepository repository = createBlueprintContainer().getRepository();
    try {
        repository.create("circularPrototypeDriver");
        fail("Did not throw exception");
    } catch (CircularDependencyException e) {
    // that's what we expect
    }
    try {
        repository.create("circularPrototype");
        fail("Did not throw exception");
    } catch (CircularDependencyException e) {
    // that's what we expect
    }
}
Also used : BlueprintRepository(org.apache.aries.blueprint.container.BlueprintRepository) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException)

Example 3 with CircularDependencyException

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

the class WiringTest method testRecursive.

public void testRecursive() throws Exception {
    BlueprintRepository repository = createBlueprintContainer().getRepository();
    try {
        repository.create("recursiveConstructor");
        fail("Did not throw exception");
    } catch (ComponentDefinitionException e) {
        if (e.getCause() instanceof CircularDependencyException) {
        // that's what we expect
        } else {
            fail("Did not throw expected exception");
            throw e;
        }
    }
    PojoRecursive pojo;
    pojo = (PojoRecursive) repository.create("recursiveSetter");
    assertNotNull(pojo);
    pojo = (PojoRecursive) repository.create("recursiveInitMethod");
    assertNotNull(pojo);
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) BlueprintRepository(org.apache.aries.blueprint.container.BlueprintRepository) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException) PojoRecursive(org.apache.aries.blueprint.pojos.PojoRecursive)

Example 4 with CircularDependencyException

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

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

the class DependencyGraph method findCircuit.

private void findCircuit(Node node, ArrayList<Recipe> stack) {
    if (stack.contains(node.recipe)) {
        ArrayList<Recipe> circularity = new ArrayList<Recipe>(stack.subList(stack.indexOf(node.recipe), stack.size()));
        // remove anonymous nodes from circularity list
        for (Iterator<Recipe> iterator = circularity.iterator(); iterator.hasNext(); ) {
            Recipe recipe = iterator.next();
            if (recipe != node.recipe && recipe.getName() == null) {
                iterator.remove();
            }
        }
        // add ending node to list so a full circuit is shown
        circularity.add(node.recipe);
        throw new CircularDependencyException(circularity);
    }
    stack.add(node.recipe);
    for (Node reference : node.references) {
        findCircuit(reference, stack);
    }
}
Also used : RefRecipe(org.apache.aries.blueprint.di.RefRecipe) Recipe(org.apache.aries.blueprint.di.Recipe) ArrayList(java.util.ArrayList) CircularDependencyException(org.apache.aries.blueprint.di.CircularDependencyException)

Aggregations

CircularDependencyException (org.apache.aries.blueprint.di.CircularDependencyException)5 ArrayList (java.util.ArrayList)3 Recipe (org.apache.aries.blueprint.di.Recipe)3 BlueprintRepository (org.apache.aries.blueprint.container.BlueprintRepository)2 RefRecipe (org.apache.aries.blueprint.di.RefRecipe)2 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)2 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Future (java.util.concurrent.Future)1 CollectionRecipe (org.apache.aries.blueprint.di.CollectionRecipe)1 ExecutionContext (org.apache.aries.blueprint.di.ExecutionContext)1 IdRefRecipe (org.apache.aries.blueprint.di.IdRefRecipe)1 PojoRecursive (org.apache.aries.blueprint.pojos.PojoRecursive)1 ExtendedBlueprintContainer (org.apache.aries.blueprint.services.ExtendedBlueprintContainer)1 Capture (org.easymock.Capture)1 BeforeClass (org.junit.BeforeClass)1 ReifiedType (org.osgi.service.blueprint.container.ReifiedType)1