Example usage for javax.servlet ServletContext getAttributeNames

List of usage examples for javax.servlet ServletContext getAttributeNames

Introduction

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

Prototype

public Enumeration<String> getAttributeNames();

Source Link

Document

Returns an Enumeration containing the attribute names available within this ServletContext.

Usage

From source file:com.bank.manager.configs.ContextCleanupListener.java

/**
 * Find all ServletContext attributes which implement {@link DisposableBean}
 * and destroy them, removing all affected ServletContext attributes eventually.
 * @param sc the ServletContext to check
 *//*from ww w .j a  v  a2s . com*/
static void cleanupAttributes(ServletContext sc) {
    Enumeration<String> attrNames = sc.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attrName = attrNames.nextElement();
        if (attrName.startsWith("org.springframework.")) {
            Object attrValue = sc.getAttribute(attrName);
            if (attrValue instanceof DisposableBean) {
                try {
                    ((DisposableBean) attrValue).destroy();
                } catch (Throwable ex) {
                    logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'",
                            ex);
                }
            }
        }
    }
}

From source file:com.apdplat.platform.spring.APDPlatContextLoaderListener.java

/**
 * Find all ServletContext attributes which implement {@link DisposableBean}
 * and destroy them, removing all affected ServletContext attributes eventually.
 * @param sc the ServletContext to check
 *//*from w w w  . ja  v  a2 s  .co m*/
static void cleanupAttributes(ServletContext sc) {
    Enumeration attrNames = sc.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attrName = (String) attrNames.nextElement();
        if (attrName.startsWith("org.springframework.")) {
            Object attrValue = sc.getAttribute(attrName);
            if (attrValue instanceof DisposableBean) {
                try {
                    ((DisposableBean) attrValue).destroy();
                } catch (Throwable ex) {
                    logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'",
                            ex);
                }
            }
        }
    }
}

From source file:gov.nih.nci.cabig.ctms.web.WebTools.java

@SuppressWarnings({ "unchecked" })
public static SortedMap<String, Object> servletContextAttributesToMap(final ServletContext context) {
    return namedAttributesToMap(context.getAttributeNames(), new AttributeAccessor<Object>() {
        public Object getAttribute(String name) {
            return context.getAttribute(name);
        }//from w  w  w  .  j  a  v a  2s .co m
    });
}

From source file:com.googlecode.psiprobe.tools.ApplicationUtils.java

public static List getApplicationAttributes(Context context) {
    List attrs = new ArrayList();
    ServletContext servletCtx = context.getServletContext();
    for (Enumeration e = servletCtx.getAttributeNames(); e.hasMoreElements();) {
        String attrName = (String) e.nextElement();
        Object attrValue = servletCtx.getAttribute(attrName);

        Attribute attr = new Attribute();
        attr.setName(attrName);/*from   w  ww . j ava  2 s  .  co m*/
        attr.setValue(attrValue);
        attr.setType(ClassUtils.getQualifiedName(attrValue.getClass()));
        attrs.add(attr);
    }
    return attrs;
}

From source file:psiprobe.tools.ApplicationUtils.java

/**
 * Gets the application attributes.// w w  w  .  j a  v  a 2s . c  o  m
 *
 * @param context the context
 * @return the application attributes
 */
public static List<Attribute> getApplicationAttributes(Context context) {
    List<Attribute> attrs = new ArrayList<>();
    ServletContext servletCtx = context.getServletContext();
    for (String attrName : Collections.list(servletCtx.getAttributeNames())) {
        Object attrValue = servletCtx.getAttribute(attrName);

        Attribute attr = new Attribute();
        attr.setName(attrName);
        attr.setValue(attrValue);
        attr.setType(ClassUtils.getQualifiedName(attrValue.getClass()));
        attrs.add(attr);
    }
    return attrs;
}

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);
    }//from w  w  w  . j  av a2s . co  m
    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.red5.net.websocket.WebSocketPlugin.java

/**
 * Returns a new instance of WsServerContainer if one does not already exist.
 * /* w  w  w . j a v  a  2  s  . c  o  m*/
 * @param servletContext
 * @return WsServerContainer
 */
