Further documentation updates and improvements
[remote/remote-ws.git] / src / moteaccess / AbstractMoteAccess.java
blobe631885dedb49a8cf73fdd4aad4ba3e1cc2a9593
1 package moteaccess;
3 import java.sql.ResultSet;
4 import util.SQLHelper;
6 /** Generic base class for assigning mote usage privileges.
8 * This is an abstract class with the generic code to assign mote usage privileges.
9 * To integrate any custom reservation system, this class must be inherited and the
10 * abstract methods implemented.
11 * @author zept
13 public abstract class AbstractMoteAccess {
15 /** Acquire mote control privileges using specific session.
17 * @param mote_ids IDs of mote to control.
18 * @param session_id ID of session that wants privileges.
19 * @return A boolean array with entries for each requested mote.
20 * @throws Exception
22 public boolean[] getMoteControlPrivileges(long[] mote_ids, String session_id) throws Exception
24 boolean[] results = new boolean[mote_ids.length];
26 for (int i = 0; i < mote_ids.length; i++) {
27 results[i] = getMoteControlPrivilege(mote_ids[i], session_id);
30 return results;
33 /** Acquire control privilege for one mote.
35 * @param mote_id ID of mote to control.
36 * @param session_id ID of session that wants control privilege.
37 * @return True if mote control privilege was granted.
38 * @throws Exception
40 protected boolean getMoteControlPrivilege(long mote_id, String session_id) throws Exception
42 boolean access = false;
43 SQLHelper sql = null;
45 try {
46 // Check for current privileged session
47 String query = "select priv_session_id from mote " +
48 "where id="+mote_id;
49 sql = new SQLHelper();
50 sql.openDB();
51 ResultSet sqlres = sql.retrieve(query);
52 if (sqlres.next()){
53 String privsess = sqlres.getString("priv_session_id");
54 if (privsess == null || privsess.compareTo(session_id) == 0) {
55 access = true;
56 } else {
57 // check privileges with reservation system
58 if (hasReservation(mote_id,session_id))
60 access = true;
64 if (access) {
65 query = "update mote set priv_session_id="+session_id+
66 " where id="+mote_id;
67 sql.execute(query);
69 return access;
71 } finally {
72 sql.closeDB();
76 /** Check if session can reserve a given mote.
78 * This method must be implemented to integrate a custom
79 * reservation system. It is also a point for integration
80 * between user account system and reservation system.
82 * Note that the user model should specify the relevant
83 * reservation key upon session authentication. The details of
84 * how to do this are application specific.
86 * @param mote_id Database key for the mote to check
87 * @param session_id Database key for the session requesting access.
88 * @return True if there is a current reservation, false otherwise
89 * @throws Exception
91 abstract protected boolean hasReservation(long mote_id, String session_id) throws Exception;