View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.system;
17  
18  import javax.servlet.ServletContext;
19  import javax.servlet.http.HttpSession;
20  import javax.servlet.http.HttpSessionEvent;
21  import javax.servlet.http.HttpSessionListener;
22  
23  /***
24   * @author someda
25   */
26  public class TgwStatusListener implements HttpSessionListener {
27  
28  	private static long sessionCount = 0;
29  	
30  	public void sessionCreated(HttpSessionEvent event) {		
31  		HttpSession session = event.getSession();		
32  		ServletContext sc = session.getServletContext();
33  		incrementSessionCount();		
34  		setSessionCountAttribute(sc);
35  	}
36  
37  	public void sessionDestroyed(HttpSessionEvent event) {
38  		HttpSession session = event.getSession();		
39  		ServletContext sc = session.getServletContext();		
40  		decrementSessionCount();		
41  		setSessionCountAttribute(sc);
42  	}
43  
44  	private synchronized static void incrementSessionCount(){
45  		sessionCount++;
46  	}
47  	
48  	private synchronized static void decrementSessionCount(){
49  		sessionCount--;
50  	}
51  	
52  	private void setSessionCountAttribute(ServletContext context){
53  		Long count = new Long(sessionCount);
54  		context.setAttribute(Constants.CATTR_SESSIONCOUNT,count);		
55  	}
56  	
57  }