pier 01/07/18 19:45:33 Modified: webapp/java WarpConfigurationHandler.java WarpConnector.java Log: Added support for automatic host/application deployment. Revision Changes Path 1.7 +130 -15 jakarta-tomcat-connectors/webapp/java/WarpConfigurationHandler.java Index: WarpConfigurationHandler.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/webapp/java/WarpConfigurationHandler.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- WarpConfigurationHandler.java 2001/07/18 23:12:49 1.6 +++ WarpConfigurationHandler.java 2001/07/19 02:45:33 1.7 @@ -56,12 +56,16 @@ * ========================================================================= */ package org.apache.catalina.connector.warp; +import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.ServerSocket; -import java.net.Socket; +import java.net.URL; +import org.apache.catalina.Container; +import org.apache.catalina.Context; +import org.apache.catalina.Deployer; +import org.apache.catalina.Host; +import org.apache.catalina.core.StandardHost; + public class WarpConfigurationHandler { /* ==================================================================== */ @@ -82,19 +86,130 @@ packet.setType(Constants.TYPE_CONF_WELCOME); packet.writeUnsignedShort(Constants.VERS_MAJOR); packet.writeUnsignedShort(Constants.VERS_MINOR); - packet.writeInteger(-1); + packet.writeInteger(connection.getConnector().uniqueId); connection.send(packet); - - connection.recv(packet); - String appl=packet.readString(); - String host=packet.readString(); - int port=packet.readUnsignedShort(); - String path=packet.readString(); + + // Loop for configuration packets + while (true) { + connection.recv(packet); + + switch (packet.getType()) { + + case Constants.TYPE_CONF_DEPLOY: { + String appl=packet.readString(); + String host=packet.readString(); + int port=packet.readUnsignedShort(); + String path=packet.readString(); + Context context=null; + packet.reset(); + + if (Constants.DEBUG) + logger.log("Deploying web application \""+appl+"\" "+ + "under <http://"+host+":"+port+path+">"); + try { + context=deploy(connection,logger,appl,host,path); + } catch (Exception e) { + logger.log(e); + } + if (context==null) { + String msg="Error deploying web application \""+appl+ + "\" under <http://"+host+":"+port+path+">"; + logger.log(msg); + packet.setType(Constants.TYPE_ERROR); + packet.writeString(msg); + connection.send(packet); + } else { + int k=connection.getConnector().applicationId(context); + packet.setType(Constants.TYPE_CONF_APPLIC); + packet.writeInteger(k); + packet.writeString(context.getDocBase()); + connection.send(packet); + if (Constants.DEBUG) + logger.debug("Application \""+appl+"\" deployed "+ + "under <http://"+host+":"+port+path+ + "> with root="+context.getDocBase()+ + " ID="+k); + } + break; + } + + case Constants.TYPE_CONF_DONE: { + packet.reset(); + packet.setType(Constants.TYPE_CONF_PROCEED); + connection.send(packet); + return(true); + } + + default: { + logger.log("Invalid packet with type "+packet.getType()); + return(false); + } + } + } + } + + /** Deploy a web application */ + private Context deploy(WarpConnection connection, WarpLogger logger, + String applName, String hostName, String applPath) + throws IOException { + synchronized (connection.getConnector()) { + + Container container=connection.getConnector().getContainer(); + + Host host=(Host)container.findChild(hostName); + if (host==null) { + host=new StandardHost(); + host.setName(hostName); + host.setParent(container); + host.setAppBase(connection.getConnector().getAppBase()); + container.addChild(host); + if (Constants.DEBUG) + logger.debug("Created new host "+host.getName()); + } else if (Constants.DEBUG) { + logger.debug("Reusing instance of Host \""+host.getName()+"\""); + } + + // TODO: Set up mapping mechanism for performance - if (Constants.DEBUG) - logger.debug("Deploying application \""+appl+"\" under \"http://"+ - host+":"+port+path); + if (applPath.endsWith("/")) + applPath=applPath.substring(0,applPath.length()-1); + + Context appl=(Context)host.findChild(applPath); + + if (appl==null) { - return(true); + if (Constants.DEBUG) + logger.debug("No application for \""+applPath+"\""); + + Deployer deployer=(Deployer)host; + File file=new File(host.getAppBase()+File.separator+applName); + if (!file.isAbsolute()) { + file=new File(System.getProperty("catalina.home"), + host.getAppBase()+File.separator+applName); + } + + if (!file.exists()) { + logger.log("Cannot find \""+file.getPath()+"\" for appl. \""+ + applName); + return(null); + } + + String path=file.getCanonicalPath(); + URL url=new URL("file",null,path); + if (path.toLowerCase().endsWith(".war")) + url=new URL("jar:"+url.toString()+"!/"); + + if (Constants.DEBUG) + logger.debug("Application URL \""+url.toString()+"\""); + + deployer.install(applPath, url); + return(deployer.findDeployedApp(applPath)); + + } else { + if (Constants.DEBUG) + logger.debug("Found application for \""+appl.getName()+"\""); + return(appl); + } + } } } 1.15 +42 -1 jakarta-tomcat-connectors/webapp/java/WarpConnector.java Index: WarpConnector.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/webapp/java/WarpConnector.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- WarpConnector.java 2001/07/18 23:12:49 1.14 +++ WarpConnector.java 2001/07/19 02:45:33 1.15 @@ -60,9 +60,12 @@ import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; +import java.util.Random; +import java.util.Vector; import org.apache.catalina.Connector; import org.apache.catalina.Container; +import org.apache.catalina.Context; import org.apache.catalina.Lifecycle; import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleException; @@ -89,6 +92,10 @@ private ServerSocket server=null; /** Our <code>WarpLogger</code>. */ private WarpLogger logger=null; + /** Our list of deployed web applications. */ + private Vector applications=new Vector(); + /** The unique ID of this connector instance. */ + protected int uniqueId=-1; /* -------------------------------------------------------------------- */ /* Bean variables */ @@ -113,6 +120,8 @@ private int port=8008; /** The server socket backlog length. */ private int acceptCount=10; + /** The server appBase for hosts created via WARP. */ + private String appBase="webapps"; /* -------------------------------------------------------------------- */ /* Lifecycle variables */ @@ -132,7 +141,9 @@ public WarpConnector() { super(); this.logger=new WarpLogger(this); - if (Constants.DEBUG) logger.debug("Instance created"); + this.uniqueId=new Random().nextInt(); + if (Constants.DEBUG) + logger.debug("Instance created (ID="+this.uniqueId+")"); } /* ==================================================================== */ @@ -353,6 +364,24 @@ if (Constants.DEBUG) logger.debug("Setting acceptCount to "+count); } + /** + * Get the applications base directory for hosts created via WARP. + */ + public String getAppBase() { + return (this.appBase); + } + + + /** + * Set the applications base directory for hosts created via WARP. + * + * @param appBase The appbase property. + */ + public void setAppBase(String appbase) { + this.appBase = appBase; + + if (Constants.DEBUG) logger.debug("Setting appBase to "+appBase); + } /* ==================================================================== */ /* Lifecycle methods */ @@ -419,6 +448,18 @@ /* ==================================================================== */ /* Public methods */ /* ==================================================================== */ + + /** + * Return the application ID for a given <code>Context</code>. + */ + protected int applicationId(Context context) { + int id=this.applications.indexOf(context); + if (id==-1) { + this.applications.add(context); + id=this.applications.indexOf(context); + } + return(id); + } /** * Create (or allocate) and return a Request object suitable for