1 22 package org.jboss.varia.scheduler; 23 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 import java.util.StringTokenizer ; 27 28 import javax.management.JMException ; 29 import javax.management.MBeanServer ; 30 import javax.management.MalformedObjectNameException ; 31 import javax.management.ObjectName ; 32 33 44 public class SingleScheduleProvider extends AbstractScheduleProvider 45 implements SingleScheduleProviderMBean 46 { 47 48 52 56 private ObjectName mSchedulableMBean; 57 private String mSchedulableMBeanMethod; 58 private String mSchedulableMBeanMethodName; 59 private String [] mMethodSignature = new String [ 0 ]; 60 61 private SimpleDateFormat mDateFormatter; 62 private Date mStartDate; 63 private String mStartDateString; 64 private long mSchedulePeriod; 65 private long mInitialRepetitions; 66 67 68 private int mScheduleID; 69 70 74 77 public SingleScheduleProvider() 78 { 79 } 80 81 85 public void startProviding() throws JMException 86 { 87 mScheduleID = addSchedule( 88 mSchedulableMBean, 89 mSchedulableMBeanMethodName, 90 mMethodSignature, 91 mStartDate, 92 mSchedulePeriod, 93 (int) mInitialRepetitions 94 ); 95 } 96 97 public void stopProviding() 98 { 99 try 100 { 101 removeSchedule( mScheduleID ); 102 } 103 catch (JMException jme) 104 { 105 log.error( "Could not remove Schedule in stop providing", jme ); 106 } 107 } 108 109 114 public ObjectName getTargetName() 115 { 116 return mSchedulableMBean; 117 } 118 119 127 public void setTargetName(ObjectName pTargetObjectName) 128 { 129 if (pTargetObjectName == null) 130 { 131 throw new IllegalArgumentException ("Schedulable MBean must be specified"); 132 } 133 else 134 { 135 this.mSchedulableMBean = pTargetObjectName; 136 } 137 } 138 139 144 public String getTargetMethod() 145 { 146 return mSchedulableMBeanMethod; 147 } 148 149 180 public void setTargetMethod(String pTargetMethod) throws IllegalArgumentException 181 { 182 if (pTargetMethod == null) 183 { 184 mSchedulableMBeanMethod = null; 185 return; 186 } 187 int lIndex = pTargetMethod.indexOf('('); 188 String lMethodName = ""; 189 if( lIndex < 0 ) 190 { 191 lMethodName = pTargetMethod.trim(); 192 mMethodSignature = new String [ 0 ]; 193 } 194 else 195 if( lIndex > 0 ) 196 { 197 lMethodName = pTargetMethod.substring(0, lIndex).trim(); 198 } 199 if (lMethodName.equals("")) 200 { 201 lMethodName = "perform"; 202 } 203 if (lIndex >= 0) 204 { 205 int lIndex2 = pTargetMethod.indexOf(')'); 206 if (lIndex2 < lIndex) 207 { 208 throw new IllegalArgumentException ("Schedulable MBean Method: closing bracket must be after opening bracket"); 209 } 210 if (lIndex2 < pTargetMethod.length() - 1) 211 { 212 String lRest = pTargetMethod.substring(lIndex2 + 1).trim(); 213 if (lRest.length() > 0) 214 { 215 throw new IllegalArgumentException ("Schedulable MBean Method: nothing should be after closing bracket"); 216 } 217 } 218 String lArguments = pTargetMethod.substring(lIndex + 1, lIndex2).trim(); 219 if (lArguments.equals("")) 220 { 221 mMethodSignature = new String [0]; 222 } 223 else 224 { 225 StringTokenizer lTokenizer = new StringTokenizer (lArguments, ","); 226 mMethodSignature = new String [lTokenizer.countTokens()]; 227 for (int i = 0; lTokenizer.hasMoreTokens(); i++) 228 { 229 mMethodSignature[i] = lTokenizer.nextToken().trim(); 230 } 231 } 232 } 233 mSchedulableMBeanMethodName = lMethodName; 234 mSchedulableMBeanMethod = pTargetMethod; 235 } 236 237 243 public long getPeriod() 244 { 245 return mSchedulePeriod; 246 } 247 248 258 public void setPeriod(long pPeriod) 259 { 260 if (pPeriod <= 0) 261 { 262 throw new IllegalArgumentException ("Schedulable Period may be not less or equals than 0"); 263 } 264 mSchedulePeriod = pPeriod; 265 } 266 267 272 public String getDateFormat() 273 { 274 if (mDateFormatter == null) 275 mDateFormatter = new SimpleDateFormat (); 276 277 return mDateFormatter.toPattern(); 278 } 279 280 287 public void setDateFormat(String dateFormat) 288 { 289 if (dateFormat == null || dateFormat.trim().length() == 0) 290 mDateFormatter = new SimpleDateFormat (); 291 else 292 mDateFormatter = new SimpleDateFormat (dateFormat); 293 } 294 295 301 public String getStartDate() 302 { 303 return mStartDateString; 304 } 305 306 334 public void setStartDate(String pStartDate) 335 { 336 mStartDateString = pStartDate == null ? "" : pStartDate.trim(); 337 if (mStartDateString.equals("")) 338 { 339 mStartDate = new Date (0); 340 } 341 else if (mStartDateString.equals("NOW")) 342 { 343 mStartDate = new Date (new Date ().getTime() + 1000); 344 } 345 else 346 { 347 try 348 { 349 long lDate = new Long (pStartDate).longValue(); 350 mStartDate = new Date (lDate); 351 } 352 catch (Exception e) 353 { 354 try 355 { 356 if (mDateFormatter == null) 357 { 358 mDateFormatter = new SimpleDateFormat (); 359 } 360 mStartDate = mDateFormatter.parse(mStartDateString); 361 } 362 catch (Exception e2) 363 { 364 log.error ("Could not parse given date string: " + mStartDateString, e2); 365 throw new IllegalArgumentException ("Schedulable Date is not of correct format"); 366 } 367 } 368 } 369 log.debug("Initial Start Date is set to: " + mStartDate); 370 } 371 372 377 public long getRepetitions() 378 { 379 return mInitialRepetitions; 380 } 381 382 392 public void setRepetitions(long pNumberOfCalls) 393 { 394 if (pNumberOfCalls <= 0) 395 { 396 pNumberOfCalls = -1; 397 } 398 mInitialRepetitions = pNumberOfCalls; 399 } 400 401 405 public ObjectName getObjectName(MBeanServer pServer, ObjectName pName) 406 throws MalformedObjectNameException 407 { 408 return pName == null ? SingleScheduleProviderMBean.OBJECT_NAME : pName; 409 } 410 } 411 | Popular Tags |