|
@@ -0,0 +1,186 @@
|
|
|
+package com.cxfws.config.service;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.logging.Level;
|
|
|
+import java.util.logging.Logger;
|
|
|
+
|
|
|
+import javax.jws.WebService;
|
|
|
+
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.cxf.feature.Feature;
|
|
|
+import org.apache.cxf.interceptor.Interceptor;
|
|
|
+import org.apache.cxf.message.Message;
|
|
|
+import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.core.io.support.ResourcePatternResolver;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+public class CxfServiceQuickReleaseFactory implements InitializingBean, ApplicationContextAware{
|
|
|
+ private final static Logger logger = Logger.getLogger(CxfServiceQuickReleaseFactory.class.getName());
|
|
|
+
|
|
|
+ private ApplicationContext context;
|
|
|
+ private String scanPackage;
|
|
|
+ private String quickRelease;
|
|
|
+
|
|
|
+ private List<Feature> jaxwsfeatures;
|
|
|
+ private List<Interceptor<? extends Message>> jaxwsinterceptors;
|
|
|
+
|
|
|
+ //webService.url
|
|
|
+ @Value("${webService.url:null}")
|
|
|
+ private String webServiceUrl;
|
|
|
+ //webService.port
|
|
|
+ @Value("${webService.port:null}")
|
|
|
+ private String webServicePort;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+ //System.out.println(this.context+"------afterPropertiesSet-------"+scanPackage);
|
|
|
+ if("false".equalsIgnoreCase(quickRelease)){
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(StringUtils.isBlank(scanPackage)||StringUtils.isBlank(webServiceUrl)||StringUtils.isBlank(webServicePort)){
|
|
|
+ throw new RuntimeException("快速发布web service因为缺少必要参数而失败");
|
|
|
+ }
|
|
|
+ ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
+ List<String> lists=getResources(resolver,scanPackage);
|
|
|
+ //System.out.println("------->"+new JSONArray(lists).toString());
|
|
|
+ if(null!=lists&&!lists.isEmpty()){
|
|
|
+ for(String pid:lists){
|
|
|
+ Object object = context.getBean(pid);
|
|
|
+
|
|
|
+ if(logger.isLoggable(Level.INFO)){
|
|
|
+ logger.info(new StringBuffer().append("http://").append(webServiceUrl).append(":")
|
|
|
+ .append(webServicePort).append("/").append(pid).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
|
|
|
+ //factoryBean.setAddress("http://127.0.0.1:8089/"+pid);
|
|
|
+ factoryBean.setAddress(new StringBuffer().append("http://").append(webServiceUrl).append(":")
|
|
|
+ .append(webServicePort).append("/").append(pid).toString());
|
|
|
+ //factoryBean.setServiceClass(object.getClass().getInterfaces()[0]);
|
|
|
+ factoryBean.setServiceClass(context.getType(pid));
|
|
|
+ factoryBean.setServiceBean(object);
|
|
|
+
|
|
|
+ //org.apache.cxf.feature.AbstractFeature
|
|
|
+ //org.apache.cxf.feature.Feature
|
|
|
+ //factoryBean.getInInterceptors()
|
|
|
+ if(null != jaxwsfeatures&&!jaxwsfeatures.isEmpty()){
|
|
|
+ factoryBean.getFeatures().addAll(jaxwsfeatures);
|
|
|
+ }
|
|
|
+ if(null != jaxwsinterceptors&&!jaxwsinterceptors.isEmpty()){
|
|
|
+ factoryBean.getInInterceptors().addAll(jaxwsinterceptors);
|
|
|
+ }
|
|
|
+ //发布服务 publish()...
|
|
|
+ factoryBean.create();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> getResources(ResourcePatternResolver resolver,
|
|
|
+ String location) throws IOException {
|
|
|
+ List<String> resourcePathList = null;
|
|
|
+ for (Resource resource : resolver.getResources(location)) {
|
|
|
+ String description = resource == null ? "" : resource
|
|
|
+ .getDescription();
|
|
|
+ //System.out.println(description);
|
|
|
+
|
|
|
+ String classesDir=new StringBuffer().append(File.separator).append("classes").append(File.separator).toString();
|
|
|
+ String classEndStr=".class]";
|
|
|
+ if(description.endsWith(classEndStr)&&description.indexOf(classesDir)>0){
|
|
|
+ description=StringUtils.replace(description.substring(description.indexOf(classesDir)+classesDir.length(),description.indexOf(classEndStr)),File.separator, ".") ;
|
|
|
+ try {
|
|
|
+ Class clazz = Class.forName(description);
|
|
|
+
|
|
|
+ if(!clazz.isInterface()){
|
|
|
+ String springid=StringUtils.uncapitalize(clazz.getSimpleName());
|
|
|
+ Annotation[] annotations=clazz.getAnnotations();
|
|
|
+ boolean isquick=false;
|
|
|
+ boolean isService=false;
|
|
|
+ for(Annotation annotation:annotations){
|
|
|
+ if(!isquick&&annotation.annotationType().isAssignableFrom(WebService.class)){
|
|
|
+ isquick=true;
|
|
|
+ }
|
|
|
+ if(annotation.annotationType().isAssignableFrom(Service.class)){
|
|
|
+ isService=true;
|
|
|
+ Service serviceAnnotation = (Service) annotation;
|
|
|
+ if(StringUtils.isNotBlank(serviceAnnotation.value())){
|
|
|
+ springid=serviceAnnotation.value();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //System.out.println( springid +"----"+isquick+"----"+isService);
|
|
|
+ if(isquick&&isService){
|
|
|
+ if(null == resourcePathList){
|
|
|
+ resourcePathList = new ArrayList<String>();
|
|
|
+ }
|
|
|
+ resourcePathList.add(springid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resourcePathList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getScanPackage() {
|
|
|
+ return scanPackage;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setScanPackage(String scanPackage) {
|
|
|
+ this.scanPackage = scanPackage;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setApplicationContext(ApplicationContext context) throws BeansException {
|
|
|
+ this.context=context;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getQuickRelease() {
|
|
|
+ return quickRelease;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setQuickRelease(String quickRelease) {
|
|
|
+ this.quickRelease = quickRelease;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Feature> getJaxwsfeatures() {
|
|
|
+ return jaxwsfeatures;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setJaxwsfeatures(List<Feature> jaxwsfeatures) {
|
|
|
+ this.jaxwsfeatures = jaxwsfeatures;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Interceptor<? extends Message>> getJaxwsinterceptors() {
|
|
|
+ return jaxwsinterceptors;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setJaxwsinterceptors(List<Interceptor<? extends Message>> jaxwsinterceptors) {
|
|
|
+ this.jaxwsinterceptors = jaxwsinterceptors;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|