本文整理汇总了Java中javax.servlet.ServletContextListener类的典型用法代码示例。如果您正苦于以下问题:Java ServletContextListener类的具体用法?Java ServletContextListener怎么用?Java ServletContextListener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ServletContextListener类属于javax.servlet包,在下文中一共展示了ServletContextListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: prepareAndgetContextListener
import javax.servlet.ServletContextListener; //导入依赖的package包/类
/**
* Prepares the logger context. Locates the context and
* sets the configuration file.
* @return the logger context
*/
private ServletContextListener prepareAndgetContextListener() {
try {
if (StringUtils.isNotBlank(this.loggerContextPackageName)) {
final Collection<URL> set = ClasspathHelper.forPackage(this.loggerContextPackageName);
final Reflections reflections = new Reflections(new ConfigurationBuilder().addUrls(set).setScanners(new SubTypesScanner()));
final Set<Class<? extends ServletContextListener>> subTypesOf = reflections.getSubTypesOf(ServletContextListener.class);
final ServletContextListener loggingContext = subTypesOf.iterator().next().newInstance();
this.context.setInitParameter(this.logConfigurationField, this.logConfigurationFile.getURI().toString());
return loggingContext;
}
return null;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
示例2: contextInitialized
import javax.servlet.ServletContextListener; //导入依赖的package包/类
public void contextInitialized(ServletContextEvent sce) {
ctx = WebApplicationContextUtils.getWebApplicationContext(sce
.getServletContext());
if (ctx == null) {
logger.warn("cannot find applicationContext");
return;
}
Collection<ServletContextListener> servletContextListeners = ctx
.getBeansOfType(ServletContextListener.class).values();
for (ServletContextListener servletContextListener : servletContextListeners) {
servletContextListener.contextInitialized(sce);
}
}
示例3: addListener
import javax.servlet.ServletContextListener; //导入依赖的package包/类
@Override
public void addListener(final Class<? extends EventListener> listenerClass) {
ensureNotInitialized();
ensureNotProgramaticListener();
if (ApplicationListeners.listenerState() != NO_LISTENER &&
ServletContextListener.class.isAssignableFrom(listenerClass)) {
throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
}
InstanceFactory<? extends EventListener> factory = null;
try {
factory = deploymentInfo.getClassIntrospecter().createInstanceFactory(listenerClass);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
final ListenerInfo listener = new ListenerInfo(listenerClass, factory);
deploymentInfo.addListener(listener);
deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
}
示例4: callContextInitialized
import javax.servlet.ServletContextListener; //导入依赖的package包/类
@Override
public void callContextInitialized(ServletContextListener l, ServletContextEvent e)
{
try
{
//toggle state of the dynamic API so that the listener cannot use it
if(isProgrammaticListener(l))
this.getServletContext().setEnabled(false);
super.callContextInitialized(l, e);
}
finally
{
//untoggle the state of the dynamic API
this.getServletContext().setEnabled(true);
}
}
示例5: testContextLoaderListenerWithDefaultContext
import javax.servlet.ServletContextListener; //导入依赖的package包/类
@Test
public void testContextLoaderListenerWithDefaultContext() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
"/org/springframework/web/context/WEB-INF/applicationContext.xml " +
"/org/springframework/web/context/WEB-INF/context-addition.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
assertTrue("Has father", context.containsBean("father"));
assertTrue("Has rod", context.containsBean("rod"));
assertTrue("Has kerry", context.containsBean("kerry"));
assertTrue("Not destroyed", !lb.isDestroyed());
assertFalse(context.containsBean("beans1.bean1"));
assertFalse(context.containsBean("beans1.bean2"));
listener.contextDestroyed(event);
assertTrue("Destroyed", lb.isDestroyed());
assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
示例6: testContextLoaderWithInvalidContext
import javax.servlet.ServletContextListener; //导入依赖的package包/类
@Test
public void testContextLoaderWithInvalidContext() throws Exception {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
"org.springframework.web.context.support.InvalidWebApplicationContext");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
try {
listener.contextInitialized(event);
fail("Should have thrown ApplicationContextException");
}
catch (ApplicationContextException ex) {
// expected
assertTrue(ex.getCause() instanceof ClassNotFoundException);
}
}
示例7: newListenerInstance
import javax.servlet.ServletContextListener; //导入依赖的package包/类
private void newListenerInstance(){
if(listenerMetadatas.isEmpty()){
return;
}
ClassLoader classloader= pluginContext.getClassLoader();
try {
for(String lisenterClass: listenerMetadatas){
Class<?> clazz = classloader.loadClass(lisenterClass);
EventListener listener = (EventListener)clazz.newInstance();
injectComponentAware(listener);
if(listener instanceof ServletContextListener){
((ServletContextListener)listener).contextInitialized(new ServletContextEvent(getServletContext()));
}
listenerIntances.add(listener);
}
PluginWebInstanceRepository.registerListeners(listenerIntances);
logger.info("Complete to new and register listener instance");
} catch (Exception e) {
throw new PuzzleException("New Plugin ["+pluginContext.getName()+"] Listener instance failure",e);
}
}
示例8: start
import javax.servlet.ServletContextListener; //导入依赖的package包/类
/**
* Initializes the Shiro Security Framework.
*/
@Override
public void start(BundleContext context) throws Exception {
cacheProviderTracker = new CacheProviderTracker(context, CacheProvider.class);
cacheProviderTracker.open();
// Register the Shiro EnvironmentLoaderListener first.
Dictionary<String, Object> shiroListenerProps = new Hashtable<>();
shiroListenerProps.put(Constants.SERVICE_VENDOR, "AdeptJ");
shiroListenerProps.put("osgi.http.whiteboard.listener", "true");
servRegShiroListener = context.registerService(ServletContextListener.class, new ExtEnvironmentLoaderListener(),
shiroListenerProps);
// Now Register the ShiroFilter.
Dictionary<String, Object> shiroFilterProps = new Hashtable<>();
shiroFilterProps.put(Constants.SERVICE_VENDOR, "AdeptJ");
shiroFilterProps.put("osgi.http.whiteboard.filter.name", "Shiro Filter");
shiroFilterProps.put("osgi.http.whiteboard.filter.pattern", "/*");
shiroFilterProps.put("osgi.http.whiteboard.filter.asyncSupported", "true");
shiroFilterProps.put("osgi.http.whiteboard.filter.dispatcher",
new String[] { "REQUEST", "INCLUDE", "FORWARD", "ASYNC", "ERROR" });
servRegShiroFilter = context.registerService(Filter.class, new ShiroFilter(), shiroFilterProps);
}
示例9: onStartup
import javax.servlet.ServletContextListener; //导入依赖的package包/类
@Override
public void onStartup(ServletContext webappContext) throws ServletException {
ModuleDataExtractor extractor = new ModuleDataExtractor(module);
environment.assureModule(module);
String fullRestResource = "/" + module.getContext() + "/*";
ServerData serverData = new ServerData(environment.getModuleBean(module).getPort(),
Arrays.asList(),
rootContext, fullRestResource, module);
List<FilterData> filterDataList = extractor.createFilteredDataList(serverData);
List<ServletData> servletDataList = extractor.createServletDataList(serverData);
new ServletConfigurer(serverData, LinkedListX.fromIterable(servletDataList)).addServlets(webappContext);
new FilterConfigurer(serverData, LinkedListX.fromIterable(filterDataList)).addFilters(webappContext);
PersistentList<ServletContextListener> servletContextListenerData = LinkedListX.fromIterable(module.getListeners(serverData)).filter(i->!(i instanceof ContextLoader));
PersistentList<ServletRequestListener> servletRequestListenerData = LinkedListX.fromIterable(module.getRequestListeners(serverData));
new ServletContextListenerConfigurer(serverData, servletContextListenerData, servletRequestListenerData).addListeners(webappContext);
}
示例10: getListeners
import javax.servlet.ServletContextListener; //导入依赖的package包/类
default List<ServletContextListener> getListeners(ServerData data) {
List<ServletContextListener> list = new ArrayList<>();
if (data.getRootContext() instanceof WebApplicationContext) {
list.add(new ContextLoaderListener(
(WebApplicationContext) data.getRootContext()));
}
ListX<Plugin> modules = PluginLoader.INSTANCE.plugins.get();
ListX<ServletContextListener> listeners = modules.stream()
.filter(module -> module.servletContextListeners() != null)
.flatMapI(Plugin::servletContextListeners)
.map(fn -> fn.apply(data))
.to().listX();
return listeners.plusAll(list);
}
示例11: addListeners
import javax.servlet.ServletContextListener; //导入依赖的package包/类
public void addListeners(ServletContext webappContext) {
serverData.getRootContext()
.getBeansOfType(ServletContextListener.class)
.values()
.stream()
.peek(this::logListener)
.forEach(listener -> webappContext.addListener(listener));
listenerData.forEach(it -> webappContext.addListener(it));
serverData.getRootContext()
.getBeansOfType(ServletRequestListener.class)
.values()
.stream()
.peek(this::logListener)
.forEach(listener -> webappContext.addListener(listener));
listenerRequestData.forEach(it -> webappContext.addListener(it));
}
示例12: startServer
import javax.servlet.ServletContextListener; //导入依赖的package包/类
@Override
protected void startServer(int port, final Action<ServerHttpExchange> requestAction) throws
Exception {
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);
ServletContextHandler handler = new ServletContextHandler();
handler.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
Servlet servlet = new AsityServlet().onhttp(requestAction);
ServletRegistration.Dynamic reg = context.addServlet(AsityServlet.class.getName(), servlet);
reg.setAsyncSupported(true);
reg.addMapping(TEST_URI);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
});
server.setHandler(handler);
server.start();
}
示例13: testServletContextListener
import javax.servlet.ServletContextListener; //导入依赖的package包/类
private void testServletContextListener(ServletContext servletContext) throws Exception {
ResteasyAutoConfiguration resteasyAutoConfiguration = new ResteasyAutoConfiguration();
BeanFactoryPostProcessor beanFactoryPostProcessor = ResteasyAutoConfiguration.springBeanProcessor();
ServletContextListener servletContextListener = resteasyAutoConfiguration.resteasyBootstrapListener(beanFactoryPostProcessor);
Assert.assertNotNull(servletContextListener);
ServletContextEvent sce = new ServletContextEvent(servletContext);
servletContextListener.contextInitialized(sce);
ResteasyProviderFactory servletContextProviderFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName());
Dispatcher servletContextDispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName());
Registry servletContextRegistry = (Registry) servletContext.getAttribute(Registry.class.getName());
Assert.assertNotNull(servletContextProviderFactory);
Assert.assertNotNull(servletContextDispatcher);
Assert.assertNotNull(servletContextRegistry);
// Exercising fully cobertura branch coverage
servletContextListener.contextDestroyed(sce);
ServletContextListener servletContextListener2 = resteasyAutoConfiguration.resteasyBootstrapListener(beanFactoryPostProcessor);
servletContextListener2.contextDestroyed(sce);
}
示例14: init
import javax.servlet.ServletContextListener; //导入依赖的package包/类
/**
* Init method called by the main servlet when the wrapping servlet is initialized. This means that the context is
* taken into service by the system.
*
* @param parent The parent servlet context. Just for some delegation actions
*/
void init(ServletContext parent) {
// Set up the tracking of event listeners.
BundleContext bc = getOwner().getBundleContext();
delegate = parent;
Collection<Class<? extends EventListener>> toTrack = Arrays.asList(HttpSessionListener.class,
ServletRequestListener.class, HttpSessionAttributeListener.class, ServletRequestAttributeListener.class,
ServletContextListener.class);
Collection<String> objectFilters = toTrack.stream().
map((c) -> "(" + Constants.OBJECTCLASS + "=" + c.getName() + ")").collect(Collectors.toList());
String filterString = "|" + String.join("", objectFilters);
eventListenerTracker = startTracking(filterString,
new Tracker<EventListener, EventListener>(bc, getContextPath(), (e) -> e, (e) -> { /* No destruct */}));
// Initialize the servlets.
ServletContextEvent event = new ServletContextEvent(this);
call(ServletContextListener.class, (l) -> l.contextInitialized(event));
servlets.values().forEach((s) -> init(s));
// And the filters.
filters.values().forEach((f) -> init(f));
// Set up the tracking of servlets and filters.
servletTracker = startTracking(Constants.OBJECTCLASS + "=" + Servlet.class.getName(),
new Tracker<Servlet, String>(bc, getContextPath(), this::addServlet, this::removeServlet));
filterTracker = startTracking(Constants.OBJECTCLASS + "=" + Filter.class.getName(),
new Tracker<Filter, String>(bc, getContextPath(), this::addFilter, this::removeFilter));
}
示例15: startServer
import javax.servlet.ServletContextListener; //导入依赖的package包/类
@Override
protected void startServer() throws Exception {
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);
ServletContextHandler handler = new ServletContextHandler();
handler.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
Servlet servlet = new VibeAtmosphereServlet().onhttp(performer.serverAction());
ServletRegistration.Dynamic reg = context.addServlet(VibeAtmosphereServlet.class.getName(), servlet);
reg.setAsyncSupported(true);
reg.setInitParameter(ApplicationConfig.DISABLE_ATMOSPHEREINTERCEPTOR, Boolean.TRUE.toString());
reg.addMapping("/test");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {}
});
server.setHandler(handler);
server.start();
}