Search in sources :

Example 1 with SchedulerOutputPathResolver

use of org.pentaho.platform.web.http.api.resources.SchedulerOutputPathResolver 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 2 with SchedulerOutputPathResolver

use of org.pentaho.platform.web.http.api.resources.SchedulerOutputPathResolver in project pentaho-platform by pentaho.

the class SchedulerServiceTest method testCreateJobException.

@Test
public void testCreateJobException() throws Exception {
    List<JobScheduleParam> jobParameters = new ArrayList<>();
    JobScheduleParam jobScheduleParam1 = mock(JobScheduleParam.class);
    doReturn("name1").when(jobScheduleParam1).getName();
    doReturn("value1").when(jobScheduleParam1).getValue();
    jobParameters.add(jobScheduleParam1);
    Job job = mock(Job.class);
    JobScheduleRequest scheduleRequest = mock(JobScheduleRequest.class);
    doReturn("className").when(scheduleRequest).getActionClass();
    doReturn("jobName").when(scheduleRequest).getJobName();
    doReturn(jobParameters).when(scheduleRequest).getJobParameters();
    doNothing().when(scheduleRequest).setJobName(anyString());
    doReturn(true).when(schedulerService).isPdiFile(any(RepositoryFile.class));
    SchedulerOutputPathResolver schedulerOutputPathResolver = mock(SchedulerOutputPathResolver.class);
    doReturn("outputFile").when(schedulerOutputPathResolver).resolveOutputFilePath();
    SimpleJobTrigger simpleJobTrigger = mock(SimpleJobTrigger.class);
    ComplexJobTriggerProxy complexJobTriggerProxy = mock(ComplexJobTriggerProxy.class);
    CronJobTrigger cronJobTrigger = mock(CronJobTrigger.class);
    RepositoryFile repositoryFile = mock(RepositoryFile.class);
    doReturn("file.ext").when(repositoryFile).getName();
    Map<String, Serializable> metadata = mock(Map.class);
    doReturn(metadata).when(schedulerService.repository).getFileMetadata(anyString());
    doReturn(true).when(metadata).containsKey(RepositoryFile.SCHEDULABLE_KEY);
    doReturn("True").when(metadata).get(RepositoryFile.SCHEDULABLE_KEY);
    doReturn(simpleJobTrigger).when(scheduleRequest).getSimpleJobTrigger();
    doReturn(complexJobTriggerProxy).when(scheduleRequest).getComplexJobTrigger();
    doReturn(cronJobTrigger).when(scheduleRequest).getCronJobTrigger();
    doReturn(false).when(schedulerService.policy).isAllowed(SchedulerAction.NAME);
    doReturn("file.ext").when(scheduleRequest).getInputFile();
    doReturn(repositoryFile).when(schedulerService.repository).getFile(anyString());
    doReturn("ext.backgroundExecution").when(schedulerService).resolveActionId(anyString());
    doReturn(true).when(schedulerService).getAutoCreateUniqueFilename(any(JobScheduleRequest.class));
    doReturn(job).when(schedulerService.scheduler).createJob(anyString(), anyString(), any(Map.class), any(IJobTrigger.class), any(IBackgroundExecutionStreamProvider.class));
    doReturn(Class.class).when(schedulerService).getAction(anyString());
    doReturn(job).when(schedulerService.scheduler).createJob(anyString(), any(Class.class), any(Map.class), any(IJobTrigger.class));
    // Test 1
    try {
        schedulerService.createJob(scheduleRequest);
        fail();
    } catch (SecurityException e) {
    // Should catch it
    }
    // Test 2
    doReturn(true).when(schedulerService.policy).isAllowed(SchedulerAction.NAME);
    doReturn("false").when(metadata).get(RepositoryFile.SCHEDULABLE_KEY);
    try {
        schedulerService.createJob(scheduleRequest);
        fail();
    } catch (IllegalAccessException e) {
    // Should catch it
    }
    // Test 3
    doReturn("").when(scheduleRequest).getInputFile();
    doThrow(new ClassNotFoundException()).when(schedulerService).getAction(anyString());
    try {
        schedulerService.createJob(scheduleRequest);
        fail();
    } catch (RuntimeException e) {
    // Should catch it
    }
    verify(scheduleRequest, times(7)).getSimpleJobTrigger();
    verify(scheduleRequest, times(3)).getInputFile();
    verify(schedulerService.policy, times(3)).isAllowed(SchedulerAction.NAME);
    verify(schedulerService.repository, times(1)).getFile(anyString());
    verify(scheduleRequest, times(1)).getJobName();
    verify(scheduleRequest, times(2)).setJobName(anyString());
    verify(scheduleRequest, times(7)).getActionClass();
    verify(schedulerService.repository, times(1)).getFileMetadata(anyString());
    verify(schedulerService, times(1)).isPdiFile(any(RepositoryFile.class));
    verify(schedulerService, times(1)).handlePDIScheduling(any(RepositoryFile.class), any(HashMap.class), any(HashMap.class));
    verify(scheduleRequest, times(7)).getActionClass();
    verify(schedulerService).getAction(anyString());
}
Also used : JobScheduleParam(org.pentaho.platform.web.http.api.resources.JobScheduleParam) IBackgroundExecutionStreamProvider(org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider) Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) SchedulerOutputPathResolver(org.pentaho.platform.web.http.api.resources.SchedulerOutputPathResolver) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) ComplexJobTriggerProxy(org.pentaho.platform.web.http.api.resources.ComplexJobTriggerProxy) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Job(org.pentaho.platform.api.scheduler2.Job) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) Map(java.util.Map) HashMap(java.util.HashMap) CronJobTrigger(org.pentaho.platform.api.scheduler2.CronJobTrigger) Test(org.junit.Test)

