Java Code Examples for javax.servlet.ServletContext#getMajorVersion()

The following examples show how to use javax.servlet.ServletContext#getMajorVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletVersionInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: BootstrapContainerInitializer.java    From ldp4j with Apache License 2.0 6 votes vote down vote up
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);
					}
				}
			);
}
 
Example 3
Source File: ServletVersionInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: JSR356WebsocketInitializer.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    getLogger().debug("Executing contextInitialized");
    ServletContext servletContext = sce.getServletContext();
    if (servletContext.getMajorVersion() < 3) {
        return;
    }

    init(servletContext);
}
 
Example 5
Source File: Parameters.java    From javamelody with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: DWAdapter.java    From wiztowar with MIT License 5 votes vote down vote up
/**
 * 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");
}
 
Example 7
Source File: Log4jServletContainerInitializer.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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, "/*");
    }
}
 
Example 8
Source File: ApmAsyncListener.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
private boolean isJBossEap6(AsyncEvent event) {
    final ServletContext context = event.getSuppliedRequest().getServletContext();
    return context.getMajorVersion() == 3 && context.getMinorVersion() == 0 && System.getProperty("jboss.home.dir") != null;
}
 
Example 9
Source File: ServletWrapper.java    From restcommander with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 10
Source File: EnvUtil.java    From audit4j-core with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}