1 22 package org.jboss.varia.scheduler; 23 24 import java.security.InvalidParameterException ; 25 import java.text.SimpleDateFormat ; 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 import java.util.Iterator ; 29 import java.util.StringTokenizer ; 30 31 import javax.management.JMException ; 32 import javax.management.MalformedObjectNameException ; 33 import javax.management.MBeanServer ; 34 import javax.management.ObjectName ; 35 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Text ; 40 41 63 public class XMLScheduleProvider 64 extends AbstractScheduleProvider 65 implements XMLScheduleProviderMBean 66 { 67 68 72 76 private Element mSchedules; 77 78 79 private ArrayList mIDList = new ArrayList (); 80 81 85 88 public XMLScheduleProvider() 89 { 90 } 91 92 96 101 public Element getSchedules() { 102 return mSchedules; 103 } 104 105 110 public void setSchedules( final Element pSchedules ) { 111 mSchedules = pSchedules; 112 } 113 114 119 public void startProviding() 120 throws Exception 121 { 122 try { 123 NodeList lSchedules = mSchedules.getElementsByTagName( "schedule" ); 124 for( int i = 0; i < lSchedules.getLength(); i++ ) { 125 Node lSchedule = lSchedules.item( i ); 126 NodeList lAttributes = lSchedule.getChildNodes(); 127 Text 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 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 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 lMethodSignature = lItem.getData(); 146 lItem = getNode( lAttributes, "date-format" ); 147 String 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 (dateFormat); 155 } 156 catch (Exception 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 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 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 lRepeptions = lItem.getData(); 180 try { 181 int lID = addSchedule( 183 new ObjectName ( lTarget ), 184 lMethodName, 185 getSignature( lMethodSignature ), 186 getStartDate( lStartDate, dateFormat ), 187 new Long ( lPeriod ).longValue(), 188 new Integer ( lRepeptions ).intValue() 189 ); 190 mIDList.add( new Integer ( lID ) ); 191 } 192 catch( NumberFormatException nfe ) { 193 log.error( "Could not convert a number", nfe ); 194 } 195 } 196 } catch( Exception e ) { 197 e.printStackTrace(); 198 throw e; 199 } 200 } 201 202 211 protected Text getNode( NodeList pList, String pName ) { 212 if( pList == null ) { 213 return null; 214 } 215 for( int i = 0; i < pList.getLength(); i++ ) { 216 Node lNode = pList.item(i); 217 switch( lNode.getNodeType() ) { 218 case Node.ELEMENT_NODE: 219 Element lChild = (Element ) lNode; 220 if( lChild.getNodeName().equals( pName ) ) { 221 return (Text ) lChild.getFirstChild(); 222 } 223 } 224 } 225 return null; 226 } 227 228 234 public void stopProviding() { 235 Iterator i = mIDList.iterator(); 236 while( i.hasNext() ) { 237 Integer lID = (Integer ) i.next(); 238 try { 239 removeSchedule( lID.intValue() ); 240 } 241 catch( JMException jme ) { 242 log.error( "Could not remove Schedule in stop providing", jme ); 243 } 244 } 245 } 246 247 251 protected String [] getSignature( String pMethodSignature ) 252 { 253 if( pMethodSignature == null || "".equals( pMethodSignature.trim() ) ) { 254 return new String [ 0 ]; 255 } 256 StringTokenizer lTokenizer = new StringTokenizer ( pMethodSignature, "," ); 257 String [] lReturn = new String [ lTokenizer.countTokens() ]; 258 int i = 0; 259 while( lTokenizer.hasMoreTokens() ) { 260 lReturn[ i++ ] = lTokenizer.nextToken().trim(); 261 } 262 return lReturn; 263 } 264 265 277 protected Date getStartDate( String pStartDate, String dateFormat ) { 278 pStartDate = pStartDate == null ? "" : pStartDate.trim(); 279 Date lReturn = null; 280 if( pStartDate.equals( "" ) ) { 281 lReturn = new Date ( 0 ); 282 } else 283 if( pStartDate.equals( "NOW" ) ) { 284 lReturn = new Date ( new Date ().getTime() + 1000 ); 285 } else { 286 try { 287 long lDate = new Long ( pStartDate ).longValue(); 288 lReturn = new Date ( lDate ); 289 } 290 catch( Exception e ) { 291 try { 292 SimpleDateFormat dateFormatter = null; 293 if( dateFormat == null || dateFormat.trim().length() == 0 ) 294 dateFormatter = new SimpleDateFormat (); 295 else 296 dateFormatter = new SimpleDateFormat (dateFormat); 297 lReturn = dateFormatter.parse( pStartDate ); 298 } 299 catch( Exception e2 ) { 300 log.error( "Could not parse given date string: " + pStartDate, e2 ); 301 throw new InvalidParameterException ( "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 314 public ObjectName getObjectName( 315 MBeanServer pServer, 316 ObjectName pName 317 ) 318 throws MalformedObjectNameException 319 { 320 return pName == null ? XMLScheduleProviderMBean.OBJECT_NAME : pName; 321 } 322 } 323 | Popular Tags |