KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > CompositeAspectComponent


1 /*
2   Copyright (C) 2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.ACManager;
26
27 /**
28  * This class is the root class for the composite aspect components
29  * defined by JAC aspect programmers.
30  *
31  * <p>A composite aspect is a kind of facade that factorizes and
32  * simplifies the use of a set of sub-aspects (called chidren
33  * aspects). For instance, the user and authentication aspects of JAC
34  * can be factorized in on unique aspect in order to simplify the use
35  * (factorization) and to eliminate useless dependencies.
36  *
37  * @author <a HREF="mailto:renaud@aopsys.com">Renaud Pawlak</a> */

38
39 public class CompositeAspectComponent extends AspectComponent {
40     static Logger logger = Logger.getLogger("aspects.composite");
41
42     Map JavaDoc childClasses=new HashMap JavaDoc();
43     Map JavaDoc children=new HashMap JavaDoc();
44     Map JavaDoc defaultConfigs=new HashMap JavaDoc();
45
46     /**
47      * The default constructor.
48      *
49      * <p>Should be redefined be the programmer in order to declare the child aspects.
50      *
51      * @see #addChild(String,Class) */

52     public CompositeAspectComponent () {
53         super();
54     }
55
56     /**
57      * Invoke this method in the constructor in order to disable the
58      * application of the default configuration for a given child
59      * aspect component.
60      *
61      * <p>Note that the default configs are defined by the
62      * <code>getDefaultConfig</code> method. By default, all the
63      * children's default configs are applied. */

64     protected void disableDefaultConfigs(String JavaDoc childName) {
65         defaultConfigs.put(childName,Boolean.FALSE);
66     }
67
68     /**
69      * Declares a new child for this composite (should be invoked from
70      * the constructor).
71      *
72      * @param name the child's name (can be any unique id)
73      * @param acClass the aspect component's class of the child */

74     protected void addChild(String JavaDoc name,Class JavaDoc acClass) {
75         logger.debug("addChild("+name+
76                   ","+acClass+")"+" to composite "+getName());
77         childClasses.put(name,acClass);
78     }
79
80     /**
81      * Removes a child for this composite (do not use unless you know
82      * what you are doing).
83      *
84      * @param name the child's name as given in the {@link
85      * #addChild(String,Class)} method */

86     protected void removeChild(String JavaDoc name) {
87         logger.debug("removeChild("+name+
88                   ") from composite "+getName()+")");
89         childClasses.remove(name); }
90
91     /**
92      * Returns a child aspect component from its name.
93      *
94      * <p>This method should be used in configuration methods of the
95      * composite to delegate the configuration to the child
96      * aspects. */

97     protected AspectComponent getChild(String JavaDoc name) {
98         logger.debug("getChild("+name+
99                   ") in composite "+getName());
100         return (AspectComponent)children.get(name);
101     }
102
103     /**
104      * Returns the internal name of a child (applicationName.name).
105      *
106      * @param name the child's name */

107     protected String JavaDoc getChildActualName(String JavaDoc name) {
108         logger.debug("getChildActualName("+name+
109                   ") in composite "+getName());
110         return getChild(name).getApplication()+"."+name;
111     }
112
113     /**
114      * Returns the internal name of a child, as it is registered in
115      * the JAC ACManager.
116      *
117      * <p>Note that this method will return null if the child is not
118      * yet registered in the AC manager. This is the case at
119      * configuration time. Thus, {@link #getChildActualName(String)}
120      * should be prefered.
121      *
122      * @param name the child's name */

123     protected String JavaDoc getChildRegisteredName(String JavaDoc name) {
124         logger.debug("getChildActualName("+name+
125                   ") in composite "+getName());
126         return ACManager.getACM().getName(getChild(name));
127     }
128
129     /**
130      * This method is upcalled by the system when the composite aspect
131      * is about to be configured.
132      *
133      * <p>This method instantiates all the declared children (with
134      * {@link #addChild(String,Class)} method. */

135     public void beforeConfiguration() throws Exception JavaDoc {
136         logger.debug("beforeConfiguration()"+
137                   " for composite "+getName());
138         Iterator JavaDoc it=childClasses.entrySet().iterator();
139         while(it.hasNext()) {
140             Map.Entry JavaDoc entry=(Map.Entry JavaDoc)it.next();
141             Class JavaDoc acClass=(Class JavaDoc)entry.getValue();
142             AspectComponent instance = (AspectComponent) acClass.newInstance();
143             instance.setApplication(getApplication());
144             children.put(entry.getKey(),instance);
145             // apply the default configurations if needed
146
String JavaDoc[] defaults = instance.getDefaultConfigs();
147             for (int i=0; i<defaults.length; i++) {
148                 instance.configure((String JavaDoc)entry.getKey(),defaults[i]);
149             }
150         }
151
152     }
153
154     /**
155      * This method is upcalled by the system when the composite aspect
156      * is actually registered in the AC manager.
157      *
158      * <p>It symply registers all the children with the right
159      * names. */

160     public final void doRegister() {
161         logger.debug("registerChildren()"+
162                      " for composite "+getName());
163         Iterator JavaDoc it=children.entrySet().iterator();
164         while(it.hasNext()) {
165             Map.Entry JavaDoc entry=(Map.Entry JavaDoc)it.next();
166             AspectComponent ac=(AspectComponent)entry.getValue();
167             ACManager.get().register( ac.getApplication() +
168                                       "." + entry.getKey(), ac );
169         }
170         logger.debug("end registerChildren() => ACM dump: "+
171                      ACManager.get().getPrintableString());
172
173     }
174
175
176     /**
177      * This method is upcalled by the system when the composite aspect
178      * is actually unregistered in the AC manager.
179      *
180      * <p>It symply unregisters all the children. */

181     public final void doUnregister() {
182         logger.debug("unregisterChildren()"+
183                      " for composite "+getName());
184         Iterator JavaDoc it=children.entrySet().iterator();
185         while(it.hasNext()) {
186             Map.Entry JavaDoc entry=(Map.Entry JavaDoc)it.next();
187             AspectComponent ac=(AspectComponent)entry.getValue();
188             ACManager.get().unregister( ac.getApplication() +
189                                         "." + entry.getKey() );
190         }
191     }
192
193 }
194
Popular Tags