1 22 package org.jboss.varia.scheduler; 23 24 import java.security.InvalidParameterException ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.text.SimpleDateFormat ; 29 import java.util.ArrayList ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.StringTokenizer ; 33 34 import javax.management.JMException ; 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.MBeanServer ; 37 import javax.management.ObjectName ; 38 import javax.naming.InitialContext ; 39 import javax.sql.DataSource ; 40 41 79 public class DBScheduleProvider 80 extends AbstractScheduleProvider 81 implements DBScheduleProviderMBean 82 { 83 84 88 92 private String mDataSourceName; 93 private String mSQLStatement; 94 95 96 private ArrayList mIDList = new ArrayList (); 97 98 102 105 public DBScheduleProvider() 106 { 107 } 108 109 113 118 public String getDataSourceName() { 119 return mDataSourceName; 120 } 121 122 129 public void setDataSourceName( String pDataSourceName ) { 130 mDataSourceName = pDataSourceName; 131 } 132 133 138 public String getSQLStatement() { 139 return mSQLStatement; 140 } 141 142 148 public void setSQLStatement( String pSQLStatement ) { 149 mSQLStatement = pSQLStatement; 150 } 151 152 157 public void startProviding() 158 throws Exception 159 { 160 Connection lConnection = null; 161 PreparedStatement lStatement = null; 162 try { 163 Object lTemp = new InitialContext ().lookup( mDataSourceName ); 164 DataSource lDB = (DataSource ) lTemp; 165 lConnection = lDB.getConnection(); 166 lStatement = lConnection.prepareStatement( mSQLStatement ); 167 ResultSet lResult = lStatement.executeQuery(); 168 while( lResult.next() ) { 169 int lID = addSchedule( 170 new ObjectName ( 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 ( lID ) ); 178 } 179 } 180 finally { 181 if( lStatement != null ) { 182 try { 183 lStatement.close(); 184 } 185 catch( Exception e ) {} 186 } 187 if( lConnection != null ) { 188 try { 189 lConnection.close(); 190 } 191 catch( Exception e ) {} 192 } 193 } 194 } 195 196 202 public void stopProviding() { 203 Iterator i = mIDList.iterator(); 204 while( i.hasNext() ) { 205 Integer lID = (Integer ) i.next(); 206 try { 207 removeSchedule( lID.intValue() ); 208 } 209 catch( JMException jme ) { 210 log.error( "Could not remove Schedule in stop providing", jme ); 211 } 212 } 213 } 214 215 219 protected String [] getSignature( String pMethodSignature ) 220 { 221 if( pMethodSignature == null || "".equals( pMethodSignature.trim() ) ) { 222 return new String [ 0 ]; 223 } 224 StringTokenizer lTokenizer = new StringTokenizer ( pMethodSignature, "," ); 225 String [] lReturn = new String [ lTokenizer.countTokens() ]; 226 int i = 0; 227 while( lTokenizer.hasMoreTokens() ) { 228 lReturn[ i++ ] = lTokenizer.nextToken().trim(); 229 } 230 return lReturn; 231 } 232 233 245 protected Date getStartDate( String pStartDate, String dateFormat ) { 246 pStartDate = pStartDate == null ? "" : pStartDate.trim(); 247 Date lReturn = null; 248 if( pStartDate.equals( "" ) ) { 249 lReturn = new Date ( 0 ); 250 } else 251 if( pStartDate.equals( "NOW" ) ) { 252 lReturn = new Date ( new Date ().getTime() + 1000 ); 253 } else { 254 try { 255 long lDate = new Long ( pStartDate ).longValue(); 256 lReturn = new Date ( lDate ); 257 } 258 catch( Exception e ) { 259 try { 260 SimpleDateFormat dateFormatter = null; 261 if( dateFormat == null || dateFormat.trim().length() == 0 ) 262 dateFormatter = new SimpleDateFormat (); 263 else 264 dateFormatter = new SimpleDateFormat (dateFormat); 265 lReturn = dateFormatter.parse( pStartDate ); 266 } 267 catch( Exception e2 ) { 268 log.error( "Could not parse given date string: " + pStartDate, e2 ); 269 throw new InvalidParameterException ( "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 282 public ObjectName getObjectName( 283 MBeanServer pServer, 284 ObjectName pName 285 ) 286 throws MalformedObjectNameException 287 { 288 return pName == null ? DBScheduleProviderMBean.OBJECT_NAME : pName; 289 } 290 } 291 | Popular Tags |