001package net.gdface.codegen.webclient; 002 003import org.slf4j.Logger; 004import org.slf4j.LoggerFactory; 005 006import net.gdface.codegen.CodeGenUtils; 007import net.gdface.codegen.Method; 008import net.gdface.exception.ServiceRuntime; 009import net.gdface.utils.Assert; 010 011public class StubClient<T> extends WebClient<T> { 012 private static final Logger logger = LoggerFactory.getLogger(StubClient.class); 013 014 private final String stubPackage; 015 private final String serviceName; 016 private final String stubClassSimpleName; 017 private final String stubClassName; 018 019 public StubClient(Class<T> interfaceClass, Class<? extends T> refClass, Class<?> serviceClass, String stubPackage) { 020 super(interfaceClass, refClass, serviceClass); 021 Assert.notEmpty(stubPackage, "stubPackage"); 022 this.stubPackage = stubPackage; 023 getImportedList().put("AxisFault", org.apache.axis2.AxisFault.class); 024 getImportedList().put("RemoteException", java.rmi.RemoteException.class); 025 serviceName = serviceClass.getSimpleName(); 026 stubClassSimpleName = serviceName + "Stub"; 027 stubClassName = stubPackage + "." + stubClassSimpleName; 028 } 029 030 public final Class<?> toStubClass(Method method) throws ClassNotFoundException { 031 return toStubClass(method.getName()); 032 } 033 034 public final Class<?> toStubClass(String methodName) throws ClassNotFoundException { 035 return Class.forName(toStubClassName(methodName)); 036 } 037 038 public final String toStubClassName(Method method) throws ClassNotFoundException { 039 return toStubClassName(method.getName()); 040 } 041 042 public final String toStubClassName(String methodName) { 043 Assert.notEmpty(methodName, "methodName"); 044 StringBuffer buffer = new StringBuffer(); 045 buffer.append(stubPackage).append(".").append(stubClassSimpleName).append("."); 046 buffer.append(CodeGenUtils.firstUpperCase(methodName)); 047 return buffer.toString(); 048 } 049 050 public final String toStubClassName(Class<?> parameterClass) { 051 Assert.notNull(parameterClass, "parameterClass"); 052 if (parameterClass.isArray()) 053 return toStubClassName(parameterClass.getComponentType()) + "[]"; 054 else { 055 StringBuffer buffer = new StringBuffer(); 056 if(!CodeGenUtils.isBaseDataType(parameterClass)) 057 buffer.append(stubPackage).append(".").append(stubClassSimpleName).append("."); 058 buffer.append(parameterClass.getSimpleName()); 059 return buffer.toString(); 060 } 061 } 062 063 public final Class<?> getParmameterClassForSetMethod(Class<?> methodClazz, String setMethod) 064 throws NoSuchMethodException { 065 Assert.notNull(methodClazz, "methodClazz"); 066 for (java.lang.reflect.Method set : methodClazz.getDeclaredMethods()) { 067 if (set.getName().equals(setMethod)) 068 return set.getParameterTypes()[0]; 069 } 070 throw new NoSuchMethodException(); 071 } 072 073 public final String toSetMethod(String parameterName) { 074 Assert.notEmpty(parameterName, "parameterName"); 075 StringBuffer buffer = new StringBuffer("set"); 076 buffer.append(CodeGenUtils.firstUpperCase(parameterName)); 077 return buffer.toString(); 078 } 079 080 public final Class<?> toStubExpection(Class<? extends Throwable> expection) throws ClassNotFoundException { 081 return Class.forName(toStubExpectionClassName(expection)); 082 } 083 084 public final String toStubExpectionClassName(Class<? extends Throwable> expection) throws ClassNotFoundException { 085 Assert.notNull(expection, "expection"); 086 StringBuffer buffer = new StringBuffer(); 087 buffer.append(stubPackage).append("."); 088 buffer.append(serviceName).append(expection.getSimpleName()).append("Exception"); 089 return buffer.toString(); 090 } 091 092 public final Class<?> toExpection(Method method, Class<? extends Throwable> exception) 093 throws ClassNotFoundException { 094 Assert.notNull(exception, "exception"); 095 String name = exception.getSimpleName().replaceAll(serviceName + "(\\w+)Exception", "$1"); 096 if (ServiceRuntime.class.getSimpleName().equals(name)) 097 return ServiceRuntime.class; 098 for (Class<?> exp : CodeGenUtils.toSet(method.getExceptionTypes())) { 099 if (exp.getSimpleName().equals(name)) 100 return exp; 101 } 102 throw new ClassNotFoundException(String.format("Cant found Exception Class [%s] map to %s", name, 103 exception.getSimpleName())); 104 } 105 106 @Override 107 public boolean compile() { 108 boolean compileOk = false; 109 try { 110 if (super.compile()) { 111 method2PortMap = createMethod2PortMapMap(); 112 addImportedClassFromMethods(method2PortMap.values()); 113 compileOk = true; 114 } 115 } catch (NoSuchMethodException e) { 116 logger.error(e.toString()); 117 } 118 return compileOk; 119 } 120 121 /** 122 * @return stubClassSimpleName 123 */ 124 public String getStubClassSimpleName() { 125 return stubClassSimpleName; 126 } 127 128 /** 129 * @return stubClassName 130 */ 131 public String getStubClassName() { 132 return stubClassName; 133 } 134 135}