下面列出了javax.servlet.ServletContext#getMajorVersion ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Advice.OnMethodEnter(suppress = Throwable.class)
@SuppressWarnings("Duplicates") // duplication is fine here as it allows to inline code
private static void onEnter(@Advice.Argument(0) @Nullable ServletConfig servletConfig) {
if (alreadyLogged) {
return;
}
alreadyLogged = true;
int majorVersion = -1;
int minorVersion = -1;
String serverInfo = null;
if (servletConfig != null) {
ServletContext servletContext = servletConfig.getServletContext();
if (null != servletContext) {
majorVersion = servletContext.getMajorVersion();
minorVersion = servletContext.getMinorVersion();
serverInfo = servletContext.getServerInfo();
}
}
logger.info("Servlet container info = {}", serverInfo);
if (majorVersion < 3) {
logger.warn("Unsupported servlet version detected: {}.{}, no Servlet transaction will be created", majorVersion, minorVersion);
}
}
private void initializeContainer(final ServletContext context) {
if(!initialized.compareAndSet(false, true)) {
return;
}
final String serverInfo = context.getServerInfo();
final int majorVersion = context.getMajorVersion();
final int minorVersion = context.getMinorVersion();
LOGGER.debug("Starting container {} {}.{}",serverInfo,majorVersion,minorVersion);
Runtime.
getRuntime().
addShutdownHook(
new Thread() {
@Override
public void run() {
LOGGER.debug("Shutting down container {} {}.{}",serverInfo,majorVersion,minorVersion);
}
}
);
}
@Advice.OnMethodEnter(suppress = Throwable.class)
@SuppressWarnings("Duplicates") // duplication is fine here as it allows to inline code
private static void onEnter(@Advice.This Servlet servlet) {
if (alreadyLogged) {
return;
}
alreadyLogged = true;
ServletConfig servletConfig = servlet.getServletConfig();
int majorVersion = -1;
int minorVersion = -1;
String serverInfo = null;
if (servletConfig != null) {
ServletContext servletContext = servletConfig.getServletContext();
if (null != servletContext) {
majorVersion = servletContext.getMajorVersion();
minorVersion = servletContext.getMinorVersion();
serverInfo = servletContext.getServerInfo();
}
}
logger.info("Servlet container info = {}", serverInfo);
if (majorVersion < 3) {
logger.warn("Unsupported servlet version detected: {}.{}, no Servlet transaction will be created", majorVersion, minorVersion);
}
}
@Override
public void contextInitialized(ServletContextEvent sce) {
getLogger().debug("Executing contextInitialized");
ServletContext servletContext = sce.getServletContext();
if (servletContext.getMajorVersion() < 3) {
return;
}
init(servletContext);
}
public static String getContextPath(ServletContext context) {
// cette méthode retourne le contextPath de la webapp
// en utilisant ServletContext.getContextPath si servlet api 2.5
// ou en se débrouillant sinon
// (on n'a pas encore pour l'instant de request pour appeler HttpServletRequest.getContextPath)
if (context.getMajorVersion() == 2 && context.getMinorVersion() >= 5
|| context.getMajorVersion() > 2) {
// api servlet 2.5 (Java EE 5) minimum pour appeler ServletContext.getContextPath
return context.getContextPath();
}
final URL webXmlUrl;
try {
webXmlUrl = context.getResource("/WEB-INF/web.xml");
} catch (final MalformedURLException e) {
throw new IllegalStateException(e);
}
String contextPath = webXmlUrl.toExternalForm();
contextPath = contextPath.substring(0, contextPath.indexOf("/WEB-INF/web.xml"));
final int indexOfWar = contextPath.indexOf(".war");
if (indexOfWar > 0) {
contextPath = contextPath.substring(0, indexOfWar);
}
// tomcat peut renvoyer une url commençant pas "jndi:/localhost"
// (v5.5.28, webapp dans un répertoire)
if (contextPath.startsWith("jndi:/localhost")) {
contextPath = contextPath.substring("jndi:/localhost".length());
}
final int lastIndexOfSlash = contextPath.lastIndexOf('/');
if (lastIndexOfSlash != -1) {
contextPath = contextPath.substring(lastIndexOfSlash);
}
return contextPath;
}
/**
* This method is adapted from ServerFactory.createInternalServlet.
*/
private void createInternalServlet(final ExtendedEnvironment env, final ServletContext context) {
if (context.getMajorVersion() >= 3) {
// Add the Task servlet
final ServletRegistration.Dynamic taskServlet = context.addServlet("TaskServlet", new TaskServlet(env.getTasks()));
taskServlet.setAsyncSupported(true);
taskServlet.addMapping("/tasks/*");
// Add the Admin servlet
final ServletRegistration.Dynamic adminServlet = context.addServlet("AdminServlet", new AdminServlet());
adminServlet.setAsyncSupported(true);
adminServlet.addMapping("/admin/*");
} else throw new IllegalStateException("The WizToWar adapter doesn't support servlet versions under 3");
}
@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
if (servletContext.getMajorVersion() > 2 && servletContext.getEffectiveMajorVersion() > 2 &&
!"true".equalsIgnoreCase(servletContext.getInitParameter(
Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED
))) {
final Logger LOGGER = StatusLogger.getLogger();
LOGGER.debug("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");
final FilterRegistration.Dynamic filter =
servletContext.addFilter("log4jServletFilter", Log4jServletFilter.class);
if (filter == null) {
LOGGER.warn("WARNING: In a Servlet 3.0+ application, you should not define a " +
"log4jServletFilter in web.xml. Log4j 2 normally does this for you automatically. Log4j 2 " +
"web auto-initialization has been canceled.");
return;
}
final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
initializer.start();
initializer.setLoggerContext(); // the application is just now starting to start up
servletContext.addListener(new Log4jServletContextListener());
filter.setAsyncSupported(true); // supporting async when the user isn't using async has no downsides
filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
}
}
private boolean isJBossEap6(AsyncEvent event) {
final ServletContext context = event.getSuppliedRequest().getServletContext();
return context.getMajorVersion() == 3 && context.getMinorVersion() == 0 && System.getProperty("jboss.home.dir") != null;
}
public static boolean isGreaterThan(ServletContext context, int majorVersion, int minorVersion) {
int contextMajorVersion = context.getMajorVersion();
int contextMinorVersion = context.getMinorVersion();
return (contextMajorVersion > majorVersion) || (contextMajorVersion == majorVersion && contextMinorVersion > minorVersion);
}
/**
* Checks if is servlet spec 3 or higher.
*
* @param context
* the context
* @return true, if is servlet spec3 or higher
*/
public static boolean isServletSpec3OrHigher(ServletContext context) {
if (context.getMajorVersion() >= 3) {
return true;
}
return false;
}