KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > aop > dynaop > ComponentAspectsCollection


1 /*****************************************************************************
2  * Copyright (c) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Idea by Rachel Davies, Original code by various *
9  *****************************************************************************/

10 package org.nanocontainer.aop.dynaop;
11
12 import dynaop.Aspects;
13 import org.picocontainer.PicoInitializationException;
14
15 import java.lang.reflect.Constructor JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 /**
23  * Represents the collection of component scoped aspects for a Pico container.
24  * Manages a collection of <code>ComponentAspect</code> objects, and knows how
25  * to register their aspects.
26  *
27  * @author Stephen Molitor
28  * @version $Revision: 3144 $
29  */

30 class ComponentAspectsCollection {
31
32     private final Collection JavaDoc componentsAspects = new ArrayList JavaDoc();
33
34     /**
35      * Adds a component aspect to this collection.
36      *
37      * @param componentAspect the component aspect to add.
38      */

39     void add(ComponentAspect componentAspect) {
40         componentsAspects.add(componentAspect);
41     }
42
43     /**
44      * Registers all aspects whose component pointcut matches
45      * <code>componentKey</code>. Creates and returns a new
46      * <code>dynaop.Aspects</code> object that is the union of the component
47      * and container scoped aspects. By copying the container scoped aspects to
48      * a new <code>dynaop.Aspects</code> and adding the component aspects to
49      * this new object, we avoid having to create proxies on top of proxies.
50      *
51      * @param componentKey the component key.
52      * @param containerAspects the container scoped aspects.
53      * @return a new <code>dynaop.Aspects</code> object that contains
54      * everything in <code>containerAspects</code> plus the component
55      * aspects that match <code>componentKey</code>.
56      */

57     Aspects registerAspects(Object JavaDoc componentKey, Aspects containerAspects) {
58         Aspects aspects = copyAspects(containerAspects);
59         Iterator JavaDoc iterator = componentsAspects.iterator();
60         while (iterator.hasNext()) {
61             ComponentAspect componentAspect = (ComponentAspect) iterator.next();
62             componentAspect.registerAspect(componentKey, aspects);
63         }
64         return aspects;
65     }
66
67     private static Aspects copyAspects(Aspects aspects) {
68         // TODO: Lobby Bob Lee to make the Aspects copy constructor public.
69
try {
70             Constructor JavaDoc constructor = getAspectsCopyConstructor();
71             constructor.setAccessible(true);
72             return (Aspects) constructor.newInstance(new Object JavaDoc[]{aspects});
73         } catch (SecurityException JavaDoc e) {
74             throw new PicoInitializationException("security exception copying dynaop.Aspects", e);
75         } catch (IllegalArgumentException JavaDoc e) {
76             throw new PicoInitializationException("illegal argument passed to dynaop.Aspects copy constructor", e);
77         } catch (InstantiationException JavaDoc e) {
78             throw new PicoInitializationException("error instantiating dynaop.Aspects copy constructor object", e);
79         } catch (IllegalAccessException JavaDoc e) {
80             throw new PicoInitializationException("illegal access exception while trying to make dynaop.Aspects copy constructor accessible", e);
81         } catch (InvocationTargetException JavaDoc e) {
82             throw new PicoInitializationException("dynaop.Aspects copy constructor threw an exception", e);
83         }
84     }
85
86     private static Constructor JavaDoc getAspectsCopyConstructor() {
87         final Class JavaDoc[] params = new Class JavaDoc[]{Aspects.class};
88         Constructor JavaDoc[] constructors = Aspects.class.getDeclaredConstructors();
89         for (int i = 0; i < constructors.length; ++i) {
90             Constructor JavaDoc constructor = constructors[i];
91             if (Arrays.equals(params, constructor.getParameterTypes())) {
92                 return constructor;
93             }
94         }
95         throw new PicoInitializationException("dynaop.Aspects copy constructor not found");
96     }
97
98 }
Popular Tags