KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > AbstractJavaContainer


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

20 package org.apache.cactus.integration.ant.container;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24
25 import org.apache.tools.ant.taskdefs.Java;
26 import org.apache.tools.ant.types.Path;
27 import org.apache.tools.ant.types.Environment.Variable;
28
29 /**
30  * Abstract base class for containers that perform the starting and stopping
31  * of the server by executing Java classes in a forked JVM.
32  *
33  * @version $Id: AbstractJavaContainer.java,v 1.14 2004/12/10 13:50:22 felipeal Exp $
34  */

35 public abstract class AbstractJavaContainer extends AbstractContainer
36 {
37     // Instance Variables ------------------------------------------------------
38

39     /**
40      * The file to which output of the container should be written.
41      */

42     private File JavaDoc output;
43
44     /**
45      * Whether output of the container should be appended to an existing file,
46      * or the existing file should be truncated.
47      */

48     private boolean append;
49
50
51     /**
52      * The arguments for JVM.
53      */

54     private String JavaDoc jvmArgs;
55
56     // Public Methods ----------------------------------------------------------
57

58     /**
59      * Sets the file to which output of the container should be written.
60      *
61      * @param theOutput The output file to set
62      */

63     public final void setOutput(File JavaDoc theOutput)
64     {
65         this.output = theOutput;
66     }
67
68     /**
69      * Sets whether output of the container should be appended to an existing
70      * file, or the existing file should be truncated.
71      *
72      * @param isAppend Whether output should be appended
73      */

74     public final void setAppend(boolean isAppend)
75     {
76         this.append = isAppend;
77     }
78
79
80     /**
81      * Sets the arguments for JVM.
82      *
83      * @param theJVMArgs The arguments
84      */

85     public final void setJVMArgs(String JavaDoc theJVMArgs)
86     {
87         this.jvmArgs = theJVMArgs;
88     }
89
90     // Protected Methods -------------------------------------------------------
91

92     /**
93      * Creates a preinitialized instance of the Ant Java task to be used for
94      * shutting down the container.
95      *
96      * @return The created task instance
97      */

98     protected final Java createJavaForShutDown()
99     {
100         Java java = (Java) createAntTask("java");
101         java.setFork(true);
102
103         // Add extra container classpath entries specified by the user.
104
addExtraClasspath(java);
105         
106         return java;
107     }
108
109     /**
110      * Creates a preinitialized instance of the Ant Java task to be used for
111      * starting down the container.
112      *
113      * @return The created task instance
114      */

115     protected final Java createJavaForStartUp()
116     {
117         Java java = (Java) createAntTask("java");
118         java.setFork(true);
119         java.setOutput(this.output);
120         java.setAppend(this.append);
121
122         // pass arguments to the JVM
123
if (this.jvmArgs != null)
124         {
125             getLog().trace(
126                 "Passing arguments to the container JVM: " + this.jvmArgs);
127             java.createJvmarg().setLine(this.jvmArgs);
128         }
129
130         // Add extra container classpath entries specified by the user.
131
addExtraClasspath(java);
132        
133         // Add Cactus properties for the server side
134
if (getSystemProperties() != null)
135         {
136             for (int i = 0; i < getSystemProperties().length; i++)
137             {
138                 java.addSysproperty(
139                     createSysProperty(getSystemProperties()[i].getKey(),
140                         getSystemProperties()[i].getValue()));
141             }
142         }
143         
144         return java;
145     }
146
147     /**
148      * Add extra container classpath entries specified by the user.
149      *
150      * @param theJavaCommand the java command used to start/stop the container
151      */

152     private void addExtraClasspath(Java theJavaCommand)
153     {
154         Path classpath = theJavaCommand.createClasspath();
155         if (getContainerClasspath() != null)
156         {
157             classpath.addExisting(getContainerClasspath());
158         }
159     }
160     
161     /**
162      * Convenience method to create an Ant environment variable that points to
163      * a file.
164      *
165      * @param theKey The key or name of the variable
166      * @param theFile The file the variable should point to
167      * @return The created environment variable
168      */

169     protected final Variable createSysProperty(String JavaDoc theKey, File JavaDoc theFile)
170     {
171         Variable var = new Variable();
172         var.setKey(theKey);
173         var.setFile(theFile);
174         return var;
175     }
176
177     /**
178      * Convenience method to create an Ant environment variable that contains
179      * a path.
180      *
181      * @param theKey The key or name of the variable
182      * @param thePath The path
183      * @return The created environment variable
184      */

185     protected final Variable createSysProperty(String JavaDoc theKey, Path thePath)
186     {
187         Variable var = new Variable();
188         var.setKey(theKey);
189         var.setPath(thePath);
190         return var;
191     }
192
193     /**
194      * Convenience method to create an Ant environment variable that contains a
195      * string.
196      *
197      * @param theKey The key or name of the variable
198      * @param theValue The value
199      * @return The created environment variable
200      */

201     protected final Variable createSysProperty(String JavaDoc theKey, String JavaDoc theValue)
202     {
203         Variable var = new Variable();
204         var.setKey(theKey);
205         var.setValue(theValue);
206         return var;
207     }
208
209     /**
210      * Returns the file containing the JDK tools (such as the compiler). This
211      * method must not be called on Mac OSX as there is no tools.jar file on
212      * that platform (everything is included in classes.jar).
213      *
214      * @return The tools.jar file
215      * @throws FileNotFoundException If the tools.jar file could not be found
216      */

217     protected final File JavaDoc getToolsJar() throws FileNotFoundException JavaDoc
218     {
219         String JavaDoc javaHome = System.getProperty("java.home");
220         File JavaDoc toolsJar = new File JavaDoc(javaHome, "../lib/tools.jar");
221         if (!toolsJar.isFile())
222         {
223             throw new FileNotFoundException JavaDoc(toolsJar.getAbsolutePath());
224         }
225         return toolsJar;
226     }
227  
228     /**
229      * Adds the tools.jar to the classpath, except for Mac OSX as it is not
230      * needed.
231      *
232      * @param theClasspath the classpath object to which to add the tools.jar
233      */

234     protected final void addToolsJarToClasspath(Path theClasspath)
235     {
236         // On OSX, the tools.jar classes are included in the classes.jar so
237
// there is no need to include any tools.jar file to the cp.
238
if (!isOSX())
239            {
240             try
241             {
242                 theClasspath.createPathElement().setLocation(getToolsJar());
243             }
244             catch (FileNotFoundException JavaDoc fnfe)
245             {
246                 getLog().warn(
247                     "Couldn't find tools.jar (needed for JSP compilation)");
248             }
249         }
250     }
251
252     /**
253      * @return The arguments for JVM.
254      */

255     protected final String JavaDoc getJVMArgs()
256     {
257         return this.jvmArgs;
258     }
259
260     // Private Methods -------------------------------------------------------
261

262     /**
263      * Is the user running on a Macintosh OS X system? Heuristic derived from
264      * <a HREF="http://developer.apple.com/technotes/tn/tn2042.html#Section0_1">
265      * Apple Tech Note 2042</a>.
266      *
267      * @return true if the user's system is determined to be Mac OS X.
268      */

269     private boolean isOSX()
270     {
271         return (System.getProperty("mrj.version") != null);
272     }
273
274 }
275
Popular Tags