package org.jboss.varia.scheduler;
import java.security.InvalidParameterException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 javax.naming.InitialContext;
import javax.sql.DataSource;
public class DBScheduleProvider
extends AbstractScheduleProvider
implements DBScheduleProviderMBean
{
private String mDataSourceName;
private String mSQLStatement;
private ArrayList mIDList = new ArrayList();
public DBScheduleProvider()
{
}
public String getDataSourceName() {
return mDataSourceName;
}
public void setDataSourceName( String pDataSourceName ) {
mDataSourceName = pDataSourceName;
}
public String getSQLStatement() {
return mSQLStatement;
}
public void setSQLStatement( String pSQLStatement ) {
mSQLStatement = pSQLStatement;
}
public void startProviding()
throws Exception
{
Connection lConnection = null;
PreparedStatement lStatement = null;
try {
Object lTemp = new InitialContext().lookup( mDataSourceName );
DataSource lDB = (DataSource) lTemp;
lConnection = lDB.getConnection();
lStatement = lConnection.prepareStatement( mSQLStatement );
ResultSet lResult = lStatement.executeQuery();
while( lResult.next() ) {
int lID = addSchedule(
new ObjectName( lResult.getString( 1 ) ),
lResult.getString( 2 ),
getSignature( lResult.getString( 3 ) ),
getStartDate( lResult.getString( 4 ), lResult.getString( 7 ) ),
lResult.getLong( 5 ),
lResult.getInt( 6 )
);
mIDList.add( new Integer( lID ) );
}
}
finally {
if( lStatement != null ) {
try {
lStatement.close();
}
catch( Exception e ) {}
}
if( lConnection != null ) {
try {
lConnection.close();
}
catch( Exception e ) {}
}
}
}
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 ? DBScheduleProviderMBean.OBJECT_NAME : pName;
}
}