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

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

Introduction

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

Prototype

public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)
        throws IllegalStateException 

Source Link

Document

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

Usage

From source file:org.granite.grails.integration.GrailsClassGetter.java

private ClassGetter getDelegate() {
    if (delegate == null) {
        GraniteContext context = GraniteContext.getCurrentInstance();
        ServletContext sc = ((HttpGraniteContext) context).getServletContext();
        ApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

        grailsApplication = (GrailsApplication) springContext.getBean("grailsApplication");
        GrailsPluginManager manager = (GrailsPluginManager) springContext.getBean("pluginManager");

        if (manager.hasGrailsPlugin("app-engine"))
            delegate = new DataNucleusClassGetter();
        else// w  ww . j ava 2s . c o  m
            delegate = new HibernateClassGetter();
    }
    return delegate;
}

From source file:org.cometd.server.SpringFrameworkConfigurationTest.java

@Test
public void testXMLSpringConfiguration() throws Exception {
    int port = this.port;
    server.stop();//from  ww  w.  j ava2 s .  c o  m
    // Add Spring listener
    context.addEventListener(new ContextLoaderListener());
    context.getInitParams().put(ContextLoader.CONFIG_LOCATION_PARAM, "classpath:/applicationContext.xml");
    connector.setPort(port);
    server.start();

    WebApplicationContext applicationContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(context.getServletContext());
    assertNotNull(applicationContext);

    int sweepPeriod = (Integer) applicationContext.getBean("sweepPeriod");

    BayeuxServerImpl bayeuxServer = (BayeuxServerImpl) applicationContext.getBean("bayeux");
    assertNotNull(bayeuxServer);
    assertTrue(bayeuxServer.isStarted());
    assertEquals(sweepPeriod, bayeuxServer.getOption("sweepPeriod"));

    assertSame(bayeuxServer, cometdServlet.getBayeux());

    Request handshake = newBayeuxRequest("" + "[{" + "\"channel\": \"/meta/handshake\","
            + "\"version\": \"1.0\"," + "\"minimumVersion\": \"1.0\","
            + "\"supportedConnectionTypes\": [\"long-polling\"]" + "}]");
    ContentResponse response = handshake.send();
    assertEquals(200, response.getStatus());
}

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

/**
 * ?DI//from  w ww  . j  a  v  a 2s .co 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.sbu.controller.Update_Form_Employer_Controller.java

@Override
public void init() throws ServletException {
    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    skillsService = context.getBean(SkillsManager.class);
    employeeService = context.getBean(EmployeeManager.class);
    projectService = context.getBean(ProjectManager.class);
    employerService = context.getBean(EmployerManager.class);
    feedService = context.getBean(FeedManager.class);
}

From source file:org.efs.openreports.dispatcher.XMLADispatcher.java

public void init(ServletConfig servletConfig) throws ServletException {
    ApplicationContext appContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletConfig.getServletContext());

    directoryProvider = (DirectoryProvider) appContext.getBean("directoryProvider", DirectoryProvider.class);

    super.init(servletConfig);
}

From source file:ee.pri.rl.blog.web.servlet.FileDownloadServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    WebApplicationContext context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    BlogService blogService = (BlogService) context.getBean("blogService");

    String path = req.getRequestURI().substring(req.getContextPath().length());
    log.debug("Requested file " + path);

    if (path.startsWith("/")) {
        path = path.substring(1);/*  w ww  . j a  v  a  2 s .  c  o m*/
    }
    if (path.startsWith("files/")) {
        path = path.substring("files/".length());
    }

    int slashIndex = path.indexOf('/');
    if (slashIndex > 0) {
        String entryName = path.substring(0, slashIndex);
        log.debug("Entry name: " + entryName);
        boolean authenticated = Session.exists() && ((BlogSession) Session.get()).isAuthenticated();
        if (blogService.isPrivateEntry(entryName) && !authenticated) {
            log.warn("Tried to access private files");
            resp.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
    }

    File directory = new File(blogService.getSetting(SettingName.UPLOAD_PATH).getValue());
    File file = new File(directory, path);

    if (!file.exists()) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        log.warn("File " + file + " does not exist");
        return;
    }

    // Check if the requested file is still inside the upload dir.
    if (!FileUtil.insideDirectory(file, directory)) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        log.warn("File " + file + " is not inside upload dir");
        return;
    }

    try {
        String calculatedTag = blogService.getUploadedFileTag(path);
        String tag = req.getHeader("If-None-Match");

        if (tag == null) {
            log.debug("Tag not found, sending file");
            sendFile(path, calculatedTag, directory, resp);
        } else if (tag.equals(calculatedTag)) {
            log.debug("Tag matches, sending 304");
            sendNotModified(calculatedTag, resp);
        } else {
            log.debug("Tag does not match, sending file");
            sendFile(path, calculatedTag, directory, resp);
        }
    } catch (NoSuchFileException e) {
        log.warn("File " + file + " does not exist");
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

}

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

/**
 * ?DI// ww w  .java2s.c  o m
 * 
 * @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:kurosawa.dictionary.main.server.DictionaryServiceImpl.java

public void init() throws ServletException {
    try {/*from   w  w  w.jav  a2 s  .c om*/
        log.info("WebApplicationContextUtils");
        // applicationContext.xml????Bean???
        // ??? Autowire?
        WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())
                .getAutowireCapableBeanFactory().autowireBean(this);
        log.info("init WebApplicationContextUtils end");
    } catch (IllegalStateException ex) {
        // ?????web.xml????
        throw new ServletException("Couldn't get Spring's WebApplicationContext. "
                + "Please check whether ContextLoaderListener exists " + "in your web.xml.");
    }
}

From source file:com.cubusmail.gwtui.server.services.CubusStartupListener.java

public void contextInitialized(ServletContextEvent servletcontextevent) {

    try {//  w  w  w  . j a v a  2  s . c  om
        WebApplicationContext context = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletcontextevent.getServletContext());
        BeanFactory.setContext(context);
    } catch (Throwable e) {
        log.fatal(e.getMessage(), e);
        throw new IllegalStateException("Could not load " + CubusConstants.LOGIN_MODULE_CONFIG_FILE);
    }

    try {
        URL test = CubusStartupListener.class.getClassLoader()
                .getResource(CubusConstants.LOGIN_MODULE_CONFIG_FILE);
        System.setProperty(CubusConstants.JAAS_PROPERTY_NANE, test.getFile());
    } catch (Throwable e) {
        log.fatal(e.getMessage(), e);
        throw new IllegalStateException("Could not load " + CubusConstants.LOGIN_MODULE_CONFIG_FILE);
    }
}

From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.DbManagerFilter.java

protected DbManager lookupDbManager() {
    WebApplicationContext wac = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    return (DbManager) wac.getBean("mongodbManager", DbManager.class);
}