KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > Tasks > SessionManager


1 package com.Yasna.forum.Tasks;
2
3 import com.Yasna.forum.ForumFactory;
4 import com.Yasna.forum.ForumMessage;
5 import com.Yasna.forum.SessionVO;
6 import com.Yasna.forum.database.DbConnectionManager;
7
8 import java.util.LinkedList JavaDoc;
9 import java.util.Calendar JavaDoc;
10 import java.sql.Connection JavaDoc;
11 import java.sql.PreparedStatement JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.sql.ResultSet JavaDoc;
14
15 /**
16  * Copyright (C) 2001 Yasna.com. All rights reserved.
17  *
18  * ===================================================================
19  * The Apache Software License, Version 1.1
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  *
25  * 1. Redistributions of source code must retain the above copyright
26  * notice, this list of conditions and the following disclaimer.
27  *
28  * 2. Redistributions in binary form must reproduce the above copyright
29  * notice, this list of conditions and the following disclaimer in
30  * the documentation and/or other materials provided with the
31  * distribution.
32  *
33  * 3. The end-user documentation included with the redistribution,
34  * if any, must include the following acknowledgment:
35  * "This product includes software developed by
36  * Yasna.com (http://www.yasna.com)."
37  * Alternately, this acknowledgment may appear in the software itself,
38  * if and wherever such third-party acknowledgments normally appear.
39  *
40  * 4. The names "Yazd" and "Yasna.com" must not be used to
41  * endorse or promote products derived from this software without
42  * prior written permission. For written permission, please
43  * contact yazd@yasna.com.
44  *
45  * 5. Products derived from this software may not be called "Yazd",
46  * nor may "Yazd" appear in their name, without prior written
47  * permission of Yasna.com.
48  *
49  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
50  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
53  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
56  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  * ====================================================================
62  *
63  * This software consists of voluntary contributions made by many
64  * individuals on behalf of Yasna.com. For more information
65  * on Yasna.com, please see <http://www.yasna.com>.
66  */

