    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.List;
    023
    024import org.apache.torque.TorqueException;
    025import org.apache.torque.criteria.Criteria;
    026import org.apache.turbine.util.TurbineException;
    027
    028/**
    029 * Service for a cron like scheduler.
    030 *
    031 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    032 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    033 * @version $Id: TorqueSchedulerService.java 534527 2007-05-02 16:10:59Z tv $
    034 *
    035 * @deprecated Use {@link QuartzSchedulerService} instead
    036 */
    037@Deprecated
    038public class TorqueSchedulerService extends AbstractSchedulerService
    039{
    040    /**
    041     * Load all jobs from configuration storage
    042     *
    043     * @return the list of pre-configured jobs
    044     * @throws TurbineException
    045     */
    046    @Override
    047    protected List<? extends JobEntry> loadJobs() throws TurbineException
    048    {
    049        // Load all from cold storage.
    050        try
    051        {
    052            List<JobEntryTorque> jobsTorque = JobEntryTorquePeer.doSelect(new Criteria());
    053
    054            for (JobEntryTorque job : jobsTorque)
    055            {
    056                job.calcRunTime();
    057            }
    058
    059            return jobsTorque;
    060        }
    061        catch (TorqueException e)
    062        {
    063            throw new TurbineException("Error retrieving initial job list from persistent storage.", e);
    064        }
    065    }
    066
    067    /**
    068     * @see org.apache.turbine.services.schedule.ScheduleService#newJob(int, int, int, int, int, java.lang.String)
    069     */
    070    @Override
    071    public JobEntry newJob(int sec, int min, int hour, int wd, int day_mo, String task) throws TurbineException
    072    {
    073        JobEntryTorque jet = new JobEntryTorque();
    074        jet.setSecond(sec);
    075        jet.setMinute(min);
    076        jet.setHour(hour);
    077        jet.setWeekDay(wd);
    078        jet.setDayOfMonth(day_mo);
    079        jet.setTask(task);
    080
    081        return jet;
    082    }
    083
    084    /**
    085     * Get a specific Job from Storage.
    086     *
    087     * @param oid
    088     *            The int id for the job.
    089     * @return A JobEntry.
    090     * @throws TurbineException
    091     *                job could not be retrieved.
    092     */
    093    @Override
    094    public JobEntry getJob(int oid) throws TurbineException
    095    {
    096        try
    097        {
    098            JobEntryTorque je = JobEntryTorquePeer.retrieveByPK(oid);
    099            return scheduleQueue.getJob(je);
    100        }
    101        catch (TorqueException e)
    102        {
    103            throw new TurbineException("Error retrieving job from persistent storage.", e);
    104        }
    105    }
    106
    107    /**
    108     * Remove a job from the queue.
    109     *
    110     * @param je
    111     *            A JobEntry with the job to remove.
    112     * @throws TurbineException
    113     *                job could not be removed
    114     */
    115    @Override
    116    public void removeJob(JobEntry je) throws TurbineException
    117    {
    118        try
    119        {
    120            // First remove from DB.
    121            Criteria c = new Criteria().where(JobEntryTorquePeer.JOB_ID, Integer.valueOf(je.getJobId()));
    122            JobEntryTorquePeer.doDelete(c);
    123
    124            // Remove from the queue.
    125            scheduleQueue.remove(je);
    126
    127            // restart the scheduler
    128            restart();
    129        }
    130        catch (TorqueException e)
    131        {
    132            throw new TurbineException("Problem removing Scheduled Job: " + je.getTask(), e);
    133        }
    134    }
    135
    136    /**
    137     * Add or update a job.
    138     *
    139     * @param je
    140     *            A JobEntry with the job to modify
    141     * @throws TurbineException
    142     *             job could not be updated
    143     */
    144    @Override
    145    public void updateJob(JobEntry je) throws TurbineException
    146    {
    147        try
    148        {
    149            je.calcRunTime();
    150
    151            // Update the queue.
    152            if (je.isNew())
    153            {
    154                scheduleQueue.add(je);
    155            }
    156            else
    157            {
    158                scheduleQueue.modify(je);
    159            }
    160
    161            if (je instanceof JobEntryTorque)
    162            {
    163                ((JobEntryTorque)je).save();
    164            }
    165
    166            restart();
    167        }
    168        catch (TorqueException e)
    169        {
    170            throw new TurbineException("Problem persisting Scheduled Job: " + je.getTask(), e);
    171        }
    172        catch (TurbineException e)
    173        {
    174            throw new TurbineException("Problem updating Scheduled Job: " + je.getTask(), e);
    175        }
    176    }
    177}