Example usage for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext

List of usage examples for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext

Introduction

In this page you can find the example usage for org.springframework.web.context.support WebApplicationContextUtils getWebApplicationContext.

Prototype

@Nullable
public static WebApplicationContext getWebApplicationContext(ServletContext sc) 

Source Link

Document

Find the root WebApplicationContext for this web app, typically loaded via org.springframework.web.context.ContextLoaderListener .

Usage

From source file:cn.im47.cloud.storage.ftp.FtpServerListener.java

public void contextInitialized(ServletContextEvent sce) {
    logger.debug("Starting FtpServer");

    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    FtpServer server = (FtpServer) ctx.getBean("myFtpServer");
    sce.getServletContext().setAttribute(FTPSERVER_CONTEXT_NAME, server);

    try {//from   ww  w. ja  v  a  2 s .  c om
        server.start();
        logger.debug("FtpServer started");
    } catch (Exception e) {
        logger.error("Failed to start FtpServer", e);
        throw new RuntimeException("Failed to start FtpServer", e);
    }
}

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

/**
 * Unregister the mbean.// w  ww .  j  a v  a 2 s.c o 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:com.eryansky.common.web.servlet.StaticContentServlet.java

/**
 * ??.//from   ww w.  j  a v  a 2  s . com
 */
@Override
public void init() throws ServletException {
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    CacheManager ehcacheManager = (CacheManager) context.getBean("ehCacheManager");
    contentInfoCache = ehcacheManager.getCache("contentInfoCache");

    //?mimeTypes, css,.
    mimetypesFileTypeMap = new MimetypesFileTypeMap();
    mimetypesFileTypeMap.addMimeTypes("text/css css");
}

From source file:org.sakaiproject.imagegallery.web.ReasonableRsacServlet.java

@Override
public void init(ServletConfig config) {
    ServletContext sc = config.getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc);
    rsacbeanlocator = (RSACBeanLocator) wac.getBean(RSACBeanLocator.RSAC_BEAN_LOCATOR_NAME);
}

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

/**
 * {@inheritDoc}//www . j  a  va2s  . c  o m
 */
public void contextInitialized(ServletContextEvent event) {
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    EntityManagerFactory emf = (EntityManagerFactory) ctx.getBean("entityManagerFactory");

    PropertiesProvider propertiesProvider = new PropertiesProviderFactoryImpl().createPropertiesProvider(emf);
    Properties properties = propertiesProvider.load(NAMESPACE);
    try {
        log.info("store namespace: " + NAMESPACE);
        propertiesProvider.store(properties, NAMESPACE);
    } catch (Exception e) {
        throw new PaasManagerServerRuntimeException(e);
    }
}

From source file:jetbrick.ioc.injectors.SpringBeanFieldInjector.java

private ApplicationContext getApplicationContext(Ioc ioc) {
    ApplicationContext ctx = ioc.getBean(ApplicationContext.class);
    if (ctx != null) {
        return ctx;
    }/*www .j  a  va 2  s. c o  m*/

    ServletContext sc = ioc.getBean(ServletContext.class);
    if (sc != null) {
        ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
    }

    if (ctx == null) {
        throw new IllegalStateException("No Spring Found!");
    }

    return ctx;
}

From source file:com.lushapp.common.web.servlet.StaticContentServlet.java

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    String cacheManager = config.getInitParameter("CacheManager");
    CacheManager ehcacheManager = (CacheManager) context.getBean(cacheManager);
    String cacheKey = config.getInitParameter("cacheKey");
    contentInfoCache = ehcacheManager.getCache(cacheKey);

    //?mimeTypes, css,.
    mimetypesFileTypeMap = new MimetypesFileTypeMap();
    mimetypesFileTypeMap.addMimeTypes("text/css css");
}

From source file:com.autentia.tnt.listener.StartupListener.java

public void contextInitialized(ServletContextEvent sce) {
    try {/*from   w w  w. ja  v a2s  .c o m*/
        // Dump traces now as if nothing had happended before
        log.info("--------------------------------------------------------------------------------");
        log.info("contextInitialized - starting up application");
        log.info("contextInitialized - saving Spring's context for use by all application");

        // Save Spring context
        SpringUtils.configure(WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()));

        // Get configuration directory
        String cfgDir = ConfigurationUtil.getDefault().getConfigDir();
        log.info("contextInitialized - configuration directory set to '" + cfgDir + "'");

        // Normalized cfgDir
        if (cfgDir.endsWith("/") || cfgDir.endsWith("\\")) {
            cfgDir = cfgDir.substring(0, cfgDir.length() - 1);
        }

        // Check configuration directory
        if (!new File(cfgDir).isDirectory()) {
            throw new FileNotFoundException(cfgDir);
        }

        // Configure LOG4J
        String log4jProperties = cfgDir + FILE_LOG4J;
        if (!new File(log4jProperties).exists()) {
            throw new FileNotFoundException(log4jProperties);
        }
        PropertyConfigurator.configure(log4jProperties);
        log.info("contextInitialized - configuring LOG4J system with file " + log4jProperties);

        // Try to create essential directories
        File uploadPath = new File(ConfigurationUtil.getDefault().getUploadPath());
        log.info("contextInitialized - checking upload directory " + uploadPath.getAbsolutePath());
        uploadPath.mkdirs();
        if (!uploadPath.isDirectory()) {
            throw new FileNotFoundException(uploadPath.getAbsolutePath());
        }

        // Load reports
        if (ConfigurationUtil.getDefault().getLoadingReportOnLoad() > 0) {
            log.info("contextInitialized - loading reports in report manager");
            ReportManager.getReportManager();
        } else {
            log.info("contextInitialized - loading reports on startup disabled");
        }

        // Check database
        log.info("contextInitialized - checking database version");
        ApplicationLock.refresh();
    } catch (FileNotFoundException e) {
        log.fatal("contextInitialized - configuration dir or file not found (" + e.getMessage()
                + "): application will not be started", e);
        throw new IllegalStateException("Config dir or file not found", e);
    }
    /* Moved to Spring 
    catch( ConfigurationException e )
    {
      log.fatal("contextInitialized - error reading application configuration: application will not be started",e);
      throw new IllegalStateException("Error reading applocation configuration",e);
    }
    */
}

From source file:org.callistasoftware.netcare.web.util.TestDataHelper.java

public TestDataHelper(final ServletContext sc) {
    final WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(sc);
    setCareActorRepository(wc.getBean(CareActorRepository.class));
    setCareUnitRepository(wc.getBean(CareUnitRepository.class));
    setCountyCouncilRepository(wc.getBean(CountyCouncilRepository.class));
    setRoleRepository(wc.getBean(RoleRepository.class));
    setMeasureUnitRepository(wc.getBean(MeasureUnitRepository.class));
}

From source file:com.ideabase.repository.test.webservice.BaseRestfullControllerTestcase.java

private WebApplicationContext mockWebapplicationContext() {
    final ServletContext servletContext = new MockServletContext();
    return WebApplicationContextUtils.getWebApplicationContext(servletContext);
}