67
68 public class SessionManager {
69     private LinkedList JavaDoc newSessions;
70     // This linked list will retain all the new messages and will notify the people that needs to be notified.
71
private Thread JavaDoc worker;
72     private static int counter=0;
73     public SessionManager(){
74         // this is called from the factory and it will retain a handle on it.
75
if(counter==0){
76             System.out.println("Starting Session Manager (you should only see this message once)");
77             counter++;
78             newSessions = new LinkedList JavaDoc();
79             // We are going to start a new worker thread to handle all the emails.
80
worker = new Thread JavaDoc(new SessionWatchWorker(this));
81             worker.setDaemon(true);
82             worker.start();
83         }
84     }
85     public synchronized void addMessage(String JavaDoc sID,String JavaDoc IP,int uID){
86         newSessions.addLast(new SessionVO(sID,IP,uID));
87         // There is now a message to be processed and the Thread needs to be notified
88
notify();
89     }
90     public synchronized SessionVO getNextMessage(){
91         if(newSessions.isEmpty()){
92             try{
93                 //System.out.println("waiting for a new sessions");
94
wait();
95                 //System.out.println("finished waiting");
96
}catch (InterruptedException JavaDoc ie) {}
97         }
98         return (SessionVO)newSessions.removeFirst();
99     }
100
101     public class SessionWatchWorker implements Runnable JavaDoc{
102         private static final String JavaDoc GET_SESSION=
103                 "select sessionID from yazdSessions where sessionID=?";
104         private static final String JavaDoc UPDATE_SESSION="update yazdSessions set userID=?,IP=?,lasttime=? where sessionID=?";
105         private static final String JavaDoc INSERT_SESSION="insert into yazdSessions (sessionID,userID,IP,lasttime,initime) values (?,?,?,?,?)";
106         private static final String JavaDoc DELETE_SESION="delete from yazdSessions where lasttime < ?";
107         private static final String JavaDoc GET_CURRENT_COUNT="select count(*) as cnt from yazdSessions";
108         private static final String JavaDoc GET_STATS="select maxusercount from yazdUserStats where day_dt=?";
109         private static final String JavaDoc INSERT_STATS="insert into yazdUserStats(day_dt,usercount,maxusercount,maxuserdt) values(?,?,?,?)";
110         private static final String JavaDoc UPDATE_COUNT="update yazdUserStats set usercount=usercount+1 where day_dt=?";
111         private static final String JavaDoc UPDATE_COUNT_MAX="update yazdUserStats set maxusercount=?,maxuserdt=? where day_dt=?";
112
113         private SessionManager manager;
114         public SessionWatchWorker(SessionManager m){
115             this.manager=m;
116         }
117         public void run(){
118             while(true){
119                 updatesssions(manager.getNextMessage());
120             }
121         }
122         private void updatesssions(SessionVO session){
123             Calendar JavaDoc now = Calendar.getInstance();
124             int now_in_minutes = (int)(now.getTimeInMillis()*1.0/(1000.0 * 60.0));
125             int now_in_seconds = (int)(now.getTimeInMillis()*1.0/1000.0);
126             int now_today = (int)(now_in_minutes * 1.0/(60.0 * 24.0));
127             Connection JavaDoc con = null;
128             PreparedStatement JavaDoc pstmt = null;
129             boolean newsession = false;
130             try {
131                 con = DbConnectionManager.getConnection();
132                 pstmt = con.prepareStatement(GET_SESSION);
133                 pstmt.setString(1,session.getSessionID());
134                 ResultSet JavaDoc rs = pstmt.executeQuery();
135                 if(rs.next()){
136                     pstmt = con.prepareStatement(UPDATE_SESSION);
137                     pstmt.setInt(1,session.getUserID());
138                     pstmt.setString(2,session.getIP());
139                     pstmt.setInt(3,now_in_minutes);
140                     pstmt.setString(4,session.getSessionID());
141                     pstmt.executeUpdate();
142                 }else{
143                     newsession=true;
144                     pstmt = con.prepareStatement(INSERT_SESSION);
145                     pstmt.setString(1,session.getSessionID());
146                     pstmt.setInt(2,session.getUserID());
147                     pstmt.setString(3,session.getIP());
148                     pstmt.setInt(4,now_in_minutes);
149             pstmt.setInt(5,now_in_seconds);
150                     pstmt.executeUpdate();
151                 }
152                 // We now delete the old sessions
153
pstmt = con.prepareStatement(DELETE_SESION);
154                 pstmt.setInt(1,now_in_minutes - 6);
155                 pstmt.executeUpdate();
156                 //get the count of current sessions
157
pstmt = con.prepareStatement(GET_CURRENT_COUNT);
158                 rs = pstmt.executeQuery();
159                 int sessioncount=0;
160                 if(rs.next()){
161                     sessioncount=rs.getInt("cnt");
162                 }
163                 //get max session count
164
int maxcount=0;
165                 pstmt = con.prepareStatement(GET_STATS);
166                 pstmt.setInt(1,now_today);
167                 rs = pstmt.executeQuery();
168                 if(rs.next()){
169                     maxcount = rs.getInt("maxusercount");
170                     if(maxcount < sessioncount){
171                         //update the session max count and user count
172
pstmt = con.prepareStatement(UPDATE_COUNT_MAX);
173                         pstmt.setInt(1,sessioncount);
174                         pstmt.setString(2,Long.toString(now.getTimeInMillis()));
175                         pstmt.setInt(3,now_today);
176                         pstmt.executeUpdate();
177                     }
178                     if(newsession){
179                         //just update the user count
180
pstmt = con.prepareStatement(UPDATE_COUNT);
181                         pstmt.setInt(1,now_today);
182                         pstmt.executeUpdate();
183                     }
184                 }else{
185                     //insert max user count
186
pstmt = con.prepareStatement(INSERT_STATS);
187                     pstmt.setInt(1,now_today);
188                     pstmt.setInt(2,sessioncount);
189                     pstmt.setInt(3,sessioncount);
190                     pstmt.setString(4,Long.toString(now.getTimeInMillis()));
191                     pstmt.executeUpdate();
192                 }
193                 
194             }
195             catch( SQLException JavaDoc sqle ) {
196                 System.err.println("SessionManager (394) Exception:"+sqle.getMessage());
197                 sqle.printStackTrace();
198             }
199             catch (Exception JavaDoc e) {
200                 System.err.println("SessionManager (3847) Exception:"+e.getMessage());
201             }
202             finally {
203                 try { pstmt.close(); }
204                 catch (Exception JavaDoc e) { e.printStackTrace(); }
205                 try { con.close(); }
206                 catch (Exception JavaDoc e) { e.printStackTrace(); }
207             }
208         }
209     }
210
211 }
212
Popular Tags