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:com.github.glue.mvc.guice.GuiceConfigListener.java

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    super.contextInitialized(servletContextEvent);
    final ServletContext servletContext = servletContextEvent.getServletContext();
    servletContext.setAttribute(IOCCONTAINER, new GuiceContainer(createInjector()));
}

From source file:net.ontopia.topicmaps.webed.impl.utils.TagUtils.java

public static Map getSchemaRegistry(ServletContext servletContext) {
    Map schemas = (Map) servletContext.getAttribute(Constants.AA_SCHEMAS);
    if (schemas != null)
        return schemas;

    // Read in schemas for the topicmaps and provide them to the app context
    String schemasRootDir = servletContext.getInitParameter(Constants.SCTXT_SCHEMAS_ROOTDIR);
    if (schemasRootDir != null)
        schemasRootDir = servletContext.getRealPath(schemasRootDir);

    schemas = new HashMap();
    if (schemasRootDir == null) {
        servletContext.setAttribute(Constants.AA_SCHEMAS, schemas);
        log.debug("No schema directory configured; registry empty");
        return schemas;
    }/*  ww w  . j a va  2 s. c om*/

    log.debug("Reading schemas from " + schemasRootDir);
    TopicMapRepositoryIF repository = NavigatorUtils.getTopicMapRepository(servletContext);
    Collection refkeys = repository.getReferenceKeys();
    Iterator iter = refkeys.iterator();
    while (iter.hasNext()) {
        String refkey = (String) iter.next();
        TopicMapReferenceIF reference = repository.getReferenceByKey(refkey);
        String tmid = reference.getId();
        try {
            OSLSchemaReader reader = new OSLSchemaReader(new File(schemasRootDir, tmid + ".osl"));
            OSLSchema schema = (OSLSchema) reader.read();
            schemas.put(tmid, schema);
            log.info("Loaded schema for " + tmid);
        } catch (java.io.IOException e) {
            log.info("Warning: " + e.getMessage());
        } catch (SchemaSyntaxException e) {
            log.error("Schema syntax error: " + e.getMessage());
            Locator loc = e.getErrorLocation();
            log.error("Location: " + loc.getSystemId() + ":" + loc.getLineNumber() + ":" + loc.getColumnNumber()
                    + ".");
        }
    }

    servletContext.setAttribute(Constants.AA_SCHEMAS, schemas);
    return schemas;
}

From source file:v7cr.InitDB.java

