KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > util > TimerReceiver


1 /*
2
3 *
4 * TimerReceiver.java -
5 * Copyright (C) 2003 Ecoo Team
6 * valdes@loria.fr
7 *
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */

23
24 package hero.util;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import hero.interfaces.BnNodeHookLocal;
29 import hero.interfaces.BnNodeLocal;
30 import hero.interfaces.BnProjectLocal;
31 import hero.interfaces.BnProjectLocalHome;
32 import hero.interfaces.BnProjectUtil;
33 import hero.interfaces.UserSessionUtil;
34 import hero.util.values.BonitaHookValue;
35 import hero.util.values.BonitaNodeValue;
36 import hero.util.values.BonitaProjectValue;
37
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.InstanceAlreadyExistsException JavaDoc;
41 import javax.management.MBeanRegistrationException JavaDoc;
42 import javax.management.NotCompliantMBeanException JavaDoc;
43 import javax.management.MalformedObjectNameException JavaDoc;
44 import javax.management.NotificationListener JavaDoc;
45 import javax.management.Notification JavaDoc;
46 import javax.naming.Context JavaDoc;
47 import org.objectweb.jonas.service.ServiceException;
48
49 //import objectweb
50
import org.objectweb.jonas.jmx.JmxService;
51 import org.objectweb.jonas.service.AbsServiceImpl;
52 import org.objectweb.jonas.service.ServiceManager;
53
54 public class TimerReceiver extends AbsServiceImpl implements TimerReceiverMBean, NotificationListener JavaDoc {
55
56     private MBeanServer JavaDoc mbeanServer = null;
57     public final static String JavaDoc SERVICE_NAME = "jonas:type=service,name=TimerReceiver";
58     private static final UserSessionUtil EngineSessionUtil = null;
59
60     public void doInit(Context JavaDoc ctx) throws ServiceException {
61         // Get the JMX Server via JMX Service
62

63                try {
64                 JmxService jmx = (JmxService) ServiceManager.getInstance().getJmxService();
65                  mbeanServer = jmx.getJmxServer();
66            } catch (Exception JavaDoc e) {
67             //the JMX service may not be started
68
throw new ServiceException("JMX Server is not started...");
69            }
70     }
71     
72     public void doStart() throws ServiceException {
73         try {
74             // Register MailService MBean : MailServiceImplMBean
75
mbeanServer.registerMBean(this,new ObjectName JavaDoc(SERVICE_NAME));
76             System.out.println("TimerReceiver :: Start Service");
77         } catch (InstanceAlreadyExistsException JavaDoc iae) {
78             throw new ServiceException("Cannot start the TimerReceiver Service (Already Exists)" , iae);
79         } catch (MBeanRegistrationException JavaDoc mbre) {
80             throw new ServiceException("Cannot start the TimerReceiver Service (MBean registration error)" , mbre);
81         } catch (NotCompliantMBeanException JavaDoc ncmbe) {
82             throw new ServiceException("Cannot start the TimerReceiver Service (MBean Not compliant error)" , ncmbe);
83         } catch (MalformedObjectNameException JavaDoc mone) {
84             throw new ServiceException("Cannot start the TimerReceiver Service (ObjectName Malformed)" , mone);
85         }
86     }
87
88     public void handleNotification(Notification JavaDoc n, Object JavaDoc handback) {
89         BnProjectLocal mProject=null;
90         BonitaProjectValue model;
91         BonitaNodeValue node;
92         Collection JavaDoc hooks;
93         
94         String JavaDoc type=(String JavaDoc)n.getType();
95         String JavaDoc projectName=type.substring(type.indexOf(":")+1);
96         projectName = projectName.substring(0,projectName.indexOf(":"));
97         String JavaDoc nodeName=type.substring(0,type.indexOf(":"));
98         try{
99             BnProjectLocalHome pHome = BnProjectUtil.getLocalHome();
100             mProject=pHome.findByName(projectName);
101         
102         }catch(Exception JavaDoc ple){ple.printStackTrace();}
103         
104         try {
105             model = mProject.getModel();
106             node = BonitaServiceValue.getNodeFromCache(model,nodeName);
107             
108             
109             if (nodeName.matches(".*_instance.*"))
110                 nodeName=nodeName.substring(0,nodeName.indexOf("_instance"));
111
112             BonitaNodeValue nodeLocal;
113             nodeLocal = BonitaServiceValue.getNodeFromCache(model,nodeName);
114                     
115             hooks = nodeLocal.getHooks();
116             for (Iterator JavaDoc i = hooks.iterator(); i.hasNext();) {
117                 BonitaHookValue hk = (BonitaHookValue) i.next();
118                 if (hk.getEvent().equalsIgnoreCase(hero.interfaces.Constants.Nd.ONDEADLINE)) {
119                     hero.hook.Hook h = hero.hook.Hook.make(hk.getName(), hero.interfaces.Constants.Nd.ONDEADLINE, hk.getType());
120                     //h.execute(this, hero.interfaces.Constants.Nd.ONDEADLINE, node);
121
}
122             }
123                         
124         }catch(HeroException hh){hh.printStackTrace();};
125     }
126     
127     public void doStop() throws ServiceException {
128         if (mbeanServer != null) {
129             try {
130                 mbeanServer.unregisterMBean(new ObjectName JavaDoc(SERVICE_NAME));
131             } catch (Exception JavaDoc e) {
132                 throw new ServiceException("Cannot stop the TimerReceiver Service (JMX).", e);
133             }
134         }
135         System.out.println("TimerReceiver :: Stop Service");
136     }
137 }
138
Popular Tags