001package net.gdface.codegen.webclient;
002
003import java.io.File;
004import java.util.ArrayList;
005import java.util.Arrays;
006import java.util.Iterator;
007
008import net.gdface.codegen.Method;
009import net.gdface.exception.ServiceRuntime;
010import net.gdface.utils.Assert;
011import net.gdface.utils.Judge;
012
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016public class GSoapClient<T> extends WebClient<T> {
017        private static final Logger logger = LoggerFactory.getLogger(GSoapClient.class);
018        private final String serviceName;
019
020        private final GSoapHeaderHelper gsoapHelper;
021        /**
022         * @param interfaceClass
023         * @param refClass
024         * @param serviceClass
025         * @param serviceName
026         * @param stubFolder gsoap stub代码文件夹
027         * @param stubPrefix gsoap stub代码文件前缀
028         */
029        public GSoapClient(Class<T> interfaceClass, Class<? extends T> refClass, Class<?> serviceClass, String serviceName,
030                        File stubFolder, String stubPrefix) {
031                super(interfaceClass, refClass, serviceClass);
032                Assert.notNull(stubFolder, "stubFolder");
033                // stubFolder不是文件夹则抛出异常
034                if(!stubFolder.isDirectory())
035                        throw new IllegalArgumentException(String.format("%s must be a existing directory",stubFolder.getAbsolutePath()));
036                this.serviceName = Judge.isEmpty(serviceName) ? serviceClass.getSimpleName() : serviceName;
037                this.gsoapHelper=new GSoapHeaderHelper(stubFolder,serviceClass, stubPrefix);
038        }
039
040        @Override
041        protected void createMethodsNeedGenerated() {
042                // 将所有接口中的方法加入methodsNeedGenerated,后续再用removeMethodsNotPort删除不在webservice接口中实现的方法               
043                ArrayList<java.lang.reflect.Method> interfaceMethods = new ArrayList<java.lang.reflect.Method>(
044                                Arrays.asList(interfaceClass.getMethods()));
045                try {
046                        for (Iterator<java.lang.reflect.Method> it = interfaceMethods.iterator(); it.hasNext();) {
047                                java.lang.reflect.Method im = it.next();
048                                this.methodsNeedGenerated.add(new Method(refClass.getMethod(im.getName(), im.getParameterTypes()),
049                                                this.paramTable.getParameterNames(im.getName(), im.getParameterTypes())));
050                        }
051                } catch (NoSuchMethodException e) {
052                        throw new RuntimeException(e);
053                }
054        }
055        /**
056         * 从 methodsNeedGenerated  删除非webservice接口实现方法
057         */
058        protected void removeMethodsNotPort() {
059                for (Iterator<Method> it = methodsNeedGenerated.iterator(); it.hasNext();) {
060                        Method im = it.next();
061                        if(getServicePort(im)==null){
062                                logger.info("remove method {}",im.getDocSignature());
063                                it.remove();
064                        }
065                }
066        }
067        @Override
068        public boolean compile() {
069                boolean compileOk = false;
070                try {
071                        if (super.compile()) {
072                                removeMethodsNotPort();
073                                method2PortMap = createMethod2PortMapMap();
074                                addImportedClassFromMethods(method2PortMap.values());
075                                compileOk = true;
076                        }
077                } catch (NoSuchMethodException e) {
078                        logger.error(e.toString());
079                }
080                return compileOk;
081        }
082
083        /**
084         * @return serviceName
085         */
086        public String getServiceName() {
087                return serviceName;
088        }
089
090        /**
091         * @return gsoapHelper
092         */
093        public GSoapHeaderHelper getGsoapHelper() {
094                return gsoapHelper;
095        }
096        
097        public Class<?> getServiceRuntimeClass(){
098                return ServiceRuntime.class;
099        }
100}