KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.InvalidParameterException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import javax.management.JMException JavaDoc;
35 import javax.management.MalformedObjectNameException JavaDoc;
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.sql.DataSource JavaDoc;
40
41 /**
42  * This Provider get its Scheduler from a Database and then adds
43  * all the Schedules to the Schedule Manager.
44  * The "SQL Statement" must deliver the following attributes:
45  * Index Content Data Type
46  * ----------------------------------
47  * 1., Target, String
48  * 2., Method_Name, String
49  * 3., Method_Signature, String
50  * 4., Start_Date, String
51  * 5., Period, long
52  * 6., Repetitions, int
53  * 7., Date_Format, String
54  * ATTENTION: The "Target" is Object Name of the target MBean as
55  * String, the "Method_Signature" is a list of attributes separated
56  * by colons which can contain:
57  * <ul>
58  * <li>NOTIFICATION which will be replaced by the timers notification instance
59  * (javax.management.Notification)</li>
60  * <li>DATE which will be replaced by the date of the notification call
61  * (java.util.Date)</li>
62  * <li>REPETITIONS which will be replaced by the number of remaining repetitions
63  * (long)</li>
64  * <li>SCHEDULER_NAME which will be replaced by the Object Name of the Scheduler
65  * (javax.management.ObjectName)</li>
66  * <li>any full qualified Class name which the Scheduler will be set a "null" value
67  * for it</li>
68  * </ul>
69  * The "Period" is an long value greater than 0.
70  * The "Repetitions" can be set to "-1" which means unlimited repetitions.
71  * The "Date_Format" can be null or blank to signify locale usage
72  *
73  * @jmx:mbean name="jboss:service=DBScheduleProvider"
74  * extends="org.jboss.varia.scheduler.AbstractScheduleProviderMBean"
75  *
76  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
77  * @version $Revision: 37459 $
78  */

