package org.jboss.varia.scheduler;
import java.security.InvalidParameterException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.management.JMException;
import javax.management.MalformedObjectNameException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class XMLScheduleProvider
extends AbstractScheduleProvider
implements XMLScheduleProviderMBean
{
private Element mSchedules;
private ArrayList mIDList = new ArrayList();
public XMLScheduleProvider()
{
}
public Element getSchedules() {
return mSchedules;
}
public void setSchedules( final Element pSchedules ) {
mSchedules = pSchedules;
}
public void startProviding()
throws Exception
{
try {
NodeList lSchedules = mSchedules.getElementsByTagName( "schedule" );
for( int i = 0; i < lSchedules.getLength(); i++ ) {
Node lSchedule = lSchedules.item( i );
NodeList lAttributes = lSchedule.getChildNodes();
Text lItem = getNode( lAttributes, "target-mbean-name" );
if( lItem == null ) {
log.error( "No 'target-mbean-name' is specified therefore this Schedule is ignored" );
continue;
}
log.info( "Got 'target-mbean-name' element: " + lItem + ", node value: " + lItem.getData() + lItem.getChildNodes() );
String lTarget = lItem.getData();
lItem = getNode( lAttributes, "target-method-name" );
if( lItem == null ) {
log.error( "No 'target-method-name' is specified therefore this Schedule is ignored" );
continue;
}
String lMethodName = lItem.getData();
lItem = getNode( lAttributes, "target-method-signature" );
if( lItem == null ) {
log.error( "No 'target-method-signature' is specified therefore this Schedule is ignored" );
continue;
}
String lMethodSignature = lItem.getData();
lItem = getNode( lAttributes, "date-format" );
String dateFormat = null;
if (lItem != null)
{
dateFormat = lItem.getData();
if (dateFormat != null && dateFormat.trim().length() != 0)
try
{
new SimpleDateFormat(dateFormat);
}
catch (Exception e)
{
log.error( "Invalid date format therefore this Schedule is ignored", e);
continue;
}
}
lItem = getNode( lAttributes, "start-date" );
if( lItem == null ) {
log.error( "No 'start-date' is specified therefore this Schedule is ignored" );
continue;
}
String lStartDate = lItem.getData();
lItem = getNode( lAttributes, "period" );
if( lItem == null ) {
log.error( "No 'period' is specified therefore this Schedule is ignored" );
continue;
}
String lPeriod = lItem.getData();
lItem = getNode( lAttributes, "repetitions" );
if( lItem == null ) {
log.error( "No 'repetitions' is specified therefore this Schedule is ignored" );
continue;
}
String lRepeptions = lItem.getData();
try {
int lID = addSchedule(
new ObjectName( lTarget ),
lMethodName,
getSignature( lMethodSignature ),
getStartDate( lStartDate, dateFormat ),
new Long( lPeriod ).longValue(),
new Integer( lRepeptions ).intValue()
);
mIDList.add( new Integer( lID ) );
}
catch( NumberFormatException nfe ) {
log.error( "Could not convert a number", nfe );
}
}
} catch( Exception e ) {
e.printStackTrace();
throw e;
}
}
protected Text getNode( NodeList pList, String pName ) {
if( pList == null ) {
return null;
}
for( int i = 0; i < pList.getLength(); i++ ) {
Node lNode = pList.item(i);
switch( lNode.getNodeType() ) {
case Node.ELEMENT_NODE:
Element lChild = (Element) lNode;
if( lChild.getNodeName().equals( pName ) ) {
return (Text) lChild.getFirstChild();
}
}
}
return null;
}
public void stopProviding() {
Iterator i = mIDList.iterator();
while( i.hasNext() ) {
Integer lID = (Integer) i.next();
try {
removeSchedule( lID.intValue() );
}
catch( JMException jme ) {
log.error( "Could not remove Schedule in stop providing", jme );
}
}
}
protected String[] getSignature( String pMethodSignature )
{
if( pMethodSignature == null || "".equals( pMethodSignature.trim() ) ) {
return new String[ 0 ];
}
StringTokenizer lTokenizer = new StringTokenizer( pMethodSignature, "," );
String[] lReturn = new String[ lTokenizer.countTokens() ];
int i = 0;
while( lTokenizer.hasMoreTokens() ) {
lReturn[ i++ ] = lTokenizer.nextToken().trim();
}
return lReturn;
}
protected Date getStartDate( String pStartDate, String dateFormat ) {
pStartDate = pStartDate == null ? "" : pStartDate.trim();
Date lReturn = null;
if( pStartDate.equals( "" ) ) {
lReturn = new Date( 0 );
} else
if( pStartDate.equals( "NOW" ) ) {
lReturn = new Date( new Date().getTime() + 1000 );
} else {
try {
long lDate = new Long( pStartDate ).longValue();
lReturn = new Date( lDate );
}
catch( Exception e ) {
try {
SimpleDateFormat dateFormatter = null;
if( dateFormat == null || dateFormat.trim().length() == 0 )
dateFormatter = new SimpleDateFormat();
else
dateFormatter = new SimpleDateFormat(dateFormat);
lReturn = dateFormatter.parse( pStartDate );
}
catch( Exception e2 ) {
log.error( "Could not parse given date string: " + pStartDate, e2 );
throw new InvalidParameterException( "Schedulable Date is not of correct format" );
}
}
}
log.debug( "Initial Start Date is set to: " + lReturn );
return lReturn;
}
public ObjectName getObjectName(
MBeanServer pServer,
ObjectName pName
)
throws MalformedObjectNameException
{
return pName == null ? XMLScheduleProviderMBean.OBJECT_NAME : pName;
}
}