1 28 package org.objectweb.carol.rmi.multi; 29 30 import java.rmi.NoSuchObjectException ; 31 import java.rmi.Remote ; 32 import java.rmi.RemoteException ; 33 import java.util.ArrayList ; 34 import java.util.Enumeration ; 35 import java.util.Hashtable ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 39 import javax.rmi.CORBA.PortableRemoteObjectDelegate ; 40 41 import org.objectweb.carol.util.configuration.ConfigurationRepository; 42 import org.objectweb.carol.util.configuration.ProtocolConfiguration; 43 import org.objectweb.carol.util.configuration.TraceCarol; 44 45 55 public class MultiPRODelegate implements PortableRemoteObjectDelegate { 56 57 60 private Hashtable exported = new Hashtable (); 61 62 65 private List proDelegates = null; 66 67 70 public MultiPRODelegate() { 71 72 ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations(); 74 proDelegates = new ArrayList (); 75 for (int i = 0; i < protocolConfigurations.length; i++) { 76 PortableRemoteObjectDelegate proDelegate = protocolConfigurations[i].getProtocol().getPortableRemoteObject(); 77 proDelegates.add(proDelegate); 78 } 79 } 80 81 88 public void exportObject(Remote obj) throws RemoteException { 89 for (Iterator it = proDelegates.iterator(); it.hasNext();) { 90 ((PortableRemoteObjectDelegate ) it.next()).exportObject(obj); 91 } 92 if (TraceCarol.isDebugExportCarol()) { 93 TraceCarol.debugExportCarol("Export object " + obj.getClass().getName()); 94 addObject(obj.getClass().getName()); 95 } 96 } 97 98 105 public void unexportObject(Remote obj) throws NoSuchObjectException { 106 for (Iterator it = proDelegates.iterator(); it.hasNext();) { 107 ((PortableRemoteObjectDelegate ) it.next()).unexportObject(obj); 108 } 109 if (TraceCarol.isDebugExportCarol()) { 110 TraceCarol.debugExportCarol("Unexport object " + obj.getClass().getName()); 111 TraceCarol.debugExportCarol("UnExported objects list:\n" + getExportedObjects()); 112 removeObject(obj.getClass().getName()); 113 } 114 } 115 116 127 public void connect(Remote target, Remote source) throws RemoteException { 128 for (Iterator it = proDelegates.iterator(); it.hasNext();) { 129 ((PortableRemoteObjectDelegate ) it.next()).connect(target, source); 130 } 131 } 132 133 141 public Object narrow(Object narrowFrom, Class narrowTo) throws ClassCastException { 142 return ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject().narrow(narrowFrom, narrowTo); 143 } 144 145 154 public Remote toStub(Remote obj) throws NoSuchObjectException { 155 return ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject().toStub(obj); 156 } 157 158 162 private String getExportedObjects() { 163 String result = "Exported Objects:\n"; 164 int resultInt = 0; 165 for (Enumeration e = exported.keys(); e.hasMoreElements();) { 166 String ck = (String ) e.nextElement(); 167 int on = ((Integer ) exported.get(ck)).intValue(); 168 result += "" + on + " instances of " + ck + "\n"; 169 resultInt += on; 170 } 171 result += "Total number of exported objects=" + resultInt; 172 return result; 173 } 174 175 179 private void removeObject(String className) { 180 if (exported.containsKey(className)) { 181 if (((Integer ) exported.get(className)).intValue() != 1) { 182 exported.put(className, new Integer (((Integer ) exported.get(className)).intValue() - 1)); 183 } else { 184 exported.remove(className); 185 } 186 } 187 } 188 189 193 private void addObject(String className) { 194 if (exported.containsKey(className)) { 195 exported.put(className, new Integer (((Integer ) exported.get(className)).intValue() + 1)); 196 } else { 197 exported.put(className, new Integer (1)); 198 } 199 } 200 201 } | Popular Tags |