    001package org.apache.turbine.services.schedule;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022import java.util.ArrayList;
    023import java.util.List;
    024
    025import org.apache.commons.configuration.Configuration;
    026import org.apache.commons.lang.StringUtils;
    027import org.apache.turbine.util.TurbineException;
    028
    029/**
    030 * Service for a cron like scheduler that uses the
    031 * TurbineResources.properties file instead of the database.
    032 * The methods that operate on jobs ( get,add,update,remove )
    033 * only operate on the queue in memory and changes are not reflected
    034 * to the properties file which was used to initialize the jobs.
    035 * An example is given below.  The job names are the class names that
    036 * extend ScheduledJob.
    037 *
    038 * <PRE>
    039 *
    040 * services.SchedulerService.scheduler.jobs=scheduledJobName,scheduledJobName2
    041 *
    042 * services.SchedulerService.scheduler.job.scheduledJobName.ID=1
    043 * services.SchedulerService.scheduler.job.scheduledJobName.SECOND=-1
    044 * services.SchedulerService.scheduler.job.scheduledJobName.MINUTE=-1
    045 * services.SchedulerService.scheduler.job.scheduledJobName.HOUR=7
    046 * services.SchedulerService.scheduler.job.scheduledJobName.WEEKDAY=-1
    047 * services.SchedulerService.scheduler.job.scheduledJobName.DAY_OF_MONTH=-1
    048 *
    049 * services.SchedulerService.scheduler.job.scheduledJobName2.ID=1
    050 * services.SchedulerService.scheduler.job.scheduledJobName2.SECOND=-1
    051 * services.SchedulerService.scheduler.job.scheduledJobName2.MINUTE=-1
    052 * services.SchedulerService.scheduler.job.scheduledJobName2.HOUR=7
    053 * services.SchedulerService.scheduler.job.scheduledJobName2.WEEKDAY=-1
    054 * services.SchedulerService.scheduler.job.scheduledJobName2.DAY_OF_MONTH=-1
    055 *
    056 * </PRE>
    057 *
    058 * Based on TamboraSchedulerService written by John Thorhauer.
    059 *
    060 * @author <a href="mailto:ekkerbj@netscpae.net">Jeff Brekke</a>
    061 * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
    062 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    063 * @version $Id: TurbineNonPersistentSchedulerService.java 534527 2007-05-02 16:10:59Z tv $
    064 *
    065 * @deprecated Use QuartzSchedulerService instead
    066 */
    067@Deprecated
    068public class TurbineNonPersistentSchedulerService extends AbstractSchedulerService
    069{
    070    /**
    071     * @see org.apache.turbine.services.schedule.AbstractSchedulerService#loadJobs()
    072     */
    073    @Override
    074    protected List<? extends JobEntry> loadJobs() throws TurbineException
    075    {
    076        Configuration conf = getConfiguration();
    077        List<Object> jobProps = conf.getList("scheduler.jobs");
    078        List<JobEntry> jobs = new ArrayList<JobEntry>();
    079
    080        // If there are scheduler.jobs defined then set up a job vector
    081        // for the scheduleQueue
    082        if (!jobProps.isEmpty())
    083        {
    084            for (int i = 0; i < jobProps.size(); i++)
    085            {
    086                String jobName = (String)jobProps.get(i);
    087                String jobPrefix = "scheduler.job." + jobName;
    088
    089                String jobId = conf.getString(jobPrefix + ".ID", null);
    090                if (StringUtils.isEmpty(jobId))
    091                {
    092                    throw new TurbineException(
    093                            "There is an error in the TurbineResources.properties file. \n"
    094                            + jobPrefix + ".ID is not found.\n");
    095                }
    096
    097                int sec = conf.getInt(jobPrefix + ".SECOND", -1);
    098                int min = conf.getInt(jobPrefix + ".MINUTE", -1);
    099                int hr = conf.getInt(jobPrefix + ".HOUR", -1);
    100                int wkday = conf.getInt(jobPrefix + ".WEEKDAY", -1);
    101                int dayOfMonth = conf.getInt(jobPrefix + ".DAY_OF_MONTH", -1);
    102
    103                JobEntry je = newJob(
    104                        sec,
    105                        min,
    106                        hr,
    107                        wkday,
    108                        dayOfMonth,
    109                        jobName);
    110                je.setJobId(Integer.parseInt(jobId));
    111                jobs.add(je);
    112            }
    113        }
    114
    115        return jobs;
    116    }
    117
    118    /**
    119     * @see org.apache.turbine.services.schedule.ScheduleService#newJob(int, int, int, int, int, java.lang.String)
    120     */
    121    @Override
    122    public JobEntry newJob(int sec, int min, int hour, int wd, int day_mo, String task) throws TurbineException
    123    {
    124        return new JobEntryNonPersistent(sec, min, hour, wd, day_mo, task);
    125    }
    126
    127    /**
    128     * This method returns the job element from the internal queue.
    129     *
    130     * @param oid The int id for the job.
    131     * @return A JobEntry.
    132     * @throws TurbineException could not retrieve job
    133     */
    134    @Override
    135    public JobEntry getJob(int oid)
    136            throws TurbineException
    137    {
    138        JobEntry je = new JobEntryNonPersistent();
    139        je.setJobId(oid);
    140        return scheduleQueue.getJob(je);
    141    }
    142
    143    /**
    144     * Remove a job from the queue.
    145     *
    146     * @param je A JobEntry with the job to remove.
    147     */
    148    @Override
    149    public void removeJob(JobEntry je)
    150    {
    151        // Remove from the queue.
    152        scheduleQueue.remove(je);
    153        restart();
    154    }
    155
    156    /**
    157     * Add/update a job
    158     *
    159     * @param je A JobEntry with the job to modify
    160     * @throws TurbineException job could not be updated
    161     */
    162    @Override
    163    public void updateJob(JobEntry je)
    164            throws TurbineException
    165    {
    166        try
    167        {
    168            je.calcRunTime();
    169
    170            // Update the queue.
    171            scheduleQueue.modify(je);
    172            restart();
    173        }
    174        catch (Exception e)
    175        {
    176            throw new TurbineException("Problem updating Scheduled Job: " + je.getTask(), e);
    177        }
    178    }
    179}