1 23 24 29 30 package com.sun.enterprise.admin.selfmanagement.event; 31 32 import javax.management.timer.Timer ; 33 import javax.management.MBeanServer ; 34 import javax.management.ObjectName ; 35 import java.util.Date ; 36 import java.text.SimpleDateFormat ; 37 import java.util.logging.Level ; 38 39 import com.sun.enterprise.config.serverbeans.ElementProperty; 40 import com.sun.enterprise.admin.selfmanagement.configuration.JavaBeanConfigurator; 41 import com.sun.enterprise.util.SystemPropertyConstants; 42 import static com.sun.enterprise.admin.selfmanagement.event.ManagementRuleConstants.*; 43 48 public class TimerEventFactory extends EventAbstractFactory{ 49 50 51 52 TimerEventFactory() { 53 super(); 54 EventBuilder.getInstance().addEventFactory(EVENT_TIMER, this); 55 } 56 57 public Event instrumentEvent( 58 ElementProperty[] properties, String description ) { 59 66 String datePattern = null; 67 String dateString = null; 68 long period = 0; 69 long numberOfOccurrences = 0; 70 String message = "timer notification"; 71 String type = "timer"; 72 SimpleDateFormat format = null; 73 74 for( int i = 0; i < properties.length; i++ ){ 75 ElementProperty property = properties[i]; 76 String propertyName = property.getName( ).toLowerCase( ); 77 if (propertyName.equals(PROPERTY_TIMER_PATTERN)) 78 datePattern = property.getValue(); 79 if (propertyName.equals(PROPERTY_TIMER_DATESTRING)) 80 dateString = property.getValue(); 81 if (propertyName.equals(PROPERTY_TIMER_PERIOD)) 82 period = Long.parseLong(property.getValue()); 83 if (propertyName.equals(PROPERTY_TIMER_NUMBER_OF_OCCURRENCES) ) 84 numberOfOccurrences = Long.parseLong(property.getValue()); 85 if (propertyName.equals(PROPERTY_TIMER_MESSAGE) ) 86 message = property.getValue(); 87 88 } 89 90 95 if (datePattern == null) 96 datePattern = defaultPattern; 97 98 99 Date d = null; 100 try { 101 if (dateString == null) 102 d = new Date (); 103 else 104 d = new SimpleDateFormat (datePattern).parse(dateString); 105 } catch (java.text.ParseException pex) { 106 throw new IllegalArgumentException ( 107 sm.getString("selfmgmt_event.invalid_event_property","dateString","timer"),pex); 108 } 109 Integer id = 0; 110 if (period > 0) { 111 if (numberOfOccurrences > 0 ) 112 id = getTimer().addNotification(type,message,null,d,period,numberOfOccurrences); 113 else id = getTimer().addNotification(type,message,null,d,period); 114 115 } else { 116 id = getTimer().addNotification(type,message,null,d); 117 } 118 return new TimerEvent(id, new TimerEventFilter(id), description); 119 } 120 121 void removeEvent(int id) { 122 try { 123 getTimer().removeNotification(id); 124 } catch (javax.management.InstanceNotFoundException iex) { 125 _logger.log(Level.WARNING,"smgt.internal_error", iex); 126 } 127 } 128 129 private synchronized Timer getTimer() { 130 if (timer != null) 131 return timer; 132 try { 133 timer = (Timer )getMBeanServer().instantiate("javax.management.timer.Timer"); 134 getMBeanServer().registerMBean(timer,TimerEvent.getTimerObjectName()); 135 timer.start(); 136 return timer; 137 } catch (Exception ex) { 138 _logger.log(Level.WARNING,"smgt.internal_error", ex); 139 } 140 return timer; 141 } 142 143 static TimerEventFactory getInstance() { 144 return instance; 145 } 146 147 private static TimerEventFactory instance = new TimerEventFactory(); 148 private static final String defaultPattern = "MM/dd/yyyy HH:mm:ss"; 149 private static javax.management.timer.Timer timer = null; 150 151 } 152 | Popular Tags |