@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent e) {
    try {/*from   w  w w . j a  v a2s . c  om*/
        Mongo db = new Mongo();

        ServletContext c = e.getServletContext();
        c.setAttribute(getClass().getName(), db);

        // check if the "roles" collection
        // exists, if not create it

        if (getDBCollection(c, "roles").findOne() == null) {
            String json = IOUtils.toString(getClass().getResourceAsStream("roles.json"), "UTF-8");
            List<DBObject> l = (List<DBObject>) JSON.parse(json);
            DBCollection roles = getDBCollection(c, "roles");
            for (DBObject r : l) {
                r.put("_version", 1);
                roles.save(r);
            }

        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:stubs.edu.cornell.mannlib.vitro.webapp.startup.StartupStatusStub.java

public StartupStatusStub(ServletContext ctx) {
    ctx.setAttribute(ATTRIBUTE_NAME, this);
}

From source file:com.openkm.servlet.HtmlPreviewServlet.java

/**
 * //from   w  ww .  ja v a  2s  .c o  m
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    log.debug("doGet({}, {})", request, response);
    String uuid = WebUtils.getString(request, "uuid");
    InputStream fis = null;

    try {
        fis = OKMDocument.getInstance().getContent(null, uuid, false);
        StringWriter writer = new StringWriter();
        IOUtils.copy(fis, writer, "UTF-8");
        String content = writer.getBuffer().toString();

        ServletContext sc = getServletContext();
        sc.setAttribute("content", content);
        sc.getRequestDispatcher("/html_preview.jsp").forward(request, response);
    } catch (PathNotFoundException e) {
        sendErrorRedirect(request, response, e);
    } catch (AccessDeniedException e) {
        sendErrorRedirect(request, response, e);
    } catch (RepositoryException e) {
        sendErrorRedirect(request, response, e);
    } catch (DatabaseException e) {
        sendErrorRedirect(request, response, e);
    } finally {
        IOUtils.closeQuietly(fis);
    }
}

From source file:org.guzz.web.context.ContextLoader.java

public GuzzContext initGuzzContext(ServletContext sc, GuzzContext guzzContext) throws Exception {
    sc.setAttribute(GuzzWebApplicationContextUtil.GUZZ_SERVLET_CONTEXT_KEY, guzzContext);
    this.guzzContext = guzzContext;

    return guzzContext;
}

From source file:com.legstar.host.servlet.InitiatorServlet.java

/**
 * Initialization of the servlet. Loads configuration file and creates an
 * instance of the engine handler./*from w ww  . j  a  v a2 s. c  o m*/
 *
 * @param config the complete configuration hierarchy
 * @throws ServletException if an error occurs
 */
public void init(final ServletConfig config) throws ServletException {
    String configFileName = config.getInitParameter(CONFIG_PARAM);
    if (configFileName == null || configFileName.length() == 0) {
        throw new ServletException("Web.xml does not contain the " + CONFIG_PARAM + " parameter.");
    }

    LOG.info("Initializing with " + configFileName + " configuration file.");

    try {
        LegStarConfigCommons legStarConfig = new LegStarConfigCommons(configFileName);
        EngineHandler serverHandler = new EngineHandler(legStarConfig.getPoolingEngineConfig());

        serverHandler.init();
        ServletContext servletContext = config.getServletContext();
        servletContext.setAttribute(ENGINE_HANDLER_ID, serverHandler);

    } catch (EngineStartupException e) {
        LOG.error("Failed to start engine.", e);
        throw new ServletException(e);
    } catch (LegStarConfigurationException e) {
        LOG.error("Failed to start engine.", e);
        throw new ServletException(e);
    }
}

From source file:net.jforum.core.VRaptorSpringProvider.java

@Override
protected ConfigurableWebApplicationContext getParentApplicationContext(ServletContext context) {
    ConfigurableWebApplicationContext springContext = super.getParentApplicationContext(context);
    context.setAttribute(ConfigKeys.SPRING_CONTEXT, springContext);
    return springContext;
}

From source file:org.apache.axis.transport.http.AxisServletBase.java

/**
 * put the engine back in to the context.
 * @param context servlet context to use
 * @param engine reference to the engine. If null, the engine is removed
 *//*from   w ww  .jav  a  2 s  .c o m*/
private static void storeEngine(HttpServlet servlet, AxisServer engine) {
    ServletContext context = servlet.getServletContext();
    String axisServletName = servlet.getServletName();
    if (engine == null) {
        context.removeAttribute(axisServletName + ATTR_AXIS_ENGINE);
        // find if there is other AxisEngine in Context
        AxisServer server = (AxisServer) context.getAttribute(ATTR_AXIS_ENGINE);

        // no other AxisEngine in ServletContext
        if (server != null && servlet.getServletName().equals(server.getName())) {
            context.removeAttribute(ATTR_AXIS_ENGINE);
        }
    } else {
        if (context.getAttribute(ATTR_AXIS_ENGINE) == null) {
            // first Axis servlet to store its AxisEngine
            // use default name
            context.setAttribute(ATTR_AXIS_ENGINE, engine);
        }
        context.setAttribute(axisServletName + ATTR_AXIS_ENGINE, engine);
    }
}

From source file:com.serotonin.m2m2.web.M2M2ContextListener.java

private void constantsInitialize(ServletContext ctx) {
    ctx.setAttribute("constants.Common.NEW_ID", Common.NEW_ID);

    ctx.setAttribute("constants.DataTypes.BINARY", DataTypes.BINARY);
    ctx.setAttribute("constants.DataTypes.MULTISTATE", DataTypes.MULTISTATE);
    ctx.setAttribute("constants.DataTypes.NUMERIC", DataTypes.NUMERIC);
    ctx.setAttribute("constants.DataTypes.ALPHANUMERIC", DataTypes.ALPHANUMERIC);
    ctx.setAttribute("constants.DataTypes.IMAGE", DataTypes.IMAGE);

    ctx.setAttribute("constants.Permissions.DataPointAccessTypes.NONE", Permissions.DataPointAccessTypes.NONE);
    ctx.setAttribute("constants.Permissions.DataPointAccessTypes.READ", Permissions.DataPointAccessTypes.READ);
    ctx.setAttribute("constants.Permissions.DataPointAccessTypes.SET", Permissions.DataPointAccessTypes.SET);
    ctx.setAttribute("constants.Permissions.DataPointAccessTypes.DATA_SOURCE",
            Permissions.DataPointAccessTypes.DATA_SOURCE);
    ctx.setAttribute("constants.Permissions.DataPointAccessTypes.ADMIN",
            Permissions.DataPointAccessTypes.ADMIN);

    ctx.setAttribute("constants.EventType.EventTypeNames.DATA_POINT", EventType.EventTypeNames.DATA_POINT);
    ctx.setAttribute("constants.EventType.EventTypeNames.DATA_SOURCE", EventType.EventTypeNames.DATA_SOURCE);
    ctx.setAttribute("constants.EventType.EventTypeNames.SYSTEM", EventType.EventTypeNames.SYSTEM);
    ctx.setAttribute("constants.EventType.EventTypeNames.PUBLISHER", EventType.EventTypeNames.PUBLISHER);
    ctx.setAttribute("constants.EventType.EventTypeNames.AUDIT", EventType.EventTypeNames.AUDIT);

    ctx.setAttribute("constants.SystemEventType.TYPE_SYSTEM_STARTUP", SystemEventType.TYPE_SYSTEM_STARTUP);
    ctx.setAttribute("constants.SystemEventType.TYPE_SYSTEM_SHUTDOWN", SystemEventType.TYPE_SYSTEM_SHUTDOWN);
    ctx.setAttribute("constants.SystemEventType.TYPE_MAX_ALARM_LEVEL_CHANGED",
            SystemEventType.TYPE_MAX_ALARM_LEVEL_CHANGED);
    ctx.setAttribute("constants.SystemEventType.TYPE_USER_LOGIN", SystemEventType.TYPE_USER_LOGIN);
    ctx.setAttribute("constants.SystemEventType.TYPE_SET_POINT_HANDLER_FAILURE",
            SystemEventType.TYPE_SET_POINT_HANDLER_FAILURE);
    ctx.setAttribute("constants.SystemEventType.TYPE_EMAIL_SEND_FAILURE",
            SystemEventType.TYPE_EMAIL_SEND_FAILURE);
    ctx.setAttribute("constants.SystemEventType.TYPE_PROCESS_FAILURE", SystemEventType.TYPE_PROCESS_FAILURE);
    ctx.setAttribute("constants.SystemEventType.TYPE_LICENSE_CHECK", SystemEventType.TYPE_LICENSE_CHECK);

    ctx.setAttribute("constants.AuditEventType.TYPE_DATA_SOURCE", AuditEventType.TYPE_DATA_SOURCE);
    ctx.setAttribute("constants.AuditEventType.TYPE_DATA_POINT", AuditEventType.TYPE_DATA_POINT);
    ctx.setAttribute("constants.AuditEventType.TYPE_POINT_EVENT_DETECTOR",
            AuditEventType.TYPE_POINT_EVENT_DETECTOR);
    ctx.setAttribute("constants.AuditEventType.TYPE_EVENT_HANDLER", AuditEventType.TYPE_EVENT_HANDLER);

    ctx.setAttribute("constants.UserComment.TYPE_EVENT", UserComment.TYPE_EVENT);
    ctx.setAttribute("constants.UserComment.TYPE_POINT", UserComment.TYPE_POINT);

    String[] codes = { "common.access.read", "common.access.set", "common.alarmLevel.none",
            "common.alarmLevel.info", "common.alarmLevel.urgent", "common.alarmLevel.critical",
            "common.alarmLevel.lifeSafety", "common.disabled", "common.administrator", "common.user",
            "common.disabledToggle", "common.enabledToggle", "common.maximize", "common.minimize",
            "common.loading", "js.help.error", "js.help.related", "js.help.lastUpdated", "common.sendTestEmail",
            "js.email.noRecipients", "js.email.addMailingList", "js.email.addUser", "js.email.addAddress",
            "js.email.noRecipForEmail", "js.email.testSent", "events.silence", "events.unsilence",
            "header.mute", "header.unmute", };
    Map<String, TranslatableMessage> messages = new HashMap<String, TranslatableMessage>();
    for (String code : codes)
        messages.put(code, new TranslatableMessage(code));
    ctx.setAttribute("clientSideMessages", messages);
}