Search in sources :

Example 6 with JobConfiguration

use of org.pentaho.di.job.JobConfiguration 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)

Example 7 with JobConfiguration

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

the class RegisterPackageServlet method generateBody.

@Override
WebResult generateBody(HttpServletRequest request, HttpServletResponse response, boolean useXML) throws KettleException, IOException {
    FileObject tempFile = KettleVFS.createTempFile("export", ".zip", System.getProperty("java.io.tmpdir"));
    OutputStream out = KettleVFS.getOutputStream(tempFile, false);
    IOUtils.copy(request.getInputStream(), out);
    out.flush();
    IOUtils.closeQuietly(out);
    String archiveUrl = tempFile.getName().toString();
    // the resource to load
    String load = request.getParameter(PARAMETER_LOAD);
    if (!Utils.isEmpty(load)) {
        String fileUrl = MessageFormat.format(ZIP_CONT, archiveUrl, load);
        boolean isJob = TYPE_JOB.equalsIgnoreCase(request.getParameter(PARAMETER_TYPE));
        String resultId;
        if (isJob) {
            Node node = getConfigNodeFromZIP(archiveUrl, Job.CONFIGURATION_IN_EXPORT_FILENAME, JobExecutionConfiguration.XML_TAG);
            JobExecutionConfiguration jobExecutionConfiguration = new JobExecutionConfiguration(node);
            JobMeta jobMeta = new JobMeta(fileUrl, jobExecutionConfiguration.getRepository());
            JobConfiguration jobConfiguration = new JobConfiguration(jobMeta, jobExecutionConfiguration);
            Job job = createJob(jobConfiguration);
            resultId = job.getContainerObjectId();
        } else {
            Node node = getConfigNodeFromZIP(archiveUrl, Trans.CONFIGURATION_IN_EXPORT_FILENAME, TransExecutionConfiguration.XML_TAG);
            TransExecutionConfiguration transExecutionConfiguration = new TransExecutionConfiguration(node);
            TransMeta transMeta = new TransMeta(fileUrl, transExecutionConfiguration.getRepository());
            TransConfiguration transConfiguration = new TransConfiguration(transMeta, transExecutionConfiguration);
            Trans trans = createTrans(transConfiguration);
            resultId = trans.getContainerObjectId();
        }
        return new WebResult(WebResult.STRING_OK, fileUrl, resultId);
    }
    return null;
}
Also used : JobMeta(org.pentaho.di.job.JobMeta) OutputStream(java.io.OutputStream) Node(org.w3c.dom.Node) TransMeta(org.pentaho.di.trans.TransMeta) JobExecutionConfiguration(org.pentaho.di.job.JobExecutionConfiguration) TransConfiguration(org.pentaho.di.trans.TransConfiguration) 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)

Example 8 with JobConfiguration

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

the class ExecuteJobServlet method doGet.

