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:io.github.benas.todolist.web.servlet.user.account.DeleteAccountServlet.java

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(servletConfig.getServletContext());
    userService = applicationContext.getBean(UserService.class);
}

From source file:net.tirasa.connid.bundles.soap.wssample.DefaultContentLoader.java

@Override
public void contextInitialized(final ServletContextEvent sce) {
    final WebApplicationContext springContext = WebApplicationContextUtils
            .getWebApplicationContext(sce.getServletContext());

    if (springContext == null) {
        LOG.error("Invalid Spring context");
        return;/*from  w ww.  j  av  a2  s  .  c o  m*/
    }

    final DataSource dataSource = springContext.getBean(DataSource.class);
    localDataSource = dataSource;

    final DefaultDataTypeFactory dbUnitDataTypeFactory = (DefaultDataTypeFactory) springContext
            .getBean("dbUnitDataTypeFactory");

    final Connection conn = DataSourceUtils.getConnection(dataSource);

    // create schema
    final StringBuilder statement = new StringBuilder();

    final InputStream dbschema = DefaultContentLoader.class.getResourceAsStream(DBSCHEMA);

    final BufferedReader buff = new BufferedReader(new InputStreamReader(dbschema));

    String line = null;
    try {
        while ((line = buff.readLine()) != null) {
            if (!line.isEmpty() && !line.startsWith("--")) {
                statement.append(line);
            }
        }
    } catch (IOException e) {
        LOG.error("Error reading file " + DBSCHEMA, e);
        return;
    }

    Statement st = null;
    try {
        st = conn.createStatement();
        st.execute(statement.toString());
    } catch (SQLException e) {
        LOG.error("Error creating schema:\n" + statement.toString(), e);
        return;
    } finally {
        try {
            st.close();
        } catch (Throwable t) {
            // ignore exception
        }
    }

    try {
        IDatabaseConnection dbUnitConn = new DatabaseConnection(conn);

        final DatabaseConfig config = dbUnitConn.getConfig();
        config.setProperty("http://www.dbunit.org/properties/datatypeFactory", dbUnitDataTypeFactory);

        boolean existingData = false;
        final IDataSet existingDataSet = dbUnitConn.createDataSet();
        for (final ITableIterator itor = existingDataSet.iterator(); itor.next() && !existingData;) {
            existingData = (itor.getTable().getRowCount() > 0);
        }

        final FlatXmlDataSetBuilder dataSetBuilder = new FlatXmlDataSetBuilder();
        dataSetBuilder.setColumnSensing(true);
        final IDataSet dataSet = dataSetBuilder.build(getClass().getResourceAsStream("/content.xml"));
        DatabaseOperation.REFRESH.execute(dbUnitConn, dataSet);
    } catch (Throwable t) {
        LOG.error("Error loding default content", t);
    } finally {
        DataSourceUtils.releaseConnection(conn, dataSource);
    }
}

From source file:de.interseroh.report.webapp.SessionListener.java

private ApplicationContext getApplicationContext(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
    return ctx;//w w  w.j  a v a2s .c o  m
}

From source file:pl.exsio.frameset.vaadin.bootstrap.servlet.FramesetServlet.java

@Override
public void init(ServletConfig config) throws ServletException {
    this.applicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());

    if (config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER) != null) {
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setParent(this.applicationContext);
        context.setConfigLocation(config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER));
        context.setServletConfig(config);
        context.setServletContext(config.getServletContext());
        context.refresh();/*w  ww  . j  a v  a 2  s.  c om*/

        this.applicationContext = context;
    }

    if (config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER) != null) {
        this.systemMessagesBeanName = config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER);
    }

    if (FramesetApplicationContext.getApplicationContext() == null) {
        FramesetApplicationContext.setApplicationContext(applicationContext);
    }

    super.init(config);
}

