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.strohalm.cyclos.http.LifecycleListener.java

/**
 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
 *//*from w ww.  ja  v  a 2 s  .  c  om*/
@Override
public void contextInitialized(final ServletContextEvent event) {
    LoggedUser.runAsSystem(new Callable<Void>() {
        @Override
        public Void call() {
            try {
                final ServletContext context = event.getServletContext();

                final WebApplicationContext applicationContext = WebApplicationContextUtils
                        .getWebApplicationContext(context);
                SpringHelper.injectBeans(applicationContext, LifecycleListener.this);

                applicationService.initialize();

                context.setAttribute("systemOnline", applicationService.isOnline());
                context.setAttribute("cyclosVersion", applicationService.getCyclosVersion());

                // Run web initializations
                final Collection<LocalWebInitialization> initializations = applicationContext
                        .getBeansOfType(LocalWebInitialization.class).values();
                runAll(initializations);

                final LocalSettings settings = settingsService.getLocalSettings();
                context.log(settings.getApplicationName() + " initialized");

                // Suggest a GC in order to keep the heap low right after a startup
                System.gc();
            } catch (final Throwable e) {
                LOG.error("Error on LifecycleListener.contextInitialized()", e);
                throw new RuntimeException(e);
            }
            return null; // required by compiler
        }
    });
}

From source file:com.centeractive.ws.server.core.SoapServer.java

private void configureWebContext() {
    ServletContext servletContext = getServletContext();
    GenericWebApplicationContext webContext = new GenericWebApplicationContext();
    webContext.setServletContext(servletContext);
    webContext.setParent(context);//from  w ww.j a v a2  s  . co m
    webContext.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext);
    if (webContext != null) {
        endpoint = webContext.getBean(ENDPOINT_BEAN_NAME, GenericContextDomEndpoint.class);
    }
}

From source file:be.fedict.trust.xkms2.ServiceConsumerServletContextListener.java

@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {
    LOG.debug("context initialized");
    ServletContext servletContext = event.getServletContext();
    Enumeration<String> attributeNames = servletContext.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        String attributeName = attributeNames.nextElement();
        LOG.debug("servlet context attribute: " + attributeName);
    }/*  ww w.j  av a2s.  c  om*/
    LOG.debug("trust service injected: " + (null != this.trustService));
    /*
     * Via the Servlet Context we can make the Trust Service EJB3 reference
     * available to the JAW-WS endpoints.
     */
    servletContext.setAttribute(TrustService.class.getName(), this.trustService);
}

From source file:org.rhq.enterprise.gui.startup.Configurator.java

/**
 * There is a single properties file in the web application that defines the default user properties. That
 * properties file is loaded in and stored in the servlet context so a user that logs in but doesn't have
 * preferences set yet can obtain these default settings.
 *//* w w  w.  jav a 2 s. c  om*/
private void loadPreferences(ServletContext ctx) {
    try {
        Configuration userPrefs = new Configuration();
        Properties userProps = loadProperties(ctx, DEFAULT_USER_PREFERENCES_FILE);
        Enumeration keys = userProps.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            userPrefs.put(new PropertySimple(key, userProps.getProperty(key)));
        }

        ctx.setAttribute(Constants.DEF_USER_PREFS, userPrefs);
    } catch (Exception e) {
        log.error("failed to load user preferences at [" + DEFAULT_USER_PREFERENCES_FILE + "]: ", e);
    }
}

From source file:org.seasar.struts.hotdeploy.plugin.HotdeployPlugIn.java

private void initValidatorResources(ServletContext context, ModuleConfig config) {
    S2Container container = getContainer();
    if (!container.hasComponentDef(ValidatorResourcesWrapper.class)) {
        return;/* ww w.  java2s.c om*/
    }

    ValidatorResourcesWrapper resourcesWrapper = (ValidatorResourcesWrapper) container
            .getComponent(ValidatorResourcesWrapper.class);
    ValidatorResources resources = (ValidatorResources) context
            .getAttribute(ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix());
    resourcesWrapper.init(resources);
    context.setAttribute(ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix(), resourcesWrapper);
}

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

/**
 * Edit text stamp// w  w  w. ja  va  2  s  .c  o  m
 */
