tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alonso Dominguez <alonso.do...@gmail.com>
Subject Launching an embedded Tomcat from Maven 1.x
Date Wed, 20 Apr 2005 15:04:31 GMT
Hi there!

I'm working to embed a Tomcat server as a servlet container inside an
Avalon Framework with a Loom engine. I looked for information how to
launch the Catalina service from Java and finally I wrote the next
file:

package org.jlabase.framework.tomcat.startup;

import java.io.File;
import java.net.InetAddress;
import org.apache.catalina.*;
import org.apache.catalina.connector.*;
import org.apache.catalina.realm.*;
import org.apache.catalina.startup.*;
import org.apache.tomcat.util.*;
import org.jlabase.framework.tomcat.*;

/**
 * @author <a href="mailto:alonso.domin@gmail.com">A. Alonso Dominguez</a>
 * @version 1.0
 */
public class TomcatImpl implements Tomcat {
	private String path = null;
	private Embedded catalina = null;
	private Host host = null;
	private Context rootContext;
	private int port = 8080;
	
	public String getPath() {
		return path;
	}
	public void setPath(String value) {
		path = value;
	}
	
	public int getPort() {
		return port;
	}
	public void setPort(int value) {
		port = value;
	}
	
	public void start() throws Exception {
		Engine engine = null;
		
		// Create an embedded server
		catalina = new Embedded();
		catalina.setCatalinaHome(getPath());
		// Set the MemoryRealm
		MemoryRealm mr = new MemoryRealm();
		catalina.setRealm(mr);
		// Create an engine
		engine = catalina.createEngine();
		engine.setDefaultHost("localhost");
		
		// Create a default virtual host
		host = catalina.createHost("localhost", getPath() + "/webapps");
		engine.addChild(host);
		
		// Create the ROOT context
		rootContext = catalina.createContext("", getPath() + "/webapps/ROOT");
		rootContext.setReloadable(false);
		rootContext.addWelcomeFile("index.jsp");
		host.addChild(rootContext);
		
		// Create the Manager context
		Context managerCtx = catalina.createContext("/manager", getPath() +
"/webapps/manager");
		managerCtx.setPrivileged(true);
		host.addChild(managerCtx);
		
		// Assemble the container hierarchy
		catalina.addEngine(engine);
		
		// TODO Repair the Connector bug
		String addr = null;
		Connector connector = null;
		InetAddress address = null;
		try {
			connector = new Connector();
			connector.setSecure(false);
			address = InetAddress.getLocalHost();
			if(address != null) {
				IntrospectionUtils.setProperty(connector, "address", address.toString());
			}
			IntrospectionUtils.setProperty(connector, "port", new
Integer(getPort()).toString());
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		connector.setEnableLookups(false);
		catalina.addConnector(connector);
		catalina.start();   // Starts the embedded server
	}
	
	public void stop() throws Exception {
		catalina.stop();
	}
	
	public static void main(String args[]) {
		System.out.println("Creating server instance...");
		TomcatImpl tomcat = new TomcatImpl();
		tomcat.setPath(
			new File(System.getProperty("jlbframework.tomcat.home", 
				System.getProperty("basedir", "."))).getAbsolutePath()
		);
		
		try {
			System.out.println("Using CATALINA_HOME = " + tomcat.getPath());
			System.out.println("Starting server on port " + tomcat.getPort());
			tomcat.start();
			//tomcat.catalina.setAwait(true);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}

My intention is to configure the Catalina service from this class and
use a simple Ant-like script in Maven to launch the main method of
this class. So, my next step was write the maven.xml file, this is:

<project xmlns:ant="jelly:ant" xmlns:j="jelly:core"
xmlns:u="jelly:util" default="loom:sar">
  
  <goal name="jlbframework:tomcat-init">
    <ant:path id="tomcat.classpath">
      <j:forEach var="artifact" items="${pom.artifacts}">
        <j:set var="dependency" value="${artifact.dependency}" />
        <j:if test="${dependency.getProperty('sar.bundle')=='true'}">
          <ant:fileset
dir="${maven.repo.local}/${dependency.artifactDirectory}/jars"
prefix="lib">
            <ant:echo>Adding artifact: ${dependency.artifact} to the
Tomcat classpath.</ant:echo>
            <ant:include name="${dependency.artifact}" />
          </ant:fileset>
        </j:if>
      </j:forEach>
      <ant:pathelement path="${jlbframework.tomcat.home}/conf" />
    </ant:path>  
  </goal>
  
  <goal name="jlbframework:tomcat-start"
prereqs="jar:jar,jlbframework:tomcat-init">
    <ant:echo>Starting Tomcat in stand-alone mode...</ant:echo>
    <ant:java jar="${maven.build.dir}/${maven.final.name}.jar"
fork="true" maxmemory="15M" failonerror="true"
      classpathref="tomcat.classpath">
      <ant:sysproperty key="basedir" value="${basedir}" />
      <ant:sysproperty key="jlbframework.home" value="${jlbframework.home}" />
      <ant:sysproperty key="jlbframework.tomcat.home"
value="${jlbframework.tomcat.home}" />
    </ant:java>
  </goal>
  
</project>

And this are the contents of the project.properties file:

jlbframework.home=${basedir}/../..
jlbframework.tomcat.home=${basedir}

maven.xdoc.date=left
maven.xdoc.version=${pom.currentVersion}

maven.eclipse.resources.addtoclasspath=true

maven.jar.mainclass=org.jlabase.framework.tomcat.startup.TomcatImpl

Well, after all I tried to launch tomcat using the goal
'jlbframework:tomcat-start' and this what I get:

...
jar:jar:

jlbframework:tomcat-init:
    [echo] Adding artifact: commons-el-1.0.jar to the Tomcat classpath.
    [echo] Adding artifact: commons-logging-1.0.4.jar to the Tomcat classpath.
    [echo] Adding artifact: commons-modeler-1.1M1.jar to the Tomcat classpath.
    [echo] Adding artifact: catalina-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: tomcat-util-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: servlet-api-5.0.18.jar to the Tomcat classpath.
    [echo] Adding artifact: servlets-default-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: jsp-api-5.0.18.jar to the Tomcat classpath.
    [echo] Adding artifact: catalina-optional-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: jasper-compiler-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: jasper-compiler-jdt-5.5.9.jar to the
Tomcat classpath.
    [echo] Adding artifact: jasper-runtime-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: naming-factory-5.0.18.jar to the Tomcat classpath.
    [echo] Adding artifact: naming-resources-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: tomcat-coyote-5.5.9.jar to the Tomcat classpath.
    [echo] Adding artifact: tomcat-http-5.5.9.jar to the Tomcat classpath.

jlbframework:tomcat-start:
    [echo] Starting Tomcat in stand-alone mode...
    [java] Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/catalina/Realm

BUILD FAILED
File...... /home/alonso/projects/jlabase/jlabase-framework/modules/jlbframework-tomcat-module/maven.xml
Element... ant:java
Line...... 22
Column.... 39
Java returned: 1
Total time: 25 seconds
Finished at: Wed Apr 20 14:50:28 CEST 2005

So, finally Java doesn't founds the Tomcat API. Can anyone help me with this?

Regards,
Alonso

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message