一、前言
基于airavata-embedded-tomcat实现的增强tomcat服务org.apache.airavata.server.BetterTomcat、org.apache.airavata.server.ServerMain源码,对于需要集成和自定义servlet服务器容器等应用场景,可以参考源码其实现原理。
二、源码说明
1.org.apache.airavata.server.ServerMain入口启动类
package org.apache.airavata.server;@b@@b@import java.io.File;@b@import org.apache.airavata.common.utils.AiravataUtils;@b@import org.apache.airavata.common.utils.ServerSettings;@b@import org.apache.catalina.Wrapper;@b@import org.apache.catalina.core.StandardContext;@b@import org.apache.catalina.deploy.FilterDef;@b@import org.apache.catalina.deploy.FilterMap;@b@import org.apache.catalina.startup.Tomcat;@b@@b@public class ServerMain@b@{@b@ private Tomcat embedded = null;@b@@b@ public void startTomcat()@b@ throws Exception@b@ {@b@ AiravataUtils.setExecutionAsServer();@b@ String protocol = (ServerSettings.isEnableHttps()) ? "https" : "http";@b@ BetterTomcat tomcat = new BetterTomcat(Integer.parseInt(ServerSettings.getTomcatPort(protocol)));@b@ tomcat.addContext("/airavata-server", System.getenv("AIRAVATA_HOME"));@b@ Wrapper axis2Servlet = tomcat.addServlet("/airavata-server", "AxisServlet", "org.apache.axis2.transport.http.AxisServlet");@b@ axis2Servlet.addMapping("/servlet/AxisServlet");@b@ axis2Servlet.addMapping("*.jws");@b@ axis2Servlet.addMapping("/services/*");@b@ axis2Servlet.addInitParameter("axis2.repository.path", System.getenv("AIRAVATA_HOME") + File.separator + "repository");@b@ axis2Servlet.addInitParameter("axis2.xml.path", System.getenv("AIRAVATA_HOME") + File.separator + "bin" + File.separator + "axis2.xml");@b@@b@ axis2Servlet.setLoadOnStartup(1);@b@@b@ StandardContext context = (StandardContext)tomcat.getTomcat().addContext("/airavata", System.getenv("AIRAVATA_HOME"));@b@ Wrapper experimentServlet = tomcat.addServlet("/airavata", "Airavata Experiment Service", "com.sun.jersey.spi.container.servlet.ServletContainer");@b@ experimentServlet.addInitParameter("com.sun.jersey.config.property.packages", "org.apache.airavata.services.experiment;org.codehaus.jackson.jaxrs");@b@ experimentServlet.setLoadOnStartup(1);@b@@b@ Wrapper configurationServlet = tomcat.addServlet("/airavata", "Airavata Server Configuration Service", "com.sun.jersey.spi.container.servlet.ServletContainer");@b@ configurationServlet.addInitParameter("com.sun.jersey.config.property.packages", "org.apache.airavata.services.server;org.codehaus.jackson.jaxrs");@b@ configurationServlet.setLoadOnStartup(1);@b@@b@ Wrapper registryServlet = tomcat.addServlet("/airavata", "Airavata Registry Service", "com.sun.jersey.spi.container.servlet.ServletContainer");@b@ registryServlet.addInitParameter("com.sun.jersey.config.property.packages", "org.apache.airavata.services.registry.rest;org.codehaus.jackson.jaxrs");@b@ registryServlet.setLoadOnStartup(1);@b@@b@ FilterDef filter1definition = new FilterDef();@b@ filter1definition.setFilterName("AuthenticationFilter");@b@ filter1definition.setFilterClass("org.apache.airavata.services.registry.rest.security.HttpAuthenticatorFilter");@b@ filter1definition.addInitParameter("authenticatorConfigurations", "authenticators.xml");@b@ context.addFilterDef(filter1definition);@b@@b@ FilterMap filter1mapping = new FilterMap();@b@ filter1mapping.setFilterName("AuthenticationFilter");@b@ filter1mapping.addURLPattern("/user-store/*");@b@ filter1mapping.addURLPattern("/services/registry/*");@b@ filter1mapping.addURLPattern("/services/server/*");@b@ filter1mapping.addURLPattern("/services/experiment/*");@b@ context.addFilterMap(filter1mapping);@b@ registryServlet.addMapping("/services/registry/*");@b@ configurationServlet.addMapping("/services/server/*");@b@ experimentServlet.addMapping("/services/experiment/*");@b@ context.addApplicationListener("org.apache.airavata.rest.mappings.utils.RegistryListener");@b@@b@ tomcat.start();@b@ }@b@@b@ public void stopTomcat()@b@ throws Exception@b@ {@b@ this.embedded.stop();@b@ }@b@@b@ public static void main(String[] args)@b@ {@b@ try {@b@ new Thread() {@b@ public void run() {@b@ ServerMain tomcat = new ServerMain();@b@ try {@b@ tomcat.startTomcat();@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ }@b@ .start();@b@@b@ Thread.sleep(10000L);@b@ }@b@ catch (Exception e)@b@ {@b@ e.printStackTrace();@b@ }@b@ }@b@}
2.org.apache.airavata.server.BetterTomcat(基于org.apache.catalina.startup.Tomcat的自定义增强的服务器)
package org.apache.airavata.server;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@import javax.servlet.Servlet;@b@import javax.servlet.ServletException;@b@import org.apache.catalina.Context;@b@import org.apache.catalina.Engine;@b@import org.apache.catalina.Host;@b@import org.apache.catalina.LifecycleException;@b@import org.apache.catalina.LifecycleState;@b@import org.apache.catalina.Realm;@b@import org.apache.catalina.Service;@b@import org.apache.catalina.Wrapper;@b@import org.apache.catalina.connector.Connector;@b@import org.apache.catalina.core.StandardHost;@b@import org.apache.catalina.startup.Tomcat;@b@import org.apache.coyote.http11.AbstractHttp11JsseProtocol;@b@import org.apache.tomcat.util.IntrospectionUtils;@b@@b@public class BetterTomcat@b@{@b@ private Tomcat tomcat;@b@ private static Map<String, Connector> connectors = new HashMap();@b@ private boolean unpackWars;@b@@b@ public BetterTomcat()@b@ {@b@ this.tomcat = new Tomcat();@b@@b@ this.unpackWars = true;@b@@b@ this.tomcat.setConnector(new Connector(Protocol.HTTP_11.getProtocolName()));@b@ }@b@@b@ public BetterTomcat(int port)@b@ {@b@ addConnector(Protocol.HTTP_11, null, port);@b@ }@b@@b@ public void setUnpackWars(boolean unpackWars)@b@ {@b@ this.unpackWars = unpackWars;@b@ }@b@@b@ public void start()@b@ throws LifecycleException@b@ {@b@ this.tomcat.start();@b@ }@b@@b@ public void stop()@b@ throws LifecycleException@b@ {@b@ this.tomcat.stop();@b@ }@b@@b@ public Host getHost() {@b@ Host host = this.tomcat.getHost();@b@ ((StandardHost)host).setUnpackWARs(this.unpackWars);@b@ return host;@b@ }@b@@b@ public Context addWebapp(String contextPath, String webappFilePath)@b@ throws BetterTomcatException@b@ {@b@ Context context;@b@ try@b@ {@b@ context = this.tomcat.addWebapp(contextPath, webappFilePath);@b@ if (context.getState().equals(LifecycleState.STOPPED))@b@ throw new BetterTomcatException("Webapp " + context + " failed to deploy");@b@@b@ if (!(this.unpackWars)) {@b@ context.addParameter("antiJARLocking", "false");@b@ context.addParameter("antiResourceLocking", "false");@b@ }@b@ return context;@b@ } catch (ServletException e) {@b@ throw new BetterTomcatException("Webapp failed to deploy", e);@b@ }@b@ }@b@@b@ public Context addWebapp(Host host, String contextPath, String webappFilePath)@b@ {@b@ return this.tomcat.addWebapp(host, contextPath, webappFilePath);@b@ }@b@@b@ public Context addContext(String contextPath, String baseDir)@b@ {@b@ return this.tomcat.addContext(contextPath, baseDir);@b@ }@b@@b@ public Context addContext(Host host, String contextPath, String dir) {@b@ return this.tomcat.addContext(host, contextPath, dir);@b@ }@b@@b@ public Wrapper addServlet(String contextPath, String servletName, String servletClass)@b@ {@b@ return this.tomcat.addServlet(contextPath, servletName, servletClass);@b@ }@b@@b@ public Wrapper addServlet(String contextPath, String servletName, Servlet servlet)@b@ {@b@ return this.tomcat.addServlet(contextPath, servletName, servlet);@b@ }@b@@b@ public void enableNaming()@b@ {@b@ this.tomcat.enableNaming();@b@ }@b@@b@ public Connector addConnector(int port) {@b@ return addConnector(Protocol.HTTP_11, null, port);@b@ }@b@@b@ public Connector addConnector(Protocol protocol, String address, int port)@b@ {@b@ Connector connector = (Connector)connectors.get(protocol + "-" + address + "-" + port);@b@ if (connector == null) {@b@ connector = new Connector(protocol.getProtocolClass());@b@ if (address != null)@b@ IntrospectionUtils.setProperty(connector, "address", address);@b@@b@ connector.setPort(port);@b@ connector.setEnableLookups(true);@b@ connector.setProperty("bindOnInit", "false");@b@ if ((protocol.equals(Protocol.HTTPS_11)) || (protocol.equals(Protocol.HTTPS_11_NIO))) {@b@ connector.setSecure(true);@b@ connector.setAttribute("SSLEnabled", "true");@b@ connector.setScheme("https");@b@ }@b@ this.tomcat.getService().addConnector(connector);@b@ }@b@ return connector;@b@ }@b@@b@ public void setClientAuth(Connector connector, String clientAuth) {@b@ ((AbstractHttp11JsseProtocol)connector.getProtocolHandler()).setClientAuth(clientAuth);@b@ }@b@@b@ public Connector getConnector(Protocol protocol, String address, int port) {@b@ return addConnector(protocol, address, port);@b@ }@b@@b@ public void setBaseDir(String baseDir) {@b@ this.tomcat.setBaseDir(baseDir);@b@ }@b@@b@ public void setDefaultHost(String defaultHostName) {@b@ this.tomcat.setHostname(defaultHostName);@b@ this.tomcat.getEngine().setDefaultHost(defaultHostName);@b@ }@b@@b@ public void setDefaultRealm(Realm realm) {@b@ this.tomcat.setDefaultRealm(realm);@b@ }@b@@b@ public Tomcat getTomcat()@b@ {@b@ return this.tomcat;@b@ }@b@@b@ public static enum Protocol@b@ {@b@ HTTP_11, HTTPS_11, HTTP_11_NIO, HTTPS_11_NIO, HTTP_11_APR, HTTPS_11_APR, MEMORY, AJP;@b@@b@ private String protocolName;@b@ private String protocolClass;@b@@b@ public String getProtocolName()@b@ {@b@ return this.protocolName;@b@ }@b@@b@ public String getProtocolClass() {@b@ return this.protocolClass;@b@ }@b@ }@b@}