/**
 * <div id="mindtouch">
 *    <h1>/kettle/executeJob</h1>
 *    <a name="GET"></a>
 *    <h2>GET</h2>
 *    <p>Executes job from the specified repository.
 *  Connects to the repository provided as a parameter, loads the job from it and executes it.
 *  Empty response is returned or response contains output of an error happened during the job execution.
 *  Response contains <code>ERROR</code> result if error happened during job execution.</p>
 *
 *    <p><b>Example Request:</b><br />
 *    <pre function="syntax.xml">
 *    GET /kettle/executeJob/?rep=my_repository&user=my_user&pass=my_password&job=my_job&level=INFO
 *    </pre>
 *
 *    </p>
 *    <h3>Parameters</h3>
 *    <table class="pentaho-table">
 *    <tbody>
 *    <tr>
 *      <th>name</th>
 *      <th>description</th>
 *      <th>type</th>
 *    </tr>
 *    <tr>
 *    <td>rep</td>
 *    <td>Repository id to connect to.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>user</td>
 *    <td>User name to be used to connect to repository.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>pass</td>
 *    <td>User password to be used to connect to repository.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>job</td>
 *    <td>Job name to be loaded and executed.</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>level</td>
 *    <td>Logging level to be used for job execution (i.e. Debug).</td>
 *    <td>query</td>
 *    </tr>
 *    <tr>
 *    <td>*any name*</td>
 *    <td>All the other parameters will be sent to the job for using as variables.
 *  When necessary you can add custom parameters to the request.
 *  They will be used to set the job variables values.</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 contains error output of the job executed or Carte object Id
 *  if the execution was successful.</p>
 *
 *    <p><b>Example Error Response:</b></p>
 *    <pre function="syntax.xml">
 *  <webresult>
 *    <result>OK</result>
 *    <message>Job started</message>
 *    <id>74d96aa6-f29a-4bac-a26a-06a8c8f107e5</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.</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, "ExecuteJobServlet.Log.ExecuteJobRequested"));
    }
    // Options taken from PAN
    // 
    String[] knownOptions = new String[] { "rep", "user", "pass", "job", "level" };
    String repOption = request.getParameter("rep");
    String userOption = request.getParameter("user");
    String passOption = Encr.decryptPasswordOptionallyEncrypted(request.getParameter("pass"));
    String jobOption = request.getParameter("job");
    String levelOption = request.getParameter("level");
    response.setStatus(HttpServletResponse.SC_OK);
    String encoding = System.getProperty("KETTLE_DEFAULT_SERVLET_ENCODING", null);
    if (encoding != null && !Utils.isEmpty(encoding.trim())) {
        response.setCharacterEncoding(encoding);
        response.setContentType("text/html; charset=" + encoding);
    }
    PrintWriter out = response.getWriter();
    try {
        final Repository repository = openRepository(repOption, userOption, passOption);
        final JobMeta jobMeta = loadJob(repository, jobOption);
        // Set the servlet parameters as variables in the job
        // 
        String[] parameters = jobMeta.listParameters();
        Enumeration<?> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameter = (String) parameterNames.nextElement();
            String[] values = request.getParameterValues(parameter);
            // 
            if (Const.indexOfString(parameter, knownOptions) < 0) {
                // 
                if (Const.indexOfString(parameter, parameters) < 0) {
                    jobMeta.setVariable(parameter, values[0]);
                } else {
                    jobMeta.setParameterValue(parameter, values[0]);
                }
            }
        }
        JobExecutionConfiguration jobExecutionConfiguration = new JobExecutionConfiguration();
        LogLevel logLevel = LogLevel.getLogLevelForCode(levelOption);
        jobExecutionConfiguration.setLogLevel(logLevel);
        JobConfiguration jobConfiguration = new JobConfiguration(jobMeta, jobExecutionConfiguration);
        String carteObjectId = UUID.randomUUID().toString();
        SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
        servletLoggingObject.setContainerObjectId(carteObjectId);
        servletLoggingObject.setLogLevel(logLevel);
        // Create the job and store in the list...
        // 
        final Job job = new Job(repository, jobMeta, servletLoggingObject);
        job.setRepository(repository);
        job.setSocketRepository(getSocketRepository());
        getJobMap().addJob(jobMeta.getName(), carteObjectId, job, jobConfiguration);
        job.setContainerObjectId(carteObjectId);
        if (repository != null) {
            // The repository connection is open: make sure we disconnect from the repository once we
            // are done with this job.
            // 
            job.addJobListener(new JobAdapter() {

                public void jobFinished(Job job) {
                    repository.disconnect();
                }
            });
        }
        try {
            runJob(job);
            WebResult webResult = new WebResult(WebResult.STRING_OK, "Job started", carteObjectId);
            out.println(webResult.getXML());
            out.flush();
        } catch (Exception executionException) {
            String logging = KettleLogStore.getAppender().getBuffer(job.getLogChannelId(), false).toString();
            throw new KettleException("Error executing job: " + logging, executionException);
        }
    } catch (Exception ex) {
        out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "ExecuteJobServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ex))));
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) 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) LogLevel(org.pentaho.di.core.logging.LogLevel) ServletException(javax.servlet.ServletException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) Repository(org.pentaho.di.repository.Repository) Job(org.pentaho.di.job.Job) JobConfiguration(org.pentaho.di.job.JobConfiguration) PrintWriter(java.io.PrintWriter)

Example 9 with JobConfiguration

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

the class StartJobServlet method doGet.

/**
 *  <div id="mindtouch">
 *      <h1>/kettle/startJob</h1>
 *      <a name="GET"></a>
 *      <h2>GET</h2>
 *      <p>Starts the job. If the job cannot be started, an error is returned.</p>
 *
 *      <p><b>Example Request:</b><br />
 *      <pre function="syntax.xml">
 *      GET /kettle/startJob/?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 executed.</td>
 *      <td>query</td>
 *      </tr>
 *      <tr>
 *      <td>xml</td>
 *      <td>Boolean flag which sets the output format required. Use <code>Y</code> to receive XML response.</td>
 *      <td>boolean, optional</td>
 *      </tr>
 *      <tr>
 *      <td>id</td>
 *      <td>Carte job ID of the job to be executed. This parameter is optional when xml=Y is used.</td>
 *      <td>query, optional</td>
 *      </tr>
 *      </tbody>
 *      </table>
 *
 *    <h3>Response Body</h3>
 *
 *    <table class="pentaho-table">
 *      <tbody>
 *        <tr>
 *          <td align="right">text:</td>
 *          <td>HTML</td>
 *        </tr>
 *        <tr>
 *          <td align="right">media types:</td>
 *          <td>text/xml, text/html</td>
 *        </tr>
 *      </tbody>
 *    </table>
 *      <p>Response XML or HTML containing operation result. When using xml=Y <code>result</code> field indicates whether
 *    operation was successful (<code>OK</code>) or not (<code>ERROR</code>).</p>
 *
 *      <p><b>Example Response:</b></p>
 *    <pre function="syntax.xml">
 *    <?xml version="1.0" encoding="UTF-8"?>
 *    <webresult>
 *      <result>OK</result>
 *      <message>Job &#x5b;dummy_job&#x5d; was started.</message>
 *      <id>abd61143-8174-4f27-9037-6b22fbd3e229</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.</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, "StartJobServlet.Log.StartJobRequested"));
    }
    String jobName = request.getParameter("name");
    String id = request.getParameter("id");
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));
    response.setStatus(HttpServletResponse.SC_OK);
    PrintWriter out = response.getWriter();
    if (useXML) {
        response.setContentType("text/xml");
        response.setCharacterEncoding(Const.XML_ENCODING);
        out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));
    } else {
        response.setContentType("text/html;charset=UTF-8");
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>Start job</TITLE>");
        out.println("<META http-equiv=\"Refresh\" content=\"2;url=" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(jobName, "UTF-8") + "\">");
        out.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        out.println("</HEAD>");
        out.println("<BODY>");
    }
    try {
        // 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 {
            // Take the ID into account!
            // 
            entry = new CarteObjectEntry(jobName, id);
            job = getJobMap().getJob(entry);
        }
        if (job != null) {
            // 
            if (job.isInitialized() && !job.isActive()) {
                // 
                if (job.getRep() != null && !job.getRep().isConnected()) {
                    if (job.getRep().getUserInfo() != null) {
                        job.getRep().connect(job.getRep().getUserInfo().getLogin(), job.getRep().getUserInfo().getPassword());
                    } else {
                        job.getRep().connect(null, null);
                    }
                }
                cache.remove(job.getLogChannelId());
                // 
                synchronized (this) {
                    JobConfiguration jobConfiguration = getJobMap().getConfiguration(jobName);
                    String carteObjectId = UUID.randomUUID().toString();
                    SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.CARTE, null);
                    servletLoggingObject.setContainerObjectId(carteObjectId);
                    Job newJob = new Job(job.getRep(), job.getJobMeta(), servletLoggingObject);
                    newJob.setLogLevel(job.getLogLevel());
                    // Discard old log lines from the old job
                    // 
                    KettleLogStore.discardLines(job.getLogChannelId(), true);
                    getJobMap().replaceJob(entry, newJob, jobConfiguration);
                    job = newJob;
                }
            }
            runJob(job);
            String message = BaseMessages.getString(PKG, "StartJobServlet.Log.JobStarted", jobName);
            if (useXML) {
                out.println(new WebResult(WebResult.STRING_OK, message, id).getXML());
            } else {
                out.println("<H1>" + Encode.forHtml(message) + "</H1>");
                out.println("<a href=\"" + convertContextPath(GetJobStatusServlet.CONTEXT_PATH) + "?name=" + URLEncoder.encode(jobName, "UTF-8") + "&id=" + URLEncoder.encode(id, "UTF-8") + "\">" + BaseMessages.getString(PKG, "JobStatusServlet.BackToJobStatusPage") + "</a><p>");
            }
        } else {
            String message = BaseMessages.getString(PKG, "StartJobServlet.Log.SpecifiedJobNotFound", jobName);
            if (useXML) {
                out.println(new WebResult(WebResult.STRING_ERROR, message));
            } else {
                out.println("<H1>" + Encode.forHtml(message) + "</H1>");
                out.println("<a href=\"" + convertContextPath(GetStatusServlet.CONTEXT_PATH) + "\">" + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage") + "</a><p>");
            }
        }
    } catch (Exception ex) {
        if (useXML) {
            out.println(new WebResult(WebResult.STRING_ERROR, BaseMessages.getString(PKG, "StartJobServlet.Error.UnexpectedError", Const.CR + Const.getStackTracker(ex))));
        } else {
            out.println("<p>");
            out.println("<pre>");
            out.println(Encode.forHtml(Const.getStackTracker(ex)));
            out.println("</pre>");
        }
    }
    if (!useXML) {
        out.println("<p>");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}
Also used : Job(org.pentaho.di.job.Job) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) JobConfiguration(org.pentaho.di.job.JobConfiguration) ServletException(javax.servlet.ServletException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 10 with JobConfiguration

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

the class JobResource method addJob.

@PUT
@Path("/add")
@Produces({ MediaType.APPLICATION_JSON })
public JobStatus addJob(String xml) {
    // Parse the XML, create a job configuration
    // 
    // System.out.println(xml);
    // 
    JobConfiguration jobConfiguration;
    try {
        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(getClass().getName(), 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();
        job.setSocketRepository(CarteSingleton.getInstance().getSocketRepository());
        CarteSingleton.getInstance().getJobMap().addJob(job.getJobname(), carteObjectId, job, jobConfiguration);
        // 
        if (repository != null) {
            job.addJobListener(new JobAdapter() {

                public void jobFinished(Job job) {
                    repository.disconnect();
                }
            });
        }
        return getJobStatus(carteObjectId);
    } catch (KettleException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) JobMeta(org.pentaho.di.job.JobMeta) Repository(org.pentaho.di.repository.Repository) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) Job(org.pentaho.di.job.Job) JobExecutionConfiguration(org.pentaho.di.job.JobExecutionConfiguration) JobAdapter(org.pentaho.di.job.JobAdapter) JobConfiguration(org.pentaho.di.job.JobConfiguration) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Aggregations

JobConfiguration (org.pentaho.di.job.JobConfiguration)11 Job (org.pentaho.di.job.Job)9 SimpleLoggingObject (org.pentaho.di.core.logging.SimpleLoggingObject)7 JobExecutionConfiguration (org.pentaho.di.job.JobExecutionConfiguration)7 KettleException (org.pentaho.di.core.exception.KettleException)6 JobMeta (org.pentaho.di.job.JobMeta)6 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 ServletException (javax.servlet.ServletException)5 JobAdapter (org.pentaho.di.job.JobAdapter)4 Repository (org.pentaho.di.repository.Repository)4 OutputStream (java.io.OutputStream)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 FileObject (org.apache.commons.vfs2.FileObject)2 LogLevel (org.pentaho.di.core.logging.LogLevel)2 JobEntryCopy (org.pentaho.di.job.entry.JobEntryCopy)2 Trans (org.pentaho.di.trans.Trans)2 TransConfiguration (org.pentaho.di.trans.TransConfiguration)2 TransExecutionConfiguration (org.pentaho.di.trans.TransExecutionConfiguration)2