本文整理匯總了Java中javax.servlet.ServletContext.log方法的典型用法代碼示例。如果您正苦於以下問題:Java ServletContext.log方法的具體用法?Java ServletContext.log怎麽用?Java ServletContext.log使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.servlet.ServletContext
的用法示例。
在下文中一共展示了ServletContext.log方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: closeWebApplicationContext
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Close Spring's web application context for the given servlet context. If
* the default {@link #loadParentContext(ServletContext)} implementation,
* which uses ContextSingletonBeanFactoryLocator, has loaded any shared
* parent context, release one reference to that shared parent context.
* <p>If overriding {@link #loadParentContext(ServletContext)}, you may have
* to override this method as well.
* @param servletContext the ServletContext that the WebApplicationContext runs in
*/
public void closeWebApplicationContext(ServletContext servletContext) {
servletContext.log("Closing Spring root WebApplicationContext");
try {
if (this.context instanceof ConfigurableWebApplicationContext) {
((ConfigurableWebApplicationContext) this.context).close();
}
}
finally {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = null;
}
else if (ccl != null) {
currentContextPerThread.remove(ccl);
}
servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (this.parentContextRef != null) {
this.parentContextRef.release();
}
}
}
示例2: setWebAppRootSystemProperty
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Set a system property to the web application root directory.
* The key of the system property can be defined with the "webAppRootKey"
* context-param in {@code web.xml}. Default is "webapp.root".
* <p>Can be used for tools that support substition with {@code System.getProperty}
* values, like log4j's "${key}" syntax within log file locations.
* @param servletContext the servlet context of the web application
* @throws IllegalStateException if the system property is already set,
* or if the WAR file is not expanded
* @see #WEB_APP_ROOT_KEY_PARAM
* @see #DEFAULT_WEB_APP_ROOT_KEY
* @see WebAppRootListener
* @see Log4jWebConfigurer
*/
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
Assert.notNull(servletContext, "ServletContext must not be null");
String root = servletContext.getRealPath("/");
if (root == null) {
throw new IllegalStateException(
"Cannot set web app root system property when WAR file is not expanded");
}
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
String oldValue = System.getProperty(key);
if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) {
throw new IllegalStateException(
"Web app root system property already set to different value: '" +
key + "' = [" + oldValue + "] instead of [" + root + "] - " +
"Choose unique values for the 'webAppRootKey' context-param in your web.xml files!");
}
System.setProperty(key, root);
servletContext.log("Set web app root system property: '" + key + "' = [" + root + "]");
}
示例3: init
import javax.servlet.ServletContext; //導入方法依賴的package包/類
public void init(final ServletConfig config) {
try {
this.delegate.init(config);
} catch (final Throwable t) {
// let the service method know initialization failed.
this.initSuccess = false;
/*
* no matter what went wrong, our role is to capture this error and
* prevent it from blocking initialization of the servlet. logging
* overkill so that our deployer will find a record of this problem
* even if unfamiliar with Commons Logging and properly configuring
* it.
*/
final String message = "SafeDispatcherServlet: \n"
+ "The Spring DispatcherServlet we wrap threw on init.\n"
+ "But for our having caught this error, the servlet would not have initialized.";
// logger it via Commons Logging
LOGGER.error(message, t);
// logger it to the ServletContext
ServletContext context = config.getServletContext();
context.log(message, t);
/*
* record the error so that the application has access to later
* display a proper error message based on the exception.
*/
context.setAttribute(CAUGHT_THROWABLE_KEY, t);
}
}
示例4: contextInitialized
import javax.servlet.ServletContext; //導入方法依賴的package包/類
public void contextInitialized(final ServletContextEvent sce) {
try {
this.delegate.contextInitialized(sce);
} catch (final Throwable t) {
/*
* no matter what went wrong, our role is to capture this error and
* prevent it from blocking initialization of the context. logging
* overkill so that our deployer will find a record of this problem
* even if unfamiliar with Commons Logging and properly configuring
* it.
*/
final String message = "SafeContextLoaderListener: \n"
+ "The Spring ContextLoaderListener we wrap threw on contextInitialized.\n"
+ "But for our having caught this error, the web application context would not have initialized.";
// logger it via Commons Logging
logger.error(message, t);
// logger it to the ServletContext
ServletContext context = sce.getServletContext();
context.log(message, t);
/*
* record the error so that the application has access to later
* display a proper error message based on the exception.
*/
context.setAttribute(CAUGHT_THROWABLE_KEY, t);
}
}
示例5: contextInitialized
import javax.servlet.ServletContext; //導入方法依賴的package包/類
@Override
public void contextInitialized( ServletContextEvent servletEvent ) {
ServletContext servletContext = servletEvent.getServletContext();
servletContext.log("Servlet context initialized event is received. Starting registering configurators");
try {
new ClasspathUtils().logProblematicJars();
} catch (RuntimeException e) {
log.warn("Error caught while trying to get all JARs in classpath", e);
// do not rethrow exception as this will stop deployment on incompliant servers like JBoss
}
// create the default web service configurator
String pathToConfigFile = servletContext.getRealPath("/WEB-INF");
AgentConfigurator defaultConfigurator = new AgentConfigurator(pathToConfigFile);
TemplateActionsConfigurator templateActionsConfigurator = new TemplateActionsConfigurator(pathToConfigFile);
List<Configurator> configurators = new ArrayList<Configurator>();
configurators.add(defaultConfigurator);
configurators.add(templateActionsConfigurator);
log.info("Initializing ATS Agent web service, start component registration");
try {
MainComponentLoader.getInstance().initialize(configurators);
} catch (AgentException ae) {
throw new RuntimeException("Unable to initialize Agent component loader", ae);
}
}
示例6: onStartup
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Delegate the {@code ServletContext} to any {@link WebApplicationInitializer}
* implementations present on the application classpath.
*
* <p>Because this class declares @{@code HandlesTypes(WebApplicationInitializer.class)},
* Servlet 3.0+ containers will automatically scan the classpath for implementations
* of Spring's {@code WebApplicationInitializer} interface and provide the set of all
* such types to the {@code webAppInitializerClasses} parameter of this method.
*
* <p>If no {@code WebApplicationInitializer} implementations are found on the
* classpath, this method is effectively a no-op. An INFO-level log message will be
* issued notifying the user that the {@code ServletContainerInitializer} has indeed
* been invoked but that no {@code WebApplicationInitializer} implementations were
* found.
*
* <p>Assuming that one or more {@code WebApplicationInitializer} types are detected,
* they will be instantiated (and <em>sorted</em> if the @{@link
* org.springframework.core.annotation.Order @Order} annotation is present or
* the {@link org.springframework.core.Ordered Ordered} interface has been
* implemented). Then the {@link WebApplicationInitializer#onStartup(ServletContext)}
* method will be invoked on each instance, delegating the {@code ServletContext} such
* that each instance may register and configure servlets such as Spring's
* {@code DispatcherServlet}, listeners such as Spring's {@code ContextLoaderListener},
* or any other Servlet API componentry such as filters.
*
* @param webAppInitializerClasses all implementations of
* {@link WebApplicationInitializer} found on the application classpath
* @param servletContext the servlet context to be initialized
* @see WebApplicationInitializer#onStartup(ServletContext)
* @see AnnotationAwareOrderComparator
*/
@Override
public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException {
List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();
if (webAppInitializerClasses != null) {
for (Class<?> waiClass : webAppInitializerClasses) {
// Be defensive: Some servlet containers provide us with invalid classes,
// no matter what @HandlesTypes says...
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer) waiClass.newInstance());
}
catch (Throwable ex) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
return;
}
AnnotationAwareOrderComparator.sort(initializers);
servletContext.log("Spring WebApplicationInitializers detected on classpath: " + initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
示例7: shutdownLogging
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Shut down log4j, properly releasing all file locks
* and resetting the web app root system property.
* @param servletContext the current ServletContext
* @see WebUtils#removeWebAppRootSystemProperty
*/
public static void shutdownLogging(ServletContext servletContext) {
servletContext.log("Shutting down log4j");
try {
Log4jConfigurer.shutdownLogging();
}
finally {
// Remove the web app root system property.
if (exposeWebAppRoot(servletContext)) {
WebUtils.removeWebAppRootSystemProperty(servletContext);
}
}
}
示例8: testGetLogs
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Verify that calls to <code>ServletContext.log()</code> methods can
* be retrieved and asserted.
*/
public void testGetLogs()
{
String message = "some test log";
ServletContext context = config.getServletContext();
context.log(message);
Vector logs = ((ServletContextWrapper) context).getLogs();
assertEquals("Found more than one log message", logs.size(), 1);
assertTrue("Cannot find expected log message : [" + message + "]",
logs.contains("some test log"));
}
示例9: initWebApplicationContext
import javax.servlet.ServletContext; //導入方法依賴的package包/類
/**
* Initialize Spring's web application context for the given servlet context,
* using the application context provided at construction time, or creating a new one
* according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and
* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
* @param servletContext current servlet context
* @return the new WebApplicationContext
* @see #ContextLoader(WebApplicationContext)
* @see #CONTEXT_CLASS_PARAM
* @see #CONFIG_LOCATION_PARAM
*/
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}