1 package org.sapia.ubik.rmi.naming.remote; 2 3 import java.lang.reflect.InvocationHandler ; 4 import java.lang.reflect.Proxy ; 5 import java.lang.reflect.UndeclaredThrowableException ; 6 import java.util.Iterator ; 7 8 import javax.naming.Name ; 9 import javax.naming.NamingException ; 10 11 import org.sapia.ubik.mcast.DomainName; 12 import org.sapia.ubik.net.Uri; 13 import org.sapia.ubik.net.UriSyntaxException; 14 import org.sapia.ubik.rmi.Consts; 15 import org.sapia.ubik.rmi.server.Hub; 16 import org.sapia.ubik.rmi.server.RemoteRefReliable; 17 import org.sapia.ubik.rmi.server.RemoteRefStateless; 18 import org.sapia.ubik.rmi.server.Stateless; 19 import org.sapia.ubik.rmi.server.StatelessStubTable; 20 import org.sapia.ubik.rmi.server.Stub; 21 import org.sapia.ubik.rmi.server.StubContainer; 22 import org.sapia.ubik.rmi.server.StubInvocationHandler; 23 24 25 33 public class StubTweaker { 34 public static Object tweak(Iterator boundObjects, Object toBind) { 35 RemoteRefStateless newRef = null; 36 37 if (toBind instanceof StubContainer && 38 ((StubContainer) toBind).getStubInvocationHandler() instanceof RemoteRefStateless) { 39 newRef = (RemoteRefStateless) ((StubContainer) toBind).getStubInvocationHandler(); 40 41 RemoteRefStateless oldRef; 42 Object bound; 43 44 while (boundObjects.hasNext()) { 45 bound = boundObjects.next(); 46 47 if (bound instanceof StubContainer && 48 ((StubContainer) bound).getStubInvocationHandler() instanceof RemoteRefStateless) { 49 oldRef = (RemoteRefStateless) ((StubContainer) bound).getStubInvocationHandler(); 50 newRef.addSibling(oldRef); 51 } 52 } 53 } 54 55 if (newRef != null) { 56 StatelessStubTable.registerStatelessRef(newRef); 57 } 58 59 return toBind; 60 } 61 62 public static Object tweak(String baseUrl, Name name, DomainName domainName, 63 String mcastAddress, int mcastPort, Object toBind) 64 throws NamingException { 65 try { 66 if (!(toBind instanceof Stub && Proxy.isProxyClass(toBind.getClass()))) { 67 Stub stub = null; 68 69 try { 70 if (toBind instanceof Stateless) { 71 stub = (Stub) Hub.toStatelessStub(name, domainName.toString(), 72 (Stub) Hub.toStub(toBind)); 73 } else { 74 stub = (Stub) Hub.toStub(toBind); 75 stub = (Stub) Hub.toReliableStub(stub); 76 77 Uri uri = Uri.parse(baseUrl); 78 StringBuffer newUri = new StringBuffer (); 79 newUri.append(uri.getScheme()).append("://").append(uri.getHost()); 80 81 if (uri.getPort() != Uri.UNDEFINED_PORT) { 82 newUri.append(':').append("" + uri.getPort()); 83 } 84 85 newUri.append('/').append(name); 86 87 Uri newUriObj = Uri.parse(newUri.toString()); 88 newUriObj.getQueryString().addParameter(RemoteInitialContextFactory.UBIK_DOMAIN_NAME, 89 domainName.toString()); 90 newUriObj.getQueryString().addParameter(Consts.MCAST_ADDR_KEY, 91 mcastAddress); 92 newUriObj.getQueryString().addParameter(Consts.MCAST_PORT_KEY, 93 "" + mcastPort); 94 95 if (Proxy.isProxyClass(stub.getClass())) { 96 ((RemoteRefReliable) Proxy.getInvocationHandler(stub)).setUp(newUriObj.toString()); 97 } 98 } 99 100 if (Proxy.isProxyClass(stub.getClass())) { 101 InvocationHandler handler = Proxy.getInvocationHandler(stub); 102 103 if (handler instanceof StubInvocationHandler) { 104 toBind = ((StubInvocationHandler) handler).toStubContainer(stub); 105 } 106 } else { 107 toBind = stub; 108 } 109 } catch (UriSyntaxException e) { 110 NamingException ne = new NamingException ("Invalid URI"); 111 ne.setRootCause(ne); 112 throw ne; 113 } catch (ClassCastException e) { 114 } 116 117 return toBind; 118 } else { 119 return toBind; 120 } 121 } catch (UndeclaredThrowableException e) { 122 NamingException ne = new NamingException ("Could not bind object"); 123 ne.setRootCause(e.getUndeclaredThrowable()); 124 throw ne; 125 } catch (java.rmi.RemoteException e) { 126 NamingException ne = new NamingException ("Could not bind object"); 127 ne.setRootCause(e); 128 throw ne; 129 } 130 } 131 } 132 | Popular Tags |