曾经做过两年的Axis2开发Webservice的各种接口,传参是XML报文,到如今已经快两年没怎么接触过Webservice相关的内容了。前一周,接了个需求,需要对外部系统提供一个Webservice接口,于是又回来折腾Webservice相关的东东。
因为看到外部系统之前与我们公司核心系统对接的Webservice采用的都是axis1.4的,于是,我就用axis1.4下弄了个小demo,因为考虑到业务需求,将最简单的demo升级了一下,传参入参自己设计了一套javaBean,就是之前的一篇文章里的demo。之后再结合业务需求,将这个demo放入到现有的系统中,各种测试都没有问题。
然而,虽然通过demo和网上的资料知道了Webservice的搭建过程,却不知道Webservice底层是怎么操作的,server-config.wsdd文件又是怎么加载的,于是,又把axis1.4相关的jar包都大致浏览了一遍。
首先,配置web.xml的时候,有这样的一段代码:
<listener> <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class> </listener> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AxisServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
可以看到,axis其实本质还是一个servlet,通过路径org.apache.axis.transport.http.AxisServlet,找到这个servlet的初始化方法:
public class AxisServlet extends AxisServletBase: public void init() throws ServletException { super.init(); ServletContext context = getServletConfig().getServletContext(); isDebug = log.isDebugEnabled(); if (isDebug) { log.debug("In servlet init"); } this.transportName = getOption(context, "transport.name", "http"); if (JavaUtils.isTrueExplicitly(getOption(context, "use-servlet-security", null))) { this.securityProvider = new ServletSecurityProvider(); } this.enableList = JavaUtils.isTrueExplicitly(getOption(context, "axis.enableListQuery", null)); this.jwsClassDir = getOption(context, "axis.jws.servletClassDir", null); this.disableServicesList = JavaUtils.isTrue(getOption(context, "axis.disableServiceList", "false")); this.servicesPath = getOption(context, "axis.servicesPath", "/services/"); if (this.jwsClassDir != null) { if (getHomeDir() != null) this.jwsClassDir = (getHomeDir() + this.jwsClassDir); } else { this.jwsClassDir = getDefaultJWSClassDir(); } initQueryStringHandlers(); try { ServiceAdmin.setEngine(getEngine(), context.getServerInfo()); } catch (AxisFault af) { exceptionLog.info("Exception setting AxisEngine on ServiceAdmin " + af); } }
一般初始化的时候都会去加载各种需要的配置文件,这个servlet继承的父类里有这样的方法:(由上到下,分别是方法调用的过程)
AxisServletBase: public void init() throws ServletException { ServletContext context = getServletConfig().getServletContext(); this.webInfPath = context.getRealPath("/WEB-INF"); this.homeDir = context.getRealPath("/"); isDebug = log.isDebugEnabled(); if (log.isDebugEnabled()) log.debug("In AxisServletBase init"); this.isDevelopment = JavaUtils.isTrueExplicitly(getOption(context, "axis.development.system", null)); } protected String getOption(ServletContext context, String param, String dephault) { String value = AxisProperties.getProperty(param); if (value == null) { value = getInitParameter(param); } if (value == null) value = context.getInitParameter(param); try { AxisServer engine = getEngine(this); if ((value == null) && (engine != null)) value = (String)engine.getOption(param); } catch (AxisFault axisFault) { } return (value != null) ? value : dephault; } public static AxisServer getEngine(HttpServlet servlet) throws AxisFault { AxisServer engine = null; if (isDebug) { log.debug("Enter: getEngine()"); } ServletContext context = servlet.getServletContext(); synchronized (context) { engine = retrieveEngine(servlet); if (engine == null) { Map environment = getEngineEnvironment(servlet); engine = AxisServer.getServer(environment); engine.setName(servlet.getServletName()); storeEngine(servlet, engine); } } if (isDebug) { log.debug("Exit: getEngine()"); } return engine; } protected static Map getEngineEnvironment(HttpServlet servlet) { Map environment = new HashMap(); String attdir = servlet.getInitParameter("axis.attachments.Directory"); if (attdir != null) { environment.put("axis.attachments.Directory", attdir); } ServletContext context = servlet.getServletContext(); environment.put("servletContext", context); String webInfPath = context.getRealPath("/WEB-INF"); if (webInfPath != null) { environment.put("servlet.realpath", webInfPath + File.separator + "attachments"); } EngineConfiguration config = EngineConfigurationFactoryFinder.newFactory(servlet).getServerEngineConfig(); if (config != null) { environment.put("engineConfig", config); } return environment; }
于是,我发现了EngineConfigurationFactoryFinder.newFactory(servlet).getServerEngineConfig();这个方法在axis的jar包中的这个类里:EngineConfigurationFactoryServlet
private static EngineConfiguration getServerEngineConfig(ServletConfig cfg) { ServletContext ctx = cfg.getServletContext(); String configFile = cfg.getInitParameter("axis.ServerConfigFile"); if (configFile == null) { configFile = AxisProperties.getProperty("axis.ServerConfigFile"); } if (configFile == null) { configFile = "server-config.wsdd"; } String appWebInfPath = "/WEB-INF"; FileProvider config = null; String realWebInfPath = ctx.getRealPath(appWebInfPath); if ((realWebInfPath == null) || (!new File(realWebInfPath, configFile).exists())) { String name = appWebInfPath + "/" + configFile; InputStream is = ctx.getResourceAsStream(name); if (is != null) { config = new FileProvider(is); } if (config == null) { log.error(Messages.getMessage("servletEngineWebInfError03", name)); } } if ((config == null) && (realWebInfPath != null)) { try { config = new FileProvider(realWebInfPath, configFile); } catch (ConfigurationException e) { log.error(Messages.getMessage("servletEngineWebInfError00"), e); } } if (config == null) { log.warn(Messages.getMessage("servletEngineWebInfWarn00")); try { InputStream is = ClassUtils.getResourceAsStream(AxisServer.class, "server-config.wsdd"); config = new FileProvider(is); } catch (Exception e) { log.error(Messages.getMessage("servletEngineWebInfError02"), e); } } return config; } }
找到这里,我们就可以知道,axis 的servlet是怎么取找server-config.wsdd文件,进而知道我们配置了那些service了。