KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > compiler > StubFactory


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop.compiler;
20
21 import gcc.*;
22 import gcc.rmi.iiop.*;
23 import gcc.rmi.iiop.compiler.StubClass;
24 import gcc.rmi.iiop.compiler.StubCompiler;
25 import gcc.util.*;
26 import java.util.*;
27
28 public class StubFactory
29 {
30     protected static StubFactory _sf;
31
32     protected StubFactory()
33     {
34     }
35
36     public static StubFactory getInstance()
37     {
38         if (_sf == null)
39         {
40             synchronized( StubFactory.class )
41             {
42                 if (_sf == null)
43                 {
44                     _sf = new StubFactory();
45                     _sf.init();
46                 }
47             }
48         }
49
50         return _sf;
51     }
52
53     // private data
54

55     private static HashMap _stubClassMap;
56
57     // internal methods
58

59     protected void init()
60     {
61         _stubClassMap = new HashMap();
62     }
63
64     protected Class loadStub(Class remoteInterface)
65     {
66         System.out.println( "StubFactory.loadStub(): remoteInterface: " + remoteInterface );
67         String className = remoteInterface.getName();
68         String stubClassName = JavaClass.addPackageSuffix(className, "iiop_stubs");
69         System.out.println( "StubFactory.loadStub(): stubClassName: " + stubClassName );
70
71         Class sc = null;
72         //
73
// Try to load the stub. No need to generate if the stub is already present.
74
//
75
try
76         {
77             sc = Class.forName( stubClassName + "_Stub" );
78         }
79         catch (ClassNotFoundException e)
80         {
81             e.printStackTrace();
82         }
83
84
85         if (sc == null)
86         {
87             //
88
// Try generating stub class now.
89
//
90

91             StubCompiler stubCompiler = new StubCompiler( remoteInterface );
92             System.out.println( "StubFactory.loadStub(): stubCompiler: " + stubCompiler );
93             sc = stubCompiler.getStubClass();
94             System.out.println( "StubFactory.loadStub(): sc: " + sc );
95         }
96
97         /*
98         if (sc.stubClass != null)
99         {
100             try
101             {
102                 sc.getInstance = sc.stubClass.getMethod("$getInstance", ArrayUtil.EMPTY_CLASS_ARRAY);
103             }
104             catch (Exception ex)
105             {
106                 throw new SystemException(ex);
107             }
108         }
109         */

110
111         return sc;
112     }
113
114     // public methods
115

116     public ObjectRef getStub(Class remoteInterface)
117     {
118         System.out.println( "StubFactory.getStub(): remoteInterface: " + remoteInterface );
119         try
120         {
121             Class sc = (Class)_stubClassMap.get(remoteInterface);
122             System.out.println( "StubFactory.getStub(): sc: " + sc );
123             if (sc == null)
124             {
125                 synchronized (_stubClassMap)
126                 {
127                     sc = (Class)_stubClassMap.get(remoteInterface);
128                     if (sc == null)
129                     {
130                         sc = loadStub(remoteInterface);
131                         System.out.println( "StubFactory.getStub(): sc: " + sc );
132                         _stubClassMap.put(remoteInterface, sc);
133                     }
134                 }
135             }
136
137             if (sc == null)
138             {
139                 throw new SystemException( "Error: Unable to load stub for remote interface: " + remoteInterface );
140             }
141
142             java.lang.Object sobj = sc.newInstance();
143
144             if (! (sobj instanceof ObjectRef) )
145             {
146                 throw new SystemException( "Error: Stub for remote interface: '" + remoteInterface + "' is not a valid ObjectRef." );
147             }
148
149             return (ObjectRef)sobj;
150             //return (ObjectRef)sc.getInstance.invoke(sc.stubClass, ArrayUtil.EMPTY_OBJECT_ARRAY);
151
}
152         catch (Exception ex)
153         {
154             throw new SystemException(ex);
155         }
156     }
157
158     public Object getStub(String remoteInterface)
159     {
160         return getStub(ThreadContext.loadClass(remoteInterface));
161     }
162 }
163
Popular Tags