Skip to content

Instantly share code, notes, and snippets.

@roundrop
Last active August 29, 2015 14:01
Show Gist options
  • Save roundrop/96edc5f4d7135e60a2d0 to your computer and use it in GitHub Desktop.
Save roundrop/96edc5f4d7135e60a2d0 to your computer and use it in GitHub Desktop.
QuercusServletの初期化
package jp.roundrop.quercus.servlet;
import com.caucho.quercus.QuercusContext;
import com.caucho.quercus.lib.session.QuercusSessionManager;
import com.caucho.quercus.servlet.QuercusServlet;
import com.caucho.util.Alarm;
import org.slf4j.bridge.SLF4JBridgeHandler;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.lang.reflect.Field;
import java.util.logging.Handler;
import java.util.logging.LogManager;
public class MyQuercusServlet extends QuercusServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
setupSessionAlarm();
setupLogger();
}
private static final long ONE_MINUTE = 1 * 60 * 1000L;
/**
* Quercus が管理するセッションのタイムアウトタイマーのセットアップ
*/
private void setupSessionAlarm() {
int sessionTimeout = Integer.valueOf(getServletContext().getInitParameter("quercus.session-timeout"));
Field contextField = null;
try {
contextField = QuercusServlet.class.getDeclaredField("_quercus");
} catch (NoSuchFieldException ignore) {}
contextField.setAccessible(true);
QuercusContext quercusContext = null;
try {
quercusContext = (QuercusContext) contextField.get(this);
} catch (IllegalAccessException ignore) {}
QuercusSessionManager sessionManager = quercusContext.getQuercusSessionManager();
Field sessionTimeoutField = null;
try {
sessionTimeoutField = QuercusSessionManager.class.getDeclaredField("_sessionTimeout");
} catch (NoSuchFieldException ignore) {}
sessionTimeoutField.setAccessible(true);
try {
sessionTimeoutField.setLong(sessionManager, ONE_MINUTE * sessionTimeout);
} catch (IllegalAccessException ignore) {}
new Alarm(sessionManager).queue(ONE_MINUTE);
}
/**
* java.util.logging.Logger を SLF4J にブリッジするためのセットアップ
*/
private void setupLogger() {
java.util.logging.Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers();
for (Handler handler : handlers) {
rootLogger.removeHandler(handler);
}
SLF4JBridgeHandler.install();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- セッションタイムアウト -->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<context-param>
<param-name>quercus.session-timeout</param-name>
<param-value>60</param-value>
</context-param>
<!-- Quercus サーブレット -->
<servlet>
<servlet-name>quercusServlet</servlet-name>
<!--servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class-->
<servlet-class>jp.roundrop.quercus.servlet.MyQuercusServlet</servlet-class>
<init-param>
<param-name>ini-file</param-name>
<param-value>WEB-INF/php.ini</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>quercusServlet</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment