KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > SrvSession


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server;
18
19 import java.net.InetAddress JavaDoc;
20
21 import javax.transaction.Status JavaDoc;
22 import javax.transaction.SystemException JavaDoc;
23 import javax.transaction.UserTransaction JavaDoc;
24
25 import org.alfresco.error.AlfrescoRuntimeException;
26 import org.alfresco.filesys.server.auth.ClientInfo;
27 import org.alfresco.filesys.server.core.SharedDevice;
28 import org.alfresco.filesys.server.core.SharedDeviceList;
29 import org.alfresco.service.transaction.TransactionService;
30
31 /**
32  * Server Session Base Class
33  * <p>
34  * Base class for server session implementations for different protocols.
35  */

36 public abstract class SrvSession
37 {
38
39     // Network server this session is associated with
40

41     private NetworkServer m_server;
42
43     // Session id/slot number
44

45     private int m_sessId;
46
47     // Unique session id string
48

49     private String JavaDoc m_uniqueId;
50
51     // Process id
52

53     private int m_processId = -1;
54
55     // Session/user is logged on/validated
56

57     private boolean m_loggedOn;
58
59     // Client details
60

61     private ClientInfo m_clientInfo;
62
63     // Challenge key used for this session
64

65     private byte[] m_challenge;
66
67     // Debug flags for this session
68

69     private int m_debug;
70     private String JavaDoc m_dbgPrefix;
71
72     // Session shutdown flag
73

74     private boolean m_shutdown;
75
76     // Protocol type
77

78     private String JavaDoc m_protocol;
79
80     // Remote client/host name
81

82     private String JavaDoc m_remoteName;
83
84     // Authentication token, used during logon
85

86     private Object JavaDoc m_authToken;
87
88     // List of dynamic/temporary shares created for this session
89

90     private SharedDeviceList m_dynamicShares;
91     
92     // Active transaction and read/write flag
93

94     private UserTransaction JavaDoc m_transaction;
95     private boolean m_readOnlyTrans;
96     
97     // Request and transaction counts
98

99     protected int m_reqCount;
100     protected int m_transCount;
101     protected int m_transConvCount;
102     
103     /**
104      * Class constructor
105      *
106      * @param sessId int
107      * @param srv NetworkServer
108      * @param proto String
109      * @param remName String
110      */

111     public SrvSession(int sessId, NetworkServer srv, String JavaDoc proto, String JavaDoc remName)
112     {
113         m_sessId = sessId;
114         m_server = srv;
115
116         setProtocolName(proto);
117         setRemoteName(remName);
118     }
119
120     /**
121      * Add a dynamic share to the list of shares created for this session
122      *
123      * @param shrDev SharedDevice
124      */

125     public final void addDynamicShare(SharedDevice shrDev) {
126         
127         // Check if the dynamic share list must be allocated
128

129         if ( m_dynamicShares == null)
130             m_dynamicShares = new SharedDeviceList();
131             
132         // Add the new share to the list
133

134         m_dynamicShares.addShare(shrDev);
135     }
136     
137     /**
138      * Return the authentication token
139      *
140      * @return Object
141      */

142     public final Object JavaDoc getAuthenticationToken()
143     {
144         return m_authToken;
145     }
146     
147     /**
148      * Determine if the authentication token is set
149      *
150      * @return boolean
151      */

152     public final boolean hasAuthenticationToken()
153     {
154         return m_authToken != null ? true : false;
155     }
156     
157     /**
158      * Return the session challenge key
159      *
160      * @return byte[]
161      */

162     public final byte[] getChallengeKey()
163     {
164         return m_challenge;
165     }
166
167     /**
168      * Determine if the challenge key has been set for this session
169      *
170      * @return boolean
171      */

172     public final boolean hasChallengeKey()
173     {
174         return m_challenge != null ? true : false;
175     }
176
177     /**
178      * Return the process id
179      *
180      * @return int
181      */

182     public final int getProcessId()
183     {
184         return m_processId;
185     }
186
187     /**
188      * Return the remote client network address
189      *
190      * @return InetAddress
191      */

192     public abstract InetAddress JavaDoc getRemoteAddress();
193
194     /**
195      * Return the session id for this session.
196      *
197      * @return int
198      */

199     public final int getSessionId()
200     {
201         return m_sessId;
202     }
203
204     /**
205      * Return the server this session is associated with
206      *
207      * @return NetworkServer
208      */

209     public final NetworkServer getServer()
210     {
211         return m_server;
212     }
213
214     /**
215      * Check if the session has valid client information
216      *
217      * @return boolean
218      */

219     public final boolean hasClientInformation()
220     {
221         return m_clientInfo != null ? true : false;
222     }
223
224     /**
225      * Return the client information
226      *
227      * @return ClientInfo
228      */

229     public final ClientInfo getClientInformation()
230     {
231         return m_clientInfo;
232     }
233
234     /**
235      * Determine if the session has any dynamic shares
236      *
237      * @return boolean
238      */

239     public final boolean hasDynamicShares() {
240         return m_dynamicShares != null ? true : false;
241     }
242
243     /**
244      * Return the list of dynamic shares created for this session
245      *
246      * @return SharedDeviceList
247      */

248     public final SharedDeviceList getDynamicShareList() {
249         return m_dynamicShares;
250     }
251
252     /**
253      * Determine if the protocol type has been set
254      *
255      * @return boolean
256      */

257     public final boolean hasProtocolName()
258     {
259         return m_protocol != null ? true : false;
260     }
261
262     /**
263      * Return the protocol name
264      *
265      * @return String
266      */

267     public final String JavaDoc getProtocolName()
268     {
269         return m_protocol;
270     }
271
272     /**
273      * Determine if the remote client name has been set
274      *
275      * @return boolean
276      */

277     public final boolean hasRemoteName()
278     {
279         return m_remoteName != null ? true : false;
280     }
281
282     /**
283      * Return the remote client name
284      *
285      * @return String
286      */

287     public final String JavaDoc getRemoteName()
288     {
289         return m_remoteName;
290     }
291
292     /**
293      * Determine if the session is logged on/validated
294      *
295      * @return boolean
296      */

297     public final boolean isLoggedOn()
298     {
299         return m_loggedOn;
300     }
301
302     /**
303      * Determine if the session has been shut down
304      *
305      * @return boolean
306      */

307     public final boolean isShutdown()
308     {
309         return m_shutdown;
310     }
311
312     /**
313      * Return the unique session id
314      *
315      * @return String
316      */

317     public final String JavaDoc getUniqueId()
318     {
319         return m_uniqueId;
320     }
321
322     /**
323      * Determine if the specified debug flag is enabled.
324      *
325      * @return boolean
326      * @param dbg int
327      */

328     public final boolean hasDebug(int dbgFlag)
329     {
330         if ((m_debug & dbgFlag) != 0)
331             return true;
332         return false;
333     }
334
335     /**
336      * Set the authentication token
337      *
338      * @param authToken Object
339      */

340     public final void setAuthenticationToken(Object JavaDoc authToken)
341     {
342         m_authToken = authToken;
343     }
344     
345     /**
346      * Set the client information
347      *
348      * @param client ClientInfo
349      */

350     public final void setClientInformation(ClientInfo client)
351     {
352         m_clientInfo = client;
353     }
354
355     /**
356      * Set the session challenge key
357      *
358      * @param key byte[]
359      */

360     public final void setChallengeKey(byte[] key)
361     {
362         m_challenge = key;
363     }
364
365     /**
366      * Set the debug output interface.
367      *
368      * @param flgs int
369      */

370     public final void setDebug(int flgs)
371     {
372         m_debug = flgs;
373     }
374
375     /**
376      * Set the debug output prefix for this session
377      *
378      * @param prefix String
379      */

380     public final void setDebugPrefix(String JavaDoc prefix)
381     {
382         m_dbgPrefix = prefix;
383     }
384
385     /**
386      * Set the logged on/validated status for the session
387      *
388      * @param loggedOn boolean
389      */

390     public final void setLoggedOn(boolean loggedOn)
391     {
392         m_loggedOn = loggedOn;
393     }
394
395     /**
396      * Set the process id
397      *
398      * @param id int
399      */

400     public final void setProcessId(int id)
401     {
402         m_processId = id;
403     }
404
405     /**
406      * Set the protocol name
407      *
408      * @param name String
409      */

410     public final void setProtocolName(String JavaDoc name)
411     {
412         m_protocol = name;
413     }
414
415     /**
416      * Set the remote client name
417      *
418      * @param name String
419      */

420     public final void setRemoteName(String JavaDoc name)
421     {
422         m_remoteName = name;
423     }
424
425     /**
426      * Set the session id for this session.
427      *
428      * @param id int
429      */

430     public final void setSessionId(int id)
431     {
432         m_sessId = id;
433     }
434
435     /**
436      * Set the unique session id
437      *
438      * @param unid String
439      */

440     public final void setUniqueId(String JavaDoc unid)
441     {
442         m_uniqueId = unid;
443     }
444
445     /**
446      * Set the shutdown flag
447      *
448      * @param flg boolean
449      */

450     protected final void setShutdown(boolean flg)
451     {
452         m_shutdown = flg;
453     }
454
455     /**
456      * Close the network session
457      */

458     public void closeSession()
459     {
460         // Release any dynamic shares owned by this session
461

462         if ( hasDynamicShares()) {
463             
464             // Close the dynamic shares
465

466             getServer().getShareMapper().deleteShares(this);
467         }
468     }
469     
470     /**
471      * Create and start a transaction, if not already active
472      *
473      * @param transService TransactionService
474      * @param readOnly boolean
475      * @return boolean
476      * @exception AlfrescoRuntimeException
477      */

478     public final boolean beginTransaction(TransactionService transService, boolean readOnly)
479         throws AlfrescoRuntimeException
480     {
481         boolean created = false;
482         
483         // If there is an active transaction check that it is the required type
484

485         if ( m_transaction != null)
486         {
487             // Check if the current transaction is marked for rollback
488

489             try
490             {
491                 
492                 if ( m_transaction.getStatus() == Status.STATUS_MARKED_ROLLBACK ||
493                      m_transaction.getStatus() == Status.STATUS_ROLLEDBACK ||
494                      m_transaction.getStatus() == Status.STATUS_ROLLING_BACK)
495                 {
496                     // Rollback the current transaction
497

498                     m_transaction.rollback();
499                 }
500             }
501             catch ( SystemException JavaDoc ex)
502             {
503             }
504             
505             // Check if the transaction is a write transaction, if write has been requested
506

507             if ( readOnly == false && m_readOnlyTrans == true)
508             {
509                 // Commit the read-only transaction
510

511                 try
512                 {
513                     m_transaction.commit();
514                     m_transConvCount++;
515                 }
516                 catch ( Exception JavaDoc ex)
517                 {
518                     throw new AlfrescoRuntimeException("Failed to commit read-only transaction, " + ex.getMessage());
519                 }
520                 finally
521                 {
522                     // Clear the active transaction
523

524                     m_transaction = null;
525                 }
526             }
527         }
528         
529         // Create the transaction
530

531         if ( m_transaction == null)
532         {
533             try
534             {
535                 m_transaction = transService.getUserTransaction(readOnly);
536                 m_transaction.begin();
537                 
538                 created = true;
539                 
540                 m_readOnlyTrans = readOnly;
541                 
542                 m_transCount++;
543             }
544             catch (Exception JavaDoc ex)
545             {
546                 throw new AlfrescoRuntimeException("Failed to create transaction, " + ex.getMessage());
547             }
548         }
549         
550         return created;
551     }
552     
553     /**
554      * End a transaction by either committing or rolling back
555      *
556      * @exception AlfrescoRuntimeException
557      */

558     public final void endTransaction()
559         throws AlfrescoRuntimeException
560     {
561         // Check if there is an active transaction
562

563         if ( m_transaction != null)
564         {
565             try
566             {
567                 // Commit or rollback the transaction
568

569                 if ( m_transaction.getStatus() == Status.STATUS_MARKED_ROLLBACK)
570                 {
571                     // Transaction is marked for rollback
572

573                     m_transaction.rollback();
574                 }
575                 else
576                 {
577                     // Commit the transaction
578

579                     m_transaction.commit();
580                 }
581             }
582             catch ( Exception JavaDoc ex)
583             {
584                 throw new AlfrescoRuntimeException("Failed to end transaction", ex);
585             }
586             finally
587             {
588                 // Clear the current transaction
589

590                 m_transaction = null;
591         }
592     }
593     
594     }
595     /**
596      * Determine if the session has an active transaction
597      *
598      * @return boolean
599      */

600     public final boolean hasUserTransaction()
601     {
602         return m_transaction != null ? true : false;
603     }
604     
605     /**
606      * Get the active transaction and clear the stored transaction
607      *
608      * @return UserTransaction
609      */

610     public final UserTransaction JavaDoc getUserTransaction()
611     {
612         UserTransaction JavaDoc trans = m_transaction;
613         m_transaction = null;
614         return trans;
615     }
616 }
617
Popular Tags