KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > naming > NamingAC


1 /*
2   Copyright (C) 2001-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 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.aspects.naming;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22 import org.apache.log4j.Logger;
23 import org.objectweb.jac.core.*;
24 import org.objectweb.jac.core.Interaction;
25 import org.objectweb.jac.core.rtti.ClassItem;
26 import org.objectweb.jac.util.ExtArrays;
27 import org.objectweb.jac.util.Log;
28
29 /**
30  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
31  */

32
33 /**
34  * Handles the naming aspect within the JAC system.
35  *
36  * <p>This aspect component automatically registers all the created
37  * JAC object into the sole instance of <code>NameRepository</code>.
38  *
39  * @see NameRepository */

40
41 public class NamingAC extends AspectComponent {
42
43     static Logger logger = Logger.getLogger("naming");
44     static Logger loggerSerial = Logger.getLogger("serialization");
45
46     /**
47      * Creates a naming aspect component.
48      *
49      * <p>It creates the name repository if it does not exist yet.
50      *
51      * @see NameRepository
52      */

53     public NamingAC() {
54
55         setSystemListener(true);
56
57         NameRepository nr = (NameRepository) NameRepository.get();
58         if (nr == null) {
59             System.out.println ("Error: cannot create name repository.");
60             System.exit(-1);
61         }
62     }
63
64     NameGenerator nameGen = new NameGenerator();
65
66     /**
67      * Name a new instance using the <code>nameObject()</code> and
68      * <code>generateName()</code> methods.
69      *
70      * @see #nameObject(Object,String)
71      * @see NameGenerator#generateName(String)
72      */

73     public void whenUsingNewInstance(Interaction interaction) {
74         //if (interaction.wrappee.getClass().getName().startsWith("org.objectweb.jac.lib.java.util.")) {
75
// do nothing for collections
76
// return;
77
//}
78
if (interaction.wrappee==null) {
79             // do nothing for static methods
80
return;
81         }
82         logger.debug("when using new instance "+interaction.wrappee.getClass());
83         if (NameRepository.get().getName(interaction.wrappee)!=null) {
84             return;
85         }
86         if ( !(interaction.wrappee instanceof AspectComponent) ) {
87             nameObject(interaction.wrappee, (String JavaDoc)attr(Naming.FORCE_NAME));
88             attrdef(Naming.FORCE_NAME, null);
89         }
90         //System.out.println( "EO Naming " + interaction.wrappee );
91
}
92
93     /**
94      * This method is upcalled when a new object is instanciated from a
95      * remote site.
96      *
97      * <p>The name that is passed is the name of the remote reference
98      * that has been used to create the object. Thus, the default
99      * behavior of the naming aspect is to register the new object into
100      * the name repository.
101      *
102      * @param newInstance the remotly created new instance
103      * @param name the name that was forwarded from the creator host
104      *
105      * @see NameRepository
106      * @see org.objectweb.jac.util.Repository#register(String,Object) */

107
108     public void whenRemoteInstantiation(Wrappee newInstance, String JavaDoc name) {
109         logger.debug("whenRemoteInstantiation "+newInstance+","+name);
110         NameRepository.get().register(name, newInstance);
111     }
112
113     /**
114      * Name a single object by registering it in the name repository
115      * (see the <code>NameRepository</code> class).
116      *
117      * <p>If the repository does not exist, it is created.
118      *
119      * @param object the object to name
120      * @param name the given name
121      * @return the new name of the object
122      *
123      * @see NameRepository
124      * @see org.objectweb.jac.util.Repository#register(String,Object)
125      */

126     public String JavaDoc nameObject(Object JavaDoc object, String JavaDoc name) {
127         NameRepository nr = (NameRepository)NameRepository.get();
128         if (nr==null) {
129             logger.error("Error: cannot create name repository.");
130             throw new RuntimeException JavaDoc("Error: cannot create name repository.");
131         }
132         if (name == null) {
133             name = nameGen.generateName(object.getClass().getName());
134             logger.debug("generated name "+object+" -> "+name);
135             name = ACManager.getACM().whenNameObject(object,name);
136         } else {
137             logger.debug("forced name "+object+" -> "+name);
138         }
139         logger.debug("Registering "+object+" -> "+name);
140         nr.register(name, object);
141         return name;
142     }
143
144     /**
145      * Returns the counters used to generate unique names
146      */

147     public Map JavaDoc getNameCounters() {
148         Hashtable JavaDoc counters = new Hashtable JavaDoc();
149         counters.putAll(nameGen);
150         ((ACManager)ACManager.get()).getNameCounters(counters);
151         return counters;
152     }
153
154     public void updateNameCounters(Map JavaDoc counters) {
155         nameGen.update(counters);
156     }
157
158     /**
159      * Add the name to the <code>SerializedJacObject</code> structure
160      * when serialized.
161      *
162      * <p>This allows the name to be serialized with a JAC object so
163      * that the object can be named with the same name when deserialized
164      * (see <code>BindingAC</code>).
165      *
166      * @param finalObject the structure corresponding to the object
167      * that is being serialized
168      *
169      * @see BindingAC#whenDeserialized(SerializedJacObject,Wrappee)
170      * @see org.objectweb.jac.core.SerializedJacObject#setACInfos(String,Object)
171      */

172
173     public Wrappee whenSerialized(Wrappee orgObject,
174                                   SerializedJacObject finalObject) {
175
176         if(orgObject.getClass().getName().startsWith("org.objectweb.jac.lib.java.util")) {
177             return orgObject;
178         }
179
180         NameRepository nr = (NameRepository) NameRepository.get();
181         String JavaDoc name = nr.getName(orgObject);
182         if (name!=null) {
183             loggerSerial.debug("NAMING: serializing object " + name );
184             finalObject.setACInfos(ACManager.get().getName(this), name);
185         } else {
186             if (Wrapping.isExtendedBy(orgObject,null,
187                                       org.objectweb.jac.wrappers.ForwardingWrapper.class)) {
188                 loggerSerial.debug("NAMING: Serializing forwardee for " + orgObject );
189                 Object JavaDoc newOrg = Wrapping.invokeRoleMethod(orgObject,
190                                                           "getForwardee",
191                                                           ExtArrays.emptyObjectArray);
192                 if (newOrg!=null) {
193                     orgObject = (Wrappee)newOrg;
194                 } else {
195                     logger.warn("Oooops!! Forwardee is " + newOrg);
196                 }
197             }
198             name = nr.getName(orgObject);
199             if (name!=null) {
200                 loggerSerial.debug("NAMING: serializing object " + name +" (PASS 2)");
201                 finalObject.setACInfos(ACManager.get().getName(this), name);
202             } else {
203                 logger.warn("Oooops!! Object is still unamed: "+orgObject);
204                 //System.out.println( "NAMING: still a forwarder? => " + orgObject.isExtendedBy( jac.wrappers.ForwarderWrapper.class) );
205
}
206         }
207         return orgObject;
208     }
209 }
210
Popular Tags