1 28 29 package com.caucho.iiop; 30 31 import com.caucho.java.AbstractGenerator; 32 import com.caucho.server.util.CauchoSystem; 33 import com.caucho.vfs.MergePath; 34 import com.caucho.vfs.Path; 35 36 import javax.ejb.EJBHome ; 37 import javax.ejb.EJBObject ; 38 import java.io.IOException ; 39 import java.lang.reflect.Method ; 40 41 public class IiopStubCompiler extends AbstractGenerator { 42 private Class _cl; 43 44 public IiopStubCompiler(Class cl) 45 { 46 _cl = cl; 47 48 String name = cl.getName(); 49 50 53 int p = name.lastIndexOf('.'); 54 if (p > 0) { 55 String pkg = name.substring(0, p); 56 String tail = name.substring(p + 1); 57 setFullClassName(pkg + "._" + tail + "_Stub"); 58 } 59 else 60 setFullClassName("_" + name + "_Stub"); 61 } 62 63 66 public void generateJava() 67 throws Exception 68 { 69 printHeader(); 70 71 Method []methods = _cl.getMethods(); 72 for (int i = 0; i < methods.length; i++) { 73 Method method = methods[i]; 74 75 if (method.getDeclaringClass().isAssignableFrom(EJBHome .class) || 76 method.getDeclaringClass().isAssignableFrom(EJBObject .class)) 77 continue; 78 79 printMethod(method); 80 } 81 82 printFooter(); 83 } 84 85 88 private void printHeader() 89 throws IOException 90 { 91 println("/*"); 92 println(" * Generated by Resin-EJB IiopStubCompiler"); 93 println(" */"); 94 println(); 95 96 if (getPackageName() != null) 97 println("package " + getPackageName() + ";"); 98 99 println(); 100 print("public class " + getClassName()); 101 if (EJBHome .class.isAssignableFrom(_cl)) 102 println(" extends com.caucho.iiop.client.IiopHomeStub"); 103 else 104 println(" extends com.caucho.iiop.client.IiopStub"); 105 println(" implements " + _cl.getName() + " {"); 106 pushDepth(); 107 108 println("private static final String[] _type_ids = {"); 109 println(" \"RMI:" + _cl.getName() + ":0000000000000000\""); 110 println("};"); 111 112 println(); 113 println("public String[] _ids()"); 114 println("{"); 115 println(" return _type_ids;"); 116 println("}"); 117 } 118 119 122 private void printMethod(Method method) 123 throws IOException 124 { 125 printMethodHeader(method); 126 println("{"); 127 pushDepth(); 128 129 println("org.omg.CORBA_2_3.portable.OutputStream os = null;"); 130 println("try {"); 131 pushDepth(); 132 133 println("os = (org.omg.CORBA_2_3.portable.OutputStream) _request(\"" + method.getName() + "\", true);"); 134 135 Class []args = method.getParameterTypes(); 136 for (int i = 0; i < args.length; i++) { 137 printSetValue(args[i], "a" + i); 138 } 139 140 println("org.omg.CORBA_2_3.portable.InputStream is;"); 141 println("is = (org.omg.CORBA_2_3.portable.InputStream) _invoke(os);"); 142 143 Class returnType = method.getReturnType(); 144 145 if (returnType.equals(void.class)) { 146 } 147 else { 148 print("return "); 149 printGetValue(returnType); 150 println(";"); 151 } 152 153 popDepth(); 154 println("} catch (org.omg.CORBA.portable.ApplicationException e) {"); 155 println(" org.omg.CORBA_2_3.portable.InputStream in = (org.omg.CORBA_2_3.portable.InputStream) e.getInputStream();"); 157 println(" String name = in.read_string();"); 158 println(" Throwable ex = (Throwable) in.read_value();"); 159 for (Class ex : method.getExceptionTypes()) { 160 println(" if (ex instanceof " + ex.getName() + ")"); 161 println(" throw (" + ex.getName() + ") ex;"); 162 } 163 println(" throw new java.rmi.RemoteException(ex.getMessage(), ex);"); 164 println("} catch (Exception e) {"); 165 println(" e.printStackTrace();"); 166 println(" throw new java.rmi.RemoteException(e.getMessage(), e);"); 167 println("}"); 168 169 popDepth(); 170 println("}"); 171 } 172 173 private void printGetValue(Class type) 174 throws IOException 175 { 176 if (type.equals(boolean.class)) 177 print("is.read_boolean()"); 178 else if (type.equals(byte.class)) 179 print("is.read_octet()"); 180 else if (type.equals(short.class)) 181 print("is.read_short()"); 182 else if (type.equals(int.class)) 183 print("is.read_long()"); 184 else if (type.equals(long.class)) 185 print("is.read_longlong()"); 186 else if (type.equals(float.class)) 187 print("is.read_float()"); 188 else if (type.equals(double.class)) 189 print("is.read_double()"); 190 else if (type.equals(char.class)) 191 print("is.read_wchar()"); 192 else if (javax.ejb.EJBObject .class.isAssignableFrom(type) || 193 java.rmi.Remote .class.isAssignableFrom(type)) { 194 print("("); 198 printJavaClass(type); 199 print(") is.read_Object("); 200 printJavaClass(type); 201 print(".class)"); 202 } 203 else { 204 print("("); 205 printJavaClass(type); 206 print(") is.read_value("); 207 printJavaClass(type); 208 print(".class)"); 209 } 210 } 211 212 void printJavaClass(Class type) 213 throws IOException 214 { 215 if (type.isArray()) { 216 printJavaClass(type.getComponentType()); 217 print("[]"); 218 } 219 else 220 print(type.getName()); 221 } 222 223 private void printSetValue(Class type, String value) 224 throws IOException 225 { 226 if (type.equals(boolean.class)) 227 println("os.write_boolean(" + value + ");"); 228 else if (type.equals(byte.class)) 229 println("os.write_octet(" + value + ");"); 230 else if (type.equals(short.class)) 231 println("os.write_short(" + value + ");"); 232 else if (type.equals(int.class)) 233 println("os.write_long(" + value + ");"); 234 else if (type.equals(long.class)) 235 println("os.write_longlong(" + value + ");"); 236 else if (type.equals(float.class)) 237 println("os.write_float(" + value + ");"); 238 else if (type.equals(double.class)) 239 println("os.write_double(" + value + ");"); 240 else if (type.equals(char.class)) 241 println("os.write_wchar(" + value + ");"); 242 else 243 println("os.write_value((java.io.Serializable) " + value + ");"); 244 } 245 246 249 private void printFooter() 250 throws IOException 251 { 252 popDepth(); 253 println("}"); 254 } 255 256 public static Class create(String className) 257 throws Exception 258 { 259 Class cl = CauchoSystem.loadClass(className); 260 261 MergePath mergePath = new MergePath(); 262 mergePath.addClassPath(); 263 264 Path classPath = mergePath.lookup(className.replace('.', '/') + ".class"); 265 classPath = ((MergePath) classPath).getBestPath(); 266 267 Path dir = classPath.getParent(); 268 int i = 0; 269 while ((i = className.indexOf('.', i + 1)) >= 0) { 270 dir = dir.getParent(); 271 } 272 273 dir.mkdirs(); 274 275 IiopStubCompiler compiler = new IiopStubCompiler(cl); 276 compiler.setClassDir(dir); 277 Class gen = compiler.preload(); 278 if (gen != null) 279 return gen; 280 281 compiler.generate(); 282 283 return compiler.compile(); 284 } 285 286 289 public static void main(String []argv) 290 throws Exception 291 { 292 String className = argv[0]; 293 294 System.out.println(create(className)); 295 } 296 } 297 | Popular Tags |