    001package org.apache.turbine.services.schedule;
    002
    003
    004/*
    005 * Licensed to the Apache Software Foundation (ASF) under one
    006 * or more contributor license agreements.  See the NOTICE file
    007 * distributed with this work for additional information
    008 * regarding copyright ownership.  The ASF licenses this file
    009 * to you under the Apache License, Version 2.0 (the
    010 * "License"); you may not use this file except in compliance
    011 * with the License.  You may obtain a copy of the License at
    012 *
    013 *   http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing,
    016 * software distributed under the License is distributed on an
    017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    018 * KIND, either express or implied.  See the License for the
    019 * specific language governing permissions and limitations
    020 * under the License.
    021 */
    022
    023
    024import java.util.List;
    025
    026import org.apache.turbine.services.TurbineServices;
    027import org.apache.turbine.util.TurbineException;
    028
    029/**
    030 * This is a facade class to provide easy access to the Scheduler
    031 * service.  All access methods are static and act upon the current
    032 * instance of the scheduler service.
    033 *
    034 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    035 * @version $Id: TurbineScheduler.java 1692656 2015-07-25 21:26:13Z tv $
    036 * @see org.apache.turbine.services.schedule.ScheduleService
    037 */
    038public abstract class TurbineScheduler
    039{
    040    /**
    041     * Factory method for a new Job
    042     *
    043     * Schedule a job to run on a certain point of time.<br>
    044     *
    045     * Example 1: Run the DefaultScheduledJob at 8:00am every 15th of
    046     * the month - <br>
    047     *
    048     * JobEntry je = newJob(0,0,8,-1,15,"DefaultScheduledJob");<br>
    049     *
    050     * Example 2: Run the DefaultScheduledJob at 8:00am every day -
    051     * <br>
    052     *
    053     * JobEntry je = newJob(0,0,8,-1,-1,"DefaultScheduledJob");<br>
    054     *
    055     * Example 3: Run the DefaultScheduledJob every 2 hours. - <br>
    056     *
    057     * JobEntry je = newJob(0,120,-1,-1,-1,"DefaultScheduledJob");<br>
    058     *
    059     * Example 4: Run the DefaultScheduledJob every 30 seconds. - <br>
    060     *
    061     * JobEntry je = newJob(30,-1,-1,-1,-1,"DefaultScheduledJob");<br>
    062     *
    063     * @param sec Value for entry "seconds".
    064     * @param min Value for entry "minutes".
    065     * @param hour Value for entry "hours".
    066     * @param wd Value for entry "week days".
    067     * @param day_mo Value for entry "month days".
    068     * @param task Task to execute.
    069     *
    070     * @return A JobEntry.
    071     * @exception TurbineException could not create job
    072     */
    073    public static JobEntry newJob(int sec,
    074            int min,
    075            int hour,
    076            int wd,
    077            int day_mo,
    078            String task) throws TurbineException
    079    {
    080        return getService().newJob(sec, min, hour, wd, day_mo, task);
    081    }
    082
    083    /**
    084     * Get a specific Job from Storage.
    085     *
    086     * @param oid The int id for the job.
    087     * @return A JobEntry.
    088     * @exception TurbineException job could not be retrieved
    089     */
    090    public static JobEntry getJob(int oid)
    091            throws TurbineException
    092    {
    093        return getService().getJob(oid);
    094    }
    095
    096    /**
    097     * Add a new job to the queue.
    098     *
    099     * @param je A JobEntry with the job to add.
    100     * @exception TurbineException job could not be added
    101     */
    102    public static void addJob(JobEntry je)
    103            throws TurbineException
    104    {
    105        getService().addJob(je);
    106    }
    107
    108    /**
    109     * Add or update a job
    110     *
    111     * @param je A JobEntry with the job to modify
    112     * @exception TurbineException job could not be updated
    113     */
    114    public static void updateJob(JobEntry je)
    115            throws TurbineException
    116    {
    117        getService().updateJob(je);
    118    }
    119
    120    /**
    121     * Remove a job from the queue.
    122     *
    123     * @param je A JobEntry with the job to remove.
    124     * @exception TurbineException job could not be removed
    125     */
    126    public static void removeJob(JobEntry je)
    127            throws TurbineException
    128    {
    129        getService().removeJob(je);
    130    }
    131
    132    /**
    133     * List jobs in the queue.  This is used by the scheduler UI.
    134     *
    135     * @return A Vector of jobs.
    136     */
    137    public static List<? extends JobEntry> listJobs()
    138    {
    139        return getService().listJobs();
    140    }
    141
    142    /**
    143     * Determines if the scheduler service is currently active.
    144     *
    145     * @return Status of the scheduler service.
    146     */
    147    public static boolean isEnabled()
    148    {
    149        return getService().isEnabled();
    150    }
    151
    152    /**
    153     * Starts the scheduler if not already running.
    154     */
    155    public static void startScheduler()
    156    {
    157        getService().startScheduler();
    158    }
    159
    160    /**
    161     * Stops the scheduler if ti is currently running.
    162     */
    163    public static void stopScheduler()
    164    {
    165        getService().stopScheduler();
    166    }
    167
    168    /**
    169     * Utility method for accessing the service
    170     * implementation
    171     *
    172     * @return a ScheduleService implementation instance
    173     */
    174    private static ScheduleService getService()
    175    {
    176        return (ScheduleService) TurbineServices
    177                .getInstance().getService(ScheduleService.SERVICE_NAME);
    178    }
    179
    180}