1 17 18 package org.objectweb.jac.aspects.naming; 19 20 import java.util.Hashtable ; 21 import java.util.Map ; 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 32 33 40 41 public class NamingAC extends AspectComponent { 42 43 static Logger logger = Logger.getLogger("naming"); 44 static Logger loggerSerial = Logger.getLogger("serialization"); 45 46 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 73 public void whenUsingNewInstance(Interaction interaction) { 74 if (interaction.wrappee==null) { 79 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 )attr(Naming.FORCE_NAME)); 88 attrdef(Naming.FORCE_NAME, null); 89 } 90 } 92 93 107 108 public void whenRemoteInstantiation(Wrappee newInstance, String name) { 109 logger.debug("whenRemoteInstantiation "+newInstance+","+name); 110 NameRepository.get().register(name, newInstance); 111 } 112 113 126 public String nameObject(Object object, String name) { 127 NameRepository nr = (NameRepository)NameRepository.get(); 128 if (nr==null) { 129 logger.error("Error: cannot create name repository."); 130 throw new RuntimeException ("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 147 public Map getNameCounters() { 148 Hashtable counters = new Hashtable (); 149 counters.putAll(nameGen); 150 ((ACManager)ACManager.get()).getNameCounters(counters); 151 return counters; 152 } 153 154 public void updateNameCounters(Map counters) { 155 nameGen.update(counters); 156 } 157 158 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 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 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 } 206 } 207 return orgObject; 208 } 209 } 210 | Popular Tags |