Example 3 with SchedulerOutputPathResolver

use of org.pentaho.platform.web.http.api.resources.SchedulerOutputPathResolver in project pentaho-platform by pentaho.

the class SchedulerServiceTest method testCreateJob.

@Test
public void testCreateJob() throws Exception {
    List<JobScheduleParam> jobParameters = new ArrayList<>();
    JobScheduleParam jobScheduleParam1 = mock(JobScheduleParam.class);
    doReturn("name1").when(jobScheduleParam1).getName();
    doReturn("value1").when(jobScheduleParam1).getValue();
    jobParameters.add(jobScheduleParam1);
    Job job = mock(Job.class);
    JobScheduleRequest scheduleRequest = mock(JobScheduleRequest.class);
    doReturn("className").when(scheduleRequest).getActionClass();
    doReturn("jobName").when(scheduleRequest).getJobName();
    doReturn(jobParameters).when(scheduleRequest).getJobParameters();
    doNothing().when(scheduleRequest).setJobName(anyString());
    doReturn("timezone").when(scheduleRequest).getTimeZone();
    doNothing().when(schedulerService).updateStartDateForTimeZone(scheduleRequest);
    doReturn(true).when(schedulerService).isPdiFile(any(RepositoryFile.class));
    SchedulerOutputPathResolver schedulerOutputPathResolver = mock(SchedulerOutputPathResolver.class);
    doReturn("outputFile").when(schedulerOutputPathResolver).resolveOutputFilePath();
    SimpleJobTrigger simpleJobTrigger = mock(SimpleJobTrigger.class);
    ComplexJobTriggerProxy complexJobTriggerProxy = mock(ComplexJobTriggerProxy.class);
    CronJobTrigger cronJobTrigger = mock(CronJobTrigger.class);
    RepositoryFile repositoryFile = mock(RepositoryFile.class);
    doReturn("file.ext").when(repositoryFile).getName();
    Map<String, Serializable> metadata = mock(Map.class);
    doReturn(metadata).when(schedulerService.repository).getFileMetadata(anyString());
    doReturn(true).when(metadata).containsKey(RepositoryFile.SCHEDULABLE_KEY);
    doReturn("true").when(metadata).get(RepositoryFile.SCHEDULABLE_KEY);
    doReturn(simpleJobTrigger).when(scheduleRequest).getSimpleJobTrigger();
    doReturn(complexJobTriggerProxy).when(scheduleRequest).getComplexJobTrigger();
    doReturn(cronJobTrigger).when(scheduleRequest).getCronJobTrigger();
    doReturn(true).when(schedulerService.policy).isAllowed(SchedulerAction.NAME);
    doReturn("file.ext").when(scheduleRequest).getInputFile();
    doReturn(repositoryFile).when(schedulerService.repository).getFile(anyString());
    doReturn("ext.backgroundExecution").when(schedulerService).resolveActionId(anyString());
    doReturn(true).when(schedulerService).getAutoCreateUniqueFilename(any(JobScheduleRequest.class));
    doReturn(job).when(schedulerService.scheduler).createJob(anyString(), anyString(), any(Map.class), any(IJobTrigger.class), any(IBackgroundExecutionStreamProvider.class));
    doReturn(Class.class).when(schedulerService).getAction(anyString());
    doReturn(job).when(schedulerService.scheduler).createJob(anyString(), any(Class.class), any(Map.class), any(IJobTrigger.class));
    // Test 1
    schedulerService.createJob(scheduleRequest);
    // Test 2
    doReturn("").when(scheduleRequest).getJobName();
    schedulerService.createJob(scheduleRequest);
    // Test 3
    doReturn("").when(scheduleRequest).getInputFile();
    doReturn("").when(scheduleRequest).getActionClass();
    schedulerService.createJob(scheduleRequest);
    verify(scheduleRequest, times(15)).getSimpleJobTrigger();
    verify(scheduleRequest, times(11)).getInputFile();
    verify(schedulerService.policy, times(3)).isAllowed(SchedulerAction.NAME);
    verify(schedulerService.repository, times(2)).getFile(anyString());
    verify(scheduleRequest, times(9)).getJobName();
    verify(scheduleRequest, times(3)).setJobName(anyString());
    verify(scheduleRequest, times(5)).getActionClass();
    verify(schedulerService.repository, times(2)).getFileMetadata(anyString());
    verify(schedulerService, times(3)).isPdiFile(any(RepositoryFile.class));
    verify(schedulerService, times(3)).handlePDIScheduling(any(RepositoryFile.class), any(HashMap.class), any(HashMap.class));
    verify(schedulerService, times(2)).getSchedulerOutputPathResolver(any(JobScheduleRequest.class));
    // verify( schedulerService, times( 2 ) ).resolveActionId( anyString() );
    verify(scheduleRequest, times(5)).getActionClass();
    verify(schedulerService).getAction(anyString());
    verify(schedulerService, times(3)).updateStartDateForTimeZone(scheduleRequest);
    verify(schedulerService.scheduler).createJob(anyString(), any(Class.class), any(Map.class), any(IJobTrigger.class));
}
Also used : JobScheduleParam(org.pentaho.platform.web.http.api.resources.JobScheduleParam) IBackgroundExecutionStreamProvider(org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider) Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) SchedulerOutputPathResolver(org.pentaho.platform.web.http.api.resources.SchedulerOutputPathResolver) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) ComplexJobTriggerProxy(org.pentaho.platform.web.http.api.resources.ComplexJobTriggerProxy) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Job(org.pentaho.platform.api.scheduler2.Job) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) Map(java.util.Map) HashMap(java.util.HashMap) CronJobTrigger(org.pentaho.platform.api.scheduler2.CronJobTrigger) Test(org.junit.Test)

Aggregations

Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)3 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)3 Job (org.pentaho.platform.api.scheduler2.Job)3 JobScheduleParam (org.pentaho.platform.web.http.api.resources.JobScheduleParam)3 SchedulerOutputPathResolver (org.pentaho.platform.web.http.api.resources.SchedulerOutputPathResolver)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 CronJobTrigger (org.pentaho.platform.api.scheduler2.CronJobTrigger)2 IBackgroundExecutionStreamProvider (org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider)2 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)2 ComplexJobTriggerProxy (org.pentaho.platform.web.http.api.resources.ComplexJobTriggerProxy)2 JobScheduleRequest (org.pentaho.platform.web.http.api.resources.JobScheduleRequest)2 IAction (org.pentaho.platform.api.action.IAction)1 ServiceException (org.pentaho.platform.api.engine.ServiceException)1 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)1 SchedulerException (org.pentaho.platform.api.scheduler2.SchedulerException)1