1 29 30 package hero.util; 31 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Date ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Vector ; 38 39 import javax.management.MBeanServer ; 40 import javax.management.MBeanServerFactory ; 41 import javax.management.NotificationFilter ; 42 import javax.management.ObjectName ; 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; private static ObjectName timer; 51 private static MBeanServer server; 52 53 79 80 public void startDeadlines(String nodeName, String p_id) throws HeroException{ 81 try { 82 BnNodeLocalHome lh = BnNodeUtil.getLocalHome(); 83 BnNodeLocal node = lh.findByName(nodeName,p_id); 84 85 String type = null; 86 Date lNext=null; 87 88 if (node.getDeadlines().size()!=0) { 90 System.out.println("startDeadlines : Nb of absolute deadlines = " + node.getDeadlines().size()); 92 Collection co = node.getDeadlines(); 93 Iterator it = co.iterator(); 94 ArrayList ar = new ArrayList (); 95 while (it.hasNext()){ 96 Date date = (Date )it.next(); 98 type=node.getName() + ":" + node.getBnProject().getName()+":"+date.getTime(); 99 lNext = date; 100 addNotification(lNext, type, node); 102 System.out.println("startDeadlines for absolute deadlines: lnext = " + lNext.toString() + " type = " + type + " node = " + node.getName()); 104 } 105 } 106 if (node.getRelativeDeadlines().size()!=0) { 108 System.out.println("startDeadlines : Nb of relative deadlines = " + node.getRelativeDeadlines().size()); 110 Collection co = node.getRelativeDeadlines(); 111 Iterator it = co.iterator(); 112 ArrayList ar = new ArrayList (); 113 while (it.hasNext()){ 114 Date date = (Date )it.next(); 116 type=node.getName() + ":" + node.getBnProject().getName()+":"+date.getTime(); 117 lNext=new Date (new Date ().getTime()+date.getTime()); 119 addNotification(lNext, type, node); 121 System.out.println("startDeadlines for relative deadlines: lnext = " + lNext.toString() + " type = " + type + " node = " + node.getName()); 123 } 124 } 125 } catch (Exception e) { 126 e.printStackTrace(); 127 throw new HeroException(e.getMessage()); 128 } 129 } 130 131 132 public void removeNodeNotification(String projectName, String nodeName, long date) throws HeroException { 133 try { 134 ObjectName timer = new ObjectName ("jonas:type=service,name=Timer"); 135 List list = MBeanServerFactory.findMBeanServer(null); 136 MBeanServer server = (MBeanServer ) list.iterator().next(); 137 String type=nodeName + ":" + projectName+":"+date; 138 139 server.invoke(timer,"removeNotifications",new Object [] { type },new String [] { "".getClass().getName()}); 140 System.out.println("RemoveDeadLineNotification for project: "+projectName+", node: "+nodeName + " date: " + date); 142 } catch (Exception e) {} } 144 145 private void addNotification(Date lNext, String type, BnNodeLocal node) throws Exception 146 { 147 Vector ids =(Vector ) server.invoke(timer, "getNotificationIDs", new Object [] {type,}, 148 new String [] {"".getClass().getName()}); 149 150 if (ids.isEmpty()) 151 { 152 Integer id =(Integer ) server.invoke(timer, "addNotification", new Object [] { type, 153 node.getName(), "bonita", lNext, }, 158 new String [] { 159 "".getClass().getName(), 160 "".getClass().getName(), 161 "java.lang.Object", 162 Date .class.getName()}); 163 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 ("jonas:type=service,name=Timer"); 178 ObjectName receiver = new ObjectName ("jonas:type=service,name=TimerReceiver"); 179 List list = MBeanServerFactory.findMBeanServer(null); 180 this.server = (MBeanServer ) list.iterator().next(); 181 NotificationFilter filter = new TimerFilter(); 182 this.server.addNotificationListener(timer, receiver, filter, null); 183 }catch(Exception e){ 184 e.printStackTrace(); 185 throw new HeroException(e.getMessage()); 186 } 187 } 188 } 189 | Popular Tags |