public static ServerContainer getWsServerContainerInstance(ServletContext servletContext) {
    String path = servletContext.getContextPath();
    // handle root
    if (path.length() == 0) {
        path = "/";
    }
    log.info("getWsServerContainerInstance: {}", path);
    DefaultWsServerContainer container;
    if (containerMap.containsKey(path)) {
        container = containerMap.get(path);
    } else {
        // instance a server container for WS
        container = new DefaultWsServerContainer(servletContext);
        if (log.isDebugEnabled()) {
            log.debug("Attributes: {} params: {}", Collections.list(servletContext.getAttributeNames()),
                    Collections.list(servletContext.getInitParameterNames()));
        }
        // get a configurator instance
        ServerEndpointConfig.Configurator configurator = (ServerEndpointConfig.Configurator) WebSocketPlugin
                .getWsConfiguratorInstance();
        // check for sub protocols
        log.debug("Checking for subprotocols");
        List<String> subProtocols = new ArrayList<>();
        Optional<Object> subProtocolsAttr = Optional
                .ofNullable(servletContext.getInitParameter("subProtocols"));
        if (subProtocolsAttr.isPresent()) {
            String attr = (String) subProtocolsAttr.get();
            log.debug("Subprotocols: {}", attr);
            if (StringUtils.isNotBlank(attr)) {
                if (attr.contains(",")) {
                    // split them up
                    Stream.of(attr.split(",")).forEach(entry -> {
                        subProtocols.add(entry);
                    });
                } else {
                    subProtocols.add(attr);
                }
            }
        } else {
            // default to allowing any subprotocol
            subProtocols.add("*");
        }
        log.debug("Checking for CORS");
        // check for allowed origins override in this servlet context
        Optional<Object> crossOpt = Optional.ofNullable(servletContext.getAttribute("crossOriginPolicy"));
        if (crossOpt.isPresent() && Boolean.valueOf((String) crossOpt.get())) {
            Optional<String> opt = Optional.ofNullable((String) servletContext.getAttribute("allowedOrigins"));
            if (opt.isPresent()) {
                ((DefaultServerEndpointConfigurator) configurator).setAllowedOrigins(opt.get().split(","));
            }
        }
        log.debug("Checking for endpoint override");
        // check for endpoint override and use default if not configured
        String wsEndpointClass = Optional.ofNullable((String) servletContext.getAttribute("wsEndpointClass"))
                .orElse("org.red5.net.websocket.server.DefaultWebSocketEndpoint");
        try {
            // locate the endpoint class
            Class<?> endpointClass = Class.forName(wsEndpointClass);
            log.debug("startWebSocket - endpointPath: {} endpointClass: {}", path, endpointClass);
            // build an endpoint config
            ServerEndpointConfig serverEndpointConfig = ServerEndpointConfig.Builder.create(endpointClass, path)
                    .configurator(configurator).subprotocols(subProtocols).build();
            // set the endpoint on the server container
            container.addEndpoint(serverEndpointConfig);
        } catch (Throwable t) {
            log.warn("WebSocket endpoint setup exception", t);
        }
        // store container for lookup
        containerMap.put(path, container);
        // add session listener
        servletContext.addListener(new HttpSessionListener() {

            @Override
            public void sessionCreated(HttpSessionEvent se) {
                log.debug("sessionCreated: {}", se.getSession().getId());
                ServletContext sc = se.getSession().getServletContext();
                // Don't trigger WebSocket initialization if a WebSocket Server Container is already present
                if (sc.getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE) == null) {
                    // grab the container using the servlet context for lookup
                    DefaultWsServerContainer serverContainer = (DefaultWsServerContainer) WebSocketPlugin
                            .getWsServerContainerInstance(sc);
                    // set the container to the context for lookup
                    sc.setAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE, serverContainer);
                }
            }

            @Override
            public void sessionDestroyed(HttpSessionEvent se) {
                log.debug("sessionDestroyed: {}", se);
                container.closeAuthenticatedSession(se.getSession().getId());
            }

        });
    }
    // set the container to the context for lookup
    servletContext.setAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE, container);
    return container;
}

From source file:org.psikeds.common.config.ContextLoaderListener.java

/**
 * Servlet is shutting down. Close context and destroy Spring-Beans.
 *//*from   w w  w  .  ja  v  a  2  s.  co m*/
@Override
public void contextDestroyed(final ServletContextEvent event) {
    if (this.contextLoader != null) {
        this.contextLoader.closeWebApplicationContext(event.getServletContext());
    }
    final ServletContext sc = event.getServletContext();
    final Enumeration<?> attrNames = sc.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        final String attrName = (String) attrNames.nextElement();
        if (attrName.startsWith("org.springframework.")) {
            final Object attrValue = sc.getAttribute(attrName);
            if (attrValue instanceof DisposableBean) {
                try {
                    ((DisposableBean) attrValue).destroy();
                } catch (final Exception ex) {
                    LOGGER.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'",
                            ex);
                }
            }
        }
    }
}

From source file:net.sf.jabb.stdr.jsp.SetTag.java

/**
 * Find the Spring bean from WebApplicationContext in servlet context.
 * @param beanName// w  ww  .  j ava2s.c o m
 * @return   the bean or null
 */
private Object findSpringBean(String beanName) {
    ServletContext servletContext = this.pageContext.getServletContext();

    Enumeration<String> attrNames = servletContext.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attrName = attrNames.nextElement();
        if (attrName.startsWith("org.springframework")) {
            try {
                WebApplicationContext springContext = WebApplicationContextUtils
                        .getWebApplicationContext(servletContext, attrName);
                Object bean = springContext.getBean(beanName);
                return bean;
            } catch (Exception e) {
                // continue if the bean cannot be found.
            }
        }
    }
    return null;
}

From source file:com.manydesigns.portofino.actions.admin.servletcontext.ServletContextAction.java

protected void setupForm() {
    ServletContext servletContext = context.getServletContext();
    Enumeration<String> attributeNames = servletContext.getAttributeNames();
    List<KeyValue> attributes = new ArrayList<KeyValue>();
    while (attributeNames.hasMoreElements()) {
        String key = attributeNames.nextElement();
        String value = StringUtils.abbreviate(OgnlUtils.convertValueToString(servletContext.getAttribute(key)),
                300);//from   w  w w . j a  va  2s  .  c o  m
        attributes.add(new KeyValue(key, value));
    }
    TableFormBuilder builder = new TableFormBuilder(KeyValue.class);
    builder.configNRows(attributes.size());
    builder.configMode(Mode.VIEW);
    form = builder.build();
    form.readFromObject(attributes);
}