KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > scheduler > AbstractScheduleProvider


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.varia.scheduler;
23
24 import java.util.Date JavaDoc;
25
26 import javax.management.InstanceNotFoundException JavaDoc;
27 import javax.management.JMException JavaDoc;
28 import javax.management.MBeanException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.ReflectionException JavaDoc;
31 import javax.management.MBeanServerInvocationHandler JavaDoc;
32
33 import org.jboss.system.ServiceMBeanSupport;
34
35 /**
36  * Abstract Base Class for Schedule Providers.
37  *
38  * The class used to extend HASingletonSupport, but not anymore.
39  * We can achieve the same effect without a dependency on HA jars
40  * using just a depends clause that starts/stops the schedule provide
41  * using the notifications produced by another HASingleton (see JBAS-3082)
42  *
43  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
44  * @author <a HREF="mailto:ivelin@apache.org">Ivelin Ivanov</a>
45  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
46  * @version $Revision: 58158 $
47  */

48 public abstract class AbstractScheduleProvider extends ServiceMBeanSupport
49    implements AbstractScheduleProviderMBean
50 {
51    /** The schedule manager ObjectName */
52    private ObjectName JavaDoc scheduleManagerName = ScheduleManagerMBean.OBJECT_NAME;
53    private ScheduleManagerMBean manager;
54
55    // ------------------------------------------------------------------------
56
// Constructors
57
//-------------------------------------------------------------------------
58

59    /**
60     * Default (no-args) Constructor
61     */

62    public AbstractScheduleProvider()
63    {
64    }
65
66    // -------------------------------------------------------------------------
67
// SchedulerMBean Attributes
68
// -------------------------------------------------------------------------
69

70    /**
71     * Get the Schedule Manager Name
72     *
73     * @jmx:managed-operation
74     */

75    public ObjectName JavaDoc getScheduleManagerName()
76    {
77       return scheduleManagerName;
78    }
79
80    /**
81     * Set the Schedule Manager Name
82     *
83     * @jmx:managed-operation
84     */

85    public void setScheduleManagerName(ObjectName JavaDoc scheduleManagerName)
86    {
87       this.scheduleManagerName = scheduleManagerName;
88    }
89
90    // -------------------------------------------------------------------------
91
// ScheduleMBean Operations - Override
92
// -------------------------------------------------------------------------
93

94    /**
95     * Add the Schedules to the Schedule Manager
96     *
97     * @jmx:managed-operation
98     */

99    public abstract void startProviding() throws Exception JavaDoc;
100
101    /**
102     * Stops the Provider from providing and
103     * causing him to remove all Schedules
104     *
105     * @jmx:managed-operation
106     */

107    public abstract void stopProviding();
108
109    // -------------------------------------------------------------------------
110
// Helper Methods
111
// -------------------------------------------------------------------------
112

113    /**
114     * Add a single Schedule to the Schedule Manager
115     *
116     * @param pTarget Object Name of the target MBean (receiver
117     * of the time notification)
118     * @param pMethodName Name of the Method to be called on the
119     * target
120     * @param pMethodSignature Signature of the Method
121     * @param pStart Date when the Schedule has to start
122     * @param pPeriod Time between two notifications
123     * @param pRepetitions Number of repetitions (-1 for unlimited)
124     *
125     * @return Identification of the Schedule which is used
126     * to remove it later
127     */

128    protected int addSchedule(
129          ObjectName JavaDoc pTarget, String JavaDoc pMethodName, String JavaDoc[] pMethodSignature,
130          Date JavaDoc pStart, long pPeriod, int pRepetitions) throws JMException JavaDoc
131    {
132       return manager.addSchedule(serviceName,
133                         pTarget,
134                         pMethodName,
135                         pMethodSignature,
136                         pStart,
137                         pPeriod,
138                         pRepetitions);
139    }
140
141    /**
142     * Remove a Schedule from the Schedule Manager
143     *
144     * @param pID Identification of the Schedule
145     */

146    protected void removeSchedule(int pID) throws JMException JavaDoc
147    {
148       manager.removeSchedule(pID);
149    }
150
151    // -------------------------------------------------------------------------
152
// ServiceMBean Overrides and Lifecycle Methods
153
// -------------------------------------------------------------------------
154

155    /**
156     * When the Service is started it will register itself at the
157     * Schedule Manager which makes it necessary that the Schedule Manager
158     * is already running.
159     * This allows the Schedule Manager to call {@link #startProviding
160     * startProviding()} which is the point for the Provider to add
161     * the Schedules on the Schedule Manager.
162     * ATTENTION: If you overwrite this method in a subclass you have
163     * to call this method (super.startService())
164     */

165    protected void startService() throws Exception JavaDoc
166    {
167       this.manager = (ScheduleManagerMBean)MBeanServerInvocationHandler.newProxyInstance(
168             getServer(), scheduleManagerName, ScheduleManagerMBean.class, false);
169       startScheduleProviderService();
170    }
171
172    /**
173     * When the Service is stopped it will unregister itself at the
174     * Schedule Manager.
175     * This allows the Schedule Manager to remove the Provider from its
176     * list and then call {@link #stopProviding stopProviding()} which
177     * is the point for the Provider to remove the Schedules from the
178     * Schedule Manager.
179     * ATTENTION: If you overwrite this method in a subclass you have
180     * to call this method (super.stopService())
181     */

182    protected void stopService() throws Exception JavaDoc
183    {
184       stopScheduleProviderService();
185    }
186    
187    /**
188     * Registers this schedule provider to the schedule manager
189     */

190    protected void startScheduleProviderService()
191       throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
192    {
193       manager.registerProvider(serviceName.toString());
194    }
195
196    /**
197     * Unregisters this schedule provider to the schedule manager
198     */

199    protected void stopScheduleProviderService()
200       throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
201    {
202       manager.unregisterProvider(serviceName.toString());
203    }
204    
205 }
206
Popular Tags