Search in sources :

Example 6 with IAction

use of org.pentaho.platform.api.action.IAction in project pentaho-platform by pentaho.

the class ActionDelegateTest method execute.

@SuppressWarnings("unchecked")
private void execute(String actionSequenceFile, boolean exceptionOnError, IAction... actions) throws ActionSequenceException {
    TestPluginManager pm = (TestPluginManager) PentahoSystem.get(IPluginManager.class);
    for (IAction action : actions) {
        pm.addAction(action);
    }
    // content outputs will write to this stream
    out = new ByteArrayOutputStream();
    // create SimpleOutputHandler (to handle outputs of type "response.content")
    outputHandler = new SimpleOutputHandler(out, false);
    outputHandler.setOutputPreference(IOutputHandler.OUTPUT_TYPE_DEFAULT);
    IPentahoSession session = new StandaloneSession("system");
    ISolutionEngine solutionEngine = ServiceTestHelper.getSolutionEngine();
    outputHandler.setSession(session);
    String xactionStr = ServiceTestHelper.getXAction("src/test/resources/solution/test/ActionDelegateTest", actionSequenceFile);
    // execute the action sequence, providing the outputHandler created above
    IRuntimeContext rc = solutionEngine.execute(xactionStr, actionSequenceFile, "action sequence to test the TestAction", false, true, null, false, new HashMap(), outputHandler, null, new SimpleUrlFactory(""), new ArrayList());
    int status = rc.getStatus();
    if (status == IRuntimeContext.PARAMETERS_FAIL || status == IRuntimeContext.RUNTIME_CONTEXT_RESOLVE_FAIL || status == IRuntimeContext.RUNTIME_STATUS_FAILURE || status == IRuntimeContext.RUNTIME_STATUS_INITIALIZE_FAIL || status == IRuntimeContext.RUNTIME_STATUS_SETUP_FAIL) {
        throw new ActionSequenceException("Action sequence failed!");
    }
}
Also used : ActionSequenceException(org.pentaho.platform.api.engine.ActionSequenceException) ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IAction(org.pentaho.platform.api.action.IAction) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) HashMap(java.util.HashMap) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IPluginManager(org.pentaho.platform.api.engine.IPluginManager) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext)

Example 7 with IAction

use of org.pentaho.platform.api.action.IAction in project pentaho-platform by pentaho.

the class SchedulerService method createJob.

