1 package at.ac.tuwien.sbc.valesriegler.xvsm;
3 import java.io.Serializable;
6 import java.util.Timer;
7 import java.util.TimerTask;
9 import org.mozartspaces.notifications.Notification;
10 import org.mozartspaces.notifications.NotificationListener;
11 import org.mozartspaces.notifications.Operation;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
17 * The SpaceListener provides the {@link NotificationListener} interface.
19 * In {@link SpaceListener#getEntries()} a method for reading the entities, that are normally expected from the notification,
20 * must be supplied. Thus, it's possible in {@link SpaceListener#startHandlingAbsenceOfNotifications()} to start a timer and to
21 * execute the actions, which are normally triggered by the notification, with space objects read from {@link SpaceListener#getEntries()}.
22 * If {@link SpaceListener#lookAround} is true the timer periodically checks if the corresponding {@link AbstractXVSMConnector#timeOflastOperation} is old enough (= the agent is idle for some time)
23 * and possibly executes the action as a result.
25 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
28 public abstract class SpaceListener implements NotificationListener {
29 private static final Logger log = LoggerFactory
30 .getLogger(SpaceListener.class);
32 protected boolean lookAround = true;
35 public void entryOperationFinished(Notification arg0, Operation arg1,
36 List<? extends Serializable> entries) {
37 synchronized (AbstractXVSMConnector.lockObject) {
39 onEntriesWritten(entries);
40 } catch (Exception e) {
41 // e.printStackTrace();
43 AbstractXVSMConnector.timeOflastOperation = new Date().getTime();
47 abstract List<? extends Serializable> getEntries() throws Exception;
49 abstract void onEntriesWritten(List<? extends Serializable> entries)
52 public void startHandlingAbsenceOfNotifications() {
53 if(!lookAround) return;
55 Timer timer = new Timer();
57 timer.schedule(new SpaceTask(), 500, 3000);
60 class SpaceTask extends TimerTask {
64 long time = new Date().getTime();
65 if(time-3000 <= AbstractXVSMConnector.timeOflastOperation) return;
66 synchronized(AbstractXVSMConnector.lockObject) {
69 List<? extends Serializable> entries = getEntries();
70 if(entries.size() != 0) {
71 onEntriesWritten(entries);
73 } catch (Exception e) {
74 log.info(e.getMessage());
75 // e.printStackTrace();
77 AbstractXVSMConnector.timeOflastOperation = new Date().getTime();