private void textEdit(Session session, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException, NoSuchAlgorithmException {
    log.debug("textEdit({}, {}, {})", new Object[] { session, request, response });

    if (WebUtils.getBoolean(request, "persist")) {
        StampText st = new StampText();
        st.setId(WebUtils.getInt(request, "st_id"));
        st.setName(WebUtils.getString(request, "st_name"));
        st.setDescription(WebUtils.getString(request, "st_description"));
        st.setText(WebUtils.getString(request, "st_text"));
        st.setLayer(WebUtils.getInt(request, "st_layer"));
        st.setOpacity(WebUtils.getFloat(request, "st_opacity"));
        st.setSize(WebUtils.getInt(request, "st_size"));
        st.setColor(WebUtils.getString(request, "st_color"));
        st.setAlign(WebUtils.getInt(request, "st_align"));
        st.setRotation(WebUtils.getInt(request, "st_rotation"));
        st.setExprX(WebUtils.getString(request, "st_expr_x"));
        st.setExprY(WebUtils.getString(request, "st_expr_y"));
        st.setActive(WebUtils.getBoolean(request, "st_active"));
        st.setUsers(new HashSet<String>(WebUtils.getStringList(request, "st_users")));

        StampTextDAO.update(st);

        // Activity log
        UserActivity.log(session.getUserID(), "ADMIN_STAMP_TEXT_EDIT", Integer.toString(st.getId()),
                st.toString());
    } else {
        ServletContext sc = getServletContext();
        int stId = WebUtils.getInt(request, "st_id");
        sc.setAttribute("action", WebUtils.getString(request, "action"));
        sc.setAttribute("persist", true);
        sc.setAttribute("users", AuthDAO.findAllUsers(true));
        sc.setAttribute("stamp", StampTextDAO.findByPk(stId));
        sc.getRequestDispatcher("/admin/stamp_text_edit.jsp").forward(request, response);
    }

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

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

/**
 * Edit config/*from  w  ww .  j a v a2 s  . c om*/
 */
private void edit(String userId, Map<String, String> types, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException, DatabaseException {
    ServletContext sc = getServletContext();
    String cfgKey = WebUtils.getString(request, "cfg_key");
    Config cfg = ConfigDAO.findByPk(cfgKey);
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("filter", WebUtils.getString(request, "filter"));
    sc.setAttribute("persist", true);
    sc.setAttribute("types", types);
    sc.setAttribute("cfg", cfg);
    sc.getRequestDispatcher("/admin/config_edit.jsp").forward(request, response);
}

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

/**
 * Delete config//  w  w  w .  jav a2s . c om
 */
private void delete(String userId, Map<String, String> types, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException, DatabaseException {
    ServletContext sc = getServletContext();
    String cfgKey = WebUtils.getString(request, "cfg_key");
    Config cfg = ConfigDAO.findByPk(cfgKey);
    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("filter", WebUtils.getString(request, "filter"));
    sc.setAttribute("persist", true);
    sc.setAttribute("types", types);
    sc.setAttribute("cfg", cfg);
    sc.getRequestDispatcher("/admin/config_edit.jsp").forward(request, response);
}

From source file:org.jaggeryjs.jaggery.app.mgt.TomcatJaggeryWebappsDeployer.java

private static void addSessionCreatedListners(Context context, JSONArray arr) {
    if (arr != null) {
        try {/*from ww w .j a  va 2 s  .c om*/
            JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(context.getServletContext());
            CommonManager.setJaggeryContext(sharedContext);
            RhinoEngine engine = sharedContext.getEngine();
            org.mozilla.javascript.Context cx = engine.enterContext();
            ServletContext servletContext = (ServletContext) sharedContext
                    .getProperty(org.jaggeryjs.hostobjects.web.Constants.SERVLET_CONTEXT);

            List<String> jsListeners = new ArrayList<String>();

            Object[] scripts = arr.toArray();
            for (Object script : scripts) {

                if (!(script instanceof String)) {
                    log.error("Invalid value for initScripts/destroyScripts in jaggery.conf : " + script);
                    continue;
                }
                String path = (String) script;
                path = path.startsWith("/") ? path : "/" + path;
                Stack<String> callstack = CommonManager.getCallstack(sharedContext);
                callstack.push(path);

                jsListeners.add(path);
            }

            servletContext.setAttribute(JaggeryCoreConstants.JS_CREATED_LISTENERS, jsListeners);

        } finally {
            if (org.mozilla.javascript.Context.getCurrentContext() != null) {
                RhinoEngine.exitContext();
            }
        }
    }
}

From source file:org.jaggeryjs.jaggery.app.mgt.TomcatJaggeryWebappsDeployer.java

private static void addSessionDestroyedListners(Context context, JSONArray arr) {
    if (arr != null) {
        try {//from   w  w w .  ja va2s.  c  o  m
            JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(context.getServletContext());
            CommonManager.setJaggeryContext(sharedContext);
            RhinoEngine engine = sharedContext.getEngine();
            org.mozilla.javascript.Context cx = engine.enterContext();
            ServletContext servletContext = (ServletContext) sharedContext
                    .getProperty(org.jaggeryjs.hostobjects.web.Constants.SERVLET_CONTEXT);

            List<String> jsListeners = new ArrayList<String>();

            Object[] scripts = arr.toArray();
            for (Object script : scripts) {

                if (!(script instanceof String)) {
                    log.error("Invalid value for initScripts/destroyScripts in jaggery.conf : " + script);
                    continue;
                }
                String path = (String) script;
                path = path.startsWith("/") ? path : "/" + path;
                Stack<String> callstack = CommonManager.getCallstack(sharedContext);
                callstack.push(path);

                jsListeners.add(path);
            }

            servletContext.setAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS, jsListeners);

        } finally {
            if (org.mozilla.javascript.Context.getCurrentContext() != null) {
                RhinoEngine.exitContext();
            }
        }
    }
}