All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.greenpepper.server.domain.Runner Maven / Gradle / Ivy

There is a newer version: 4.2.4
Show newest version
package com.greenpepper.server.domain;

import com.greenpepper.report.XmlReport;
import com.greenpepper.server.GreenPepperServerException;
import com.greenpepper.server.rpc.xmlrpc.XmlRpcDataMarshaller;
import com.greenpepper.server.rpc.xmlrpc.client.XmlRpcClientExecutor;
import com.greenpepper.server.rpc.xmlrpc.client.XmlRpcClientExecutorFactory;
import com.greenpepper.util.*;
import com.greenpepper.util.cmdline.CommandLineBuilder;
import com.greenpepper.util.cmdline.CommandLineExecutor;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.*;
import java.io.File;
import java.io.IOException;
import java.util.*;

import static com.greenpepper.server.rpc.xmlrpc.XmlRpcDataMarshaller.*;
import static com.greenpepper.util.IOUtils.uniquePath;
import static org.apache.commons.lang3.StringUtils.defaultString;

/**
 * Runner Class.
 * Definition of a Runner.
 * 

* Copyright (c) 2006-2007 Pyxis technologies inc. All Rights Reserved. * * @author JCHUET * @version $Id: $Id */ @Entity @Table(name="RUNNER") @SuppressWarnings("serial") public class Runner extends AbstractVersionedEntity implements Comparable { private static final Logger LOG = LoggerFactory.getLogger(Runner.class); private static final String AGENT_HANDLER = "greenpepper-agent1"; private String name; private String cmdLineTemplate; private String mainClass; private EnvironmentType envType; private String serverName; private String serverPort; private Boolean secured; private SortedSet classpaths = new TreeSet(); /** *

newInstance.

* * @param name a {@link java.lang.String} object. * @return a {@link com.greenpepper.server.domain.Runner} object. */ public static Runner newInstance(String name) { Runner runner = new Runner(); runner.setName(name); return runner; } /** *

Getter for the field name.

* * @return a {@link java.lang.String} object. */ @Basic @Column(name = "NAME", unique = true, nullable = false, length=255) public String getName() { return name; } /** *

getEnvironmentType.

* * @return a {@link com.greenpepper.server.domain.EnvironmentType} object. */ @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} ) @JoinColumn(name="ENVIRONMENT_TYPE_ID") public EnvironmentType getEnvironmentType() { return envType; } /** *

Getter for the field serverName.

* * @return a {@link java.lang.String} object. */ @Basic @Column(name = "SERVER_NAME", nullable = true, length=255) public String getServerName() { return serverName; } /** *

Getter for the field serverPort.

* * @return a {@link java.lang.String} object. */ @Basic @Column(name = "SERVER_PORT", nullable = true, length=8) public String getServerPort() { return serverPort; } /** *

isSecured.

* * @return a boolean. */ @Basic @Column(name = "SECURED", nullable = true) public boolean isSecured() { return secured != null && secured; } /** *

Getter for the field cmdLineTemplate.

* * @return a {@link java.lang.String} object. */ @Basic @Column(name = "CMD_LINE_TEMPLATE", nullable = true, length=510) public String getCmdLineTemplate() { return cmdLineTemplate; } /** *

Getter for the field mainClass.

* * @return a {@link java.lang.String} object. */ @Basic @Column(name = "MAIN_CLASS", nullable = true, length=255) public String getMainClass() { return mainClass; } /** *

Getter for the field classpaths.

* * @return a {@link java.util.SortedSet} object. */ @CollectionOfElements @JoinTable( name="RUNNER_CLASSPATHS", joinColumns={@JoinColumn(name="RUNNER_ID")} ) @Column(name = "elt", nullable = true, length=255) @Sort(type = SortType.COMPARATOR, comparator = ClasspathComparator.class) public SortedSet getClasspaths() { return classpaths; } /** *

Setter for the field name.

* * @param name a {@link java.lang.String} object. */ public void setName(String name) { this.name = name; } /** *

setEnvironmentType.

* * @param envType a {@link com.greenpepper.server.domain.EnvironmentType} object. */ public void setEnvironmentType(EnvironmentType envType) { this.envType = envType; } /** *

Setter for the field serverName.

* * @param serverName a {@link java.lang.String} object. */ public void setServerName(String serverName) { this.serverName = StringUtil.toNullIfEmpty(serverName); } /** *

Setter for the field serverPort.

* * @param serverPort a {@link java.lang.String} object. */ public void setServerPort(String serverPort) { this.serverPort = StringUtil.toNullIfEmpty(serverPort); } /** *

Setter for the field secured.

* * @param secured a {@link java.lang.Boolean} object. */ public void setSecured(Boolean secured) { this.secured = secured != null && secured; } /** *

Setter for the field cmdLineTemplate.

* * @param cmdLineTemplate a {@link java.lang.String} object. */ public void setCmdLineTemplate(String cmdLineTemplate) { this.cmdLineTemplate = StringUtil.toNullIfEmpty(cmdLineTemplate); } /** *

Setter for the field mainClass.

* * @param mainClass a {@link java.lang.String} object. */ public void setMainClass(String mainClass) { this.mainClass = StringUtil.toNullIfEmpty(mainClass); } /** *

Setter for the field classpaths.

* * @param classpaths a {@link java.util.SortedSet} object. */ public void setClasspaths(SortedSet classpaths) { this.classpaths = classpaths; } /** *

execute.

* * @param specification a {@link com.greenpepper.server.domain.Specification} object. * @param systemUnderTest a {@link com.greenpepper.server.domain.SystemUnderTest} object. * @param implementedVersion a boolean. * @param sections a {@link java.lang.String} object. * @param locale a {@link java.lang.String} object. * @return a {@link com.greenpepper.server.domain.Execution} object. */ public Execution execute(Specification specification, SystemUnderTest systemUnderTest, boolean implementedVersion, String sections, String locale) { if(isRemote()) { return executeRemotely(specification, systemUnderTest, implementedVersion, sections, locale); } else { return executeLocally(specification, systemUnderTest, implementedVersion, sections, locale); } } @SuppressWarnings("unchecked") protected Execution executeRemotely(Specification specification, SystemUnderTest systemUnderTest, boolean implementedVersion, String sections, String locale) { LOG.debug("Execute Remotely {} on agentURL {}", specification.getName(), agentUrl()); try { sections = defaultString(sections); locale = defaultString(locale); XmlRpcClientExecutor xmlrpc = XmlRpcClientExecutorFactory.newExecutor(agentUrl()); Vector params = CollectionUtil.toVector(marshallize(), systemUnderTest.marshallize(), specification.marshallize(), implementedVersion, sections, locale); Vector execParams = (Vector)xmlrpc.execute(AGENT_HANDLER+".execute", params); Execution execution = XmlRpcDataMarshaller.toExecution(execParams); execution.setSystemUnderTest(systemUnderTest); execution.setSpecification(specification); execution.setRemotelyExecuted(); return execution; } catch (Exception e) { return Execution.error(specification, systemUnderTest, sections, ExceptionUtils.stackTrace(e, "
", 15)); } } private Execution executeLocally(Specification specification, SystemUnderTest systemUnderTest, boolean implementedVersion, String sections, String locale) { try { String outpuPath = uniquePath("GreenPepperTest", ".tst"); return executeLocally(specification, systemUnderTest, implementedVersion, sections, locale, outpuPath); } catch (IOException e) { return Execution.error(specification, systemUnderTest, sections, ExceptionUtils.stackTrace(e, "
", 15)); } } protected Execution executeLocally(Specification specification, SystemUnderTest systemUnderTest, boolean implementedVersion, String sections, String locale, String outpuPath) { return executeLocally(specification, systemUnderTest, implementedVersion, sections, locale, outpuPath, null, null, false); } protected Execution executeLocally(Specification specification, SystemUnderTest systemUnderTest, boolean implementedVersion, String sections, String locale, String outpuPath, String stdOutFile, String stdErrFile, boolean redirectToLogFile) { File outputFile = null; CommandLineExecutor commandLineExecutor = null; try { outputFile = new File(outpuPath); String[] cmdLine = compileCmdLine(specification, systemUnderTest, outpuPath, implementedVersion, sections, locale); commandLineExecutor = new CommandLineExecutor(cmdLine); commandLineExecutor.setDontShowStdOut(redirectToLogFile); if (stdOutFile != null) { commandLineExecutor.setStdOutFile(stdOutFile); } if (stdErrFile != null) { commandLineExecutor.setStdErrFile(stdErrFile); } commandLineExecutor.executeAndWait(); Execution execution = Execution.newInstance(specification, systemUnderTest, XmlReport.parse(outputFile)); injectLogsInExecution(commandLineExecutor, execution); return execution; } catch (GreenPepperServerException e) { Execution execution = Execution.error(specification, systemUnderTest, sections, e.getId()); if (commandLineExecutor != null) { injectLogsInExecution(commandLineExecutor, execution); } return execution; } catch (Exception e) { Execution execution = Execution.error(specification, systemUnderTest, sections, ExceptionUtils.stackTrace(e, "
", 15)); if (commandLineExecutor != null) { injectLogsInExecution(commandLineExecutor, execution); } return execution; } finally { IOUtil.deleteFile(outputFile); } } private void injectLogsInExecution(CommandLineExecutor commandLineExecutor, Execution execution) { execution.setStdoutLogs(commandLineExecutor.getOutput()); execution.setStderrLogs(commandLineExecutor.getError()); } /** *

marshallize.

* * @return a {@link java.util.Vector} object. */ public Vector marshallize() { Vector parameters = new Vector(); parameters.add(RUNNER_NAME_IDX, name); parameters.add(RUNNER_CMDLINE_IDX, defaultString(cmdLineTemplate)); parameters.add(RUNNER_ENVTYPE_IDX, envType != null ? envType.marshallize() : EnvironmentType.newInstance("").marshallize()); parameters.add(RUNNER_SERVER_NAME_IDX, defaultString(serverName)); parameters.add(RUNNER_SERVER_PORT_IDX, defaultString(serverPort)); parameters.add(RUNNER_MAINCLASS_IDX, defaultString(mainClass)); parameters.add(RUNNER_CLASSPATH_IDX, new Vector(classpaths)); parameters.add(RUNNER_SECURED_IDX, isSecured()); return parameters; } /** *

agentUrl.

* * @return a {@link java.lang.String} object. */ public String agentUrl() { return ( isSecured() ? "https://" : "http://" ) + serverName + ":" + serverPort; } /** {@inheritDoc} */ public int compareTo(Object o) { return this.getName().compareTo(((Runner)o).getName()); } /** {@inheritDoc} */ public boolean equals(Object o) { if(o == null || !(o instanceof Runner)) { return false; } Runner runnerCompared = (Runner)o; return getName().equals(runnerCompared.getName()); } /** *

hashCode.

* * @return a int. */ public int hashCode() { return getName().hashCode(); } private String[] compileCmdLine(Specification spec, SystemUnderTest sut, String outpuPath, boolean implementedVersion, String sections, String locale) throws Exception { CommandLineBuilder cmdBuilder = new CommandLineBuilder(cmdLineTemplate); cmdBuilder.setDependencies(mergedDependencies(sut)); cmdBuilder.setMainClass(mainClass); cmdBuilder.setInputPath(URIUtil.raw(spec.getName()) + (implementedVersion ? "" : "?implemented=false")); cmdBuilder.setOutpuPath(outpuPath); cmdBuilder.setRepository(spec.getRepository().asCmdLineOption(envType)); if (cmdLineTemplate.contains("{fixtureFactoryArgs}")) { cmdBuilder.setFixtureFactory(sut.getFixtureFactory()); cmdBuilder.setFixtureFactoryArgs(sut.getFixtureFactoryArgs()); } else { cmdBuilder.setFixtureFactory(sut.fixtureFactoryCmdLineOption()); } cmdBuilder.setProjectDependencyDescriptor(sut.getProjectDependencyDescriptor()); cmdBuilder.setSections(sections); cmdBuilder.setLocale(locale); return cmdBuilder.getCmdLine(); } private Collection mergedDependencies(SystemUnderTest systemUnderTest) { Collection dependencies = new ArrayList(); dependencies.addAll(getClasspaths()); dependencies.addAll(systemUnderTest.getFixtureClasspaths()); dependencies.addAll(systemUnderTest.getSutClasspaths()); return dependencies; } @Transient protected boolean isRemote() { return !StringUtil.isEmpty(serverName) && !StringUtil.isEmpty(serverPort); } }