Example usage for javax.servlet ServletContextEvent getServletContext

List of usage examples for javax.servlet ServletContextEvent getServletContext

Introduction

In this page you can find the example usage for javax.servlet ServletContextEvent getServletContext.

Prototype

public ServletContext getServletContext() 

Source Link

Document

Return the ServletContext that changed.

Usage

From source file:org.red5.logging.ContextLoggingListener.java

public void contextInitialized(ServletContextEvent event) {
    ServletContext servletContext = event.getServletContext();
    String contextName = servletContext.getContextPath().replaceAll("/", "");
    if ("".equals(contextName)) {
        contextName = "root";
    }//w  w  w  .j a  v  a2s  . co  m
    System.out.printf("Context init: %s%n", contextName);
    ConfigurableWebApplicationContext appctx = (ConfigurableWebApplicationContext) servletContext
            .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (appctx != null) {
        System.out.printf(
                "ConfigurableWebApplicationContext is not null in ContextLoggingListener for: %s, this indicates a misconfiguration or load order problem%n",
                contextName);
    }
    try {
        // get the selector
        ContextSelector selector = Red5LoggerFactory.getContextSelector();
        // get the logger context for this servlet / app context by name
        URL url = servletContext.getResource(String.format("/WEB-INF/classes/logback-%s.xml", contextName));
        if (url != null && Files.exists(Paths.get(url.toURI()))) {
            System.out.printf("Context logger config found: %s%n", url.toURI());
        } else {
            url = servletContext.getResource("/WEB-INF/classes/logback.xml");
            if (url != null && Files.exists(Paths.get(url.toURI()))) {
                System.out.printf("Context logger config found: %s%n", url.toURI());
            }
        }
        // get the logger context for the servlet context
        LoggerContext loggerContext = url != null
                ? ((LoggingContextSelector) selector).getLoggerContext(contextName, url)
                : selector.getLoggerContext(contextName);
        // set the logger context for use elsewhere in the servlet context
        servletContext.setAttribute(Red5LoggerFactory.LOGGER_CONTEXT_ATTRIBUTE, loggerContext);
        // get the root logger for this context
        Logger logger = Red5LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME, contextName);
        logger.info("Starting up context: {}", contextName);
    } catch (Exception e) {
        System.err.printf("LoggingContextSelector is not the correct type: %s%n", e.getMessage());
        e.printStackTrace();
    }
}

From source file:de.romankreisel.faktotum.FaktotumContextListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    try (InputStream is = sce.getServletContext()
            .getResourceAsStream(FaktotumContextListener.RELATIVE_MANIFEST_PATH)) {
        Manifest manifest = new Manifest(is);
        Attributes attributes = manifest.getMainAttributes();
        String buildTimeString = attributes.getValue("Build-Time");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isNotBlank(buildTimeString)) {
            FaktotumContextListener.buildTime = simpleDateFormat.parse(buildTimeString);
        }/*from ww w  .  java  2 s  .  c  o m*/
        FaktotumContextListener.version = attributes.getValue("Implementation-Version");
        this.getLogger().info("Faktotum initialized");
        this.getLogger().info("Version: " + FaktotumContextListener.getVersion());
        this.getLogger().info("Build-Time: " + simpleDateFormat.format(FaktotumContextListener.getBuildTime()));

        this.getLogger().fine("Removing old sessions...");
        this.authenticationBean.removeOldSessions();
    } catch (IOException e) {
        this.getLogger().log(Level.WARNING, "Error reading build time from manifest", e);
    } catch (ParseException e) {
        this.getLogger().log(Level.WARNING, "Error parsing build time from manifest", e);
    } catch (Exception e) {
        this.getLogger().log(Level.SEVERE, "Error reading build time", e);
        throw e;
    }
}

From source file:org.jresponder.standalone.JrContextLoaderListener.java

/**
 * Finds the external configuration file and sets it so that
 * getExternalConfigFile() will return it.  If found it also sets
 * the system property jresponder.basedir so this can be used in
 * the Spring configuration.//from   w  w w . j a  va2 s. c om
 */
@Override
public void contextInitialized(ServletContextEvent aServletContextEvent) {

    File myDir = new File(aServletContextEvent.getServletContext().getRealPath("/"));
    try {
        for (int i = 0; i < 20; i++) {
            File myConfFile = new File(myDir, "conf/jresponder.xml");
            if (myConfFile.exists()) {
                externalConfigFile = myConfFile.getCanonicalFile();
                System.setProperty("jresponder.basedir", myDir.getCanonicalPath());
                standaloneMode = true;

                File myLog4jPropertiesFile = new File(myDir, "conf/log4j.properties");
                if (myLog4jPropertiesFile.exists()) {
                    Log4jConfigurer.initLogging(myLog4jPropertiesFile.getCanonicalPath(), 1000);
                    didStartLogging = true;
                }

                break;
            }
            myDir = new File(myDir, "..");
        }
    } catch (IOException e) {
        // should not happen
        e.printStackTrace();
    }

    // the rest is over to the Spring default
    super.contextInitialized(aServletContextEvent);

}

From source file:com.icanft.common.startup.ContextInit.java

/**
 * ?DI//from   w  w w  .j av a 2s . c o m
 * 
 * @param context ServletContextEvent
 */
