KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > selfmanagement > event > TimerEventFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * TimerEventFactory.java
26  *
27  * Created on May 23, 2005, 3:57 PM
28  */

29
30 package com.sun.enterprise.admin.selfmanagement.event;
31
32 import javax.management.timer.Timer JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.util.logging.Level JavaDoc;
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 /**
44  *
45  * This is the factory to build and configure Timer Event
46  * @author Sun Micro Systems, Inc
47  */

48 public class TimerEventFactory extends EventAbstractFactory{
49     
50     
51     /** Creates a new instance of LogEventFactory */
52     TimerEventFactory() {
53         super();
54         EventBuilder.getInstance().addEventFactory(EVENT_TIMER, this);
55     }
56     
57     public Event instrumentEvent(
58             ElementProperty[] properties, String JavaDoc description ) {
59         // expected properties:
60
// datestring : one which DateFormatter.parse understands
61
// pattern : format dd/mm/yyyy hh:mm:ss
62
// period
63
// message
64
// numberofoccurrences
65

66         String JavaDoc datePattern = null;
67         String JavaDoc dateString = null;
68         long period = 0;
69         long numberOfOccurrences = 0;
70         String JavaDoc message = "timer notification";
71         String JavaDoc type = "timer";
72         SimpleDateFormat JavaDoc format = null;
73         
74         for( int i = 0; i < properties.length; i++ ){
75             ElementProperty property = properties[i];
76             String JavaDoc 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         /**
91         if (dateString == null)
92             throw new IllegalArgumentException(
93                     sm.getString("selfmgmt_event.invalid_event_property","dateString","timer"));
94         **/

95         if (datePattern == null)
96             datePattern = defaultPattern;
97         
98         
99         Date JavaDoc d = null;
100         try {
101             if (dateString == null)
102                 d = new Date JavaDoc();
103             else
104                 d = new SimpleDateFormat JavaDoc(datePattern).parse(dateString);
105         } catch (java.text.ParseException JavaDoc pex) {
106             throw new IllegalArgumentException JavaDoc(
107                     sm.getString("selfmgmt_event.invalid_event_property","dateString","timer"),pex);
108         }
109         Integer JavaDoc 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 JavaDoc iex) {
125             _logger.log(Level.WARNING,"smgt.internal_error", iex);
126         }
127     }
128     
129     private synchronized Timer JavaDoc getTimer() {
130         if (timer != null)
131             return timer;
132         try {
133             timer = (Timer JavaDoc)getMBeanServer().instantiate("javax.management.timer.Timer");
134             getMBeanServer().registerMBean(timer,TimerEvent.getTimerObjectName());
135             timer.start();
136             return timer;
137         } catch (Exception JavaDoc 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 JavaDoc defaultPattern = "MM/dd/yyyy HH:mm:ss";
149     private static javax.management.timer.Timer JavaDoc timer = null;
150     
151 }
152
Popular Tags