KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > util > BonitaDeadline


1 /**
2  *
3  * Bonita
4  * Copyright (C) 1999 Bull S.A.
5  * Bull 68 route de versailles 78434 Louveciennes Cedex France
6  * Further information: bonita@objectweb.org
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  *
23  *
24 --------------------------------------------------------------------------
25  * $Id: BonitaDeadline.java,v 1.3 2005/03/18 14:55:05 mvaldes Exp $
26  *
27 --------------------------------------------------------------------------
28  */

29
30 package hero.util;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Vector JavaDoc;
38
39 import javax.management.MBeanServer JavaDoc;
40 import javax.management.MBeanServerFactory JavaDoc;
41 import javax.management.NotificationFilter JavaDoc;
42 import javax.management.ObjectName JavaDoc;
43
44 import hero.interfaces.BnNodeLocal;
45 import hero.interfaces.BnNodeLocalHome;
46 import hero.interfaces.BnNodeUtil;
47
48 public final class BonitaDeadline {
49     private static BonitaDeadline bdl = null; // Singleton pattern
50
private static ObjectName JavaDoc timer;
51     private static MBeanServer JavaDoc server;
52
53     /* replaced by startDeadlines
54     public void startDeadline(String nodeName, int p_id) throws HeroException{
55         try {
56             BnNodeLocalHome lh = BnNodeUtil.getLocalHome();
57             BnNodeLocal node = lh.findByName(nodeName,p_id);
58             
59             String type = null;
60             Date lNext=null;
61             if (node.getDeadline()!=null)
62             {
63                 type=node.getName() + ":" + node.getBnProject().getName()+":"+node.getDeadline().getTime();
64                 lNext=node.getDeadline();
65                 addNotification(lNext, type, node);
66             }
67             if (node.getRelativeDeadline()!=null)
68             {
69                 type=node.getName() + ":" + node.getBnProject().getName()+":"+node.getRelativeDeadline().getTime();
70                 lNext=new Date(new Date().getTime()+node.getRelativeDeadline().getTime());
71                 addNotification(lNext, type, node);
72             }
73         } catch (Exception e) {
74             e.printStackTrace();
75             throw new HeroException(e.getMessage());
76         }
77     }
78 */

79     
80     public void startDeadlines(String JavaDoc nodeName, String JavaDoc p_id) throws HeroException{
81         try {
82             BnNodeLocalHome lh = BnNodeUtil.getLocalHome();
83             BnNodeLocal node = lh.findByName(nodeName,p_id);
84             
85             String JavaDoc type = null;
86             Date JavaDoc lNext=null;
87             
88             // For absolute deadline
89
if (node.getDeadlines().size()!=0) {
90 //debug
91
System.out.println("startDeadlines : Nb of absolute deadlines = " + node.getDeadlines().size());
92                 Collection JavaDoc co = node.getDeadlines();
93                 Iterator JavaDoc it = co.iterator();
94                 ArrayList JavaDoc ar = new ArrayList JavaDoc();
95                 while (it.hasNext()){
96                     // the co contains Date elements
97
Date JavaDoc date = (Date JavaDoc)it.next();
98                     type=node.getName() + ":" + node.getBnProject().getName()+":"+date.getTime();
99                     lNext = date;
100                     // add notification for each deadline in the collection
101
addNotification(lNext, type, node);
102 //debug
103
System.out.println("startDeadlines for absolute deadlines: lnext = " + lNext.toString() + " type = " + type + " node = " + node.getName());
104                 }
105             }
106             // For relative deadline
107
if (node.getRelativeDeadlines().size()!=0) {
108 //debug
109
System.out.println("startDeadlines : Nb of relative deadlines = " + node.getRelativeDeadlines().size());
110                 Collection JavaDoc co = node.getRelativeDeadlines();
111                 Iterator JavaDoc it = co.iterator();
112                 ArrayList JavaDoc ar = new ArrayList JavaDoc();
113                 while (it.hasNext()){
114                     // the co contains Date elements
115
Date JavaDoc date = (Date JavaDoc)it.next();
116                     type=node.getName() + ":" + node.getBnProject().getName()+":"+date.getTime();
117                     //create absolute time for the notification
118
lNext=new Date JavaDoc(new Date JavaDoc().getTime()+date.getTime());
119                     // add notification for each deadline in the collection
120
addNotification(lNext, type, node);
121 //debug
122
System.out.println("startDeadlines for relative deadlines: lnext = " + lNext.toString() + " type = " + type + " node = " + node.getName());
123                 }
124             }
125         } catch (Exception JavaDoc e) {
126             e.printStackTrace();
127             throw new HeroException(e.getMessage());
128         }
129     }
130
131     
132     public void removeNodeNotification(String JavaDoc projectName, String JavaDoc nodeName, long date) throws HeroException {
133         try {
134             ObjectName JavaDoc timer = new ObjectName JavaDoc("jonas:type=service,name=Timer");
135             List JavaDoc list = MBeanServerFactory.findMBeanServer(null);
136             MBeanServer JavaDoc server = (MBeanServer JavaDoc) list.iterator().next();
137             String JavaDoc type=nodeName + ":" + projectName+":"+date;
138             
139             server.invoke(timer,"removeNotifications",new Object JavaDoc[] { type },new String JavaDoc[] { "".getClass().getName()});
140 //debug
141
System.out.println("RemoveDeadLineNotification for project: "+projectName+", node: "+nodeName + " date: " + date);
142         } catch (Exception JavaDoc e) {} // Remove and activity which deadline was already thrown
143
}
144     
145     private void addNotification(Date JavaDoc lNext, String JavaDoc type, BnNodeLocal node) throws Exception JavaDoc
146     {
147         Vector JavaDoc ids =(Vector JavaDoc) server.invoke(timer, "getNotificationIDs", new Object JavaDoc[] {type,},
148         new String JavaDoc[] {"".getClass().getName()});
149         
150         if (ids.isEmpty())
151         {
152             Integer JavaDoc id =(Integer JavaDoc) server.invoke(timer, "addNotification", new Object JavaDoc[] { type,
153                 //type
154
node.getName(), //message
155
"bonita", //user object
156
lNext, //delay time
157
},
158             new String JavaDoc[] {
159                 "".getClass().getName(),
160                 "".getClass().getName(),
161                 "java.lang.Object",
162                 Date JavaDoc.class.getName()});
163 //debug
164
System.out.println("Deadline: "+lNext+" was added for project: "+node.getBnProject().getName()+", node: "+node.getName());
165         }
166     }
167     
168     public static BonitaDeadline getInstance() throws HeroException{
169         if (bdl == null) {
170             bdl = new BonitaDeadline();
171         }
172         return bdl;
173     }
174     
175     public BonitaDeadline() throws HeroException{
176         try{
177             this.timer = new ObjectName JavaDoc("jonas:type=service,name=Timer");
178             ObjectName JavaDoc receiver = new ObjectName JavaDoc("jonas:type=service,name=TimerReceiver");
179             List JavaDoc list = MBeanServerFactory.findMBeanServer(null);
180             this.server = (MBeanServer JavaDoc) list.iterator().next();
181             NotificationFilter JavaDoc filter = new TimerFilter();
182             this.server.addNotificationListener(timer, receiver, filter, null);
183         }catch(Exception JavaDoc e){
184             e.printStackTrace();
185             throw new HeroException(e.getMessage());
186         }
187     }
188 }
189
Popular Tags