From source file:cn.zhuqi.mavenssh.web.servlet.ShowPic.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w w  w. j  a  va 2s.  c  o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Pragma", "No-cache");// ?????
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expire", 0);
    // ?Application
    ServletContext application = this.getServletContext();
    String sid = request.getParameter("id");
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);

    // ?spring IOC
    userService = (IUserService) ctx.getBean(IUserService.class);
    User user = userService.findById(Integer.valueOf(sid));
    String imgFile = user.getPicurl();
    response.setContentType(application.getMimeType(imgFile));

    FTPClientTemplate ftpClient = new FTPClientTemplate(ip, 21, username, pwd);
    ServletOutputStream sos = response.getOutputStream();
    try {
        ftpClient.get(attachment + "/" + imgFile, sos);
    } catch (Exception ex) {
    }
}

From source file:com.google.gwt.sample.dynatablemvp.server.loc.SpringServiceLocator.java

@Override
public Object getInstance(Class<?> clazz) {
    final Object bean;
    if (clazz.equals(entityClass)) {
        HttpServletRequest request = RequestFactoryServlet.getThreadLocalRequest();
        ApplicationContext context = WebApplicationContextUtils
                .getWebApplicationContext(request.getSession().getServletContext());
        bean = context.getBean(entityClass);
    } else/*from  w ww  .ja  v a 2 s.c om*/
        bean = null;
    return bean;
}

From source file:nz.co.senanque.vaadinsupport.application.SpringApplicationLoader.java

public static ApplicationContext loadContext(Application application, HttpServletRequest request) {

    // Logging/*from www . j a  v  a 2s. c o  m*/
    log.info("loading application context for Vaadin application " + application.getClass().getSimpleName());
    CURRENT_APPLICATION.set(application);

    // Find the application context associated with the servlet; it will be the parent
    ServletContext servletContext;
    try {
        // getServletContext() is a servlet AIP 3.0 method, so don't freak out if it's not there
        servletContext = (ServletContext) HttpServletRequest.class.getMethod("getServletContext")
                .invoke(request);
    } catch (Exception e) {
        servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
    }
    WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(servletContext);

    // Create and configure a new application context for this Application instance
    ConfigurableWebApplicationContext context = new XmlWebApplicationContext();
    context.setId(
            ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath()
                    + "/" + application.getClass().getSimpleName() + "-" + UNIQUE_INDEX.incrementAndGet());
    context.setParent(parent);
    context.setServletContext(servletContext);
    //context.setServletConfig(??);
    context.setNamespace(application.getClass().getSimpleName());

    // Refresh context
    context.refresh();

    CURRENT_APPLICATION.set(null);
    return context;
}

From source file:grails.util.Holders.java

/**
 *
 * @return The ApplicationContext or null if it doesn't exist
 */// w ww.j  a  va2  s. c o  m
public static ApplicationContext findApplicationContext() {
    ServletContext servletContext = getServletContext();
    if (servletContext != null) {
        return WebApplicationContextUtils.getWebApplicationContext(servletContext);
    }
    return null;
}

From source file:de.knurt.ajajajava.control.ChangeLocation.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 *
 * @param request servlet request/*from  w ww .j  a  v a2s  .c o  m*/
 * @param response servlet response
 * @throws javax.servlet.ServletException
 * @throws java.io.IOException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    WebApplicationContext context = WebApplicationContextUtils
            .getWebApplicationContext(this.getServletContext());

    // get config objects
    Config config = (Config) context.getBean("config");
    LocationPart locationPart = (LocationPart) context.getBean(request.getParameter("locationPart"));

    // set content type
    if (locationPart.getPartName().equals("javascript")) {
        response.setContentType("text/javascript;charset=" + Config.CHARSET);
    } else {
        response.setContentType("text/html;charset=" + Config.CHARSET);
    }

    Location location = this.getLocation(request, context);

    this.invokeAllAnnotatedMethods(location, request, context);

    RequestDispatcher dispatcher = this.getDispatcher(request, config, location, locationPart);
    dispatcher.include(request, response);
}