1 7 8 package com.sun.corba.se.impl.presentation.rmi ; 9 10 import java.util.Map ; 11 import java.util.HashMap ; 12 import java.util.Set ; 13 import java.util.HashSet ; 14 import java.util.List ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 18 import java.lang.reflect.Method ; 19 20 import java.rmi.Remote ; 21 22 import javax.rmi.CORBA.Tie ; 23 24 import com.sun.corba.se.spi.orbutil.proxy.InvocationHandlerFactory ; 25 26 import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ; 27 import com.sun.corba.se.spi.presentation.rmi.DynamicMethodMarshaller ; 28 import com.sun.corba.se.spi.presentation.rmi.PresentationManager ; 29 30 import com.sun.corba.se.spi.logging.CORBALogDomains ; 31 32 import com.sun.corba.se.impl.logging.ORBUtilSystemException ; 33 34 import com.sun.corba.se.impl.presentation.rmi.IDLNameTranslatorImpl ; 35 import com.sun.corba.se.impl.presentation.rmi.StubFactoryProxyImpl ; 36 37 import com.sun.corba.se.impl.orbutil.graph.Node ; 38 import com.sun.corba.se.impl.orbutil.graph.Graph ; 39 import com.sun.corba.se.impl.orbutil.graph.GraphImpl ; 40 41 public final class PresentationManagerImpl implements PresentationManager 42 { 43 private Map classToClassData ; 44 private Map methodToDMM ; 45 private PresentationManager.StubFactoryFactory staticStubFactoryFactory ; 46 private PresentationManager.StubFactoryFactory dynamicStubFactoryFactory ; 47 private ORBUtilSystemException wrapper = null ; 48 private boolean useDynamicStubs ; 49 50 public PresentationManagerImpl( boolean useDynamicStubs ) 51 { 52 this.useDynamicStubs = useDynamicStubs ; 53 wrapper = ORBUtilSystemException.get( 54 CORBALogDomains.RPC_PRESENTATION ) ; 55 56 classToClassData = new HashMap () ; 58 methodToDMM = new HashMap () ; 59 } 60 61 65 public synchronized DynamicMethodMarshaller getDynamicMethodMarshaller( 66 Method method ) 67 { 68 if (method == null) 69 return null ; 70 71 DynamicMethodMarshaller result = 72 (DynamicMethodMarshaller)methodToDMM.get( method ) ; 73 if (result == null) { 74 result = new DynamicMethodMarshallerImpl( method ) ; 75 methodToDMM.put( method, result ) ; 76 } 77 78 return result ; 79 } 80 81 public synchronized ClassData getClassData( Class cls ) 82 { 83 ClassData result = (ClassData)classToClassData.get( cls ) ; 84 if (result == null) { 85 result = new ClassDataImpl( cls ) ; 86 classToClassData.put( cls, result ) ; 87 } 88 89 return result ; 90 } 91 92 private class ClassDataImpl implements PresentationManager.ClassData 93 { 94 private Class cls ; 95 private IDLNameTranslator nameTranslator ; 96 private String [] typeIds ; 97 private PresentationManager.StubFactory sfactory ; 98 private InvocationHandlerFactory ihfactory ; 99 private Map dictionary ; 100 101 public ClassDataImpl( Class cls ) 102 { 103 this.cls = cls ; 104 Graph gr = new GraphImpl() ; 105 NodeImpl root = new NodeImpl( cls ) ; 106 Set rootSet = getRootSet( cls, root, gr ) ; 107 108 112 Class [] interfaces = getInterfaces( rootSet ) ; 113 nameTranslator = IDLNameTranslatorImpl.get( interfaces ) ; 114 typeIds = makeTypeIds( root, gr, rootSet ) ; 115 ihfactory = new InvocationHandlerFactoryImpl( 116 PresentationManagerImpl.this, this ) ; 117 dictionary = new HashMap () ; 118 } 119 120 public Class getMyClass() 121 { 122 return cls ; 123 } 124 125 public IDLNameTranslator getIDLNameTranslator() 126 { 127 return nameTranslator ; 128 } 129 130 public String [] getTypeIds() 131 { 132 return typeIds ; 133 } 134 135 public InvocationHandlerFactory getInvocationHandlerFactory() 136 { 137 return ihfactory ; 138 } 139 140 public Map getDictionary() 141 { 142 return dictionary ; 143 } 144 } 145 146 public PresentationManager.StubFactoryFactory getStubFactoryFactory( 147 boolean isDynamic ) 148 { 149 if (isDynamic) 150 return dynamicStubFactoryFactory ; 151 else 152 return staticStubFactoryFactory ; 153 } 154 155 public void setStubFactoryFactory( boolean isDynamic, 156 PresentationManager.StubFactoryFactory sff ) 157 { 158 if (isDynamic) 159 dynamicStubFactoryFactory = sff ; 160 else 161 staticStubFactoryFactory = sff ; 162 } 163 164 public Tie getTie() 165 { 166 return dynamicStubFactoryFactory.getTie( null ) ; 167 } 168 169 public boolean useDynamicStubs() 170 { 171 return useDynamicStubs ; 172 } 173 174 178 private Set getRootSet( Class target, NodeImpl root, Graph gr ) 179 { 180 Set rootSet = null ; 181 182 if (target.isInterface()) { 183 gr.add( root ) ; 184 rootSet = gr.getRoots() ; } else { 186 Class superclass = target ; 188 Set initialRootSet = new HashSet () ; 189 while ((superclass != null) && !superclass.equals( Object .class )) { 190 Node node = new NodeImpl( superclass ) ; 191 gr.add( node ) ; 192 initialRootSet.add( node ) ; 193 superclass = superclass.getSuperclass() ; 194 } 195 196 gr.getRoots() ; 198 199 gr.removeAll( initialRootSet ) ; 201 rootSet = gr.getRoots() ; 202 } 203 204 return rootSet ; 205 } 206 207 private Class [] getInterfaces( Set roots ) 208 { 209 Class [] classes = new Class [ roots.size() ] ; 210 Iterator iter = roots.iterator() ; 211 int ctr = 0 ; 212 while (iter.hasNext()) { 213 NodeImpl node = (NodeImpl)iter.next() ; 214 classes[ctr++] = node.getInterface() ; 215 } 216 217 return classes ; 218 } 219 220 private String [] makeTypeIds( NodeImpl root, Graph gr, Set rootSet ) 221 { 222 Set nonRootSet = new HashSet ( gr ) ; 223 nonRootSet.removeAll( rootSet ) ; 224 225 List result = new ArrayList () ; 227 228 if (rootSet.size() > 1) { 229 result.add( root.getTypeId() ) ; 233 } 234 235 addNodes( result, rootSet ) ; 236 addNodes( result, nonRootSet ) ; 237 238 return (String [])result.toArray( new String [result.size()] ) ; 239 } 240 241 private void addNodes( List resultList, Set nodeSet ) 242 { 243 Iterator iter = nodeSet.iterator() ; 244 while (iter.hasNext()) { 245 NodeImpl node = (NodeImpl)iter.next() ; 246 String typeId = node.getTypeId() ; 247 resultList.add( typeId ) ; 248 } 249 } 250 251 private static class NodeImpl implements Node 252 { 253 private Class interf ; 254 255 public Class getInterface() 256 { 257 return interf ; 258 } 259 260 public NodeImpl( Class interf ) 261 { 262 this.interf = interf ; 263 } 264 265 public String getTypeId() 266 { 267 return "RMI:" + interf.getName() + ":0000000000000000" ; 268 } 269 270 public Set getChildren() 271 { 272 Set result = new HashSet () ; 273 Class [] interfaces = interf.getInterfaces() ; 274 for (int ctr=0; ctr<interfaces.length; ctr++) { 275 Class cls = interfaces[ctr] ; 276 if (Remote .class.isAssignableFrom(cls) && 277 !Remote .class.equals(cls)) 278 result.add( new NodeImpl( cls ) ) ; 279 } 280 281 return result ; 282 } 283 284 public String toString() 285 { 286 return "NodeImpl[" + interf + "]" ; 287 } 288 289 public int hashCode() 290 { 291 return interf.hashCode() ; 292 } 293 294 public boolean equals( Object obj ) 295 { 296 if (this == obj) 297 return true ; 298 299 if (!(obj instanceof NodeImpl)) 300 return false ; 301 302 NodeImpl other = (NodeImpl)obj ; 303 304 return other.interf.equals( interf ) ; 305 } 306 } 307 } 308 | Popular Tags |