Dear Wiki user, You have subscribed to a wiki page or wiki category on "James Wiki" for change notification.
The following page has been changed by KalpeshSutaria: http://wiki.apache.org/james/Embedded The comment on the change is: Added the Instructions for Running JAMES in Tomcat ------------------------------------------------------------------------------ * Run JBoss + = Instructions for Running JAMES in Tomcat = + + * Download and unzip the Apache JAMES from ["http://james.apache.org/download.cgi"]. As of this date version james-2.2.0;[http://apache.secsup.org/dist/james/james-2.2.0.tar.gz] is available. + + * The PHOENIX_HOME (or JAMES_HOME if you will) should point to the unzipped james folder. It contains the necessary jar files and the config.xml file necessary for the successful invocation of Apache JAMES from Tomcat. + + * The phoenix-loader.jar is designed to startup Apache JAMES from the command line and has to be tailored to work with Tomcat. The entry point is the org.apache.avalon.phoenix.launcher.Main. Here is a working version of Main with functions that were added or modified + {{{ + public final class Main + { + private static final String MAIN_CLASS = + "org.apache.avalon.phoenix.frontends.CLIMain"; + /* + * Note: This is changed from phoenix-loader.jar to phoenix-loader-modified.jar + * for this example + */ + private static final String LOADER_JAR = "phoenix-loader-modified.jar"; + private static Object c_frontend; + + /* Comment out the System.exit statement. We work with Tomcat not alone anymore */ + public static final void main( final String[] args ) + throws Exception + { + final int exitCode = + startup( args, new HashMap(), false ); + //System.exit( exitCode ); + } + + public static ClassLoader phoenixHomeClassLoader() + { + File[] libjars = (new File(System.getProperty( "phoenix.home" )+"/lib")).listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return (name.endsWith(".jar")); + }}); + File[] binjars = (new File(System.getProperty( "phoenix.home" )+"/bin")).listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return (name.endsWith(".jar")); + }}); + File[] binlibjars = (new File(System.getProperty( "phoenix.home" )+"/bin/lib")).listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return (name.endsWith(".jar")); + }}); + File[] catalinajars = (new File(System.getProperty( "catalina.home" )+"/shared/lib")).listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return (name.endsWith(".jar")); + }}); + List jarURLs = new ArrayList(); + for (int i=0; i<libjars.length; i++) + { + try + { + jarURLs.add(libjars[i].toURL()); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + for (int i=0; i<binjars.length; i++) + { + try + { + if (!binjars[i].getName().equalsIgnoreCase("phoenix-loader.jar")) + jarURLs.add(binjars[i].toURL()); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + for (int i=0; i<binlibjars.length; i++) + { + try + { + jarURLs.add(binlibjars[i].toURL()); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + for (int i=0; i<catalinajars.length; i++) + { + try + { + jarURLs.add(catalinajars[i].toURL()); + } + catch (Exception e) + { + e.printStackTrace(); + } + } URL[] returnVal = (URL[])jarURLs.toArray( new URL[ jarURLs.size() ] ); + return new URLClassLoader( returnVal, Main.class.getClassLoader() ); + } + + protected static final int startup( final String[] args, + final Map data, + final boolean blocking ) + throws Exception + { + int exitCode; + try + { + //setup new Policy manager + Policy.setPolicy( new FreeNEasyPolicy() ); + + //Create engine ClassLoader + final URL[] urls = getEngineClassPath(); + final URLClassLoader classLoader = new URLClassLoader( urls ); + + /* + * We build our own classloader which searches for the PHOENIX_HOME directory + * for jars + */ + ClassLoader commonLibClassLoader = phoenixHomeClassLoader(); + data.put( "common.classloader", commonLibClassLoader ); + data.put( "container.classloader", classLoader ); + data.put( "phoenix.home", new File( findPhoenixHome() ) ); + + + //Setup context classloader + //Thread.currentThread().setContextClassLoader( classLoader ); + + //Create main launcher + final Class clazz = commonLibClassLoader.loadClass( MAIN_CLASS ); + final Class[] paramTypes = + new Class[]{args.getClass(), Map.class, Boolean.TYPE}; + final Method method = clazz.getMethod( "main", paramTypes ); + c_frontend = clazz.newInstance(); + + //kick the tires and light the fires.... + final Integer integer = (Integer)method.invoke( + c_frontend, new Object[]{args, data, new Boolean( blocking )} ); + exitCode = integer.intValue(); + } + catch( final Exception e ) + { + e.printStackTrace(); + exitCode = 1; + } + return exitCode; + } + /* + * The other methods namely shutdown, getEngineClassPath, findEngineLibDir, + * findPhoenixHome, and findLoaderDir are unmodified and hence not shown here. + */ + } + }}} + + * The attachment:phoenix-loader-modified.jar contains a compiled version of the Main class. + + * Also place the mailet_1_0.jar obtained by unzipping james.sar in the CATALINA_HOME/shared/lib folder. + + * Create a simple servlet to invoke JAMES as follows + {{{ + public class launchJAMES extends HttpServlet { + + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/plain"); + PrintWriter writer = response.getWriter(); + try + { + System.setProperty( "phoenix.home", "C:\\Documents and Settings\\Owner\\Desktop\\\\james-2.2.0" ); + System.setProperty( "ve.home", "C:\\VE4_0" ); + System.setProperty( "catalina.home", "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5"); + URLClassLoader childClassLoader = + new URLClassLoader( + new URL[] { + new File("C:\\Documents and Settings\\Owner\\Desktop\\james-2.2.0\\bin\\phoenix-loader-modified.jar").toURL() + }); + final Class mainClass = childClassLoader.loadClass( "org.apache.avalon.phoenix.launcher.Main" ); + final Class[] paramTypes = + new Class[]{args.getClass()}; + final Method method = mainClass.getMethod( "main", paramTypes ); + Object main_instance = mainClass.newInstance(); + method.invoke(main_instance, new Object[]{args} ); + } + catch (Exception e) + { + e.printStackTrace(); + } + writer.flush(); + writer.close(); + } + } + }}} + * Modify the web.xml appropriately to add the servlet name. Start Tomcat and deploy the webapp. + * Open a browser and point to the servlet to invoke JAMES from within Tomcat + * To shutdown use the JAMES Remote Administration Tool to shutdown JAMES as well as Tomcat. + === Hints === + * The ClassNotFoundException is the most common exception. This is a ClassLoader issue and using the JAVA_VM_ARGS=-verbose flag when starting Tomcat drops a few hints at possible problem areas. + * The mail-*.*.jar used by Tomcat and that by JAMES have to be of the same version. +