KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > rtti > MetaItem


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak, Laurent Martelli
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 License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.core.rtti;
19
20 import java.lang.reflect.*;
21 import java.util.*;
22 import org.objectweb.jac.core.ACManager;
23 import org.objectweb.jac.util.ExtArrays;
24
25 /**
26  * This class defines the super class for all the meta items whithin
27  * the rtti aspect.<p>
28  *
29  * A meta item encapsulates a <code>java.lang.reflect</code> item so
30  * that the user of this item can add extra informations
31  * (attributes). Typically this feature can be used by an aspect to
32  * tag an element of the model to react to this tag later on.<p>
33  *
34  * Examples:<p> <ul>
35  *
36  * <li>A persistence aspect can tag some field persistent, add methods
37  * that change the object states even if they do not fit naming
38  * conventions...<p>
39  *
40  * <li>A GUI can tag a given field to be invisible or a class to be
41  * displayed by a special view (eg a given Swing component)...
42  *
43  * </ul>
44  *
45  * @author Renaud Pawlak
46  * @author Laurent Martelli
47  */

48
49 public abstract class MetaItem {
50
51     /** A correspondance table between the RRTI attributes and the
52         aspect components that set them. */

53     protected static HashMap attrACs = new HashMap();
54
55     /**
56      * Unsets all the attributes of all the RTTI items that have been
57      * set by a given aspect component.<p>
58      *
59      * @param acName the aspect component name
60      * @see #unsetAttribute(String) */

61    
62     public static void unsetAttributesFor( String JavaDoc acName ) {}
63
64     /* A <code>name -> value</code> container. */
65     private HashMap attributes = new HashMap();
66
67     static ACManager acManager;
68     static Method acManagerGetObjectMethod;
69     static Method acManagerGetMethod;
70    
71     static {
72         try {
73             acManagerGetMethod =
74                 Class.forName("org.objectweb.jac.core.ACManager").getMethod("get",new Class JavaDoc[0]);
75             acManagerGetObjectMethod =
76                 Class.forName("org.objectweb.jac.core.ACManager").getMethod(
77                     "getObject",new Class JavaDoc[]{String JavaDoc.class});
78         } catch(Exception JavaDoc e) {
79             e.printStackTrace();
80         }
81     }
82
83     Object JavaDoc getAC(String JavaDoc name) {
84         Object JavaDoc ac = null;
85         try {
86             if (acManager==null) {
87                 acManager =
88                     (ACManager)acManagerGetMethod.invoke(null,ExtArrays.emptyObjectArray);
89             }
90             ac = acManagerGetObjectMethod.invoke(acManager,new Object JavaDoc[]{name});
91         } catch (Exception JavaDoc e) {
92             e.printStackTrace();
93         }
94         return ac;
95     }
96
97     boolean isRegisteredAC( String JavaDoc name ) {
98         return getAC(name)!=null;
99     }
100
101     static Method collabGetMethod = null;
102     static Method collabGetCurACMethod = null;
103
104     String JavaDoc getCurAC() {
105         /*
106           Object acName = null;
107           try {
108           Class collaboration = Class.forName("org.objectweb.jac.core.Collaboration");
109           if (collabGetMethod==null) {
110           collabGetMethod = collaboration.getMethod("get",ExtArrays.emptyClassArray);
111           }
112           Object coll = collabGetMethod.invoke(null,ExtArrays.emptyObjectArray);
113           if (collabGetCurACMethod==null) {
114           collabGetCurACMethod =
115           coll.getClass().getMethod("getCurAC",ExtArrays.emptyClassArray);
116           }
117           acName = collabGetCurACMethod.invoke(coll,ExtArrays.emptyObjectArray);
118           } catch (Exception e) {
119           e.printStackTrace();
120           }
121           return (String)acName;
122         */

123         return null;
124     }
125
126     static AttributeController accessController = null;
127
128     /**
129      * Registers a new access controller for this application.
130      *
131      * @param controller the controller object
132      */

133     public static void registerAccessController(AttributeController controller) {
134         accessController = controller;
135     }
136
137     Object JavaDoc controlledAccess(Object JavaDoc substance, String JavaDoc name, Object JavaDoc value) {
138         if ( accessController==null ||
139              ! (name.equals("GuiAC.VISIBLE") ||
140                 name.equals("GuiAC.EDITABLE") ||
141                 name.equals("GuiAC.ADDABLE") ||
142                 name.equals("GuiAC.CREATABLE") ||
143                 name.equals("GuiAC.REMOVABLE")) )
144             return value;
145
146         Object JavaDoc result = null;
147         try {
148             result = accessController.controlAttribute(substance,this,name,value);
149         } catch(Exception JavaDoc e) {
150             e.printStackTrace();
151             return value;
152         }
153         return result;
154     }
155
156
157     /**
158      * Gets the value of an attribute.
159      *
160      * @param name the name of the attribute
161      * @return the value of the attribute
162      */

163
164     public Object JavaDoc getAttribute(String JavaDoc name) {
165         return getAttribute(name,false);
166     }
167
168     /**
169      * Gets the value of an attribute even if the aspect if not yet
170      * configured and weaved.
171      *
172      * @param name the name of the attribute
173      * @return the value of the attribute
174      */

175     public Object JavaDoc getAttributeAlways(String JavaDoc name) {
176         return getAttribute(name,true);
177     }
178
179     /**
180      * Gets the value of an attribute.
181      *
182      * @param name the name of the attribute
183      * @param always if true, return a value even the aspect is not weaved
184      * @return the value of the attribute
185      *
186      */

187     public Object JavaDoc getAttribute(String JavaDoc name, boolean always) {
188         String JavaDoc acName = (String JavaDoc)attrACs.get(name);
189         if (!always && (acName!=null && (!isRegisteredAC(acName))) ) {
190             return null;
191         }
192         Object JavaDoc value=attributes.get(name);
193         if (value==null && itemClass!=null) {
194             value = itemClass.getAttribute(name);
195         }
196         return controlledAccess(null,name,value);
197     }
198
199     /**
200      * Gets the value of a boolean attribute.
201      *
202      * @param name the name of the attribute
203      * @param defValue default value for the attribute if it is not set
204      * @return the value of the attribute
205      */

206     public boolean getBoolean(String JavaDoc name, boolean defValue) {
207         Boolean JavaDoc value = (Boolean JavaDoc)getAttribute(name);
208         if (value==null)
209             return defValue;
210         else
211             return value.booleanValue();
212     }
213
214     /**
215      * Gets the value of an attribute.
216      *
217      * @param substance
218      * @param name the name of the attribute
219      * @return the value of the attribute
220      */

221
222     public Object JavaDoc getAttribute(Object JavaDoc substance, String JavaDoc name) {
223         String JavaDoc acName = (String JavaDoc)attrACs.get(name);
224         if( acName!=null && (!isRegisteredAC(acName)) ) {
225             return null;
226         }
227         Object JavaDoc value=attributes.get(name);
228         if (value==null && itemClass!=null) {
229             value = itemClass.getAttribute(name);
230         }
231         return controlledAccess(substance,name,value);
232     }
233
234     /**
235      * Sets the value of an attribute.
236      *
237      * @param name the name of the attribute
238      * @param value the value of the attribute
239      */

240
241     public final void setAttribute(String JavaDoc name, Object JavaDoc value) {
242         if (name == null)
243             return;
244         String JavaDoc acName = (String JavaDoc)getCurAC();
245         if (acName != null) {
246             if ( !attrACs.containsKey(name) ) {
247                 attrACs.put(name,acName);
248             }
249         }
250         attributes.put(name,value);
251     }
252
253     /**
254      * Unsets the value of an attribute.
255      *
256      * @param name the name of the attribute to unset
257      */

258
259     public void unsetAttribute(String JavaDoc name) {
260         attributes.remove(name);
261     }
262
263     /**
264      * This method gets the name of the meta item by delegating to the
265      * actual <code>java.lang.reflect</code> meta item.<p>
266      *
267      * @return the item name */

268
269     public abstract String JavaDoc getName();
270
271     MetaItem itemClass;
272     public void setItemClass(MetaItem itemClass) {
273         this.itemClass = itemClass;
274     }
275     public MetaItem getItemClass() {
276         return itemClass;
277     }
278 }
279
Popular Tags