1 package org.mmbase.bridge.remote.util; 2 3 import java.util.*; 4 import java.rmi.*; 5 import java.util.Vector ; 6 7 import org.mmbase.bridge.remote.*; 8 9 import org.mmbase.util.logging.*; 10 14 public abstract class ObjectWrapper { 15 private static Logger log = Logging.getLoggerInstance(ObjectWrapper.class); 16 17 20 public static Object localToRMIObject(Object o, int port) throws RemoteException { 21 if (o == null) { 23 return null; 24 } 25 if (o.getClass().getName().indexOf("mmbase") == -1 && o instanceof SortedSet) { 26 27 SortedSet in = (SortedSet)o; 28 log.debug("convert treeset from "+ in); 29 SortedSet set = new TreeSet(); 30 Iterator i = in.iterator(); 31 while(i.hasNext()){ 32 set.add(localToRMIObject(i.next(), port)); 33 } 34 log.debug("convert treeset to "+ set); 35 return set; 36 } 37 38 if (o.getClass().getName().indexOf("mmbase") == -1 && o instanceof List) { 39 List source = (List)o; 40 List list = new Vector (); 41 for (int x = 0; x < source.size(); x++) { 42 list.add(localToRMIObject(source.get(x), port)); 43 } 44 return list; 45 } 46 47 String className = o.getClass().getName(); 48 49 if (className.indexOf("mmbase") == -1) { 50 return o; 52 } 53 54 Object retval = ObjectWrapperHelper.localToRMIObject(o, port); 56 57 if (retval == null) { 58 log.warn("please add a wrapper for objects of type " + className); 59 } else { 60 log.debug(o.getClass().getName() + " -> " + retval.getClass().getName()); 61 } 62 return retval; 63 } 64 65 68 public static Object rmiObjectToRemoteImplementation(Object o) throws RemoteException { 69 if (o == null) { 70 return null; 71 } 72 73 if (o.getClass().getName().indexOf("mmbase") == -1 && o instanceof SortedSet) { 74 SortedSet in = (SortedSet)o; 75 System.err.println("convert treeset from "+ in); 76 SortedSet set = new TreeSet(); 77 Iterator i = in.iterator(); 78 while(i.hasNext()){ 79 set.add(rmiObjectToRemoteImplementation(i.next())); 80 } 81 System.err.println("convert treeset to"+ in); 82 return set; 83 } 84 85 if (o.getClass().getName().indexOf("mmbase") == -1 && o instanceof List) { 86 List source = (List)o; 87 List list = new ArrayList(); 88 for (int x = 0; x < source.size(); x++) { 89 list.add(rmiObjectToRemoteImplementation(source.get(x))); 90 } 91 return list; 92 } 93 94 Object retval = null; 95 String className = o.getClass().getName(); 96 97 if (className.indexOf("mmbase") != -1) { 98 retval = ObjectWrapperHelper.rmiObjectToRemoteImplementation(o); 99 if (retval == null) { 100 log.warn("please add a wrapper for objects of type " + className); 101 } 102 } else { 103 retval = o; 104 } 105 return retval; 107 } 108 109 public static Object remoteImplementationToRMIObject(Object o) { 110 if (o == null) { 111 return null; 112 } 113 Object retval = null; 114 if (o instanceof MappedObject) { 115 retval = ((MappedObject)o).getWrappedObject(); 116 } else { 117 retval = o; 118 } 119 return retval; 121 } 122 123 public static Object rmiObjectToLocal(Object o) throws RemoteException { 124 if (o == null) { 125 return null; 126 } 127 if (o instanceof ServerMappedObject) { 128 return StubToLocalMapper.get(((ServerMappedObject)o).getMapperCode()); 129 } else { 130 return o; 131 } 132 } 133 } 134 | Popular Tags |