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:edu.cornell.mannlib.vitro.webapp.dao.ModelAccess.java

public static ModelAccess on(ServletContext ctx) {
    Object o = ctx.getAttribute(ATTRIBUTE_NAME);
    if (o instanceof ModelAccess) {
        return (ModelAccess) o;
    } else {//  w  w  w  .j av a2  s.com
        ModelAccess ma = new ModelAccess(Scope.CONTEXT, null);
        ctx.setAttribute(ATTRIBUTE_NAME, ma);
        return ma;
    }
}

From source file:org.rhq.enterprise.gui.authentication.AuthenticateUserAction.java

/**
 * This returns <code>true</code> if the RHQ Server has been configured to allow for authentication via LDAP. <code>
 * false</code> if only database authentication is available.
 *
 * @param  context//from ww w  .  j a v a 2s .co m
 *
 * @return <code>true</code> if LDAP authentication is allowed
 *
 * @throws Exception
 */
public static boolean usingLDAPAuthentication(ServletContext context) throws Exception {
    String provider = (String) context.getAttribute(Constants.JAAS_PROVIDER_CTX_ATTR);

    if (provider == null) {
        SystemSettings systemSettings = LookupUtil.getSystemManager()
                .getSystemSettings(LookupUtil.getSubjectManager().getOverlord());
        provider = systemSettings.get(SystemSetting.LDAP_BASED_JAAS_PROVIDER);
        context.setAttribute(Constants.JAAS_PROVIDER_CTX_ATTR, provider);
    }

    return (provider != null) ? Boolean.valueOf(provider) : false;
}

From source file:org.apache.tapestry.asset.AssetExternalizer.java

/**
 *  Gets the externalizer singleton for the application.  If it does not already
 *  exist, it is created and stored into the {@link ServletContext}.
 *
 *  <p>Each Tapestry application within a single {@link ServletContext}
 *  will have its own externalizer; they are differentiated by the
 *  application name./*from  w ww.  ja  v a  2 s.c o  m*/
 *
 *  @see org.apache.tapestry.spec.ApplicationSpecification#getName()
 *
 **/

public static AssetExternalizer get(IRequestCycle cycle) {
    HttpServlet servlet = cycle.getRequestContext().getServlet();
    ServletContext context = servlet.getServletContext();

    String servletName = servlet.getServletName();

    String attributeName = "org.apache.tapestry.AssetExternalizer:" + servletName;

    AssetExternalizer result = (AssetExternalizer) context.getAttribute(attributeName);

    if (result == null) {
        result = new AssetExternalizer(cycle);
        context.setAttribute(attributeName, result);
    }

    return result;
}

From source file:es.tid.cep.esperanza.Utils.java

public static synchronized EPServiceProvider initEPService(ServletContext sc) {
    EPServiceProvider epService = (EPServiceProvider) sc.getAttribute(EPSERV_ATTR_NAME);
    if (epService == null) {
        epService = EPServiceProviderManager.getDefaultProvider();
        Map<String, Object> def = new HashMap<String, Object>();
        def.put("id", String.class);
        def.put("type", String.class);
        ConfigurationOperations cfg = epService.getEPAdministrator().getConfiguration();
        cfg.addEventType("iotEvent", def);
        sc.setAttribute(EPSERV_ATTR_NAME, epService);
    }/*from   w w  w  . ja  va  2  s . c  o m*/
    return epService;
}

From source file:com.kurento.kmf.spring.KurentoApplicationContextUtils.java

public static void registerKurentoServletContextListener(ServletContext ctx) {
    // Add listener for closing Kurento ApplicationContexts on container
    // shutdown/*from   w  w  w. j a  v  a2s  .  c o  m*/

    if (ctx.getAttribute(KURENTO_SERVLET_CONTEXT_LISTENER_ATTRIBUTE_NAME) != null) {
        log.info("Kurento ServletContextListener already registered, we don't register it again ...");
        return;
    }
    log.info("Registering Kurento ServletContextListener ...");
    ctx.setAttribute(KURENTO_SERVLET_CONTEXT_LISTENER_ATTRIBUTE_NAME, "initialized");
    ctx.addListener(KurentoServletContextListener.class);
}

From source file:org.apache.juddi.v3.client.config.WebHelper.java

/**
 * Checks the servlet context for the manager defined in the web context. Optionally, in your 
 * web.xml you can specify either the manager name if you want to use an existing manager 
 * called 'uddi-portlet-manager':// ww  w.j  av  a2s .c  o m
 * <pre>
 * &lt;context-param&gt;
 *   &lt;param-name&gt;uddi.client.manager.name&lt;/param-name&gt;
 *   &lt;param-value&gt;uddi-portlet-manager&lt;/param-value&gt;
 * &lt;/context-param&gt;
 * </pre>
 * or, if you don't want to use the default META-INF/uddi.xml file path, but 'META-INF/my-uddi.xml' instead,
 * then you can set:
 * <pre>
 * &lt;context-param&gt;
 *   &lt;param-name&gt;uddi.client.config.path&lt;/param-name&gt;
 *   &lt;param-value&gt;META-INF/my-uddi.xml&lt;/param-value&gt;
 * &lt;/context-param&gt;
 * </pre>
 * @param servletContext
 * @return a UDDI Client instance
 * @throws ConfigurationException
 */
