Example usage for javax.servlet ServletContext setAttribute

List of usage examples for javax.servlet ServletContext setAttribute

Introduction

In this page you can find the example usage for javax.servlet ServletContext setAttribute.

Prototype

public void setAttribute(String name, Object object);

Source Link

Document

Binds an object to a given attribute name in this ServletContext.

Usage

From source file:nl.nn.adapterframework.webcontrol.ConfigurationServlet.java

private void setUploadPathInServletContext() {
    try {/*ww w.j  a v a  2 s  .  c  o  m*/
        // set the directory for struts upload, that is used for instance in 'test a pipeline'
        ServletContext context = getServletContext();
        String path = AppConstants.getInstance().getResolvedProperty("upload.dir");
        // if the path is not found
        if (StringUtils.isEmpty(path)) {
            path = "/tmp";
        }
        log.debug("setting path for Struts file-upload to [" + path + "]");
        File tempDirFile = new File(path);
        context.setAttribute("javax.servlet.context.tempdir", tempDirFile);
    } catch (Exception e) {
        log.error(
                "Could not set servlet context attribute 'javax.servlet.context.tempdir' to value of ${upload.dir}",
                e);
    }
}

From source file:org.springframework.boot.web.support.SpringBootServletInitializer.java

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
    SpringApplicationBuilder builder = createSpringApplicationBuilder();
    builder.main(getClass());//from   w ww. j  a  va2  s.c  o  m
    ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
    if (parent != null) {
        this.logger.info("Root context already created (using as parent).");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
        builder.initializers(new ParentContextApplicationContextInitializer(parent));
    }
    builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
    builder.listeners(new ServletContextApplicationListener(servletContext));
    builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
    builder = configure(builder);
    SpringApplication application = builder.build();
    if (application.getSources().isEmpty()
            && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) {
        application.getSources().add(getClass());
    }
    Assert.state(!application.getSources().isEmpty(),
            "No SpringApplication sources have been defined. Either override the "
                    + "configure method or add an @Configuration annotation");
    // Ensure error pages are registered
    if (this.registerErrorPageFilter) {
        application.getSources().add(ErrorPageFilterConfiguration.class);
    }
    return run(application);
}

From source file:org.ajax4jsf.webapp.WebXml.java

@Override
public void init(ServletContext context, String filterName) throws ServletException {
    super.init(context, filterName);
    setupResourcePrefixes(context);/*from   w ww .  ja  va  2 s.  c o  m*/
    // Store Instance to context attribute.
    context.setAttribute(CONTEXT_ATTRIBUTE, this);

}

From source file:org.motechproject.server.osgi.OsgiFrameworkService.java

/**
 * Initialize and start the OSGi framework
 *///from   w w w .j ava  2s .c  o  m
public void start() {
    try {
        ServletContext servletContext = ((WebApplicationContext) applicationContext).getServletContext();

        osgiFramework.init();

        BundleContext bundleContext = osgiFramework.getBundleContext();

        // This is mandatory for Felix http servlet bridge
        servletContext.setAttribute(BundleContext.class.getName(), bundleContext);

        // install bundles
        ArrayList<Bundle> bundles = new ArrayList<Bundle>();
        for (URL url : findBundles(servletContext)) {
            logger.debug("Installing bundle [" + url + "]");
            Bundle bundle = bundleContext.installBundle(url.toExternalForm());
            bundles.add(bundle);
            storeClassCloader(bundle);
        }

        for (Bundle bundle : bundles) {
            // custom bundle loaders
            if (bundleLoaders != null) {
                for (BundleLoader loader : bundleLoaders) {
                    loader.loadBundle(bundle);
                }
            }
            bundle.start();
        }

        osgiFramework.start();
        logger.info("OSGi framework started");
    } catch (Throwable e) {
        logger.error("Failed to start OSGi framework", e);
        throw new RuntimeException(e);
    }
}

From source file:org.syncope.buildtools.H2StartStopListener.java

