KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > JamesTask


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

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 /**
32  * An attempt at a task which can launch James from Ant, and shut it down again.
33  * Doesn't really work, yet.
34  */

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 JavaDoc m_action;
42
43     public void setAction( String JavaDoc action )
44     {
45         m_action = action;
46     }
47
48     /**
49      * Set the classpath to be used for this compilation.
50      */

51     public void setClasspath(Path s) {
52         createClasspath().append(s);
53     }
54
55     /**
56      * Creates a nested classpath element
57      */

58     public Path createClasspath() {
59         return cmdl.createClasspath(project).createPath();
60     }
61
62     /**
63      * Adds a reference to a CLASSPATH defined elsewhere.
64      */

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( "log-destination", "." );
99
// m_parameters.setParameter( "log-priority", "DEBUG" );
100
// m_parameters.setParameter( "application-name", "james" );
101
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 // final Thread thread = new Thread( this, "Phoenix" );
113
// thread.start();
114
}
115         catch( final Throwable JavaDoc throwable )
116         {
117             System.out.println( "Exception in initiliaze()" );
118             throw new BuildException( throwable );
119         }
120
121         try
122         {
123             ClassLoader JavaDoc ctxLoader = Thread.currentThread().getContextClassLoader();
124             AntClassLoader loader = new AntClassLoader(ctxLoader, project, cmdl.getClasspath(), true);
125             loader.setIsolated(false);
126             loader.setThreadContextLoader();
127             // nothing
128
// m_embeddor.execute();
129
// Launch the startup thread.
130
Thread JavaDoc startThread = new StartupThread();
131             startThread.start();
132
133             // Hack to make sure that the embeddor has actually started.
134
// Need to make a change to Phoenix, so that we can wait til it's running.
135
// Yeild processor.
136
Thread.sleep( 1000 );
137             // m_embeddor will now be in use until applications are deployed.
138
synchronized ( m_embeddor ) {
139                 System.out.println( "got synch at: " + System.currentTimeMillis() );
140             }
141         }
142         catch( final Throwable JavaDoc throwable )
143         {
144             System.out.println( "Exception in execute()" );
145             throw new BuildException( throwable );
146         }
147
148     }
149
150     private class StartupThread extends Thread JavaDoc
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 JavaDoc 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 JavaDoc throwable )
193         {
194             System.out.println( "Exception in dispose()" );
195             throw new BuildException( throwable );
196         }
197     }
198 }
199
Popular Tags