KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.SimpleDateFormat JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import javax.management.JMException JavaDoc;
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40
41 /**
42  * This Provider get its Scheduler from a XML configuration string
43  * allowing the administrator to add several Schedules with one
44  * provider.
45  * The "Schedules" properties has to look like:
46  * <schedules>
47  * <schedule>
48  * <target-mbean-name/>
49  * <target-method-name/>
50  * <target-method-signature/>
51  * <start-date/>
52  * <period/>
53  * <repetitions/>
54  * </schedule>
55  * </schedules>
56  *
57  * @jmx:mbean name="jboss:service=XMLScheduleProvider"
58  * extends="org.jboss.varia.scheduler.AbstractScheduleProviderMBean"
59  *
60  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
61  * @version $Revision: 37459 $
62  */

63 public class XMLScheduleProvider
64    extends AbstractScheduleProvider
65    implements XMLScheduleProviderMBean
66 {
67
68    // -------------------------------------------------------------------------
69
// Constants
70
// -------------------------------------------------------------------------
71

72    // -------------------------------------------------------------------------
73
// Members
74
// -------------------------------------------------------------------------
75

76    private Element JavaDoc mSchedules;
77    
78    /** The ID of the Schedule used later to remove it later **/
79    private ArrayList JavaDoc mIDList = new ArrayList JavaDoc();
80    
81    // -------------------------------------------------------------------------
82
// Constructors
83
// -------------------------------------------------------------------------
84

85    /**
86     * Default (no-args) Constructor
87     **/

88    public XMLScheduleProvider()
89    {
90    }
91    
92    // -------------------------------------------------------------------------
93
// SchedulerMBean Methods
94
// -------------------------------------------------------------------------
95

96    /**
97     * @return XML configuration attribute
98     *
99     * @jmx:managed-operation
100     **/

101    public Element JavaDoc getSchedules() {
102       return mSchedules;
103    }
104    
105    /**
106     * Sets the XML configuration attribute
107     *
108     * @jmx:managed-operation
109     **/

110    public void setSchedules( final Element JavaDoc pSchedules ) {
111       mSchedules = pSchedules;
112    }
113    
114    /**
115     * Add the Schedule to the Schedule Manager
116     *
117     * @jmx:managed-operation
118     **/

119    public void startProviding()
120       throws Exception JavaDoc
121    {
122       try {
123       NodeList JavaDoc lSchedules = mSchedules.getElementsByTagName( "schedule" );
124       for( int i = 0; i < lSchedules.getLength(); i++ ) {
125          Node JavaDoc lSchedule = lSchedules.item( i );
126          NodeList JavaDoc lAttributes = lSchedule.getChildNodes();
127          Text JavaDoc lItem = getNode( lAttributes, "target-mbean-name" );
128          if( lItem == null ) {
129             log.error( "No 'target-mbean-name' is specified therefore this Schedule is ignored" );
130             continue;
131          }
132          log.info( "Got 'target-mbean-name' element: " + lItem + ", node value: " + lItem.getData() + lItem.getChildNodes() );
133          String JavaDoc lTarget = lItem.getData();
134          lItem = getNode( lAttributes, "target-method-name" );
135          if( lItem == null ) {
136             log.error( "No 'target-method-name' is specified therefore this Schedule is ignored" );
137             continue;
138          }
139          String JavaDoc lMethodName = lItem.getData();
140          lItem = getNode( lAttributes, "target-method-signature" );
141          if( lItem == null ) {
142             log.error( "No 'target-method-signature' is specified therefore this Schedule is ignored" );
143             continue;
144          }
145          String JavaDoc lMethodSignature = lItem.getData();
146          lItem = getNode( lAttributes, "date-format" );
147          String JavaDoc dateFormat = null;
148          if (lItem != null)
149          {
150             dateFormat = lItem.getData();
151             if (dateFormat != null && dateFormat.trim().length() != 0)
152             try
153             {
154                new SimpleDateFormat JavaDoc(dateFormat);
155             }
156             catch (Exception JavaDoc e)
157             {
158               log.error( "Invalid date format therefore this Schedule is ignored", e);
159               continue;
160             }
161          }
162          lItem = getNode( lAttributes, "start-date" );
163          if( lItem == null ) {
164             log.error( "No 'start-date' is specified therefore this Schedule is ignored" );
165             continue;
166          }
167          String JavaDoc lStartDate = lItem.getData();
168          lItem = getNode( lAttributes, "period" );
169          if( lItem == null ) {
170             log.error( "No 'period' is specified therefore this Schedule is ignored" );
171             continue;
172          }
173          String JavaDoc lPeriod = lItem.getData();
174          lItem = getNode( lAttributes, "repetitions" );
175          if( lItem == null ) {
176             log.error( "No 'repetitions' is specified therefore this Schedule is ignored" );
177             continue;
178          }
179          String JavaDoc lRepeptions = lItem.getData();
180          try {
181             // Add Schedule
182
int lID = addSchedule(
183                new ObjectName JavaDoc( lTarget ),
184                lMethodName,
185                getSignature( lMethodSignature ),
186                getStartDate( lStartDate, dateFormat ),
187                new Long JavaDoc( lPeriod ).longValue(),
188                new Integer JavaDoc( lRepeptions ).intValue()
189             );
190             mIDList.add( new Integer JavaDoc( lID ) );
191          }
192          catch( NumberFormatException JavaDoc nfe ) {
193             log.error( "Could not convert a number", nfe );
194          }
195       }
196       } catch( Exception JavaDoc e ) {
197          e.printStackTrace();
198          throw e;
199       }
200    }
201    
202    /**
203     * Loops of the given Node List and looks for the Element
204     * with the given Node Name
205     *
206     * @param pNodeList The list of nodes to search through
207     * @param pName The name of the node to look for
208     *
209     * @return Element if found otherwise null
210     **/

211    protected Text JavaDoc getNode( NodeList JavaDoc pList, String JavaDoc pName ) {
212       if( pList == null ) {
213          return null;
214       }
215       for( int i = 0; i < pList.getLength(); i++ ) {
216          Node JavaDoc lNode = pList.item(i);
217          switch( lNode.getNodeType() ) {
218             case Node.ELEMENT_NODE:
219                Element JavaDoc lChild = (Element JavaDoc) lNode;
220                if( lChild.getNodeName().equals( pName ) ) {
221                   return (Text JavaDoc) lChild.getFirstChild();
222                }
223          }
224       }
225       return null;
226    }
227    
228    /**
229     * Stops the Provider from providing causing
230     * the provider to remove the Schedule
231     *
232     * @jmx:managed-operation
233     */

234    public void stopProviding() {
235       Iterator JavaDoc i = mIDList.iterator();
236       while( i.hasNext() ) {
237          Integer JavaDoc lID = (Integer JavaDoc) i.next();
238          try {
239             removeSchedule( lID.intValue() );
240          }
241          catch( JMException JavaDoc jme ) {
242             log.error( "Could not remove Schedule in stop providing", jme );
243          }
244       }
245    }
246    
247    /**
248     * Converts a string of method arguments (separated by colons) into
249     * an array of string
250     **/

251    protected String JavaDoc[] getSignature( String JavaDoc pMethodSignature )
252    {
253       if( pMethodSignature == null || "".equals( pMethodSignature.trim() ) ) {
254          return new String JavaDoc[ 0 ];
255       }
256       StringTokenizer JavaDoc lTokenizer = new StringTokenizer JavaDoc( pMethodSignature, "," );
257       String JavaDoc[] lReturn = new String JavaDoc[ lTokenizer.countTokens() ];
258       int i = 0;
259       while( lTokenizer.hasMoreTokens() ) {
260          lReturn[ i++ ] = lTokenizer.nextToken().trim();
261       }
262       return lReturn;
263    }
264    
265    /**
266     * Converts the given Data string to a date
267     * where not value means 1/1/1970, "NOW" means
268     * now (plus a second), an long value means time
269     * in milliseconds since 1/1/1970 and a String is
270     * a Date string which is intepreted by a Simple
271     * Data Formatter.
272     *
273     * @param pStartDate the date
274     * @param dateFormat the dateFormat, the locale is
275     * is used when null or blank
276     */

277    protected Date JavaDoc getStartDate( String JavaDoc pStartDate, String JavaDoc dateFormat ) {
278       pStartDate = pStartDate == null ? "" : pStartDate.trim();
279       Date JavaDoc lReturn = null;
280       if( pStartDate.equals( "" ) ) {
281          lReturn = new Date JavaDoc( 0 );
282       } else
283       if( pStartDate.equals( "NOW" ) ) {
284          lReturn = new Date JavaDoc( new Date JavaDoc().getTime() + 1000 );
285       } else {
286          try {
287             long lDate = new Long JavaDoc( pStartDate ).longValue();
288             lReturn = new Date JavaDoc( lDate );
289          }
290          catch( Exception JavaDoc e ) {
291             try {
292                SimpleDateFormat JavaDoc dateFormatter = null;
293                if( dateFormat == null || dateFormat.trim().length() == 0 )
294                   dateFormatter = new SimpleDateFormat JavaDoc();
295                else
296                   dateFormatter = new SimpleDateFormat JavaDoc(dateFormat);
297                lReturn = dateFormatter.parse( pStartDate );
298             }
299             catch( Exception JavaDoc e2 ) {
300                log.error( "Could not parse given date string: " + pStartDate, e2 );
301                throw new InvalidParameterException JavaDoc( "Schedulable Date is not of correct format" );
302             }
303          }
304       }
305       log.debug( "Initial Start Date is set to: " + lReturn );
306       
307       return lReturn;
308    }
309    
310    // -------------------------------------------------------------------------
311
// Methods
312
// -------------------------------------------------------------------------
313

314    public ObjectName JavaDoc getObjectName(
315       MBeanServer JavaDoc pServer,
316       ObjectName JavaDoc pName
317    )
318       throws MalformedObjectNameException JavaDoc
319    {
320       return pName == null ? XMLScheduleProviderMBean.OBJECT_NAME : pName;
321    }
322 }
323
Popular Tags