1 17 18 package org.objectweb.jac.aspects.distribution.consistency; 19 20 import java.util.*; 21 import org.objectweb.jac.core.*; 22 import org.objectweb.jac.core.rtti.*; 23 import org.objectweb.jac.core.dist.*; 24 25 34 35 public class Consistency { 36 37 39 public static int KS_USER = 0; 40 42 public static int KS_ONE = 1; 43 45 public static int KS_ALL = 2; 46 49 public static int KS_NEXT = 3; 50 53 public static int KS_BTREE = 4; 54 55 56 public static String ALL_METHODS = "ALL"; 57 58 public static String ALL_MODIFIERS = "ALL_MODIFIERS"; 59 60 public static String ALL_GETTERS = "ALL_GETTERS"; 61 62 63 Class consistencyWrapperType; 64 65 int knowledgeStyle = 0; 66 67 int[] knowledgeGraph = null; 68 69 101 102 public Consistency ( Class consistencyWrapperType, 103 int knowledgeStyle, int[] knowledgeGraph ) { 104 this.consistencyWrapperType = consistencyWrapperType; 105 this.knowledgeStyle = knowledgeStyle; 106 this.knowledgeGraph = knowledgeGraph; 107 } 108 109 116 117 public static boolean isReplicatedOn ( String name, RemoteContainer container ) { 118 RemoteRef rr = container.bindTo ( "JAC_name_repository" ); 119 Object distobj = rr.invoke ( 120 "callOrgMethod", new Object [] { "getObject", new Object [] { name } } ); 121 if ( distobj == null ) { 122 return false; 123 } 124 return true; 125 } 126 127 137 138 protected String [] expandMethods( Class type, String [] methods ) { 139 if ( methods == null ) return null; 140 Vector newVM = new Vector(); 141 for ( int i = 0; i < methods.length; i++ ) { 142 if ( methods[i].equals(ALL_METHODS) ) { 143 newVM.addAll( Arrays.asList( 144 ClassRepository.getMethodsName(type) ) ); 145 } 146 else if ( methods[i].equals(ALL_MODIFIERS) ) { 147 newVM.addAll( Arrays.asList( 148 ClassRepository.getModifiersNames(type) ) ); 149 } 150 else if ( methods[i].equals(ALL_GETTERS) ) { 151 newVM.addAll( Arrays.asList( 152 ClassRepository.getGettersNames(type) ) ); 153 } 154 else { 155 newVM.add( methods[i] ); 156 } 157 } 158 String [] newMethods = new String [newVM.size()]; 159 for ( int i = 0; i < newMethods.length; i++ ) { 160 newMethods[i] = (String ) newVM.get(i); 161 } 162 return newMethods; 163 } 164 165 166 185 186 public void deploy ( RemoteRef[] members, Class type, 187 String [] readMethods, 188 String [] writeMethods, 189 String [] callMethods ) 190 throws WrongConsistencyDefinitionException { 191 192 ConsistencyWrapper curwrapper; 193 RemoteRef referee = null; 194 boolean ok = false; 195 196 readMethods = expandMethods( type, readMethods ); 197 writeMethods = expandMethods( type, writeMethods ); 198 callMethods = expandMethods( type, callMethods ); 199 200 try { 201 202 String kstag = ""; 203 204 if (knowledgeStyle == KS_ONE) { 205 206 referee = members[knowledgeGraph[0]]; 207 for (int i = 0; i < members.length; i++) { 208 wrapMember ( members[i], 209 new String [] {"whenRead", "whenWrite", "whenCall"}, 210 new String [][] {readMethods, writeMethods, callMethods}, 211 new RemoteRef[] { referee }, kstag ); 212 } 213 ok = true; 214 } 215 216 if (knowledgeStyle == KS_ALL) { 217 218 for (int i = 0; i < members.length; i++) { 219 wrapMember ( members[i], 220 new String [] {"whenRead", "whenWrite", "whenCall"}, 221 new String [][] {readMethods, writeMethods, callMethods}, 222 members, kstag ); 223 } 224 ok = true; 225 } 226 227 if (knowledgeStyle == KS_NEXT) { 228 229 for (int i = 0; i < members.length; i++) { 230 if ( i == members.length - 1 ) { 231 referee = members[0]; 232 } else { 233 referee = members[i + 1]; 234 } 235 wrapMember ( members[i], 236 new String [] {"whenRead", "whenWrite", "whenCall"}, 237 new String [][] {readMethods, writeMethods, callMethods}, 238 new RemoteRef[] { referee }, kstag ); 239 } 240 ok = true; 241 } 242 } catch ( Exception e ) { e.printStackTrace(); } 243 244 if (!ok) { 245 throw new WrongConsistencyDefinitionException(); 246 } 247 } 248 249 261 262 protected void wrapMember ( RemoteRef member, String [] wrappingMethods, 263 String [][] wrappedMethods, RemoteRef[] knowledge, 264 String kstag ) 265 throws InstantiationException { 266 267 if ( wrappedMethods != null && wrappingMethods != null ) { 268 269 Class wrapper_type = consistencyWrapperType; 270 ConsistencyWrapper wrapper = null; 271 272 try { 273 274 wrapper = (ConsistencyWrapper) wrapper_type.newInstance(); 275 276 if (wrapper == null) { 277 throw new InstantiationException (); 278 } 279 280 wrapper.setReadMethods ( wrappedMethods[0] ); 284 wrapper.setWriteMethods ( wrappedMethods[1] ); 285 wrapper.setCallMethods ( wrappedMethods[2] ); 286 287 } catch ( Exception e ) { 288 throw new InstantiationException (); 289 } 290 291 member.invoke( 292 "wrap", 293 new Object [] { 294 wrapper, 295 wrappingMethods, 296 wrappedMethods 297 } 298 ); 299 } 300 } 301 302 309 public static void bindToDistObj(String name, Wrappee tobind) { 310 Topology topology = Topology.get(); 311 if (topology.countContainers() < 1) 312 return; 313 RemoteContainer rc = topology.getContainer(0); 314 RemoteRef rr = rc.bindTo(name); 315 rr.invokeRoleMethod ( 316 "addMember", 317 new Object [] { RemoteRef.create(NameRepository.get().getName(tobind), tobind) } ); 318 } 319 320 } 321 322 class WrongConsistencyDefinitionException extends Exception {} 323 324 325 326 327 328 329 330 331 | Popular Tags |