KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sessionsystem > UserSession


1 package com.daffodilwoods.daffodildb.server.sessionsystem;
2
3 import java.sql.*;
4 import java.sql.Date JavaDoc;
5 import java.util.*;
6
7 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
8 import com.daffodilwoods.daffodildb.server.serversystem.*;
9 import com.daffodilwoods.daffodildb.utils.comparator.*;
10 import com.daffodilwoods.database.general.*;
11 import com.daffodilwoods.database.resource.*;
12
13 /**
14  *
15  * <p>Title: </p> User Session
16  * <p>Description: </p>
17  * Wrapper over the Session with the addendum attributes userName and privilege
18  * charcteristics of the user for which this object is constructed. It also maintains
19  * a list of UserSessionTable on which user performs read and write operations
20  * and list of save point that applied by the user.
21  */

22
23 public class UserSession implements _UserSession {
24
25    private _Session session;
26
27    /**
28     * Name of the user
29     */

30
31    private String JavaDoc userName;
32
33    /**
34     * This object is used to get the Privilege Table
35     */

36
37    private _PrivilegeCharacteristics privilegeCharacteristics;
38
39    /**
40     * maintains the list of object of UserSessionTable
41     */

42
43    private WeakOrderedKeyList userSessionTableList = new WeakOrderedKeyList(new CTusjohDbtfJoTfotjujwfDpnqbsbups());
44
45    /**
46     * List of names of the save points started by the user itself.
47     */

48
49    ArrayList savePoints = new ArrayList(5);
50
51    /**
52     * Constructs the UserSession
53     */

54
55    public UserSession(_Session session0, String JavaDoc userName0, _PrivilegeCharacteristics privilegeCharacteristics0) throws DException {
56       session = session0;
57       userName = userName0;
58       privilegeCharacteristics = privilegeCharacteristics0;
59    }
60
61    /**
62     * Provides an object of UserSessionTable for performing Read and Write Operations.
63     * @param tableName Qualified name for the table
64     * @return _UserSessionTable
65     * @throws DException
66     */

67
68    public _UserSessionTable getUserSessionTable(QualifiedIdentifier tableName) throws DException {
69       _UserSessionTable userSessionTable = (_UserSessionTable) userSessionTableList.get(tableName.getIdentifier());
70       if (userSessionTable != null)
71          return userSessionTable;
72       _SessionTable sessionTable = session.getSessionTable(tableName);
73       try {
74          _PrivilegeTable privilegeTable = privilegeCharacteristics.getPrivilegeTable(tableName);
75          userSessionTable = new UserSessionTable(tableName, sessionTable, privilegeTable, this);
76          userSessionTableList.put(tableName.getIdentifier(), userSessionTable);
77          return userSessionTable;
78       } catch (DatabaseException ex) {
79          TableException tb = new TableException("DSE810", null);
80          tb.setException(ex);
81          throw tb;
82       }
83    }
84
85    /**
86     * Provides the object of Transaction mode which containts the information of
87     * AccessMode, DiagnosticsSize and IsolationLevel
88     * @return Object of SessionTransactionMode
89     * @throws DException
90     */

91
92    public Object JavaDoc getTransactionAccessMode() throws DException {
93       return session.getTransactionMode().getTransactionAccessMode();
94    }
95
96    /**
97     * Forwards call to session to starting a save point
98     * @throws DException
99     */

100
101    public void startSavePoint() throws DException {
102       if (session.getAutoCommit())
103          return;
104       session.startSavePoint();
105    }
106
107    /**
108     * First of all it releases all save points by committing all the work done by
109     * them and then it forwards the call to session for transferrnig the records
110     * from memory to file.
111     * @param statementExecutionContext Forwards this object to session to add all the events
112     * @throws DException
113     */

114
115    public void commit(_StatementExecutionContext statementExecutionContext) throws DException {
116       int savePoints = session.getNumberOfSavePiontsStarted();
117       for (int i = 1; i <= savePoints; i++) {
118          session.commitSavePoint(statementExecutionContext);
119       }
120
121       statementExecutionContext.setUserSession(this);
122       session.commit(statementExecutionContext);
123    }
124
125    /**
126     * Undo all uncommitted work done by this session
127     * @param statementExecutionContext Forwards this object to session to add all the events
128     * @throws DException
129     */

130
131    public void rollback(_StatementExecutionContext statementExecutionContext) throws DException {
132       int savePoints = session.getNumberOfSavePiontsStarted();
133       for (int i = 1; i <= savePoints; i++) {
134          session.rollbackSavePoint(statementExecutionContext);
135       }
136       session.rollback(statementExecutionContext);
137    }
138
139    /**
140     * Provides the object of session.
141     * @return _Session
142     * @throws DException
143     */

144
145    public _Session getSession() throws DException {
146       return session;
147    }
148
149    /**
150     * Provides the Privilege Characteristics granted to user.
151     * @return _PrivilegeCharacteristics
152     * @throws DException
153     */

154
155    public _PrivilegeCharacteristics getPrivilegeCharacteristics() throws DException {
156       return privilegeCharacteristics;
157    }
158
159    /**
160     * Provides the current role of the user.
161     * @return name of the role
162     * @throws DException
163     */

164
165    public String JavaDoc getCurrentRole() throws DException {
166       return session.getCurrentRole();
167    }
168
169    /**
170     * Provides the current user name of the session.
171     * @return String
172     * @throws DException
173     */

174
175    public String JavaDoc getCurrentUser() throws DException {
176       return userName;
177    }
178
179    /**
180     * Forwards call to session
181     * @param authorizationIdentifier name of the authorization which is to be checked
182     * @return true if present in authorization list.
183     * @throws DException
184     */

185
186    public boolean isEnabledAuthorizationIdentifier(String JavaDoc authorizationIdentifier) throws DException {
187       return privilegeCharacteristics.isEnabledAuthorizationIdentifier(authorizationIdentifier);
188    }
189
190    /**
191     * Forwards call to session to get the autorizationIdentifier.
192     * @return name of the current authorizationIdentifier.
193     * @throws DException
194     */

195
196    public String JavaDoc getAuthorizationIdentifier() throws DException {
197       return session.getAuthorizationIdentifier();
198    }
199
200    /**
201     * Sets the mode for the current transaction in the session.
202     * @param sessionTransactionMode
203     * @throws DException
204     */

205
206    public void setTransactionMode(SessionTransactionMode sessionTransactionMode) throws DException {
207       session.setTransactionMode(sessionTransactionMode);
208    }
209
210    /**
211     * Returns the mode for the current transaction in the session.
212     * @return
213     * @throws DException
214     */

215
216    public SessionTransactionMode getTransactionMode() throws DException {
217       return session.getTransactionMode();
218    }
219
220    /**
221     * Provides the list of save point.
222     * @return list of save point in a vector
223     * @throws DException
224     */

225
226    public ArrayList getSavePointVector() throws DException {
227       return savePoints;
228    }
229
230    /**
231     * adds a given save point in the list.
232     * @throws DException
233     */

234
235    public void addSavePoint(Object JavaDoc obj) throws DException {
236       savePoints.add( ( (String JavaDoc) obj).toUpperCase());
237    }
238
239    public Date JavaDoc getDate() throws DException {
240       return session.getDate();
241    }
242
243    public Time getTime() throws DException {
244       return session.getTime();
245    }
246
247    public int getMonth() throws DException {
248       return session.getMonth();
249    }
250
251    public int getYear() throws DException {
252       return session.getYear();
253    }
254
255    public int getHour() throws DException {
256       return session.getHour();
257    }
258
259    public int getMinutes() throws DException {
260       return session.getMinutes();
261    }
262
263    public int getSeconds() throws DException {
264       return session.getSeconds();
265    }
266
267    /**
268     * removes the last save point from the list.
269     * @throws DException
270     */

271
272    public void releaseLastSavePoint() throws DException {
273       savePoints.remove(savePoints.size() - 1);
274    }
275
276    public TimeZone getTimeZone() throws DException {
277       return session.getTimeZone();
278    }
279
280    public void setConstraintStatus(Object JavaDoc constraintName, String JavaDoc mode) throws DException {
281       session.setConstraintStatus(constraintName, mode);
282
283    }
284
285    public java.sql.Timestamp JavaDoc getTimeStamp() throws DException {
286       return session.getTimeStamp();
287
288    }
289
290    /**
291     * returns the user name of the session.
292     * @return String
293     * @throws DException
294     */

295
296    public String JavaDoc getUserName() throws DException {
297       return userName;
298    }
299
300    public Object JavaDoc getSessionConstant() throws DException {
301       return session.getSessionConstant();
302    }
303
304    public _ServerSession getGlobalSession() throws DException {
305       return session.getGlobalSession();
306    }
307
308    public boolean prepare(_StatementExecutionContext statementExecutionContext) throws DException {
309       session.prepare(statementExecutionContext);
310       return true;
311    }
312
313    public boolean makePersistent(_StatementExecutionContext statementExecutionContext) throws DException {
314       session.makePersistent(statementExecutionContext);
315       return true;
316    }
317
318    /**
319     * will set the User name and plcae the use name to Stack through pushAuthorizationIdentifier.
320     * @param userName
321     * @throws DException
322     */

323
324    public void setAuthorizationIdentifier(String JavaDoc userName0) throws DException {
325       userName = userName0;
326       _DataDictionary dataDictionary = session.getSessionDatabase().getDataDictionary();
327       if (session.getAuthorizationIdentifier().equalsIgnoreCase(userName))
328          dataDictionary.refreshPrivilegeCharacteristics(session.getAuthorizationIdentifier() + _PrivilegeCharacteristics.AUTHORIZATION_USER);
329       else
330          dataDictionary.refreshPrivilegeCharacteristics(session.getAuthorizationIdentifier() + _PrivilegeCharacteristics.AUTHORIZATION_ROLE);
331       AuthorizationIdentifier authoriz = new AuthorizationIdentifier();
332       authoriz.setUser(userName);
333       userSessionTableList = new WeakOrderedKeyList(new CTusjohDbtfJoTfotjujwfDpnqbsbups());
334       privilegeCharacteristics = dataDictionary.getPrivilegeCharacteristics(userName, _PrivilegeCharacteristics.AUTHORIZATION_USER);
335       session.pushAuthorizationIdentifier(authoriz);
336    }
337
338    /**
339     * Sets the Privileges granted to the user.
340     * @param priCharacteristics
341     * @throws DException
342     */

343
344    public void setPrivilegeCharacteristics(_PrivilegeCharacteristics priCharacteristics) throws DException {
345       privilegeCharacteristics = priCharacteristics;
346    }
347
348    /**
349     * removes all the table from session table list
350     * @throws DException
351     */

352
353    public void removeAllTables() throws DException {
354       WeakOrderedKeyList.WeakOrderedKeyListIterator iterator = userSessionTableList.getWeakOrderedKeyListIterator();
355       if (iterator.top())
356          do {
357             Object JavaDoc key = iterator.getKey();
358             userSessionTableList.remove(key);
359          } while (iterator.next());
360       session.removeAllTables();
361    }
362
363    public void removeTable(QualifiedIdentifier tableName) throws DException {
364       userSessionTableList.remove(tableName.getIdentifier());
365       session.removeTable(tableName);
366    }
367
368    public void commitSavePoint(_StatementExecutionContext sec) throws DException {
369       if (session.getAutoCommit())
370          return;
371       sec.setUserSession(this);
372       session.commitSavePoint(sec);
373    }
374
375    public void rollbackSavePoint(_StatementExecutionContext sec) throws DException {
376       if (session.getAutoCommit()) {
377          rollback(sec);
378          return;
379       }
380
381       session.rollbackSavePoint(sec);
382    }
383
384    public void releaseSavePoint(_StatementExecutionContext sec) throws DException {
385       sec.setUserSession(this);
386       session.releaseSavePoint(sec);
387    }
388
389    public void hideSavePoint() throws DException {
390       session.hideSavePoint();
391    }
392
393    public void unhideSavePoint() throws DException {
394       session.unhideSavePoint();
395    }
396
397    public void ignoreParallelSavePoint() throws DException {
398       session.ignoreParallelSavePoint();
399    }
400
401    public void allowParallelSavePoint() throws DException {
402       session.allowParallelSavePoint();
403    }
404
405    public void checkImmediateConstraintsOnCommit() throws DException {
406       session.checkImmediateConstraintsOnCommit();
407    }
408
409    public void setRole(String JavaDoc roleName) throws DException {
410       _DataDictionary dataDictionary = session.getSessionDatabase().getDataDictionary();
411       if (session.getAuthorizationIdentifier().equalsIgnoreCase(userName))
412          dataDictionary.refreshPrivilegeCharacteristics(session.getAuthorizationIdentifier() + _PrivilegeCharacteristics.AUTHORIZATION_USER);
413       else
414          dataDictionary.refreshPrivilegeCharacteristics(session.getAuthorizationIdentifier() + _PrivilegeCharacteristics.AUTHORIZATION_ROLE);
415       AuthorizationIdentifier authoriz = new AuthorizationIdentifier();
416       userSessionTableList = new WeakOrderedKeyList(new CTusjohDbtfJoTfotjujwfDpnqbsbups());
417       if (roleName == null) {
418          privilegeCharacteristics = dataDictionary.getPrivilegeCharacteristics(userName, _PrivilegeCharacteristics.AUTHORIZATION_USER);
419          authoriz.setUser(userName);
420       } else {
421          privilegeCharacteristics = dataDictionary.getPrivilegeCharacteristics(roleName, _PrivilegeCharacteristics.AUTHORIZATION_ROLE);
422          authoriz.setRole(roleName);
423       }
424       session.pushAuthorizationIdentifier(authoriz);
425    }
426
427    public void setAutoCommit(boolean autoCommit0) {
428       session.setAutoCommit(autoCommit0);
429    }
430
431    public boolean getAutoCommit() {
432       return session.getAutoCommit();
433    }
434
435    public void startTransaction() throws DException {
436       session.startTransaction();
437    }
438 }
439
Popular Tags