1 17 18 package org.apache.james.test; 19 20 import org.apache.tools.ant.BuildException; 21 import org.apache.tools.ant.AntClassLoader; 22 import org.apache.tools.ant.types.Path; 23 import org.apache.tools.ant.types.Reference; 24 import org.apache.tools.ant.types.CommandlineJava; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.avalon.framework.parameters.Parameterizable; 27 import org.apache.avalon.framework.ExceptionUtil; 28 import org.apache.avalon.framework.CascadingRuntimeException; 29 import org.apache.avalon.phoenix.components.embeddor.SingleAppEmbeddor; 30 31 35 public final class JamesTask 36 extends org.apache.tools.ant.Task 37 { 38 private CommandlineJava cmdl = new CommandlineJava(); 39 private Parameters m_parameters; 40 private static SingleAppEmbeddor m_embeddor; 41 private String m_action; 42 43 public void setAction( String action ) 44 { 45 m_action = action; 46 } 47 48 51 public void setClasspath(Path s) { 52 createClasspath().append(s); 53 } 54 55 58 public Path createClasspath() { 59 return cmdl.createClasspath(project).createPath(); 60 } 61 62 65 public void setClasspathRef(Reference r) { 66 createClasspath().setRefid(r); 67 } 68 69 70 public void execute() throws BuildException 71 { 72 if ( m_action.equalsIgnoreCase( "start" ) ) { 73 startup(); 74 } 75 else if ( m_action.equalsIgnoreCase( "stop" ) ) { 76 shutdown(); 77 } 78 else if ( m_action.equalsIgnoreCase( "start-stop" ) ) { 79 startup(); 80 shutdown(); 81 } 82 else if ( m_action.equalsIgnoreCase( "restart" ) ) { 83 optionalShutdown(); 84 startup(); 85 } 86 else { 87 throw new BuildException( "Invalid action: '" + m_action + "'" ); 88 } 89 } 90 91 private void startup() throws BuildException 92 { 93 if ( m_embeddor != null ) { 94 throw new BuildException( "Already started" ); 95 } 96 97 m_parameters = new Parameters(); 98 m_parameters.setParameter( "application-location", "dist/apps/james.sar"); 102 103 try 104 { 105 m_embeddor = new SingleAppEmbeddor(); 106 if( m_embeddor instanceof Parameterizable ) 107 { 108 ( (Parameterizable)m_embeddor ).parameterize( m_parameters ); 109 } 110 m_embeddor.initialize(); 111 112 } 115 catch( final Throwable throwable ) 116 { 117 System.out.println( "Exception in initiliaze()" ); 118 throw new BuildException( throwable ); 119 } 120 121 try 122 { 123 ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader(); 124 AntClassLoader loader = new AntClassLoader(ctxLoader, project, cmdl.getClasspath(), true); 125 loader.setIsolated(false); 126 loader.setThreadContextLoader(); 127 Thread startThread = new StartupThread(); 131 startThread.start(); 132 133 Thread.sleep( 1000 ); 137 synchronized ( m_embeddor ) { 139 System.out.println( "got synch at: " + System.currentTimeMillis() ); 140 } 141 } 142 catch( final Throwable throwable ) 143 { 144 System.out.println( "Exception in execute()" ); 145 throw new BuildException( throwable ); 146 } 147 148 } 149 150 private class StartupThread extends Thread 151 { 152 StartupThread() 153 { 154 super( "JamesStartup" ); 155 this.setDaemon( true ); 156 } 157 158 public void run() 159 { 160 try { 161 m_embeddor.execute(); 162 } 163 catch ( Exception exc ) { 164 exc.printStackTrace(); 165 throw new CascadingRuntimeException( "Exception in execute()", exc ); 166 } 167 } 168 169 } 170 171 private void optionalShutdown() throws BuildException 172 { 173 if ( m_embeddor != null ) { 174 shutdown(); 175 } 176 } 177 178 private void shutdown() throws BuildException 179 { 180 System.out.println( "In shutdown()" ); 181 if ( m_embeddor == null ) { 182 throw new BuildException( "Not running." ); 183 } 184 185 try 186 { 187 m_embeddor.dispose(); 188 System.out.println( "Called dispose()" ); 189 m_embeddor = null; 190 m_parameters = null; 191 } 192 catch( final Throwable throwable ) 193 { 194 System.out.println( "Exception in dispose()" ); 195 throw new BuildException( throwable ); 196 } 197 } 198 } 199 | Popular Tags |