public void contextInitialized(ServletContextEvent context) {

    final ServletContext servletContext = context.getServletContext();

    this.servletContext = servletContext;

    super.contextInitialized(context);

    // FBRP
    setContext(WebApplicationContextUtils.getRequiredWebApplicationContext(context.getServletContext()));
    ContextUtil.setApplicationContext(ContextInit.context);

    // ?FBRP?
    ContextUtil.setApplicationPath(context.getServletContext().getRealPath("/"));

    LicenseColl licenseList = null;
    try {
        licenseList = (LicenseColl) ContextInit.getContext().getBean("fbrp_licenseColl");
    } catch (Exception exe) {
        log.warn("License?");
    }
    if (licenseList != null) {
        boolean licenseIsValid = ValidateLicense.validateLicense(licenseList, ContextUtil.getApplicationPath());
        ContextUtil.put("licenseIsValid", licenseIsValid, ContextUtil.SCOPE_APPLICATION);
    } else {
        ContextUtil.put("licenseIsValid", true, ContextUtil.SCOPE_APPLICATION);
    }
}

From source file:com.qm.frame.infrastructure.startup.ContextInit.java

/**
 * ?DI/*  w w w .  ja va 2  s  .  com*/
 * 
 * @param context
 *            ServletContextEvent
 */
public void contextInitialized(ServletContextEvent context) {

    final ServletContext servletContext = context.getServletContext();

    ContextInit.servletContext = servletContext;

    super.contextInitialized(context);

    // 
    setContext(WebApplicationContextUtils.getRequiredWebApplicationContext(context.getServletContext()));

    // 
    ContextUtil.setApplicationContext(ContextInit.context);

    // ??
    ContextUtil.setApplicationPath(context.getServletContext().getRealPath("/"));

    LicenseColl licenseList = null;
    try {
        licenseList = (LicenseColl) ContextInit.getContext().getBean("qm_licenseColl");
    } catch (Exception exe) {
        log.warn("License?");
    }
    if (licenseList != null) {
        boolean licenseIsValid = ValidateLicense.validateLicense(licenseList, ContextUtil.getApplicationPath());
        ContextUtil.put("licenseIsValid", licenseIsValid, ContextUtil.SCOPE_APPLICATION);
    } else {
        ContextUtil.put("licenseIsValid", true, ContextUtil.SCOPE_APPLICATION);
    }
}

From source file:com.jsquant.listener.JsquantContextListener.java

public void contextInitialized(ServletContextEvent sce) {
    contextDestroyed(sce);/*from   w w w .  j a v a  2s .  co m*/
    ServletContext context = sce.getServletContext();

    String fileCachePath = getFileCachePath(context);
    context.setAttribute(ATTR_FILE_CACHE, new FileCache(fileCachePath));

    HttpParams params = new BasicHttpParams();
    //params.setParameter("http.useragent", "Mozilla/5.0");
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    final SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
    final HttpClient httpClient = new DefaultHttpClient(manager, params);
    //HttpHost proxy = new HttpHost("someproxy.com", 80);
    //httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    context.setAttribute(ATTR_HTTP_CLIENT, httpClient);
}

From source file:org.sakaiproject.context.util.LocatorServletContextListener.java

public void contextDestroyed(ServletContextEvent sce) {
    if (locator != null && contextName != null) {
        locator.deregisterContext(contextName, sce.getServletContext());
    }/*from  w  w  w . ja va  2 s. c o  m*/
}

From source file:org.sakaiproject.portal.charon.velocity.PortalRenderEngineContextListener.java

public void contextInitialized(ServletContextEvent event) {
    try {//from  w w w .j  a  v  a 2  s  .  com
        vengine = new VelocityPortalRenderEngine();
        vengine.setContext(event.getServletContext());
        vengine.setServerConfigurationService(ServerConfigurationService.getInstance());
        vengine.setSessionManager(SessionManager.getInstance());
        vengine.init();
        vengine.setPortalService(PortalService.getInstance());
        PortalService.getInstance().addRenderEngine(Portal.DEFAULT_PORTAL_CONTEXT, vengine);
    } catch (Exception ex) {
        log.error("Failed to register render engine with the portal service, this is probably fatal ", ex);
    }

}

From source file:com.telefonica.euro_iaas.paasmanager.bootstrap.PropertiesMBeanRegisterBootstrap.java

/**
 * Unregister the mbean.//from  w w  w  . j a va 2s  .  co  m
 */
public void contextDestroyed(ServletContextEvent event) {
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    PropertiesProvider propertiesUtil = new PropertiesProviderFactoryImpl()
            .createPropertiesProvider((EntityManagerFactory) ctx.getBean("entityManagerFactory"));
    try {
        for (String namespace : propertiesUtil.getNamespaces()) {
            log.info("Unregistering mbean " + namespace);
            MBeanUtils.unregister(
                    event.getServletContext().getContextPath() + ":service=SystemConfiguration-" + namespace);
        }
    } catch (Exception e) {
        throw new PaasManagerServerRuntimeException(e);
    }

}

From source file:edu.txstate.dmlab.clusteringwiki.web.AppContextListener.java

public void contextInitialized(ServletContextEvent event) {
    super.contextInitialized(event);
    webAppCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
}