/*******************************************************************************
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.apache.kato.anttasks.tck;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Commandline.Argument;
import org.apache.tools.ant.types.Environment.Variable;
/**
* Convinience wrapper to drive the TCK setup class
* Takes the provided config and extracts from the setup classes
* any further configuration. These are added after those provided
* by the caller
*
*/
public class TCKSetupTask extends Java {
private boolean validateOnly=false;
private static File home=new File(System.getProperty("user.home"));
private String triggerclass=null;
private File configdir=null;
private List envvars=new LinkedList();
private List scenerios=new LinkedList();
public TCKSetupTask() {
super.setFork(true);
super.setFailonerror(false);
super.setClassname("org.apache.kato.tck.harness.scenario.ScenarioLauncher");
}
public void setFork(boolean s) {
throw new BuildException("cannot set fork on setup task");
}
public void setFailonerror(boolean s) {
throw new BuildException("cannot set fail on error on setup task");
}
public void execute() throws BuildException {
if(validateOnly) {
validate();
return;
}
// add the dump trigger class if present
if(triggerclass!=null) {
Argument arg=createArg();
arg.setValue("-dumptrigger");
arg=createArg();
arg.setValue(triggerclass);
}
// config for what we want to execute
configureScenerios();
// set home dir given
if(configdir!=null) {
Argument arg=createArg();
arg.setValue("-configdir");
arg=createArg();
arg.setFile(configdir);
}
else {
configdir=new File(home,".tck");
}
// add a parameter to ask for the env setup etc
Argument arg=createArg();
arg.setValue("-mode");
arg=createArg();
arg.setValue("config");
super.execute();
// load in required files..
try {
applyEnvVarSettings();
applyJVMArgSettings();
} catch (IOException e) {
throw new BuildException(e);
}
// change param to run
arg.setValue("run");
super.execute();
}
/**
* @throws IOException
*
*/
private void applyEnvVarSettings() throws IOException {
File envfile=new File(configdir,"tck.envfile.txt");
if(envfile.exists()==false) return;
FileInputStream fis=new FileInputStream(envfile);
Properties p=new Properties();
p.load(fis);
fis.close();
Enumeration e=p.elements();
while(e.hasMoreElements()) {
Object key=e.nextElement();
Object value=p.get(key);
Variable envvar=new Variable();
envvar.setKey(key.toString());
if(value!=null) {
envvar.setValue(value.toString());
}
else {
envvar.setValue("");
}
addEnv(envvar);
}
}
/**
* @throws IOException
*
*/
private void applyJVMArgSettings() throws IOException {
File vmfile=new File(configdir,"tck.jvmfile.txt");
if(vmfile.exists()==false) return;
FileInputStream fis=new FileInputStream(vmfile);
DataInputStream dis=new DataInputStream(fis);
while(true) {
String line=dis.readLine();
if(line==null) break;
Argument a=createJvmarg();
a.setValue(line);
}
dis.close();
}
/**
/**
*
*/
private void configureScenerios() {
// add scenerios defs to arguments
if(scenerios.isEmpty()) {
Argument arg=createArg();
arg.setValue("-scenario");
arg=createArg();
arg.setValue("142");
}
else {
Iterator i=scenerios.iterator();
while(i.hasNext()) {
Scenario scenerio=(Scenario) i.next();
if(scenerio.classname==null && scenerio.scenerio==null) {
throw new BuildException("scenario tag must have a name or classname attribute");
}
if(scenerio.scenerio==null) {
Argument arg=createArg();
arg.setValue("-class");
arg=createArg();
arg.setValue(scenerio.classname);
} else {
Argument arg=createArg();
arg.setValue("-scenario");
arg=createArg();
arg.setValue(scenerio.scenerio);
}
}
}
}
/**
* Lists out validation details
*/
private void validate() {
// report execution environment
System.out.println(getCommandLine().toString());
Iterator vars=envvars.iterator();
while(vars.hasNext()) {
Variable v=(Variable) vars.next();
System.out.println("envvar key="+v.getKey()+" value="+v.getValue());
}
}
public void setClassname(String s) throws BuildException {
throw new BuildException("cannot set classname on setup task");
}
public void setValidateonly(boolean value) {
validateOnly=value;
}
/**
* @return
*/
public boolean isValidateOnly() {
return validateOnly;
}
public void addEnv(Variable var) {
super.addEnv(var);
envvars.add(var); // keep a copy for us
}
public void addConfiguredScenario(Scenario s) {
scenerios.add(s);
}
public void setConfigdir(File dir) {
configdir=dir;
}
public void setDumptrigger(String className) {
this.triggerclass=className;
}
}