|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractJavaContainer.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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;
|
|
| 23 |
import java.io.FileNotFoundException;
|
|
| 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 239096 2004-12-10 13:50:22Z felipeal $
|
|
| 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 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 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 | 0 |
public final void setOutput(File theOutput) |
| 64 |
{
|
|
| 65 | 0 |
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 | 0 |
public final void setAppend(boolean isAppend) |
| 75 |
{
|
|
| 76 | 0 |
this.append = isAppend;
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* Sets the arguments for JVM.
|
|
| 82 |
*
|
|
| 83 |
* @param theJVMArgs The arguments
|
|
| 84 |
*/
|
|
| 85 | 0 |
public final void setJVMArgs(String theJVMArgs) |
| 86 |
{
|
|
| 87 | 0 |
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 | 0 |
protected final Java createJavaForShutDown()
|
| 99 |
{
|
|
| 100 | 0 |
Java java = (Java) createAntTask("java");
|
| 101 | 0 |
java.setFork(true);
|
| 102 |
|
|
| 103 |
// Add extra container classpath entries specified by the user.
|
|
| 104 | 0 |
addExtraClasspath(java); |
| 105 |
|
|
| 106 | 0 |
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 | 0 |
protected final Java createJavaForStartUp()
|
| 116 |
{
|
|
| 117 | 0 |
Java java = (Java) createAntTask("java");
|
| 118 | 0 |
java.setFork(true);
|
| 119 | 0 |
java.setOutput(this.output);
|
| 120 | 0 |
java.setAppend(this.append);
|
| 121 |
|
|
| 122 |
// pass arguments to the JVM
|
|
| 123 | 0 |
if (this.jvmArgs != null) |
| 124 |
{
|
|
| 125 | 0 |
getLog().trace( |
| 126 |
"Passing arguments to the container JVM: " + this.jvmArgs); |
|
| 127 | 0 |
java.createJvmarg().setLine(this.jvmArgs);
|
| 128 |
} |
|
| 129 |
|
|
| 130 |
// Add extra container classpath entries specified by the user.
|
|
| 131 | 0 |
addExtraClasspath(java); |
| 132 |
|
|
| 133 |
// Add Cactus properties for the server side
|
|
| 134 | 0 |
if (getSystemProperties() != null) |
| 135 |
{
|
|
| 136 | 0 |
for (int i = 0; i < getSystemProperties().length; i++) |
| 137 |
{
|
|
| 138 | 0 |
java.addSysproperty( |
| 139 |
createSysProperty(getSystemProperties()[i].getKey(), |
|
| 140 |
getSystemProperties()[i].getValue())); |
|
| 141 |
} |
|
| 142 |
} |
|
| 143 |
|
|
| 144 | 0 |
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 | 0 |
private void addExtraClasspath(Java theJavaCommand) |
| 153 |
{
|
|
| 154 | 0 |
Path classpath = theJavaCommand.createClasspath(); |
| 155 | 0 |
if (getContainerClasspath() != null) |
| 156 |
{
|
|
| 157 | 0 |
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 | 0 |
protected final Variable createSysProperty(String theKey, File theFile)
|
| 170 |
{
|
|
| 171 | 0 |
Variable var = new Variable();
|
| 172 | 0 |
var.setKey(theKey); |
| 173 | 0 |
var.setFile(theFile); |
| 174 | 0 |
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 | 0 |
protected final Variable createSysProperty(String theKey, Path thePath)
|
| 186 |
{
|
|
| 187 | 0 |
Variable var = new Variable();
|
| 188 | 0 |
var.setKey(theKey); |
| 189 | 0 |
var.setPath(thePath); |
| 190 | 0 |
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 | 0 |
protected final Variable createSysProperty(String theKey, String theValue)
|
| 202 |
{
|
|
| 203 | 0 |
Variable var = new Variable();
|
| 204 | 0 |
var.setKey(theKey); |
| 205 | 0 |
var.setValue(theValue); |
| 206 | 0 |
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 | 0 |
protected final File getToolsJar() throws FileNotFoundException |
| 218 |
{
|
|
| 219 | 0 |
String javaHome = System.getProperty("java.home");
|
| 220 | 0 |
File toolsJar = new File(javaHome, "../lib/tools.jar"); |
| 221 | 0 |
if (!toolsJar.isFile())
|
| 222 |
{
|
|
| 223 | 0 |
throw new FileNotFoundException(toolsJar.getAbsolutePath()); |
| 224 |
} |
|
| 225 | 0 |
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 | 0 |
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 | 0 |
if (!isOSX())
|
| 239 |
{
|
|
| 240 | 0 |
try
|
| 241 |
{
|
|
| 242 | 0 |
theClasspath.createPathElement().setLocation(getToolsJar()); |
| 243 |
} |
|
| 244 |
catch (FileNotFoundException fnfe)
|
|
| 245 |
{
|
|
| 246 | 0 |
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 | 0 |
protected final String getJVMArgs()
|
| 256 |
{
|
|
| 257 | 0 |
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 | 0 |
private boolean isOSX() |
| 270 |
{
|
|
| 271 | 0 |
return (System.getProperty("mrj.version") != null); |
| 272 |
} |
|
| 273 |
|
|
| 274 |
} |
|
| 275 |
|
|
||||||||||