@Override
public void contextInitialized(final ServletContextEvent sce) {
    ServletContext context = sce.getServletContext();

    File workDir = (File) sce.getServletContext().getAttribute("javax.servlet.context.tempdir");
    try {/*from   ww w.  j a  v  a  2  s.  c o m*/
        Server h2TestDb = new Server();
        h2TestDb.runTool("-baseDir", workDir.getAbsolutePath(), "-tcp", "-tcpDaemon", "-web", "-webDaemon",
                "-webPort", sce.getServletContext().getInitParameter("testdb.webport"));

        context.setAttribute(H2_TESTDB, h2TestDb);
    } catch (SQLException e) {
        LOG.error("Could not start H2 test db", e);
    }

    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
    DataSource datasource = ctx.getBean(DataSource.class);

    Connection conn = null;
    Statement stmt = null;
    try {
        conn = DataSourceUtils.getConnection(datasource);
        stmt = conn.createStatement();
        stmt.executeUpdate("RUNSCRIPT FROM 'classpath:/testdb.sql'");
    } catch (Exception e) {
        LOG.error("While loading data into testdb", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
        DataSourceUtils.releaseConnection(conn, datasource);
    }
}

From source file:com.openkm.servlet.admin.StampServlet.java

/**
 * List text stamp//w  w w .j ava2 s  . co  m
 */
private void textList(Session session, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException, PrincipalAdapterException {
    log.debug("textList({}, {}, {})", new Object[] { session, request, response });
    ServletContext sc = getServletContext();
    sc.setAttribute("stamps", StampTextDAO.findAll());
    sc.getRequestDispatcher("/admin/stamp_text_list.jsp").forward(request, response);
    log.debug("textList: void");
}

From source file:com.openkm.servlet.admin.StampServlet.java

/**
 * List image stamp/*from  www  .  ja  va 2s  .c o m*/
 */
private void imageList(Session session, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("imageList({}, {}, {})", new Object[] { session, request, response });
    ServletContext sc = getServletContext();
    sc.setAttribute("stamps", StampImageDAO.findAll());
    sc.getRequestDispatcher("/admin/stamp_image_list.jsp").forward(request, response);
    log.debug("imageList: void");
}

From source file:org.apache.struts2.s1.ActionFormValidationInterceptor.java

/**
 * Initializes the validation resources//from  w  w w. j  a  va2 s .c  o m
 */
private void initResources(ServletContext servletContext) {
    if (pathnames != null) {
        ActionContext ctx = ActionContext.getContext();
        try {

            ValidatorResources resources = this.loadResources(servletContext);

            String prefix = ctx.getActionInvocation().getProxy().getNamespace();

            servletContext.setAttribute(ValidatorPlugIn.VALIDATOR_KEY + prefix, resources);

            servletContext.setAttribute(ValidatorPlugIn.STOP_ON_ERROR_KEY + '.' + prefix,
                    (this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE));
        } catch (Exception e) {
            throw new StrutsException("Cannot load a validator resource from '" + pathnames + "'", e);
        }
    }
}

From source file:com.ikon.servlet.admin.LanguageServlet.java

/**
 * Delete language/*from  www . ja  va  2  s . c o  m*/
 */
private void delete(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("delete({}, {}, {})", new Object[] { userId, request, response });

    ServletContext sc = getServletContext();
    String lgId = WebUtils.getString(request, "lg_id");
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("persist", true);
    sc.setAttribute("lg", LanguageDAO.findByPk(lgId));
    sc.getRequestDispatcher("/admin/language_edit.jsp").forward(request, response);

    log.debug("delete: void");
}

From source file:com.ikon.servlet.admin.LanguageServlet.java

/**
 * Edit language/*from w  w w. ja v  a  2s . co  m*/
 */
private void edit(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("edit({}, {}, {})", new Object[] { userId, request, response });

    ServletContext sc = getServletContext();
    String lgId = WebUtils.getString(request, "lg_id");
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("persist", true);
    sc.setAttribute("lg", LanguageDAO.findByPk(lgId));
    sc.getRequestDispatcher("/admin/language_edit.jsp").forward(request, response);

    log.debug("edit: void");
}