KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > compiler > StubFactory


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.compiler;
19
20 import java.util.HashMap JavaDoc;
21
22 import org.apache.geronimo.interop.SystemException;
23 import org.apache.geronimo.interop.rmi.iiop.ObjectRef;
24 import org.apache.geronimo.interop.util.JavaClass;
25 import org.apache.geronimo.interop.util.ThreadContext;
26
27 public class StubFactory {
28     protected static StubFactory sf;
29
30     protected StubFactory() {
31     }
32
33     public static StubFactory getInstance() {
34         if (sf == null) {
35             synchronized (StubFactory.class) {
36                 if (sf == null) {
37                     sf = new StubFactory();
38                     sf.init();
39                 }
40             }
41         }
42
43         return sf;
44     }
45
46     private static HashMap JavaDoc stubClassMap;
47
48     protected void init() {
49         stubClassMap = new HashMap JavaDoc();
50     }
51
52     protected Class JavaDoc loadStub(Class JavaDoc remoteInterface) {
53         System.out.println("StubFactory.loadStub(): remoteInterface: " + remoteInterface);
54         String JavaDoc className = remoteInterface.getName();
55         String JavaDoc stubClassName = JavaClass.addPackageSuffix(className, "iiop_stubs");
56         System.out.println("StubFactory.loadStub(): stubClassName: " + stubClassName);
57
58         Class JavaDoc sc = null;
59         //
60
// Try to load the stub. No need to generate if the stub is already present.
61
//
62
try {
63             sc = Class.forName(stubClassName + "_Stub");
64         } catch (ClassNotFoundException JavaDoc e) {
65             e.printStackTrace();
66         }
67
68
69         if (sc == null) {
70             //
71
// Try generating stub class now.
72
//
73

74             //StubCompiler stubCompiler = new StubCompiler(remoteInterface);
75
//System.out.println("StubFactory.loadStub(): stubCompiler: " + stubCompiler);
76
//sc = stubCompiler.getStubClass();
77
System.out.println("StubFactory.loadStub(): sc: " + sc);
78         }
79
80         /*
81         if (sc.stubClass != null)
82         {
83             try
84             {
85                 sc.getInstance = sc.stubClass.getMethod("$getInstance", ArrayUtil.EMPTY_CLASS_ARRAY);
86             }
87             catch (Exception ex)
88             {
89                 throw new SystemException(ex);
90             }
91         }
92         */

93
94         return sc;
95     }
96
97     public ObjectRef getStub(Class JavaDoc remoteInterface) {
98         System.out.println("StubFactory.getStub(): remoteInterface: " + remoteInterface);
99         try {
100             Class JavaDoc sc = (Class JavaDoc) stubClassMap.get(remoteInterface);
101             System.out.println("StubFactory.getStub(): sc: " + sc);
102             if (sc == null) {
103                 synchronized (stubClassMap) {
104                     sc = (Class JavaDoc) stubClassMap.get(remoteInterface);
105                     if (sc == null) {
106                         sc = loadStub(remoteInterface);
107                         System.out.println("StubFactory.getStub(): sc: " + sc);
108                         stubClassMap.put(remoteInterface, sc);
109                     }
110                 }
111             }
112
113             if (sc == null) {
114                 throw new SystemException("Error: Unable to load stub for remote interface: " + remoteInterface);
115             }
116
117             java.lang.Object JavaDoc sobj = sc.newInstance();
118
119             if (!(sobj instanceof ObjectRef)) {
120                 throw new SystemException("Error: Stub for remote interface: '" + remoteInterface + "' is not a valid ObjectRef.");
121             }
122
123             return (ObjectRef) sobj;
124             //return (ObjectRef)sc.getInstance.invoke(sc.stubClass, ArrayUtil.EMPTY_OBJECT_ARRAY);
125
} catch (Exception JavaDoc ex) {
126             throw new SystemException(ex);
127         }
128     }
129
130     public Object JavaDoc getStub(String JavaDoc remoteInterface) {
131         return getStub(ThreadContext.loadClass(remoteInterface));
132     }
133 }
134
Popular Tags