Source Code Cross Referenced for SessionManager.java in  » Forum » yazd » com » Yasna » forum » Tasks » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Forum » yazd » com.Yasna.forum.Tasks 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.Yasna.forum.Tasks;
002:
003:        import com.Yasna.forum.ForumFactory;
004:        import com.Yasna.forum.ForumMessage;
005:        import com.Yasna.forum.SessionVO;
006:        import com.Yasna.forum.database.DbConnectionManager;
007:
008:        import java.util.LinkedList;
009:        import java.util.Calendar;
010:        import java.sql.Connection;
011:        import java.sql.PreparedStatement;
012:        import java.sql.SQLException;
013:        import java.sql.ResultSet;
014:
015:        /**
016:         * Copyright (C) 2001 Yasna.com. All rights reserved.
017:         *
018:         * ===================================================================
019:         * The Apache Software License, Version 1.1
020:         *
021:         * Redistribution and use in source and binary forms, with or without
022:         * modification, are permitted provided that the following conditions
023:         * are met:
024:         *
025:         * 1. Redistributions of source code must retain the above copyright
026:         *    notice, this list of conditions and the following disclaimer.
027:         *
028:         * 2. Redistributions in binary form must reproduce the above copyright
029:         *    notice, this list of conditions and the following disclaimer in
030:         *    the documentation and/or other materials provided with the
031:         *    distribution.
032:         *
033:         * 3. The end-user documentation included with the redistribution,
034:         *    if any, must include the following acknowledgment:
035:         *       "This product includes software developed by
036:         *        Yasna.com (http://www.yasna.com)."
037:         *    Alternately, this acknowledgment may appear in the software itself,
038:         *    if and wherever such third-party acknowledgments normally appear.
039:         *
040:         * 4. The names "Yazd" and "Yasna.com" must not be used to
041:         *    endorse or promote products derived from this software without
042:         *    prior written permission. For written permission, please
043:         *    contact yazd@yasna.com.
044:         *
045:         * 5. Products derived from this software may not be called "Yazd",
046:         *    nor may "Yazd" appear in their name, without prior written
047:         *    permission of Yasna.com.
048:         *
049:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
050:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
051:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
052:         * DISCLAIMED.  IN NO EVENT SHALL YASNA.COM OR
053:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
054:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
055:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
056:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
057:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
058:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
059:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
060:         * SUCH DAMAGE.
061:         * ====================================================================
062:         *
063:         * This software consists of voluntary contributions made by many
064:         * individuals on behalf of Yasna.com. For more information
065:         * on Yasna.com, please see <http://www.yasna.com>.
066:         */
067:
068:        public class SessionManager {
069:            private LinkedList newSessions;
070:            // This linked list will retain all the new messages and will notify the people that needs to be notified.
071:            private Thread worker;
072:            private static int counter = 0;
073:
074:            public SessionManager() {
075:                // this is called from the factory and it will retain a handle on it.
076:                if (counter == 0) {
077:                    System.out
078:                            .println("Starting Session Manager (you should only see this message once)");
079:                    counter++;
080:                    newSessions = new LinkedList();
081:                    // We are going to start a new worker thread to handle all the emails.
082:                    worker = new Thread(new SessionWatchWorker(this ));
083:                    worker.setDaemon(true);
084:                    worker.start();
085:                }
086:            }
087:
088:            public synchronized void addMessage(String sID, String IP, int uID) {
089:                newSessions.addLast(new SessionVO(sID, IP, uID));
090:                // There is now a message to be processed and the Thread needs to be notified
091:                notify();
092:            }
093:
094:            public synchronized SessionVO getNextMessage() {
095:                if (newSessions.isEmpty()) {
096:                    try {
097:                        //System.out.println("waiting for a new sessions");
098:                        wait();
099:                        //System.out.println("finished waiting");
100:                    } catch (InterruptedException ie) {
101:                    }
102:                }
103:                return (SessionVO) newSessions.removeFirst();
104:            }
105:
106:            public class SessionWatchWorker implements  Runnable {
107:                private static final String GET_SESSION = "select sessionID from yazdSessions where sessionID=?";
108:                private static final String UPDATE_SESSION = "update yazdSessions set userID=?,IP=?,lasttime=? where sessionID=?";
109:                private static final String INSERT_SESSION = "insert into yazdSessions (sessionID,userID,IP,lasttime,initime) values (?,?,?,?,?)";
110:                private static final String DELETE_SESION = "delete from yazdSessions where lasttime < ?";
111:                private static final String GET_CURRENT_COUNT = "select count(*) as cnt from yazdSessions";
112:                private static final String GET_STATS = "select maxusercount from yazdUserStats where day_dt=?";
113:                private static final String INSERT_STATS = "insert into yazdUserStats(day_dt,usercount,maxusercount,maxuserdt) values(?,?,?,?)";
114:                private static final String UPDATE_COUNT = "update yazdUserStats set usercount=usercount+1 where day_dt=?";
115:                private static final String UPDATE_COUNT_MAX = "update yazdUserStats set maxusercount=?,maxuserdt=? where day_dt=?";
116:
117:                private SessionManager manager;
118:
119:                public SessionWatchWorker(SessionManager m) {
120:                    this .manager = m;
121:                }
122:
123:                public void run() {
124:                    while (true) {
125:                        updatesssions(manager.getNextMessage());
126:                    }
127:                }
128:
129:                private void updatesssions(SessionVO session) {
130:                    Calendar now = Calendar.getInstance();
131:                    int now_in_minutes = (int) (now.getTimeInMillis() * 1.0 / (1000.0 * 60.0));
132:                    int now_in_seconds = (int) (now.getTimeInMillis() * 1.0 / 1000.0);
133:                    int now_today = (int) (now_in_minutes * 1.0 / (60.0 * 24.0));
134:                    Connection con = null;
135:                    PreparedStatement pstmt = null;
136:                    boolean newsession = false;
137:                    try {
138:                        con = DbConnectionManager.getConnection();
139:                        pstmt = con.prepareStatement(GET_SESSION);
140:                        pstmt.setString(1, session.getSessionID());
141:                        ResultSet rs = pstmt.executeQuery();
142:                        if (rs.next()) {
143:                            pstmt = con.prepareStatement(UPDATE_SESSION);
144:                            pstmt.setInt(1, session.getUserID());
145:                            pstmt.setString(2, session.getIP());
146:                            pstmt.setInt(3, now_in_minutes);
147:                            pstmt.setString(4, session.getSessionID());
148:                            pstmt.executeUpdate();
149:                        } else {
150:                            newsession = true;
151:                            pstmt = con.prepareStatement(INSERT_SESSION);
152:                            pstmt.setString(1, session.getSessionID());
153:                            pstmt.setInt(2, session.getUserID());
154:                            pstmt.setString(3, session.getIP());
155:                            pstmt.setInt(4, now_in_minutes);
156:                            pstmt.setInt(5, now_in_seconds);
157:                            pstmt.executeUpdate();
158:                        }
159:                        // We now delete the old sessions
160:                        pstmt = con.prepareStatement(DELETE_SESION);
161:                        pstmt.setInt(1, now_in_minutes - 6);
162:                        pstmt.executeUpdate();
163:                        //get the count of current sessions
164:                        pstmt = con.prepareStatement(GET_CURRENT_COUNT);
165:                        rs = pstmt.executeQuery();
166:                        int sessioncount = 0;
167:                        if (rs.next()) {
168:                            sessioncount = rs.getInt("cnt");
169:                        }
170:                        //get max session count
171:                        int maxcount = 0;
172:                        pstmt = con.prepareStatement(GET_STATS);
173:                        pstmt.setInt(1, now_today);
174:                        rs = pstmt.executeQuery();
175:                        if (rs.next()) {
176:                            maxcount = rs.getInt("maxusercount");
177:                            if (maxcount < sessioncount) {
178:                                //update the session max count and user count
179:                                pstmt = con.prepareStatement(UPDATE_COUNT_MAX);
180:                                pstmt.setInt(1, sessioncount);
181:                                pstmt.setString(2, Long.toString(now
182:                                        .getTimeInMillis()));
183:                                pstmt.setInt(3, now_today);
184:                                pstmt.executeUpdate();
185:                            }
186:                            if (newsession) {
187:                                //just update the user count
188:                                pstmt = con.prepareStatement(UPDATE_COUNT);
189:                                pstmt.setInt(1, now_today);
190:                                pstmt.executeUpdate();
191:                            }
192:                        } else {
193:                            //insert max user count
194:                            pstmt = con.prepareStatement(INSERT_STATS);
195:                            pstmt.setInt(1, now_today);
196:                            pstmt.setInt(2, sessioncount);
197:                            pstmt.setInt(3, sessioncount);
198:                            pstmt.setString(4, Long.toString(now
199:                                    .getTimeInMillis()));
200:                            pstmt.executeUpdate();
201:                        }
202:
203:                    } catch (SQLException sqle) {
204:                        System.err.println("SessionManager (394) Exception:"
205:                                + sqle.getMessage());
206:                        sqle.printStackTrace();
207:                    } catch (Exception e) {
208:                        System.err.println("SessionManager (3847) Exception:"
209:                                + e.getMessage());
210:                    } finally {
211:                        try {
212:                            pstmt.close();
213:                        } catch (Exception e) {
214:                            e.printStackTrace();
215:                        }
216:                        try {
217:                            con.close();
218:                        } catch (Exception e) {
219:                            e.printStackTrace();
220:                        }
221:                    }
222:                }
223:            }
224:
225:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.