1 28 29 package org.objectweb.fractal.rmi; 30 31 import org.objectweb.fractal.api.control.BindingController; 32 33 import org.objectweb.fractal.rmi.stub.SkeletonFactory; 34 35 import org.objectweb.jonathan.apis.binding.Identifier; 36 import org.objectweb.jonathan.apis.binding.NamingContext; 37 import org.objectweb.jonathan.apis.kernel.Context; 38 import org.objectweb.jonathan.apis.kernel.JonathanException; 39 import org.objectweb.jonathan.apis.presentation.Marshaller; 40 import org.objectweb.jonathan.apis.presentation.UnMarshaller; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.api.Logger; 44 import org.objectweb.util.monolog.api.LoggerFactory; 45 46 import java.util.HashMap ; 47 48 64 65 public class RmiAdapter 66 implements NamingContext, Runnable , BindingController 67 { 68 69 72 73 protected SkeletonFactory skeletonFactory; 74 75 78 79 protected LoggerFactory loggerFactory; 80 81 84 85 protected Logger logger; 86 87 91 92 private HashMap map; 93 94 97 98 private int counter; 99 100 103 104 private Thread waiter; 105 106 109 110 public RmiAdapter () { 111 map = new HashMap (); 112 } 113 114 118 public String [] listFc () { 119 return new String [] { "skeleton-factory", "logger-factory" }; 120 } 121 122 public Object lookupFc (final String clientItfName) { 123 if (clientItfName.equals("skeleton-factory")) { 124 return skeletonFactory; 125 } else if (clientItfName.equals("logger-factory")) { 126 return loggerFactory; 127 } 128 return null; 129 } 130 131 public void bindFc (final String clientItfName, final Object serverItf) { 132 if (clientItfName.equals("skeleton-factory")) { 133 skeletonFactory = (SkeletonFactory)serverItf; 134 } else if (clientItfName.equals("logger-factory")) { 135 loggerFactory = (LoggerFactory)serverItf; 136 logger = loggerFactory.getLogger(getClass().getName()); 137 } 138 } 139 140 public void unbindFc (final String clientItfName) { 141 if (clientItfName.equals("skeleton-factory")) { 142 skeletonFactory = null; 143 } else if (clientItfName.equals("logger-factory")) { 144 loggerFactory = null; 145 logger = null; 146 } 147 } 148 149 153 163 164 public Identifier export (final Object obj, final Context hints) 165 throws JonathanException 166 { 167 synchronized (this) { 168 Id id = (Id)map.get(obj); 170 if (id == null) { 171 if (waiter == null) { 173 waiter = new Thread (this); 174 waiter.start(); 175 } 176 Integer key = null; 177 if (hints != null) { 178 key = (Integer )hints.getValue("key", (char)0); 179 if (key != null && key.intValue() >= 0) { 180 throw new JonathanException( 181 "Cannot export objects with positive user specified keys"); 182 } 183 if (key != null && map.get(key) != null) { 184 throw new JonathanException("Key " + key + " aleady used"); 185 } 186 } 187 if (key == null) { 188 key = new Integer (counter++); 189 } 190 id = new Id(key); 191 Object skel = skeletonFactory.newSkeleton(obj); 192 map.put(key, skel); 193 map.put(obj, id); 194 if (logger != null && logger.isLoggable(BasicLevel.INFO)) { 195 logger.log( 196 BasicLevel.INFO, "Object " + obj + " exported with key " + key); 197 } 198 } 199 return id; 200 } 201 } 202 203 218 219 public Identifier decode ( 220 final byte[] data, 221 final int offset, 222 final int length) throws JonathanException 223 { 224 int key = 225 ((data[offset] & 0xFF) << 24) + 226 ((data[offset + 1] & 0xFF) << 16) + 227 ((data[offset + 2] & 0xFF) << 8) + 228 (data[offset + 3] & 0xFF); 229 return new Id(new Integer (key)); 230 } 231 232 239 240 public Identifier decode (final UnMarshaller u) throws JonathanException { 241 return new Id(new Integer (u.readInt())); 242 } 243 244 248 253 254 public void run () { 255 Object o = new Object (); 256 synchronized (o) { 257 try { 258 o.wait(); 259 } catch (Exception e) { 260 } 261 } 262 } 263 264 268 class Id implements Identifier { 269 270 Integer key; 271 272 public Id (final Integer key) { 273 this.key = key; 274 } 275 276 public NamingContext getContext () { 277 return RmiAdapter.this; 278 } 279 280 public Object bind (final Identifier[] ref, final Context hints) { 281 return map.get(key); 282 } 283 284 public void unexport () { 285 map.remove(key); 286 } 287 288 public boolean isValid () { 289 return map.containsKey(key); 290 } 291 292 public Object resolve () { 293 Object o = map.get(key); 294 if (logger != null && logger.isLoggable(BasicLevel.INFO)) { 295 logger.log(BasicLevel.INFO, "Key " + key + " resolved to " + o); 296 } 297 return o; 298 } 299 300 public byte[] encode () { 301 int key = this.key.intValue(); 302 return new byte[] { 303 (byte)((key >>> 24) & 0xFF), 304 (byte)((key >>> 16) & 0xFF), 305 (byte)((key >>> 8) & 0xFF), 306 (byte)(key & 0xFF) 307 }; 308 } 309 310 public void encode (final Marshaller m) throws JonathanException { 311 m.writeInt(key.intValue()); 312 } 313 314 public boolean equals (final Object o) { 315 if (o instanceof Id) { 316 return key.intValue() == ((Id)o).key.intValue(); 317 } 318 return false; 319 } 320 321 public int hashCode () { 322 return key.intValue(); 323 } 324 325 public String toString () { 326 return "Id[" + key.intValue() + "]"; 327 } 328 } 329 } 330 | Popular Tags |