View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gwes;
9   
10  import net.kwfgrid.gwes.exception.DatabaseException;
11  import org.apache.log4j.Logger;
12  
13  /**
14   * Manages opens and closes sessions to the remote XML db.
15   *
16   * @author Andreas Hoheisel
17   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
18   * @version $Id: XMLDBSessionManager.java 1419 2010-11-01 14:12:17Z hoheisel $
19   */
20  final class XMLDBSessionManager extends Thread {
21  
22      /**
23       * Timeout in milliseconds for a session.
24       */
25      private final static int SESSION_TIMEOUT = 1000 * 60;
26  
27      /**
28       * log4j logger
29       */
30      static final Logger logger = Logger.getLogger(XMLDBSessionManager.class);
31  
32      /**
33       * Admin session object
34       */
35      public final Object adminLock = new Object();
36  
37      /**
38       * Query session object
39       */
40      public final Object queryLock = new Object();
41  
42      /**
43       * Admin session id
44       */
45      private String adminSessionId;
46  
47      /**
48       * Query session id
49       */
50      private String querySessionId;
51  
52      /**
53       * Admin session timestamp last usage
54       */
55      private long adminTimestamp;
56  
57      /**
58       * Query session timestamp last usage
59       */
60      private long queryTimestamp;
61  
62      private XMLDB xmldb;
63  
64      static int threadcount = 0;
65  
66      /**
67       * Constructor.
68       */
69      public XMLDBSessionManager(XMLDB xmldb) throws DatabaseException {
70          super("XMLDBSessionManager#" + (++threadcount));
71          this.xmldb = xmldb;
72          start();
73      }
74  
75      /**
76       * Start the thread. Disconnects from old sessions.
77       */
78      public final void run() {
79          logger.info(this + " started ...");
80          while (!isInterrupted()) {
81              long now = System.currentTimeMillis();
82              try {
83                  logger.debug("Searching for old sessions");
84                  // search for old admin sessions
85                  synchronized (adminLock) {
86                      if (adminSessionId != null && now - adminTimestamp > SESSION_TIMEOUT) {
87                          xmldb.disconnectAdmin(adminSessionId);
88                          adminSessionId = null;
89                          logger.debug("Admin session disconnected");
90                      }
91                  }
92              } catch (DatabaseException e) {
93                  logger.error("exception: " + e, e);
94                  synchronized (adminLock) {
95                      adminSessionId = null;
96                      logger.debug("Admin session destroyed");
97                  }
98              }
99              try {
100                 // search for old query sessions
101                 synchronized (queryLock) {
102                     if (querySessionId != null && now - queryTimestamp > SESSION_TIMEOUT) {
103                         xmldb.disconnectQuery(querySessionId);
104                         querySessionId = null;
105                         logger.debug("Query session disconnected");
106                     }
107                 }
108             } catch (DatabaseException e) {
109                 logger.error("exception: " + e, e);
110                 synchronized (queryLock) {
111                     querySessionId = null;
112                     logger.debug("Query session destroyed");
113                 }
114             }
115             try {
116                 Thread.sleep(SESSION_TIMEOUT);
117             } catch (InterruptedException e1) {
118                 interrupt();
119             }
120         }
121     }
122 
123     /**
124      * Returns a valid admin session ID.
125      * Reconnects to the XMLDB using the Admin stub if it was disconnected due to timeoutRunning.
126      * You should synchronize on the adminLock object before invoking this method.
127      *
128      * @return The session ID.
129      * @throws DatabaseException
130      */
131     public String getAdminSessionId() throws DatabaseException {
132         if (adminSessionId == null) {
133             adminSessionId = xmldb.connectAdmin();
134         }
135         adminTimestamp = System.currentTimeMillis();
136         return adminSessionId;
137     }
138 
139     /**
140      * Returns a valid query session ID.
141      * Reconnects to the XMLDB using the Query stub if it was disconnected due to timeoutRunning.
142      * You should synchronize on the queryLock object before invoking this method.
143      *
144      * @return The session ID.
145      * @throws DatabaseException
146      */
147     public String getQuerySessionId() throws DatabaseException {
148         if (querySessionId == null) {
149             querySessionId = xmldb.connectQuery();
150         }
151         queryTimestamp = System.currentTimeMillis();
152         return querySessionId;
153     }
154 
155     /**
156      * Reset session, e.g., after an exception.
157      */
158     public void resetSession() {
159         // try to disconnect from admin session.
160         try {
161             synchronized (adminLock) {
162                 if (adminSessionId != null) {
163                     xmldb.disconnectAdmin(adminSessionId);
164                     logger.debug("Admin session disconnected");
165                     adminSessionId = null;
166                 }
167             }
168         } catch (DatabaseException e) {
169             adminSessionId = null;
170         }
171 
172         // try to disconnect from query session.
173         try {
174             synchronized (queryLock) {
175                 if (querySessionId != null) {
176                     xmldb.disconnectQuery(querySessionId);
177                     logger.debug("Query session disconnected");
178                     querySessionId = null;
179                 }
180             }
181         } catch (DatabaseException e) {
182             querySessionId = null;
183         }
184 
185     }
186 
187 }