/*
 * Copyright 2009 Richard Zschech.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.zschech.gwt.comet.server.impl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.servlet.ServletContextHandler.Context;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * An extension of {@link BlockingAsyncServlet} for Jetty.
 * 
 * This extension improves on the default session keep alive strategy, refreshing the connection just before the session
 * expires, by updating the session managers last access time when ever sending data down the Comet connection
 * 
 * @author Richard Zschech
 */
public class Jetty7AsyncServlet extends BlockingAsyncServlet {
	
	private SessionManager sessionManager;
	
	@Override
	public void init(ServletContext context) throws ServletException {
		super.init(context);
		sessionManager = ((WebAppContext)((Context)context).getContextHandler()).getSessionHandler().getSessionManager();
	}
	
	@Override
	protected boolean access(HttpSession httpSession) {
		sessionManager.access(httpSession, false);
		return true;
	}
}

@@ -649,24 +649,113 @@
 er.impl;%0D%0A%0D%0A
+import java.lang.reflect.InvocationTargetException;%0D%0Aimport java.lang.reflect.Method;%0D%0A%0D%0A
 import javax
@@ -866,172 +866,8 @@
 %0D%0A%0D%0A
-import org.eclipse.jetty.server.SessionManager;%0D%0Aimport org.eclipse.jetty.servlet.ServletContextHandler.Context;%0D%0Aimport org.eclipse.jetty.webapp.WebAppContext;%0D%0A%0D%0A
 /**%0D
@@ -1280,17 +1280,24 @@
 private 
-S
+Object s
 essionMa
@@ -1301,31 +1301,47 @@
 nManager
- sessionManager
+;%0D%0A%09private Method accessMethod
 ;%0D%0A%09%0D%0A%09@
@@ -1443,16 +1443,26 @@
 text);%0D%0A
+%09%09try %7B%0D%0A%09
 %09%09sessio
@@ -1476,225 +1476,802 @@
 r = 
-((WebAppContext)((Context)context).getContextHandler()).getS
+get(%22this$0._sessionHandler._sessionManager%22, context);%0D%0A%09%09%09if (sessionManager == null) %7B%0D%0A%09%09%09%09throw new ServletException(%22Error getting session manager%22);%0D%0A%09%09%09%7D%0D%0A%09%09%09accessMethod = s
 ession
-Handler().getSessionManager();%0D%0A%09%7D%0D%0A%09%0D%0A%09@Override%0D%0A%09protected boolean access(HttpSession httpSession) %7B%0D%0A%09%09sessionManager.access(httpSession, fals
+Manager.getClass().getMethod(%22access%22, HttpSession.class, Boolean.TYPE);%0D%0A%09%09%7D%0D%0A%09%09catch (SecurityException e) %7B%0D%0A%09%09%09throw new ServletException(e);%0D%0A%09%09%7D%0D%0A%09%09catch (NoSuchMethodException e) %7B%0D%0A%09%09%09throw new ServletException(e);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09@Override%0D%0A%09protected boolean access(HttpSession httpSession) %7B%0D%0A%09%09try %7B%0D%0A%09%09%09accessMethod.invoke(sessionManager, httpSession, false);%0D%0A%09%09%09return true;%0D%0A%09%09%7D%0D%0A%09%09catch (IllegalArgumentException e) %7B%0D%0A%09%09%09log(%22Error updating session last access time%22, e);%0D%0A%09%09%09return false;%0D%0A%09%09%7D%0D%0A%09%09catch (IllegalAccessException e) %7B%0D%0A%09%09%09log(%22Error updating session last access time%22, 
 e);%0D%0A
+%09
 %09%09re
@@ -2275,21 +2275,146 @@
 %09return 
-true;
+false;%0D%0A%09%09%7D%0D%0A%09%09catch (InvocationTargetException e) %7B%0D%0A%09%09%09log(%22Error updating session last access time%22, e);%0D%0A%09%09%09return false;%0D%0A%09%09%7D
 %0D%0A%09%7D%0D%0A%7D%0D

Use reflection to access the HttpSession in Jetty 7 stand alone mode