public static UDDIClient getUDDIClient(ServletContext servletContext) throws ConfigurationException {
    if (servletContext.getAttribute(JUDDI_CLIENT_NAME) != null) {
        String clientName = String.valueOf(servletContext.getAttribute(JUDDI_CLIENT_NAME));
        return UDDIClientContainer.getUDDIClient(clientName);
    } else {
        String clientName = servletContext.getInitParameter(UDDI_CLIENT_NAME);
        if (clientName != null) {
            try {
                UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
                logger.info("Client " + clientName + " was already started.");
                servletContext.setAttribute(JUDDI_CLIENT_NAME, clientName);
                return client;
            } catch (ConfigurationException ce) {
                logger.debug("Client " + clientName + " is not yet started.");
            }
        }
        String clientConfigFile = servletContext.getInitParameter(UDDI_CLIENT_CONFIG_FILE);
        if (clientConfigFile == null)
            clientConfigFile = ClientConfig.DEFAULT_UDDI_CONFIG;

        logger.info("Reading the clientName from the clientConfig file " + clientConfigFile);
        UDDIClient client = new UDDIClient(clientConfigFile);
        if (clientConfigFile == null && client.getName() == null) {
            logger.warn(
                    "Deprecated, client name set to 'default', however it should be provided in the uddi.xml");
            clientName = "default";
        }
        if (client.getName() != null) {
            logger.info("Starting Client " + client.getName() + "...");
        } else {
            throw new ConfigurationException("A client name needs to be specified in the client config file.");
        }

        client.start();
        servletContext.setAttribute(JUDDI_CLIENT_NAME, clientName);
        return client;
    }
}

From source file:org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientWebAppRegistrationUtil.java

public static void addClientCredentialsToWebContext(OAuthAppDetails oAuthAppDetails,
        ServletContext servletContext) {
    if (oAuthAppDetails != null) {
        //Check for client credentials
        if ((oAuthAppDetails.getClientKey() != null && !oAuthAppDetails.getClientKey().isEmpty())
                && (oAuthAppDetails.getClientSecret() != null
                        && !oAuthAppDetails.getClientSecret().isEmpty())) {
            servletContext.setAttribute(DynamicClientWebAppRegistrationConstants.OAUTH_CLIENT_KEY,
                    oAuthAppDetails.getClientKey());
            servletContext.setAttribute(DynamicClientWebAppRegistrationConstants.OAUTH_CLIENT_SECRET,
                    oAuthAppDetails.getClientSecret());
        } else {//  w ww . ja  va 2 s  .c o m
            log.warn("Client credentials not found for web app : " + oAuthAppDetails.getWebAppName());
        }
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.edit.Authenticate.java

/**
 * The servlet context should contain a map from User URIs to
 * {@link HttpSession}s. Get a reference to it, creating it if necessary.
 *//*ww w.  j ava  2  s.  co m*/
@SuppressWarnings("unchecked")
public static Map<String, HttpSession> getUserURISessionMapFromContext(ServletContext ctx) {
    Map<String, HttpSession> m = (Map<String, HttpSession>) ctx.getAttribute(USER_SESSION_MAP_ATTR);
    if (m == null) {
        m = new HashMap<String, HttpSession>();
        ctx.setAttribute(USER_SESSION_MAP_ATTR, m);
    }
    return m;
}

From source file:edu.cornell.mannlib.vitro.webapp.auth.permissions.PermissionRegistry.java

/**
 * Create the registry and store it in the context.
 *///  w w  w .  j  a  va2 s  .  c o m
public static void createRegistry(ServletContext ctx, Collection<? extends Permission> permissions) {
    if (ctx == null) {
        throw new NullPointerException("ctx may not be null.");
    }
    if (permissions == null) {
        throw new NullPointerException("permissions may not be null.");
    }
    if (ctx.getAttribute(ATTRIBUTE_NAME) != null) {
        throw new IllegalStateException("PermissionRegistry has already been set.");
    }

    PermissionRegistry registry = new PermissionRegistry();
    registry.addPermissions(permissions);
    ctx.setAttribute(ATTRIBUTE_NAME, registry);
}

From source file:org.intermine.bio.webservice.GFFQueryService.java

/**
 * Read the SO term name to class name mapping file and return it as a Map from class name to
 * SO term name.  The Map is cached as the SO_CLASS_NAMES attribute in the servlet context.
 *
 * @throws ServletException if the SO class names properties file cannot be found
 * @param servletContext the ServletContext
 * @return a map/*from  w w  w  .ja  v a  2s . co  m*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map<String, String> getSoClassNames(ServletContext servletContext) throws ServletException {
    final String soClassNames = "SO_CLASS_NAMES";
    Properties soNameProperties;
    if (servletContext.getAttribute(soClassNames) == null) {
        soNameProperties = new Properties();
        try {
            InputStream is = servletContext.getResourceAsStream("/WEB-INF/soClassName.properties");
            soNameProperties.load(is);
        } catch (Exception e) {
            throw new ServletException("Error loading so class name mapping file", e);
        }

        servletContext.setAttribute(soClassNames, soNameProperties);
    } else {
        soNameProperties = (Properties) servletContext.getAttribute(soClassNames);
    }

    return new HashMap<String, String>((Map) soNameProperties);
}