public Job createJob(JobScheduleRequest scheduleRequest) throws IOException, SchedulerException, IllegalAccessException {
    // Used to determine if created by a RunInBackgroundCommand
    boolean runInBackground = scheduleRequest.getSimpleJobTrigger() == null && scheduleRequest.getComplexJobTrigger() == null && scheduleRequest.getCronJobTrigger() == null;
    if (!runInBackground && !getPolicy().isAllowed(SchedulerAction.NAME)) {
        throw new SecurityException();
    }
    boolean hasInputFile = !StringUtils.isEmpty(scheduleRequest.getInputFile());
    RepositoryFile file = null;
    if (hasInputFile) {
        try {
            file = getRepository().getFile(scheduleRequest.getInputFile());
        } catch (UnifiedRepositoryException ure) {
            hasInputFile = false;
            logger.warn(ure.getMessage(), ure);
        }
    }
    // if we have an inputfile, generate job name based on that if the name is not passed in
    if (hasInputFile && StringUtils.isEmpty(scheduleRequest.getJobName())) {
        // $NON-NLS-1$
        scheduleRequest.setJobName(file.getName().substring(0, file.getName().lastIndexOf(".")));
    } else if (!StringUtils.isEmpty(scheduleRequest.getActionClass())) {
        String actionClass = scheduleRequest.getActionClass().substring(scheduleRequest.getActionClass().lastIndexOf(".") + 1);
        // $NON-NLS-1$
        scheduleRequest.setJobName(actionClass);
    } else if (!hasInputFile && StringUtils.isEmpty(scheduleRequest.getJobName())) {
        // just make up a name
        // $NON-NLS-1$
        scheduleRequest.setJobName("" + System.currentTimeMillis());
    }
    if (hasInputFile) {
        if (file == null) {
            logger.error("Cannot find input source file " + scheduleRequest.getInputFile() + " Aborting schedule...");
            throw new SchedulerException(new ServiceException("Cannot find input source file " + scheduleRequest.getInputFile()));
        }
        Map<String, Serializable> metadata = getRepository().getFileMetadata(file.getId());
        if (metadata.containsKey(RepositoryFile.SCHEDULABLE_KEY)) {
            boolean schedulable = BooleanUtils.toBoolean((String) metadata.get(RepositoryFile.SCHEDULABLE_KEY));
            if (!schedulable) {
                throw new IllegalAccessException();
            }
        }
    }
    if (scheduleRequest.getTimeZone() != null) {
        updateStartDateForTimeZone(scheduleRequest);
    }
    Job job = null;
    IJobTrigger jobTrigger = SchedulerResourceUtil.convertScheduleRequestToJobTrigger(scheduleRequest, scheduler);
    HashMap<String, Serializable> parameterMap = new HashMap<>();
    for (JobScheduleParam param : scheduleRequest.getJobParameters()) {
        parameterMap.put(param.getName(), param.getValue());
    }
    if (isPdiFile(file)) {
        parameterMap = handlePDIScheduling(file, parameterMap, scheduleRequest.getPdiParameters());
    }
    parameterMap.put(LocaleHelper.USER_LOCALE_PARAM, LocaleHelper.getLocale());
    if (scheduleRequest.getUseWorkerNodes() != null && !scheduleRequest.getUseWorkerNodes().trim().isEmpty()) {
        parameterMap.put("useWorkerNodes", scheduleRequest.getUseWorkerNodes().trim());
    }
    if (hasInputFile) {
        SchedulerOutputPathResolver outputPathResolver = getSchedulerOutputPathResolver(scheduleRequest);
        String outputFile = outputPathResolver.resolveOutputFilePath();
        String actionId = SchedulerResourceUtil.resolveActionId(scheduleRequest.getInputFile());
        final String inputFile = scheduleRequest.getInputFile();
        parameterMap.put(ActionUtil.QUARTZ_STREAMPROVIDER_INPUT_FILE, inputFile);
        job = getScheduler().createJob(scheduleRequest.getJobName(), actionId, parameterMap, jobTrigger, new RepositoryFileStreamProvider(inputFile, outputFile, getAutoCreateUniqueFilename(scheduleRequest), getAppendDateFormat(scheduleRequest)));
    } else {
        // need to locate actions from plugins if done this way too (but for now, we're just on main)
        String actionClass = scheduleRequest.getActionClass();
        try {
            @SuppressWarnings("unchecked") Class<IAction> iaction = getAction(actionClass);
            job = getScheduler().createJob(scheduleRequest.getJobName(), iaction, parameterMap, jobTrigger);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    return job;
}
Also used : JobScheduleParam(org.pentaho.platform.web.http.api.resources.JobScheduleParam) Serializable(java.io.Serializable) SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) IAction(org.pentaho.platform.api.action.IAction) HashMap(java.util.HashMap) SchedulerOutputPathResolver(org.pentaho.platform.web.http.api.resources.SchedulerOutputPathResolver) ServiceException(org.pentaho.platform.api.engine.ServiceException) RepositoryFileStreamProvider(org.pentaho.platform.web.http.api.resources.RepositoryFileStreamProvider) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Job(org.pentaho.platform.api.scheduler2.Job)

Example 8 with IAction

use of org.pentaho.platform.api.action.IAction in project pentaho-platform by pentaho.

the class ActionUtil method createActionBean.

/**
 * Returns an instance of {@link IAction} created from the provided parameters.
 *
 * @param actionClassName the name of the class being resolved
 * @param actionId        the is of the action which corresponds to some bean id
 * @return {@link IAction} created from the provided parameters.
 * @throws Exception when the {@link IAction} cannot be created for some reason
 */
public static IAction createActionBean(final String actionClassName, final String actionId, final boolean retryBeanInstantiationIfFailed) throws ActionInvocationException {
    Object actionBean = null;
    Class<?> actionClass = null;
    try {
        actionClass = resolveActionClass(actionClassName, actionId, retryBeanInstantiationIfFailed);
        actionBean = actionClass.newInstance();
    } catch (final Exception e) {
        throw new ActionInvocationException(Messages.getInstance().getErrorString("ActionUtil.ERROR_0002_FAILED_TO_CREATE_ACTION", StringUtils.isEmpty(actionId) ? (actionClass == null ? "?" : actionClass.getName()) : actionId, e));
    }
    if (!(actionBean instanceof IAction)) {
        throw new ActionInvocationException(Messages.getInstance().getErrorString("ActionUtil.ERROR_0003_ACTION_WRONG_TYPE", actionClass.getName(), IAction.class.getName()));
    }
    return (IAction) actionBean;
}
Also used : IAction(org.pentaho.platform.api.action.IAction) ActionInvocationException(org.pentaho.platform.api.action.ActionInvocationException) PluginBeanException(org.pentaho.platform.api.engine.PluginBeanException) ActionInvocationException(org.pentaho.platform.api.action.ActionInvocationException)

Example 9 with IAction

use of org.pentaho.platform.api.action.IAction in project pentaho-platform by pentaho.

the class ActionHarnessTest method testSetValue.

@Test
public void testSetValue() throws Exception {
    IAction action = new TestVarArgsAction();
    ActionHarness harness = new ActionHarness(action);
    harness.setValue("message", "test message");
    Assert.assertEquals("test message", harness.getValue("message"));
}
Also used : IAction(org.pentaho.platform.api.action.IAction) ActionHarness(org.pentaho.platform.util.beans.ActionHarness) Test(org.junit.Test)

Example 10 with IAction

use of org.pentaho.platform.api.action.IAction in project pentaho-platform by pentaho.

the class ActionUtilTest method createActionBeanHappyPath.

@Test
public void createActionBeanHappyPath() throws ActionInvocationException {
    IAction iaction = ActionUtil.createActionBean(MyTestAction.class.getName(), null);
    assertEquals(iaction.getClass(), MyTestAction.class);
}
Also used : IAction(org.pentaho.platform.api.action.IAction) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

IAction (org.pentaho.platform.api.action.IAction)15 HashMap (java.util.HashMap)9 Serializable (java.io.Serializable)8 Test (org.junit.Test)8 IBackgroundExecutionStreamProvider (org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 TestAction (org.pentaho.platform.util.bean.TestAction)3 ArrayList (java.util.ArrayList)2 ActionInvokeStatus (org.pentaho.platform.action.ActionInvokeStatus)2 ActionInvocationException (org.pentaho.platform.api.action.ActionInvocationException)2 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)2 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)2 ActionSequenceAction (org.pentaho.platform.plugin.action.builtin.ActionSequenceAction)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Date (java.util.Date)1 List (java.util.List)1 Element (org.dom4j.Element)1 ExpectedException (org.junit.rules.ExpectedException)1