Search in sources :

Example 26 with Job

use of org.pentaho.di.job.Job in project pentaho-kettle by pentaho.

the class SetVariable method setValue.

private void setValue(Object[] rowData, int i, boolean usedefault) throws KettleException {
    // Set the appropriate environment variable
    // 
    String value = null;
    if (usedefault) {
        value = environmentSubstitute(meta.getDefaultValue()[i]);
    } else {
        int index = data.outputMeta.indexOfValue(meta.getFieldName()[i]);
        if (index < 0) {
            throw new KettleException("Unable to find field [" + meta.getFieldName()[i] + "] in input row");
        }
        ValueMetaInterface valueMeta = data.outputMeta.getValueMeta(index);
        Object valueData = rowData[index];
        // 
        if (meta.isUsingFormatting()) {
            value = valueMeta.getString(valueData);
        } else {
            value = valueMeta.getCompatibleString(valueData);
        }
    }
    if (value == null) {
        value = "";
    }
    // Get variable name
    String varname = meta.getVariableName()[i];
    if (Utils.isEmpty(varname)) {
        if (Utils.isEmpty(value)) {
            throw new KettleException("Variable name nor value was specified on line #" + (i + 1));
        } else {
            throw new KettleException("There was no variable name specified for value [" + value + "]");
        }
    }
    Job parentJob = null;
    // We always set the variable in this step and in the parent transformation...
    // 
    setVariable(varname, value);
    // Set variable in the transformation
    // 
    Trans trans = getTrans();
    trans.setVariable(varname, value);
    // 
    while (trans.getParentTrans() != null) {
        trans = trans.getParentTrans();
        trans.setVariable(varname, value);
    }
    // 
    switch(meta.getVariableType()[i]) {
        case SetVariableMeta.VARIABLE_TYPE_JVM:
            System.setProperty(varname, value);
            parentJob = trans.getParentJob();
            while (parentJob != null) {
                parentJob.setVariable(varname, value);
                parentJob = parentJob.getParentJob();
            }
            break;
        case SetVariableMeta.VARIABLE_TYPE_ROOT_JOB:
            // Comments by SB
            // VariableSpace rootJob = null;
            parentJob = trans.getParentJob();
            while (parentJob != null) {
                parentJob.setVariable(varname, value);
                // rootJob = parentJob;
                parentJob = parentJob.getParentJob();
            }
            break;
        case SetVariableMeta.VARIABLE_TYPE_GRAND_PARENT_JOB:
            // Set the variable in the parent job
            // 
            parentJob = trans.getParentJob();
            if (parentJob != null) {
                parentJob.setVariable(varname, value);
            } else {
                throw new KettleStepException("Can't set variable [" + varname + "] on parent job: the parent job is not available");
            }
            // Set the variable on the grand-parent job
            // 
            VariableSpace gpJob = trans.getParentJob().getParentJob();
            if (gpJob != null) {
                gpJob.setVariable(varname, value);
            } else {
                throw new KettleStepException("Can't set variable [" + varname + "] on grand parent job: the grand parent job is not available");
            }
            break;
        case SetVariableMeta.VARIABLE_TYPE_PARENT_JOB:
            // Set the variable in the parent job
            // 
            parentJob = trans.getParentJob();
            if (parentJob != null) {
                parentJob.setVariable(varname, value);
            } else {
                throw new KettleStepException("Can't set variable [" + varname + "] on parent job: the parent job is not available");
            }
            break;
        default:
            break;
    }
    logBasic(BaseMessages.getString(PKG, "SetVariable.Log.SetVariableToValue", meta.getVariableName()[i], value));
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Job(org.pentaho.di.job.Job) Trans(org.pentaho.di.trans.Trans) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 27 with Job

use of org.pentaho.di.job.Job in project pentaho-kettle by pentaho.

the class AddExportServlet method doGet.

/**
 *    <div id="mindtouch">
 *    <h1>/kettle/addExport</h1>
 *    <a name="POST"></a>
 *    <h2>POST</h2>
 *    <p>Returns the list of users in the platform. This list is in an xml format as shown in the example response.
 *    Uploads and executes previously exported job or transformation.
 *    Uploads zip file containing job or transformation to be executed and executes it.
 *    Method relies on the input parameters to find the entity to be executed. The archive is
 *    transferred within request body.
 *
 *    <code>File url of the executed entity </code> will be returned in the Response object
 *    or <code>message</code> describing error occurred. To determine if the call is successful
 *    rely on <code>result</code> parameter in response.</p>
 *
 *    <p><b>Example Request:</b><br />
 *    <pre function="syntax.xml">
 *    POST /kettle/addExport/?type=job&load=dummy_job.kjb
 *    </pre>
 *    Request body should contain zip file prepared for Carte execution.
 *    </p>
 *    <h3>Parameters</h3>
 *    <table class="pentaho-table">
 *    <tbody>
 *    <tr>
 *      <th>name</th>
 *      <th>description</th>
 *      <th>type</th>
 *    </tr>
 *    <tr>
 *    <td>type</td>
 *    <td>The type of the entity to be executed either <code>job</code> or <code>trans</code>.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>load</td>
 *    <td>The name of the entity within archive to be executed.</td>
 *    <td>query</td>
 *    </tr>
 *    </tbody>
 *    </table>
 *
 *  <h3>Response Body</h3>
 *
 *  <table class="pentaho-table">
 *    <tbody>
 *      <tr>
 *        <td align="right">element:</td>
 *        <td>(custom)</td>
 *      </tr>
 *      <tr>
 *        <td align="right">media types:</td>
 *        <td>application/xml</td>
 *      </tr>
 *    </tbody>
 *  </table>
 *    <p>Response wraps file url of the entity that was executed or error stack trace if an error occurred.
 *     Response has <code>result</code> OK if there were no errors. Otherwise it returns ERROR.</p>
 *
 *    <p><b>Example Response:</b></p>
 *    <pre function="syntax.xml">
 *    <?xml version="1.0" encoding="UTF-8"?>
 *    <webresult>
 *      <result>OK</result>
 *      <message>zip&#x3a;file&#x3a;&#x2f;&#x2f;&#x2f;temp&#x2f;export_ee2a67de-6a72-11e4-82c0-4701a2bac6a5.zip&#x21;dummy_job.kjb</message>
 *      <id>74cf4219-c881-4633-a71a-2ed16b7db7b8</id>
 *    </webresult>
 *    </pre>
 *
 *    <h3>Status Codes</h3>
 *    <table class="pentaho-table">
 *  <tbody>
 *    <tr>
 *      <th>code</th>
 *      <th>description</th>
 *    </tr>
 *    <tr>
 *      <td>200</td>
 *      <td>Request was processed and XML response is returned.</td>
 *    </tr>
 *    <tr>
 *      <td>500</td>
 *      <td>Internal server error occurs during request processing.</td>
 *    </tr>
 *  </tbody>
 *</table>
 *</div>
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isJettyMode() && !request.getRequestURI().startsWith(CONTEXT_PATH)) {
        return;
    }
    if (log.isDebug()) {
        logDebug("Addition of export requested");
    }
    PrintWriter out = response.getWriter();
    // read from the client
    InputStream in = request.getInputStream();
    if (log.isDetailed()) {
        logDetailed("Encoding: " + request.getCharacterEncoding());
    }
    boolean isJob = TYPE_JOB.equalsIgnoreCase(request.getParameter(PARAMETER_TYPE));
    // the resource to load
    String load = request.getParameter(PARAMETER_LOAD);
    response.setContentType("text/xml");
    out.print(XMLHandler.getXMLHeader());
    response.setStatus(HttpServletResponse.SC_OK);
    OutputStream outputStream = null;
    try {
        FileObject tempFile = KettleVFS.createTempFile("export", ".zip", System.getProperty("java.io.tmpdir"));
        outputStream = KettleVFS.getOutputStream(tempFile, false);
        // Pass the input directly to a temporary file
        // 
        // int size = 0;
        int c;
        while ((c = in.read()) != -1) {
            outputStream.write(c);
        // size++;
        }
        outputStream.flush();
        outputStream.close();
        // don't close it twice
        outputStream = null;
        String archiveUrl = tempFile.getName().toString();
        String fileUrl = null;
        String carteObjectId = null;
        SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
        // 
        if (!Utils.isEmpty(load)) {
            fileUrl = "zip:" + archiveUrl + "!" + load;
            if (isJob) {
                // Open the job from inside the ZIP archive
                // 
                KettleVFS.getFileObject(fileUrl);
                // never with a repository
                JobMeta jobMeta = new JobMeta(fileUrl, null);
                // Also read the execution configuration information
                // 
                String configUrl = "zip:" + archiveUrl + "!" + Job.CONFIGURATION_IN_EXPORT_FILENAME;
                Document configDoc = XMLHandler.loadXMLFile(configUrl);
                JobExecutionConfiguration jobExecutionConfiguration = new JobExecutionConfiguration(XMLHandler.getSubNode(configDoc, JobExecutionConfiguration.XML_TAG));
                carteObjectId = UUID.randomUUID().toString();
                servletLoggingObject.setContainerObjectId(carteObjectId);
                servletLoggingObject.setLogLevel(jobExecutionConfiguration.getLogLevel());
                Job job = new Job(null, jobMeta, servletLoggingObject);
                // 
                if (jobExecutionConfiguration.isExpandingRemoteJob()) {
                    job.addDelegationListener(new CarteDelegationHandler(getTransformationMap(), getJobMap()));
                }
                // store it all in the map...
                // 
                getJobMap().addJob(job.getJobname(), carteObjectId, job, new JobConfiguration(jobMeta, jobExecutionConfiguration));
                // Apply the execution configuration...
                // 
                log.setLogLevel(jobExecutionConfiguration.getLogLevel());
                job.setArguments(jobExecutionConfiguration.getArgumentStrings());
                jobMeta.injectVariables(jobExecutionConfiguration.getVariables());
                // Also copy the parameters over...
                // 
                Map<String, String> params = jobExecutionConfiguration.getParams();
                for (String param : params.keySet()) {
                    String value = params.get(param);
                    jobMeta.setParameterValue(param, value);
                }
            } else {
                // Open the transformation from inside the ZIP archive
                // 
                TransMeta transMeta = new TransMeta(fileUrl);
                // Also read the execution configuration information
                // 
                String configUrl = "zip:" + archiveUrl + "!" + Trans.CONFIGURATION_IN_EXPORT_FILENAME;
                Document configDoc = XMLHandler.loadXMLFile(configUrl);
                TransExecutionConfiguration executionConfiguration = new TransExecutionConfiguration(XMLHandler.getSubNode(configDoc, TransExecutionConfiguration.XML_TAG));
                carteObjectId = UUID.randomUUID().toString();
                servletLoggingObject.setContainerObjectId(carteObjectId);
                servletLoggingObject.setLogLevel(executionConfiguration.getLogLevel());
                Trans trans = new Trans(transMeta, servletLoggingObject);
                // store it all in the map...
                // 
                getTransformationMap().addTransformation(trans.getName(), carteObjectId, trans, new TransConfiguration(transMeta, executionConfiguration));
            }
        } else {
            fileUrl = archiveUrl;
        }
        out.println(new WebResult(WebResult.STRING_OK, fileUrl, carteObjectId));
    } catch (Exception ex) {
        out.println(new WebResult(WebResult.STRING_ERROR, Const.getStackTracker(ex)));
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }
}
Also used : JobMeta(org.pentaho.di.job.JobMeta) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TransMeta(org.pentaho.di.trans.TransMeta) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) Document(org.w3c.dom.Document) JobExecutionConfiguration(org.pentaho.di.job.JobExecutionConfiguration) TransConfiguration(org.pentaho.di.trans.TransConfiguration) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) TransExecutionConfiguration(org.pentaho.di.trans.TransExecutionConfiguration) FileObject(org.apache.commons.vfs2.FileObject) Job(org.pentaho.di.job.Job) Trans(org.pentaho.di.trans.Trans) JobConfiguration(org.pentaho.di.job.JobConfiguration) PrintWriter(java.io.PrintWriter)

Example 28 with Job

use of org.pentaho.di.job.Job in project pentaho-kettle by pentaho.

the class AddJobServlet method doGet.

/**
 * /**
 *
 *    <div id="mindtouch">
 *    <h1>/kettle/addJob</h1>
 *    <a name="POST"></a>
 *    <h2>POST</h2>
 *    <p>Uploads and executes job configuration XML file.
 *  Uploads xml file containing job and job_execution_configuration (wrapped in job_configuration tag)
 *  to be executed and executes it. Method relies on the input parameter to determine if xml or html
 *  reply should be produced. The job_configuration xml is
 *  transferred within request body.
 *
 *  <code>Job name of the executed job </code> will be returned in the Response object
 *  or <code>message</code> describing error occurred. To determine if the call successful or not you should
 *  rely on <code>result</code> parameter in response.</p>
 *
 *    <p><b>Example Request:</b><br />
 *    <pre function="syntax.xml">
 *    POST /kettle/addJob/?xml=Y
 *    </pre>
 *    <p>Request body should contain xml containing job_configuration (job + job_execution_configuration
 *  wrapped in job_configuration tag).</p>
 *    </p>
 *    <h3>Parameters</h3>
 *    <table class="pentaho-table">
 *    <tbody>
 *    <tr>
 *      <th>name</th>
 *      <th>description</th>
 *      <th>type</th>
 *    </tr>
 *    <tr>
 *    <td>xml</td>
 *    <td>Boolean flag set to either <code>Y</code> or <code>N</code> describing if xml or html reply
 *  should be produced.</td>
 *    <td>boolean, optional</td>
 *    </tr>
 *    </tbody>
 *    </table>
 *
 *  <h3>Response Body</h3>
 *
 *  <table class="pentaho-table">
 *    <tbody>
 *      <tr>
 *        <td align="right">element:</td>
 *        <td>(custom)</td>
 *      </tr>
 *      <tr>
 *        <td align="right">media types:</td>
 *        <td>text/xml, text/html</td>
 *      </tr>
 *    </tbody>
 *  </table>
 *    <p>Response wraps job name that was executed or error stack trace
 *  if an error occurred. Response has <code>result</code> OK if there were no errors. Otherwise it returns ERROR.</p>
 *
 *    <p><b>Example Response:</b></p>
 *    <pre function="syntax.xml">
 *    <?xml version="1.0" encoding="UTF-8"?>
 *    <webresult>
 *      <result>OK</result>
 *      <message>Job &#x27;dummy_job&#x27; was added to the list with id 1e90eca8-4d4c-47f7-8e5c-99ec36525e7c</message>
 *      <id>1e90eca8-4d4c-47f7-8e5c-99ec36525e7c</id>
 *    </webresult>
 *    </pre>
 *
 *    <h3>Status Codes</h3>
 *    <table class="pentaho-table">
 *  <tbody>
 *    <tr>
 *      <th>code</th>
 *      <th>description</th>
 *    </tr>
 *    <tr>
 *      <td>200</td>
 *      <td>Request was processed and XML response is returned.</td>
 *    </tr>
 *    <tr>
 *      <td>500</td>
 *      <td>Internal server error occurs during request processing.</td>
 *    </tr>
 *  </tbody>
 *</table>
 *</div>
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isJettyMode() && !request.getRequestURI().startsWith(CONTEXT_PATH)) {
        return;
    }
    if (log.isDebug()) {
        logDebug("Addition of job requested");
    }
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));
    PrintWriter out = response.getWriter();
    // read from the client
    BufferedReader in = request.getReader();
    if (log.isDetailed()) {
        logDetailed("Encoding: " + request.getCharacterEncoding());
    }
    if (useXML) {
        response.setContentType("text/xml");
        out.print(XMLHandler.getXMLHeader());
    } else {
        response.setContentType("text/html");
        out.println("<HTML>");
        out.println("<HEAD><TITLE>Add job</TITLE></HEAD>");
        out.println("<BODY>");
    }
    response.setStatus(HttpServletResponse.SC_OK);
    try {
        // First read the complete transformation in memory from the request
        int c;
        StringBuilder xml = new StringBuilder();
        while ((c = in.read()) != -1) {
            xml.append((char) c);
        }
        // Parse the XML, create a job configuration
        // 
        // System.out.println(xml);
        // 
        JobConfiguration jobConfiguration = JobConfiguration.fromXML(xml.toString());
        JobMeta jobMeta = jobConfiguration.getJobMeta();
        JobExecutionConfiguration jobExecutionConfiguration = jobConfiguration.getJobExecutionConfiguration();
        jobMeta.setLogLevel(jobExecutionConfiguration.getLogLevel());
        jobMeta.injectVariables(jobExecutionConfiguration.getVariables());
        // If there was a repository, we know about it at this point in time.
        // 
        final Repository repository = jobConfiguration.getJobExecutionConfiguration().getRepository();
        String carteObjectId = UUID.randomUUID().toString();
        SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
        servletLoggingObject.setContainerObjectId(carteObjectId);
        servletLoggingObject.setLogLevel(jobExecutionConfiguration.getLogLevel());
        // Create the transformation and store in the list...
        // 
        final Job job = new Job(repository, jobMeta, servletLoggingObject);
        // Setting variables
        // 
        job.initializeVariablesFrom(null);
        job.getJobMeta().setInternalKettleVariables(job);
        job.injectVariables(jobConfiguration.getJobExecutionConfiguration().getVariables());
        job.setArguments(jobExecutionConfiguration.getArgumentStrings());
        // Also copy the parameters over...
        // 
        job.copyParametersFrom(jobMeta);
        job.clearParameters();
        String[] parameterNames = job.listParameters();
        for (int idx = 0; idx < parameterNames.length; idx++) {
            // Grab the parameter value set in the job entry
            // 
            String thisValue = jobExecutionConfiguration.getParams().get(parameterNames[idx]);
            if (!Utils.isEmpty(thisValue)) {
                // Set the value as specified by the user in the job entry
                // 
                jobMeta.setParameterValue(parameterNames[idx], thisValue);
            }
        }
        jobMeta.activateParameters();
        // Check if there is a starting point specified.
        String startCopyName = jobExecutionConfiguration.getStartCopyName();
        if (startCopyName != null && !startCopyName.isEmpty()) {
            int startCopyNr = jobExecutionConfiguration.getStartCopyNr();
            JobEntryCopy startJobEntryCopy = jobMeta.findJobEntry(startCopyName, startCopyNr, false);
            job.setStartJobEntryCopy(startJobEntryCopy);
        }
        job.setSocketRepository(getSocketRepository());
        // 
        if (jobExecutionConfiguration.isExpandingRemoteJob()) {
            job.addDelegationListener(new CarteDelegationHandler(getTransformationMap(), getJobMap()));
        }
        getJobMap().addJob(job.getJobname(), carteObjectId, job, jobConfiguration);
        // 
        if (repository != null) {
            job.addJobListener(new JobAdapter() {

                public void jobFinished(Job job) {
                    repository.disconnect();
                }
            });
        }
        String message = "Job '" + job.getJobname() + "' was added to the list with id " + carteObjectId;
        if (useXML) {
            out.println(new WebResult(WebResult.STRING_OK, message, carteObjectId));
        } else {
            out.println("<H1>" + message + "</H1>");
            out.println("<p><a href=\"" + convertContextPath(GetJobStatusServlet.CONTEXT_PATH) + "?name=" + job.getJobname() + "&id=" + carteObjectId + "\">Go to the job status page</a><p>");
        }
    } catch (Exception ex) {
        if (useXML) {
            out.println(new WebResult(WebResult.STRING_ERROR, Const.getStackTracker(ex)));
        } else {
            out.println("<p>");
            out.println("<pre>");
            ex.printStackTrace(out);
            out.println("</pre>");
        }
    }
    if (!useXML) {
        out.println("<p>");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}
Also used : JobMeta(org.pentaho.di.job.JobMeta) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) JobExecutionConfiguration(org.pentaho.di.job.JobExecutionConfiguration) JobAdapter(org.pentaho.di.job.JobAdapter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Repository(org.pentaho.di.repository.Repository) JobEntryCopy(org.pentaho.di.job.entry.JobEntryCopy) BufferedReader(java.io.BufferedReader) Job(org.pentaho.di.job.Job) JobConfiguration(org.pentaho.di.job.JobConfiguration) PrintWriter(java.io.PrintWriter)

Example 29 with Job

use of org.pentaho.di.job.Job in project pentaho-kettle by pentaho.

the class GetJobStatusServlet method doGet.

/**
 *   <div id="mindtouch">
 *   <h1>/kettle/jobStatus</h1>
 *   <a name="GET"></a>
 *   <h2>GET</h2>
 *   <p>Retrieves status of the specified job.
 *   Status is returned as HTML or XML output depending on the input parameters.
 *   Status contains information about last execution of the job.</p>
 *
 *   <p><b>Example Request:</b><br />
 *   <pre function="syntax.xml">
 *   GET /kettle/jobStatus/?name=dummy_job&xml=Y
 *   </pre>
 *
 *   </p>
 *   <h3>Parameters</h3>
 *   <table class="pentaho-table">
 *   <tbody>
 *   <tr>
 *   <th>name</th>
 *   <th>description</th>
 *   <th>type</th>
 *   </tr>
 *   <tr>
 *   <td>name</td>
 *   <td>Name of the job to be used for status generation.</td>
 *   <td>query</td>
 *   </tr>
 *   <tr>
 *   <td>xml</td>
 *   <td>Boolean flag which defines output format <code>Y</code> forces XML output to be generated.
 *   HTML is returned otherwise.</td>
 *   <td>boolean, optional</td>
 *   </tr>
 *   <tr>
 *   <td>id</td>
 *   <td>Carte id of the job to be used for status generation.</td>
 *   <td>query, optional</td>
 *   </tr>
 *   <tr>
 *   <td>from</td>
 *   <td>Start line number of the execution log to be included into response.</td>
 *   <td>integer, optional</td>
 *   </tr>
 *   </tbody>
 *   </table>
 *
 *   <h3>Response Body</h3>
 *   <table class="pentaho-table">
 *   <tbody>
 *   <tr>
 *   <td align="right">element:</td>
 *   <td>(custom)</td>
 *   </tr>
 *   <tr>
 *   <td align="right">media types:</td>
 *   <td>text/xml, text/html</td>
 *   </tr>
 *   </tbody>
 *   </table>
 *   <p>Response XML or HTML response containing details about the job specified.
 *   If an error occurs during method invocation <code>result</code> field of the response
 *   will contain <code>ERROR</code> status.</p>
 *
 *   <p><b>Example Response:</b></p>
 *   <pre function="syntax.xml">
 *   <?xml version="1.0" encoding="UTF-8"?>
 *   <jobstatus>
 *   <jobname>dummy_job</jobname>
 *   <id>a4d54106-25db-41c5-b9f8-73afd42766a6</id>
 *   <status_desc>Finished</status_desc>
 *   <error_desc/>
 *   <logging_string>&#x3c;&#x21;&#x5b;CDATA&#x5b;H4sIAAAAAAAAADMyMDTRNzTUNzRXMDC3MjS2MjJQ0FVIKc3NrYzPyk8CsoNLEotKFPLTFEDc1IrU5NKSzPw8Xi4j4nRm5qUrpOaVFFUqRLuE&#x2b;vpGxhKj0y0zL7M4IzUFYieybgWNotTi0pwS2&#x2b;iSotLUWE1iTPNCdrhCGtRsXi4AOMIbLPwAAAA&#x3d;&#x5d;&#x5d;&#x3e;</logging_string>
 *   <first_log_line_nr>0</first_log_line_nr>
 *   <last_log_line_nr>20</last_log_line_nr>
 *   <result>
 *   <lines_input>0</lines_input>
 *   <lines_output>0</lines_output>
 *   <lines_read>0</lines_read>
 *   <lines_written>0</lines_written>
 *   <lines_updated>0</lines_updated>
 *   <lines_rejected>0</lines_rejected>
 *   <lines_deleted>0</lines_deleted>
 *   <nr_errors>0</nr_errors>
 *   <nr_files_retrieved>0</nr_files_retrieved>
 *   <entry_nr>0</entry_nr>
 *   <result>Y</result>
 *   <exit_status>0</exit_status>
 *   <is_stopped>N</is_stopped>
 *   <log_channel_id/>
 *   <log_text>null</log_text>
 *   <result-file></result-file>
 *   <result-rows></result-rows>
 *   </result>
 *   </jobstatus>
 *   </pre>
 *
 *   <h3>Status Codes</h3>
 *   <table class="pentaho-table">
 *   <tbody>
 *   <tr>
 *   <th>code</th>
 *   <th>description</th>
 *   </tr>
 *   <tr>
 *   <td>200</td>
 *   <td>Request was processed.</td>
 *   </tr>
 *   <tr>
 *   <td>500</td>
 *   <td>Internal server error occurs during request processing.</td>
 *   </tr>
 *   </tbody>
 *   </table>
 *   </div>
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isJettyMode() && !request.getContextPath().startsWith(CONTEXT_PATH)) {
        return;
    }
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "GetJobStatusServlet.Log.JobStatusRequested"));
    }
    String jobName = request.getParameter("name");
    String id = request.getParameter("id");
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));
    int startLineNr = Const.toInt(request.getParameter("from"), 0);
    response.setStatus(HttpServletResponse.SC_OK);
    if (useXML) {
        response.setContentType("text/xml");
        response.setCharacterEncoding(Const.XML_ENCODING);
    } else {
        response.setContentType("text/html;charset=UTF-8");
    }
    // ID is optional...
    // 
    Job job;
    CarteObjectEntry entry;
    if (Utils.isEmpty(id)) {
        // get the first job that matches...
        // 
        entry = getJobMap().getFirstCarteObjectEntry(jobName);
        if (entry == null) {
            job = null;
        } else {
            id = entry.getId();
            job = getJobMap().getJob(entry);
        }
    } else {
        // 
        if (Utils.isEmpty(jobName)) {
            // Take the ID into account!
            // 
            job = getJobMap().findJob(id);
        } else {
            entry = new CarteObjectEntry(jobName, id);
            job = getJobMap().getJob(entry);
            if (job != null) {
                jobName = job.getJobname();
            }
        }
    }
    if (job != null) {
        if (useXML) {
            try {
                OutputStream out = null;
                byte[] data = null;
                String logId = job.getLogChannelId();
                boolean finishedOrStopped = job.isFinished() || job.isStopped();
                if (finishedOrStopped && (data = cache.get(logId, startLineNr)) != null) {
                    response.setContentLength(XML_HEADER.length + data.length);
                    out = response.getOutputStream();
                    out.write(XML_HEADER);
                    out.write(data);
                    out.flush();
                } else {
                    int lastLineNr = KettleLogStore.getLastBufferLineNr();
                    String logText = getLogText(job, startLineNr, lastLineNr);
                    response.setContentType("text/xml");
                    response.setCharacterEncoding(Const.XML_ENCODING);
                    SlaveServerJobStatus jobStatus = new SlaveServerJobStatus(jobName, id, job.getStatus());
                    jobStatus.setFirstLoggingLineNr(startLineNr);
                    jobStatus.setLastLoggingLineNr(lastLineNr);
                    jobStatus.setLogDate(job.getLogDate());
                    // The log can be quite large at times, we are going to putIfAbsent a base64 encoding around a compressed
                    // stream
                    // of bytes to handle this one.
                    String loggingString = HttpUtil.encodeBase64ZippedString(logText);
                    jobStatus.setLoggingString(loggingString);
                    // Also set the result object...
                    // 
                    // might be null
                    jobStatus.setResult(job.getResult());
                    String xml = jobStatus.getXML();
                    data = xml.getBytes(Charset.forName(Const.XML_ENCODING));
                    out = response.getOutputStream();
                    response.setContentLength(XML_HEADER.length + data.length);
                    out.write(XML_HEADER);
                    out.write(data);
                    out.flush();
                    if (finishedOrStopped && logId != null) {
                        cache.put(logId, xml, startLineNr);
                    }
                }
                response.flushBuffer();
            } catch (KettleException e) {
                throw new ServletException("Unable to get the job status in XML format", e);
            }
        } else {
            PrintWriter out = response.getWriter();
            int lastLineNr = KettleLogStore.getLastBufferLineNr();
            response.setContentType("text/html");
            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("<TITLE>" + BaseMessages.getString(PKG, "GetJobStatusServlet.KettleJobStatus") + "</TITLE>");
            if (EnvUtil.getSystemProperty(Const.KETTLE_CARTE_REFRESH_STATUS, "N").equalsIgnoreCase("Y")) {
                out.println("<META http-equiv=\"Refresh\" content=\"10;url=" + convertContextPath(GetJobStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(Const.NVL(jobName, ""), "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\">");
            }
            out.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            out.println("</HEAD>");
            out.println("<BODY>");
            out.println("<H1>" + BaseMessages.getString(PKG, "GetJobStatusServlet.JobStatus") + "</H1>");
            try {
                out.println("<table border=\"1\">");
                out.print("<tr> <th>" + BaseMessages.getString(PKG, "GetJobStatusServlet.Jobname") + "</th> <th>" + BaseMessages.getString(PKG, "TransStatusServlet.TransStatus") + "</th> </tr>");
                out.print("<tr>");
                out.print("<td>" + Const.NVL(Encode.forHtml(jobName), "") + "</td>");
                out.print("<td>" + job.getStatus() + "</td>");
                out.print("</tr>");
                out.print("</table>");
                out.print("<p>");
                // Show job image?
                // 
                Point max = job.getJobMeta().getMaximum();
                max.x += 20;
                max.y += 20;
                out.print("<iframe height=\"" + max.y + "\" width=\"" + max.x + "\" seamless src=\"" + convertContextPath(GetJobImageServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(jobName, "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\"></iframe>");
                out.print("<p>");
                if (job.isFinished()) {
                    out.print("<a href=\"" + convertContextPath(StartJobServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(Const.NVL(jobName, ""), "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\">" + BaseMessages.getString(PKG, "GetJobStatusServlet.StartJob") + "</a>");
                    out.print("<p>");
                } else {
                    out.print("<a href=\"" + convertContextPath(StopJobServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(Const.NVL(jobName, ""), "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\">" + BaseMessages.getString(PKG, "GetJobStatusServlet.StopJob") + "</a>");
                    out.print("<p>");
                }
                out.println("<p>");
                out.print("<a href=\"" + convertContextPath(GetJobStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(Const.NVL(jobName, ""), "UTF-8") + "&xml=y&id=" + URLEncoder.encode(id, "UTF-8") + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.ShowAsXml") + "</a><br>");
                out.print("<a href=\"" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><br>");
                out.print("<p><a href=\"" + convertContextPath(GetJobStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(Const.NVL(jobName, ""), "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.Refresh") + "</a>");
                // Put the logging below that.
                out.println("<p>");
                out.println("<textarea id=\"joblog\" cols=\"120\" rows=\"20\" wrap=\"off\" " + "name=\"Job log\" readonly=\"readonly\">" + Encode.forHtml(getLogText(job, startLineNr, lastLineNr)) + "</textarea>");
                out.println("<script type=\"text/javascript\"> ");
                out.println("  joblog.scrollTop=joblog.scrollHeight; ");
                out.println("</script> ");
                out.println("<p>");
            } catch (Exception ex) {
                out.println("<p>");
                out.println("<pre>");
                out.println(Encode.forHtml(Const.getStackTracker(ex)));
                out.println("</pre>");
            }
            out.println("<p>");
            out.println("</BODY>");
            out.println("</HTML>");
        }
    } else {
        PrintWriter out = response.getWriter();
        if (useXML) {
            out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "StartJobServlet.Log.SpecifiedJobNotFound", jobName, id)));
        } else {
            out.println("<H1>Job " + Encode.forHtml("\'" + jobName + "\'") + " could not be found.</H1>");
            out.println("<a href=\"" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><p>");
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) OutputStream(java.io.OutputStream) Point(org.pentaho.di.core.gui.Point) Point(org.pentaho.di.core.gui.Point) ServletException(javax.servlet.ServletException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) Job(org.pentaho.di.job.Job) PrintWriter(java.io.PrintWriter)

Example 30 with Job

use of org.pentaho.di.job.Job in project pentaho-kettle by pentaho.

the class RegisterJobServlet method generateBody.

@Override
WebResult generateBody(HttpServletRequest request, HttpServletResponse response, boolean useXML) throws IOException, KettleException {
    final String xml = IOUtils.toString(request.getInputStream());
    // Parse the XML, create a job configuration
    JobConfiguration jobConfiguration = JobConfiguration.fromXML(xml);
    Job job = createJob(jobConfiguration);
    String message = "Job '" + job.getJobname() + "' was added to the list with id " + job.getContainerObjectId();
    return new WebResult(WebResult.STRING_OK, message, job.getContainerObjectId());
}
Also used : Job(org.pentaho.di.job.Job) JobConfiguration(org.pentaho.di.job.JobConfiguration)

Aggregations

Job (org.pentaho.di.job.Job)95 JobMeta (org.pentaho.di.job.JobMeta)44 Test (org.junit.Test)35 Result (org.pentaho.di.core.Result)22 KettleException (org.pentaho.di.core.exception.KettleException)20 PrintWriter (java.io.PrintWriter)17 JobEntryCopy (org.pentaho.di.job.entry.JobEntryCopy)17 Trans (org.pentaho.di.trans.Trans)14 IOException (java.io.IOException)11 Before (org.junit.Before)11 Point (org.pentaho.di.core.gui.Point)11 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)11 JobExecutionConfiguration (org.pentaho.di.job.JobExecutionConfiguration)10 Repository (org.pentaho.di.repository.Repository)10 ArrayList (java.util.ArrayList)9 ServletException (javax.servlet.ServletException)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 SimpleLoggingObject (org.pentaho.di.core.logging.SimpleLoggingObject)9 JobConfiguration (org.pentaho.di.job.JobConfiguration)9