1 23 24 package com.sun.enterprise.webservice; 25 26 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.Modifier ; 29 import java.io.OutputStream ; 30 import java.io.OutputStreamWriter ; 31 import java.io.IOException ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.util.LinkedList ; 35 36 import java.util.logging.Logger ; 37 38 import javax.jws.WebMethod; 39 40 import com.sun.ejb.codegen.Generator; 41 import com.sun.ejb.codegen.GeneratorException; 42 43 import sun.rmi.rmic.IndentingWriter; 44 45 import static java.lang.reflect.Modifier .*; 46 import static com.sun.corba.ee.spi.codegen.Wrapper.*; 47 import com.sun.corba.ee.spi.codegen.Type; 48 import com.sun.corba.ee.impl.codegen.ClassGenerator; 49 import com.sun.ejb.codegen.ClassGeneratorFactory; 50 import com.sun.enterprise.util.LocalStringManagerImpl; 51 import com.sun.logging.LogDomains; 52 53 59 public class ServiceInterfaceGenerator extends Generator 60 implements ClassGeneratorFactory { 61 62 private static LocalStringManagerImpl localStrings = 63 new LocalStringManagerImpl(ServiceInterfaceGenerator.class); 64 private static Logger _logger=null; 65 static{ 66 _logger=LogDomains.getLogger(LogDomains.DPL_LOGGER); 67 } 68 69 Class sib=null; 70 String serviceIntfName; 71 String packageName; 72 String serviceIntfSimpleName; 73 Method [] intfMethods; 74 75 80 public ServiceInterfaceGenerator(ClassLoader cl, Class sib) 81 throws GeneratorException, ClassNotFoundException 82 { 83 super(); 84 85 this.sib = sib; 86 serviceIntfSimpleName = getServiceIntfName(); 87 88 packageName = getPackageName(); 89 serviceIntfName = packageName + "." + serviceIntfSimpleName; 90 91 intfMethods = calculateMethods(sib, removeDups(sib.getMethods())); 92 93 } 96 97 public String getServiceIntfName() { 98 String serviceIntfSimpleName = sib.getSimpleName(); 99 if (serviceIntfSimpleName.endsWith("EJB")) { 100 return serviceIntfSimpleName.substring(0, serviceIntfSimpleName.length()-3); 101 } else { 102 return serviceIntfSimpleName+"SEI"; 103 } 104 } 105 106 public String getPackageName() { 107 return sib.getPackage().getName()+".internal.jaxws"; 108 } 109 110 116 public String getGeneratedClass() { 117 return serviceIntfName; 118 } 119 120 public String className() { 122 return getGeneratedClass(); 123 } 124 125 private Method [] calculateMethods(Class sib, Method [] initialList) { 126 127 boolean webMethodAnnotationUsed = false; 129 List <Method > list = new ArrayList <Method >(); 130 131 for (Method m : initialList) { 132 WebMethod wm = m.getAnnotation(javax.jws.WebMethod.class); 133 if (wm!=null && webMethodAnnotationUsed==false) { 134 webMethodAnnotationUsed=true; 135 list.clear(); 137 } 138 if (wm!=null) { 139 list.add(m); 140 } else { 141 if (!webMethodAnnotationUsed && !m.getDeclaringClass().equals(java.lang.Object .class)) { 142 list.add(m); 143 } 144 } 145 } 146 return list.toArray(new Method [0]); 147 } 148 149 public ClassGenerator evaluate() { 150 151 _clear(); 152 153 if (packageName != null) { 154 _package(packageName); 155 } 156 157 _interface(PUBLIC, serviceIntfName); 158 159 for(int i = 0; i < intfMethods.length; i++) { 160 printMethod(intfMethods[i]); 161 } 162 163 _end(); 164 165 return _classGenerator() ; 166 167 } 168 169 170 171 177 public void generate(OutputStream out) 178 throws GeneratorException, IOException 179 { 180 IndentingWriter p = new IndentingWriter(new OutputStreamWriter (out)); 181 182 p.pln(""); 183 184 if (packageName != null) { 185 p.pln("package " + packageName + ";"); 186 } 187 188 p.pln(""); 189 190 p.plnI("public interface " + serviceIntfSimpleName + " {"); 191 192 p.pln(""); 193 194 for(int i = 0; i < intfMethods.length; i++) { 196 printMethod(p, intfMethods[i]); 197 } 198 199 p.pOln("}"); 200 p.close(); 201 } 202 203 private void printMethod(Method m) 204 { 205 206 boolean throwsRemoteException = false; 207 List <Type> exceptionList = new LinkedList <Type>(); 208 for(Class exception : m.getExceptionTypes()) { 209 exceptionList.add(Type.type(exception)); 210 if( exception.getName().equals("java.rmi.RemoteException") ) { 211 throwsRemoteException = true; 212 } 213 } 214 if( !throwsRemoteException ) { 215 exceptionList.add(_t("java.rmi.RemoteException")); 216 } 217 218 _method( PUBLIC | ABSTRACT, Type.type(m.getReturnType()), 219 m.getName(), exceptionList); 220 221 int i = 0; 222 for(Class param : m.getParameterTypes()) { 223 _arg(Type.type(param), "param" + i); 224 i++; 225 } 226 227 _end(); 228 } 229 230 231 237 private void printMethod(IndentingWriter p, Method m) 238 throws IOException 239 { 240 p.pln(""); 241 242 p.p("public " + printType(m.getReturnType()) + " " 244 + m.getName() + "("); 245 Class [] params = m.getParameterTypes(); 246 for(int i = 0; i < params.length; i++) { 247 if (i != 0) 248 p.p(", "); 249 p.p(printType(params[i]) + " param" + i); 250 } 251 p.p(") "); 252 Class [] exceptions = m.getExceptionTypes(); 253 boolean throwsRemoteException = false; 254 for(int i = 0; i < exceptions.length; i++) { 255 if (i == 0) 256 p.p("throws "); 257 else 258 p.p(", "); 259 String nextEx = exceptions[i].getName(); 260 p.p(nextEx); 261 if( nextEx.equals("java.rmi.RemoteException") ) { 262 throwsRemoteException = true; 263 } 264 } 265 if( exceptions.length == 0 ) { 266 p.p("throws java.rmi.RemoteException"); 267 } else if (!throwsRemoteException) { 268 p.p(", java.rmi.RemoteException"); 269 } 270 p.pln(";"); 271 p.pln(""); 272 } 273 274 } 275 | Popular Tags |