KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > presentation > rmi > PresentationManagerImpl


1 /*
2  * @(#)PresentationManagerImpl.java 1.13 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.presentation.rmi ;
9
10 import java.util.Map JavaDoc ;
11 import java.util.HashMap JavaDoc ;
12 import java.util.Set JavaDoc ;
13 import java.util.HashSet JavaDoc ;
14 import java.util.List JavaDoc ;
15 import java.util.ArrayList JavaDoc ;
16 import java.util.Iterator JavaDoc ;
17
18 import java.lang.reflect.Method JavaDoc ;
19
20 import java.rmi.Remote JavaDoc ;
21
22 import javax.rmi.CORBA.Tie JavaDoc ;
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 JavaDoc classToClassData ;
44     private Map JavaDoc 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     // XXX these should probably be WeakHashMaps.
57
classToClassData = new HashMap JavaDoc() ;
58     methodToDMM = new HashMap JavaDoc() ;
59     }
60
61 ////////////////////////////////////////////////////////////////////////////////
62
// PresentationManager interface
63
////////////////////////////////////////////////////////////////////////////////
64

65     public synchronized DynamicMethodMarshaller getDynamicMethodMarshaller(
66     Method JavaDoc 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 JavaDoc 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 JavaDoc cls ;
95     private IDLNameTranslator nameTranslator ;
96     private String JavaDoc[] typeIds ;
97     private PresentationManager.StubFactory sfactory ;
98     private InvocationHandlerFactory ihfactory ;
99     private Map JavaDoc dictionary ;
100
101     public ClassDataImpl( Class JavaDoc cls )
102     {
103         this.cls = cls ;
104         Graph gr = new GraphImpl() ;
105         NodeImpl root = new NodeImpl( cls ) ;
106         Set JavaDoc rootSet = getRootSet( cls, root, gr ) ;
107
108         // At this point, rootSet contains those remote interfaces
109
// that are not related by inheritance, and gr contains
110
// all reachable remote interfaces.
111

112         Class JavaDoc[] 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 JavaDoc() ;
118     }
119
120     public Class JavaDoc getMyClass()
121     {
122         return cls ;
123     }
124
125     public IDLNameTranslator getIDLNameTranslator()
126     {
127         return nameTranslator ;
128     }
129
130     public String JavaDoc[] getTypeIds()
131     {
132         return typeIds ;
133     }
134
135     public InvocationHandlerFactory getInvocationHandlerFactory()
136     {
137         return ihfactory ;
138     }
139
140     public Map JavaDoc 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 JavaDoc getTie()
165     {
166     return dynamicStubFactoryFactory.getTie( null ) ;
167     }
168
169     public boolean useDynamicStubs()
170     {
171     return useDynamicStubs ;
172     }
173
174 ////////////////////////////////////////////////////////////////////////////////
175
// Graph computations
176
////////////////////////////////////////////////////////////////////////////////
177

178     private Set JavaDoc getRootSet( Class JavaDoc target, NodeImpl root, Graph gr )
179     {
180     Set JavaDoc rootSet = null ;
181
182     if (target.isInterface()) {
183         gr.add( root ) ;
184         rootSet = gr.getRoots() ; // rootSet just contains root here
185
} else {
186         // Use this class and its superclasses (not Object) as initial roots
187
Class JavaDoc superclass = target ;
188         Set JavaDoc initialRootSet = new HashSet JavaDoc() ;
189         while ((superclass != null) && !superclass.equals( Object JavaDoc.class )) {
190         Node node = new NodeImpl( superclass ) ;
191         gr.add( node ) ;
192         initialRootSet.add( node ) ;
193         superclass = superclass.getSuperclass() ;
194         }
195
196         // Expand all nodes into the graph
197
gr.getRoots() ;
198
199         // remove the roots and find roots again
200
gr.removeAll( initialRootSet ) ;
201         rootSet = gr.getRoots() ;
202     }
203
204     return rootSet ;
205     }
206
207     private Class JavaDoc[] getInterfaces( Set JavaDoc roots )
208     {
209     Class JavaDoc[] classes = new Class JavaDoc[ roots.size() ] ;
210     Iterator JavaDoc 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 JavaDoc[] makeTypeIds( NodeImpl root, Graph gr, Set JavaDoc rootSet )
221     {
222     Set JavaDoc nonRootSet = new HashSet JavaDoc( gr ) ;
223     nonRootSet.removeAll( rootSet ) ;
224
225     // List<String> for the typeids
226
List JavaDoc result = new ArrayList JavaDoc() ;
227
228     if (rootSet.size() > 1) {
229         // If the rootSet has more than one element, we must
230
// put the type id of the implementation class first.
231
// Root represents the implementation class here.
232
result.add( root.getTypeId() ) ;
233     }
234
235     addNodes( result, rootSet ) ;
236     addNodes( result, nonRootSet ) ;
237
238     return (String JavaDoc[])result.toArray( new String JavaDoc[result.size()] ) ;
239     }
240
241     private void addNodes( List JavaDoc resultList, Set JavaDoc nodeSet )
242     {
243     Iterator JavaDoc iter = nodeSet.iterator() ;
244     while (iter.hasNext()) {
245         NodeImpl node = (NodeImpl)iter.next() ;
246         String JavaDoc typeId = node.getTypeId() ;
247         resultList.add( typeId ) ;
248     }
249     }
250
251     private static class NodeImpl implements Node
252     {
253     private Class JavaDoc interf ;
254
255     public Class JavaDoc getInterface()
256     {
257         return interf ;
258     }
259
260     public NodeImpl( Class JavaDoc interf )
261     {
262         this.interf = interf ;
263     }
264
265     public String JavaDoc getTypeId()
266     {
267         return "RMI:" + interf.getName() + ":0000000000000000" ;
268     }
269
270     public Set JavaDoc getChildren()
271     {
272         Set JavaDoc result = new HashSet JavaDoc() ;
273         Class JavaDoc[] interfaces = interf.getInterfaces() ;
274         for (int ctr=0; ctr<interfaces.length; ctr++) {
275         Class JavaDoc cls = interfaces[ctr] ;
276         if (Remote JavaDoc.class.isAssignableFrom(cls) &&
277             !Remote JavaDoc.class.equals(cls))
278             result.add( new NodeImpl( cls ) ) ;
279         }
280
281         return result ;
282     }
283
284     public String JavaDoc toString()
285     {
286         return "NodeImpl[" + interf + "]" ;
287     }
288
289     public int hashCode()
290     {
291         return interf.hashCode() ;
292     }
293
294     public boolean equals( Object JavaDoc 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