1 23 package com.sun.ejb.codegen; 24 25 import java.lang.reflect.Method ; 26 import java.io.*; 27 import java.util.*; 28 import java.util.logging.*; 29 import com.sun.logging.*; 30 import com.sun.ejb.EJBUtils; 31 32 import static java.lang.reflect.Modifier .*; 33 import static com.sun.corba.ee.spi.codegen.Wrapper.*; 34 import com.sun.corba.ee.spi.codegen.Type; 35 import com.sun.corba.ee.impl.codegen.ClassGenerator; 36 37 38 import sun.rmi.rmic.IndentingWriter; 39 40 import javax.ejb.EnterpriseBean ; 41 import javax.ejb.SessionBean ; 42 import javax.ejb.EntityBean ; 43 import com.sun.enterprise.deployment.*; 44 import com.sun.enterprise.util.JarClassLoader; 45 import com.sun.enterprise.util.LocalStringManagerImpl; 46 47 51 52 public class RemoteGenerator extends Generator 53 implements ClassGeneratorFactory { 54 55 private static LocalStringManagerImpl localStrings = 56 new LocalStringManagerImpl(RemoteGenerator.class); 57 private static Logger _logger=null; 58 static{ 59 _logger=LogDomains.getLogger(LogDomains.DPL_LOGGER); 60 } 61 62 private Class businessInterface; 63 private Method [] bizMethods; 64 private String remoteInterfacePackageName; 65 private String remoteInterfaceSimpleName; 66 private String remoteInterfaceName; 67 68 74 public String getGeneratedClass() { 75 return remoteInterfaceName; 76 } 77 78 public String className() { 80 return getGeneratedClass(); 81 } 82 83 88 public RemoteGenerator(ClassLoader cl, String businessIntf) 89 throws GeneratorException 90 { 91 super(); 92 93 try { 94 businessInterface = cl.loadClass(businessIntf); 95 } catch (ClassNotFoundException ex) { 96 throw new InvalidBean( 97 localStrings.getLocalString( 98 "generator.remote_interface_not_found", 99 "Remote interface not found ")); 100 } 101 102 remoteInterfaceName = EJBUtils.getGeneratedRemoteIntfName 103 (businessInterface.getName()); 104 105 remoteInterfacePackageName = getPackageName(remoteInterfaceName); 106 remoteInterfaceSimpleName = getBaseName(remoteInterfaceName); 107 108 bizMethods = removeDups(businessInterface.getMethods()); 109 110 } 113 114 public ClassGenerator evaluate() { 115 116 _clear(); 117 118 if (remoteInterfacePackageName != null) { 119 _package(remoteInterfacePackageName); 120 } else { 121 _package(); 123 } 124 125 _interface(PUBLIC, remoteInterfaceSimpleName, 126 _t("java.rmi.Remote"), 127 _t("com.sun.ejb.containers.RemoteBusinessObject")); 128 129 for(int i = 0; i < bizMethods.length; i++) { 130 printMethod(bizMethods[i]); 131 } 132 133 _end(); 134 135 return _classGenerator() ; 136 137 } 138 139 140 141 147 public void generate(OutputStream out) 148 throws GeneratorException, IOException 149 { 150 IndentingWriter p = new IndentingWriter(new OutputStreamWriter(out)); 151 152 p.pln(""); 153 154 if (remoteInterfacePackageName != null) { 155 p.pln("package " + remoteInterfacePackageName + ";"); 156 } 157 158 p.pln(""); 159 160 p.plnI("public interface " + remoteInterfaceSimpleName + " extends " + 161 "java.rmi.Remote , com.sun.ejb.containers.RemoteBusinessObject {"); 162 163 p.pln(""); 164 165 for(int i = 0; i < bizMethods.length; i++) { 167 printMethod(p, bizMethods[i]); 168 } 169 170 p.pOln("}"); 171 p.close(); 172 } 173 174 private void printMethod(Method m) 175 { 176 177 boolean throwsRemoteException = false; 178 List<Type> exceptionList = new LinkedList<Type>(); 179 for(Class exception : m.getExceptionTypes()) { 180 exceptionList.add(Type.type(exception)); 181 if( exception.getName().equals("java.rmi.RemoteException") ) { 182 throwsRemoteException = true; 183 } 184 } 185 if( !throwsRemoteException ) { 186 exceptionList.add(_t("java.rmi.RemoteException")); 187 } 188 189 exceptionList.add(_t("com.sun.ejb.containers.InternalEJBContainerException")); 190 _method( PUBLIC | ABSTRACT, Type.type(m.getReturnType()), 191 m.getName(), exceptionList); 192 193 int i = 0; 194 for(Class param : m.getParameterTypes()) { 195 _arg(Type.type(param), "param" + i); 196 i++; 197 } 198 199 _end(); 200 } 201 202 203 209 private void printMethod(IndentingWriter p, Method m) 210 throws IOException 211 { 212 p.pln(""); 213 214 p.p("public " + printType(m.getReturnType()) + " " 216 + m.getName() + "("); 217 Class [] params = m.getParameterTypes(); 218 for(int i = 0; i < params.length; i++) { 219 if (i != 0) 220 p.p(", "); 221 p.p(printType(params[i]) + " param" + i); 222 } 223 p.p(") "); 224 Class [] exceptions = m.getExceptionTypes(); 225 boolean throwsRemoteException = false; 226 for(int i = 0; i < exceptions.length; i++) { 227 if (i == 0) 228 p.p("throws "); 229 else 230 p.p(", "); 231 String nextEx = exceptions[i].getName(); 232 p.p(nextEx); 233 if( nextEx.equals("java.rmi.RemoteException") ) { 234 throwsRemoteException = true; 235 } 236 } 237 if( exceptions.length == 0 ) { 238 p.p("throws java.rmi.RemoteException"); 239 } else if (!throwsRemoteException) { 240 p.p(", java.rmi.RemoteException"); 241 } 242 p.pln(";"); 243 p.pln(""); 244 } 245 246 } 247 | Popular Tags |