这个小工具特别适合对web服务相关知识了解较少,又急需上web服务的研发队伍使用,呵呵。
工具简单介绍:
1.整个过程由web程序驱动,程序由用户(程序员等)在IE端驱动。
2.目前底层基于axis的web服务引擎。
3.一键式操作。
用户点击确认按钮,web服务即可成功发布到指定的应用下。
原理上是可以发布到指定机器的指定的应用中。
不过还有很多细节需要完善,加油,呵呵!
下面是部分源代码,今天限于时间的原因,只能提供部分主要的代码。
限于代码中部分涉及商业机密,所以待逐步整理后,陆续将代码贴出来,直到最后提供完整的src的zip包和jar包。
package cn.webservice.autodeploy.service;
import java.io.File;
import org.apache.log4j.Logger;
import cn.lottery.common.exception.BusinessException;
import cn.webservice.autodeploy.util.AntUtil;
import cn.webservice.autodeploy.util.FileUtil;
import cn.webservice.autodeploy.WSDeployConsts;
import cn.webservice.autodeploy.domain.DefaultAntBuildProperties;
import cn.webservice.autodeploy.domain.IAntBuildProperties;
import cn.webservice.autodeploy.dto.WSAutoDeployDTO;
/**
* web服务业务接口实现
* @author Administrator
*
*/
public class WSAutoDeployServiceImpl implements IWSAutoDeployService{
protected static final Logger logger = Logger.getLogger("WSAutoDeployServiceImpl.class");
/**
* 自动部署web服务
*/
public void autoDeployWebservice(WSAutoDeployDTO wsDeployDTO) throws BusinessException {
this.genAndCompileInterfaceSrc(wsDeployDTO);
this.genJavaSrcWsAbout(wsDeployDTO);
//this.handleInterfaceImplSrc(wsDeployDTO);
}
/**
* 生成并编译接口源文件
*/
protected void genAndCompileInterfaceSrc(WSAutoDeployDTO wsDeployDTO) throws BusinessException{
String interfacePkg = wsDeployDTO.getInterfacePkg().replace('.', File.separatorChar);//包路径
String filePath = WSDeployConsts.APP_CLASS_ROOT + interfacePkg + File.separator;//文件路径
logger.info("** interfacePkg = " + interfacePkg);
logger.info("** filePath = " + filePath);
FileUtil fileUtil = new FileUtil();
fileUtil.genDirectory(wsDeployDTO,filePath);
fileUtil.genInterfaceFile(wsDeployDTO,interfacePkg,filePath);
fileUtil.insertSrcToFile(wsDeployDTO,filePath);
this.complieAndMovedSrc(wsDeployDTO,filePath);
}
/**
* 生成web服务相关的源文件
*/
protected void genJavaSrcWsAbout(WSAutoDeployDTO wsDeployDTO) throws BusinessException{
try{
AntUtil antUtil = new AntUtil();
antUtil.execAntTarget(WSDeployConsts.DEPLOY_WS);
}catch(Exception e){
e.printStackTrace();
throw new BusinessException(e.getMessage());
}
}
/**
* 插入接口实现代码
* @param wsDeployDTO
*/
protected void handleInterfaceImplSrc(WSAutoDeployDTO wsDeployDTO) throws BusinessException {
}
/**
* 编译并移动接口文件src
* @param filePath
*/
private void complieAndMovedSrc(WSAutoDeployDTO wsDeployDTO,String filePath) throws BusinessException{
IAntBuildProperties antBuildProperties = new DefaultAntBuildProperties();
antBuildProperties.modifyAntProps(wsDeployDTO); //修改build.properties
AntUtil antUtil = new AntUtil();
//执行ant目标,编译interface
antUtil.execAntTarget(WSDeployConsts.COMPILE_MOVED_INTERFACE_SRC);
}
}
package cn.webservice.autodeploy.util;
import java.io.File;
import org.apache.log4j.Logger;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import cn.lottery.common.exception.BusinessException;
/**
* ant公共类
* @author Administrator
*
*/
public class AntUtil {
protected static final Logger logger = Logger.getLogger("AntUtil.class");
/**
* 执行ant target
* @param antTarget
*/
public void execAntTarget(String antTarget) throws BusinessException{
logger.info("** start execAntTarget");
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
File buildFile = new File("/.../WEB-INF/classes/build.xml");
Project p = new Project();
p.addBuildListener(consoleLogger);
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
logger.info("** helper = " + helper);
helper.parse(p, buildFile);
p.executeTarget(antTarget);
logger.info("** end execAntTarget");
}
}
package cn.webservice.autodeploy;
/**
* 全局变量
* @author Administrator
*
*/
public class WSDeployConsts {
public static final String APP_CLASS_ROOT = "/.../WEB-INF/classes/";
//==================================================================================
//目前提供的可以自定义的build.properties文件中的key
public static final String INTERFACE_NAME = "interface.name";//尚未修改
public static final String INTERFACE_PKG = "interface.pkg";
public static final String INTERFACE_SRC_PATH = "interface.src.path";//尚未修改
public static final String ABSOLUTE_INTERFACE_PATH = "interface.absolute.path";
public static final String MOVED_INTERFACE_DIR = "moved.interface.dir";
public static final String APP_NAME = "app.home"; //应用主目录
public static final String WS_PORT = "webservice.port"; //web服务的端口key
public static final String WSDL_FILE_NAME = "wsdl.filename"; //WSDL文件名称
public static final String SERVICE_NAME = "service.name"; //名字空间
public static final String MOVED_SRC_DIR = "moved.src.dir"; //生成的文件移动到一个指定的文件夹中
//ant目标名称
public static final String COMPILE_MOVED_INTERFACE_SRC = "compile-moved-interface-src";
public static final String DEPLOY_WS = "deploy-ws";
}
package cn.webservice.autodeploy.domain;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import cn.lottery.common.exception.BusinessException;
import cn.webservice.autodeploy.WSDeployConsts;
import cn.webservice.autodeploy.dto.WSAutoDeployDTO;
/**
* ant的build.properties接口default实现类
* @author Administrator
*
*/
public class DefaultAntBuildProperties implements IAntBuildProperties{
protected static final Logger logger = Logger.getLogger("DefaultAntBuildProperties.class");
/**
* 修改build.properties中的值
*/
public void modifyAntProps(WSAutoDeployDTO wsDeployDTO) throws BusinessException{
logger.info("** start modifyAntProps");
String interfacePkgHttp = "/" + wsDeployDTO.getInterfacePkg().replace('.', File.separatorChar);//包路径
logger.info("** interfacePkgHttp = " + interfacePkgHttp);
try{
//读数据
File buildFile = new File(WSDeployConsts.APP_CLASS_ROOT + "build.properties");
InputStream in = (InputStream) (new FileInputStream(buildFile));
System.out.println("** 输入流: " + in);
Properties properties = new Properties();
properties.load(in);
String interfaceName = properties.getProperty(WSDeployConsts.INTERFACE_NAME);
String interfacePkg = properties.getProperty(WSDeployConsts.INTERFACE_PKG);
String interfaceSrcPath = properties.getProperty(WSDeployConsts.INTERFACE_SRC_PATH);
String appNameValue = properties.getProperty(WSDeployConsts.APP_NAME);
String wsPortValue = properties.getProperty(WSDeployConsts.WS_PORT);
String wsdlNameValue = properties.getProperty(WSDeployConsts.WSDL_FILE_NAME);
String serviceNameValue = properties.getProperty(WSDeployConsts.SERVICE_NAME);
String movedSrcToDirValue = properties.getProperty(WSDeployConsts.MOVED_SRC_DIR);
System.out.println("** interfaceName = " + interfaceName);
System.out.println("** interfacePkg = " + interfacePkg);
System.out.println("** interfaceSrcPath = " + interfaceSrcPath);
System.out.println("** appNameValue = " + appNameValue);
System.out.println("** wsPortValue = " + wsPortValue);
System.out.println("** wsdlNameValue = " + wsdlNameValue);
System.out.println("** serviceNameValue = " + serviceNameValue);
System.out.println("** movedSrcToDirValue = " + movedSrcToDirValue);
if(appNameValue == null || wsPortValue == null || wsdlNameValue == null
|| serviceNameValue == null || movedSrcToDirValue == null){
throw new Exception("** ant属性文件缺少key值");
}
logger.info("** wsDeployDTO = " + wsDeployDTO.toString());
//写入修改后的数据
Properties p = new Properties();
p.setProperty(WSDeployConsts.INTERFACE_NAME,wsDeployDTO.getInterfaceName());
p.setProperty(WSDeployConsts.INTERFACE_PKG,wsDeployDTO.getInterfacePkg());
p.setProperty(WSDeployConsts.INTERFACE_SRC_PATH,interfacePkgHttp);//将"."转换为"/"
p.setProperty(WSDeployConsts.MOVED_INTERFACE_DIR,"/.../WEB-INF/wssrc/interface");
p.setProperty(WSDeployConsts.APP_NAME,wsDeployDTO.getAppPath());
p.setProperty(WSDeployConsts.WS_PORT,wsDeployDTO.getWsport());
p.setProperty(WSDeployConsts.WSDL_FILE_NAME,wsDeployDTO.getWsdlFileName());
p.setProperty(WSDeployConsts.SERVICE_NAME,wsDeployDTO.getServiceName());
p.setProperty(WSDeployConsts.MOVED_SRC_DIR,"/.../WEB-INF/wssrc");
FileOutputStream fos = new FileOutputStream(buildFile);
p.store(fos,"end");
logger.info("** end modifyAntProps");
}catch(Exception e){
logger.error("修改ant属性文件: " + e.getMessage());
throw new BusinessException("修改ant属性文件失败");
}
}
}