143 } 144 } 145 146 /** 147 * {@inheritDoc} 148 */ 149 public Set<Integer> activeVms() { 150 return vmManager.activeVms(); 151 } 152 153 /** 154 * Fire VmEvent events. 155 * 156 * @param active a set of Integer objects containing the vmid of 157 * the active Vms 158 * @param started a set of Integer objects containing the vmid of 159 * new Vms started since last interval. 160 * @param terminated a set of Integer objects containing the vmid of 161 * terminated Vms since last interval. 162 */ 163 private void fireVmStatusChangedEvents(Set active, Set started, 164 Set terminated) { 165 ArrayList registered = null; 166 VmStatusChangeEvent ev = null; 167 168 synchronized(listeners) { 169 registered = (ArrayList)listeners.clone(); 170 } 171 172 for (Iterator i = registered.iterator(); i.hasNext(); /* empty */) { 173 HostListener l = (HostListener)i.next(); 174 if (ev == null) { 175 ev = new VmStatusChangeEvent(this, active, started, terminated); 176 } 177 l.vmStatusChanged(ev); 178 } 179 } 180 181 /** 182 * Class to poll the local system and generate event notifications. 183 */ 184 private class NotifierTask extends CountedTimerTask { 185 public void run() { 186 super.run(); 187 188 // save the last set of active JVMs 189 Set lastActiveVms = activeVms; 190 191 // get the current set of active JVMs 192 activeVms = (HashSet<Integer>)vmManager.activeVms(); 193 194 if (activeVms.isEmpty()) { 195 return; 196 } 197 Set<Integer> startedVms = new HashSet<Integer>(); 198 Set<Object> terminatedVms = new HashSet<Object>(); 199 200 for (Iterator i = activeVms.iterator(); i.hasNext(); /* empty */) { 201 Integer vmid = (Integer)i.next(); 202 if (!lastActiveVms.contains(vmid)) { 203 // a new file has been detected, add to set 204 startedVms.add(vmid); 205 } 206 } 207 208 for (Iterator i = lastActiveVms.iterator(); i.hasNext(); 209 /* empty */) { 210 Object o = i.next(); 211 if (!activeVms.contains(o)) { 212 // JVM has terminated, remove it from the active list 213 terminatedVms.add(o); 214 } 215 } 216 217 if (!startedVms.isEmpty() || !terminatedVms.isEmpty()) { 218 fireVmStatusChangedEvents(activeVms, startedVms, 219 terminatedVms); 220 } 221 } 222 } 223 } | 143 } 144 } 145 146 /** 147 * {@inheritDoc} 148 */ 149 public Set<Integer> activeVms() { 150 return vmManager.activeVms(); 151 } 152 153 /** 154 * Fire VmEvent events. 155 * 156 * @param active a set of Integer objects containing the vmid of 157 * the active Vms 158 * @param started a set of Integer objects containing the vmid of 159 * new Vms started since last interval. 160 * @param terminated a set of Integer objects containing the vmid of 161 * terminated Vms since last interval. 162 */ 163 @SuppressWarnings("unchecked") // Cast of result of clone 164 private void fireVmStatusChangedEvents(Set<Integer> active, Set<Integer> started, 165 Set<Integer> terminated) { 166 ArrayList<HostListener> registered = null; 167 VmStatusChangeEvent ev = null; 168 169 synchronized(listeners) { 170 registered = (ArrayList)listeners.clone(); 171 } 172 173 for (Iterator<HostListener> i = registered.iterator(); i.hasNext(); /* empty */) { 174 HostListener l = i.next(); 175 if (ev == null) { 176 ev = new VmStatusChangeEvent(this, active, started, terminated); 177 } 178 l.vmStatusChanged(ev); 179 } 180 } 181 182 /** 183 * Class to poll the local system and generate event notifications. 184 */ 185 private class NotifierTask extends CountedTimerTask { 186 public void run() { 187 super.run(); 188 189 // save the last set of active JVMs 190 Set<Integer> lastActiveVms = activeVms; 191 192 // get the current set of active JVMs 193 activeVms = (HashSet<Integer>)vmManager.activeVms(); 194 195 if (activeVms.isEmpty()) { 196 return; 197 } 198 Set<Integer> startedVms = new HashSet<>(); 199 Set<Integer> terminatedVms = new HashSet<>(); 200 201 for (Iterator<Integer> i = activeVms.iterator(); i.hasNext(); /* empty */) { 202 Integer vmid = i.next(); 203 if (!lastActiveVms.contains(vmid)) { 204 // a new file has been detected, add to set 205 startedVms.add(vmid); 206 } 207 } 208 209 for (Iterator<Integer> i = lastActiveVms.iterator(); i.hasNext(); 210 /* empty */) { 211 Integer o = i.next(); 212 if (!activeVms.contains(o)) { 213 // JVM has terminated, remove it from the active list 214 terminatedVms.add(o); 215 } 216 } 217 218 if (!startedVms.isEmpty() || !terminatedVms.isEmpty()) { 219 fireVmStatusChangedEvents(activeVms, startedVms, 220 terminatedVms); 221 } 222 } 223 } 224 } |