79 public class DBScheduleProvider
80    extends AbstractScheduleProvider
81    implements DBScheduleProviderMBean
82 {
83
84    // -------------------------------------------------------------------------
85
// Constants
86
// -------------------------------------------------------------------------
87

88    // -------------------------------------------------------------------------
89
// Members
90
// -------------------------------------------------------------------------
91

92    private String JavaDoc mDataSourceName;
93    private String JavaDoc mSQLStatement;
94    
95    /** The ID of the Schedule used later to remove it later **/
96    private ArrayList JavaDoc mIDList = new ArrayList JavaDoc();
97    
98    // -------------------------------------------------------------------------
99
// Constructors
100
// -------------------------------------------------------------------------
101

102    /**
103     * Default (no-args) Constructor
104     **/

105    public DBScheduleProvider()
106    {
107    }
108    
109    // -------------------------------------------------------------------------
110
// SchedulerMBean Methods
111
// -------------------------------------------------------------------------
112

113    /**
114     * @return JNDI name of the Data Source used
115     *
116     * @jmx:managed-operation
117     **/

118    public String JavaDoc getDataSourceName() {
119       return mDataSourceName;
120    }
121    
122    /**
123     * Sets the JNDI name of the Data Source. You have
124     * to ensure that the DataSource is available when
125     * this service is started.
126     *
127     * @jmx:managed-operation
128     **/

129    public void setDataSourceName( String JavaDoc pDataSourceName ) {
130       mDataSourceName = pDataSourceName;
131    }
132    
133    /**
134     * @return SQL Statement used to access the DB
135     *
136     * @jmx:managed-operation
137     **/

138    public String JavaDoc getSQLStatement() {
139       return mSQLStatement;
140    }
141    
142    /**
143     * Sets the SQL Statement used to retrieve the data
144     * from the Database
145     *
146     * @jmx:managed-operation
147     **/

148    public void setSQLStatement( String JavaDoc pSQLStatement ) {
149       mSQLStatement = pSQLStatement;
150    }
151    
152    /**
153     * Add the Schedule to the Schedule Manager
154     *
155     * @jmx:managed-operation
156     **/

157    public void startProviding()
158       throws Exception JavaDoc
159    {
160       Connection JavaDoc lConnection = null;
161       PreparedStatement JavaDoc lStatement = null;
162       try {
163          Object JavaDoc lTemp = new InitialContext JavaDoc().lookup( mDataSourceName );
164          DataSource JavaDoc lDB = (DataSource JavaDoc) lTemp;
165          lConnection = lDB.getConnection();
166          lStatement = lConnection.prepareStatement( mSQLStatement );
167          ResultSet JavaDoc lResult = lStatement.executeQuery();
168          while( lResult.next() ) {
169             int lID = addSchedule(
170                new ObjectName JavaDoc( lResult.getString( 1 ) ),
171                lResult.getString( 2 ),
172                getSignature( lResult.getString( 3 ) ),
173                getStartDate( lResult.getString( 4 ), lResult.getString( 7 ) ),
174                lResult.getLong( 5 ),
175                lResult.getInt( 6 )
176             );
177             mIDList.add( new Integer JavaDoc( lID ) );
178          }
179       }
180       finally {
181          if( lStatement != null ) {
182             try {
183                lStatement.close();
184             }
185             catch( Exception JavaDoc e ) {}
186          }
187          if( lConnection != null ) {
188             try {
189                lConnection.close();
190             }
191             catch( Exception JavaDoc e ) {}
192          }
193       }
194    }
195    
196    /**
197     * Stops the Provider from providing causing
198     * the provider to remove the Schedule
199     *
200     * @jmx:managed-operation
201     */

202    public void stopProviding() {
203       Iterator JavaDoc i = mIDList.iterator();
204       while( i.hasNext() ) {
205          Integer JavaDoc lID = (Integer JavaDoc) i.next();
206          try {
207             removeSchedule( lID.intValue() );
208          }
209          catch( JMException JavaDoc jme ) {
210             log.error( "Could not remove Schedule in stop providing", jme );
211          }
212       }
213    }
214    
215    /**
216     * Converts a string of method arguments (separated by colons) into
217     * an array of string
218     **/

219    protected String JavaDoc[] getSignature( String JavaDoc pMethodSignature )
220    {
221       if( pMethodSignature == null || "".equals( pMethodSignature.trim() ) ) {
222          return new String JavaDoc[ 0 ];
223       }
224       StringTokenizer JavaDoc lTokenizer = new StringTokenizer JavaDoc( pMethodSignature, "," );
225       String JavaDoc[] lReturn = new String JavaDoc[ lTokenizer.countTokens() ];
226       int i = 0;
227       while( lTokenizer.hasMoreTokens() ) {
228          lReturn[ i++ ] = lTokenizer.nextToken().trim();
229       }
230       return lReturn;
231    }
232    
233    /**
234     * Converts the given Data string to a date
235     * where not value means 1/1/1970, "NOW" means
236     * now (plus a second), an long value means time
237     * in milliseconds since 1/1/1970 and a String is
238     * a Date string which is intepreted by a Simple
239     * Data Formatter.
240     *
241     * @param pStartDate the date
242     * @param dateFormat the dateFormat, the locale is
243     * is used when null or blank
244     */

245    protected Date JavaDoc getStartDate( String JavaDoc pStartDate, String JavaDoc dateFormat ) {
246       pStartDate = pStartDate == null ? "" : pStartDate.trim();
247       Date JavaDoc lReturn = null;
248       if( pStartDate.equals( "" ) ) {
249          lReturn = new Date JavaDoc( 0 );
250       } else
251       if( pStartDate.equals( "NOW" ) ) {
252          lReturn = new Date JavaDoc( new Date JavaDoc().getTime() + 1000 );
253       } else {
254          try {
255             long lDate = new Long JavaDoc( pStartDate ).longValue();
256             lReturn = new Date JavaDoc( lDate );
257          }
258          catch( Exception JavaDoc e ) {
259             try {
260                SimpleDateFormat JavaDoc dateFormatter = null;
261                if( dateFormat == null || dateFormat.trim().length() == 0 )
262                   dateFormatter = new SimpleDateFormat JavaDoc();
263                else
264                   dateFormatter = new SimpleDateFormat JavaDoc(dateFormat);
265                lReturn = dateFormatter.parse( pStartDate );
266             }
267             catch( Exception JavaDoc e2 ) {
268                log.error( "Could not parse given date string: " + pStartDate, e2 );
269                throw new InvalidParameterException JavaDoc( "Schedulable Date is not of correct format" );
270             }
271          }
272       }
273       log.debug( "Initial Start Date is set to: " + lReturn );
274       
275       return lReturn;
276    }
277    
278    // -------------------------------------------------------------------------
279
// Methods
280
// -------------------------------------------------------------------------
281

282    public ObjectName JavaDoc getObjectName(
283       MBeanServer JavaDoc pServer,
284       ObjectName JavaDoc pName
285    )
286       throws MalformedObjectNameException JavaDoc
287    {
288       return pName == null ? DBScheduleProviderMBean.OBJECT_NAME : pName;
289    }
290 }
291
Popular Tags