Example usage for javax.servlet ServletContext getInitParameterNames

List of usage examples for javax.servlet ServletContext getInitParameterNames

Introduction

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

Prototype

public Enumeration<String> getInitParameterNames();

Source Link

Document

Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.

Usage

From source file:org.getobjects.servlets.WOServletAdaptor.java

@Override
public void init(final ServletConfig _cfg) throws ServletException {
    // Jetty: org.mortbay.jetty.servlet.ServletHolder$Config@114024
    super.init(_cfg);

    String an = this.valueFromServletConfig(_cfg, "WOAppName");
    String ac = this.valueFromServletConfig(_cfg, "WOAppClass");
    if (ac == null)
        ac = an;//from  ww  w  .ja v a2s. c o m
    if (an == null && ac != null) {
        /* if only the class is set, we use the shortname of the class */
        int dotidx = ac.lastIndexOf('.');
        an = dotidx < 1 ? ac : ac.substring(dotidx + 1);
    }

    if (an == null) {
        log.warn("no WOAppName specified in servlet context: " + _cfg);
        an = WOApplication.class.getName();
    }

    /* Construct properties for the volatile "domain" from servlet init
     * parameters and context init parameters and attributes.
     * It's probably best to have a real UserDefaults concept, but for the
     * time being this is better than nothing.
     */
    final Properties properties = new Properties();
    Enumeration parameterNamesEnum = _cfg.getInitParameterNames();
    while (parameterNamesEnum.hasMoreElements()) {
        final String name = (String) parameterNamesEnum.nextElement();
        final String value = _cfg.getInitParameter(name);
        if (name != null && value != null)
            properties.put(name, value);
        else if (value == null)
            log.error("Got no value for init parameter: " + name);
    }

    /* The ServletContext may override the previous init parameters.
     * ServletContext init parameters will be overridden by attributes.
     */
    final ServletContext sctx = _cfg.getServletContext();
    if (sctx != null) {
        parameterNamesEnum = sctx.getInitParameterNames();
        while (parameterNamesEnum.hasMoreElements()) {
            final String name = (String) parameterNamesEnum.nextElement();
            properties.put(name, sctx.getInitParameter(name));
        }
        final Enumeration attributeNamesEnum = sctx.getAttributeNames();
        while (attributeNamesEnum.hasMoreElements()) {
            final String name = (String) attributeNamesEnum.nextElement();
            properties.put(name, sctx.getAttribute(name));
        }
    }

    this.initApplicationWithName(an, ac, properties);
}

From source file:org.kuali.coeus.sys.framework.service.KcServiceLocatorListener.java

/**
 * Translates context parameters from the web.xml into entries in a Properties file.
 *///from  www . ja va2 s .  co m
protected Properties getContextParameters(ServletContext context) {
    Properties properties = new Properties();
    for (String paramName : CollectionUtils.toIterable(context.getInitParameterNames())) {
        properties.put(paramName, context.getInitParameter(paramName));
    }
    return properties;
}

From source file:org.kuali.ext.mm.web.listener.StandaloneInitializeListener.java

/**
 * Translates context parameters from the web.xml into entries in a Properties file.
 *///  ww w.  j a  v a 2s . co  m
protected Properties getContextParameters(ServletContext context) {
    Properties properties = new Properties();
    Enumeration paramNames = context.getInitParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        properties.put(paramName, context.getInitParameter(paramName));
    }
    return properties;
}

From source file:org.kuali.rice.core.web.listener.KualiInitializeListener.java

/**
 * Translates context parameters from the web.xml into entries in a Properties file.
 *///from   ww  w. j a  va 2 s.com
protected Properties getContextParameters(ServletContext context) {
    Properties properties = new Properties();
    @SuppressWarnings("unchecked")
    Enumeration<String> paramNames = context.getInitParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        properties.put(paramName, context.getInitParameter(paramName));
    }
    return properties;
}

From source file:org.kuali.rice.core.web.util.PropertySources.java

/**
 * Convert {@code ServletContext} init parameters into a {@code Properties} object.
 *//*from  w ww  . j  a  va  2 s  .co m*/
public static Properties convert(ServletContext context) {
    Properties properties = new Properties();
    @SuppressWarnings("unchecked")
    Enumeration<String> paramNames = context.getInitParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        properties.put(paramName, context.getInitParameter(paramName));
    }
    return properties;
}

From source file:org.opensubsystems.core.util.servlet.WebUtils.java

/**
 * Create debug string containing all parameter names and their values from
 * the context.//from w  w w  .j  a v  a2s . co m
 *
 * @param  scContext - context to print out
 * @return String - debug string containing all parameter names and their
 *                  values from the context
 */
public static String debug(ServletContext scContext) {
    Enumeration enumNames;
    String strParam;
    StringBuilder sbfReturn = new StringBuilder();

    sbfReturn.append("ServletContext=[ServletContextName=");
    sbfReturn.append(scContext.getServletContextName());
    sbfReturn.append(";");
    for (enumNames = scContext.getInitParameterNames(); enumNames.hasMoreElements();) {
        strParam = (String) enumNames.nextElement();
        sbfReturn.append("Param=");
        sbfReturn.append(strParam);
        sbfReturn.append(" value=");
        sbfReturn.append(scContext.getInitParameter(strParam));
        if (enumNames.hasMoreElements()) {
            sbfReturn.append(";");
        }
    }
    sbfReturn.append("]");

    return sbfReturn.toString();
}

From source file:org.opensubsystems.core.util.servlet.WebUtils.java

/**
 * Get names and values for all the init parameters from the specified 
 * context.//from  w  w w.  j a  v  a 2s .  com
 * 
 * @param  scContext - context from where to retrieve the init parameters
 * @return Properties - names and values of the init parameters or empty
 *                      properties if no init parameters are specified
 */
public static Properties getInitParameters(ServletContext scContext) {
    Properties prpSettings = new Properties();
    String strName;
    String strValue;

    for (Enumeration paramNames = scContext.getInitParameterNames(); paramNames.hasMoreElements();) {
        strName = (String) paramNames.nextElement();
        strValue = scContext.getInitParameter(strName);
        prpSettings.put(strName, strValue);
    }

    return prpSettings;
}

From source file:org.pentaho.platform.web.http.context.SolutionContextListener.java

public void contextInitialized(final ServletContextEvent event) {

    ServletContext context = event.getServletContext();

    String encoding = context.getInitParameter("encoding"); //$NON-NLS-1$
    if (encoding != null) {
        LocaleHelper.setSystemEncoding(encoding);
    }// w  ww. jav a2 s .  com

    String textDirection = context.getInitParameter("text-direction"); //$NON-NLS-1$
    if (textDirection != null) {
        LocaleHelper.setTextDirection(textDirection);
    }

    String localeLanguage = context.getInitParameter("locale-language"); //$NON-NLS-1$
    String localeCountry = context.getInitParameter("locale-country"); //$NON-NLS-1$
    boolean localeSet = false;
    if ((localeLanguage != null) && !"".equals(localeLanguage) && (localeCountry != null)
            && !"".equals(localeCountry)) { //$NON-NLS-1$ //$NON-NLS-2$
        Locale[] locales = Locale.getAvailableLocales();
        if (locales != null) {
            for (Locale element : locales) {
                if (element.getLanguage().equals(localeLanguage)
                        && element.getCountry().equals(localeCountry)) {
                    LocaleHelper.setLocale(element);
                    localeSet = true;
                    break;
                }
            }
        }
    }
    if (!localeSet) {
        // do this thread in the default locale
        LocaleHelper.setLocale(Locale.getDefault());
    }
    LocaleHelper.setDefaultLocale(LocaleHelper.getLocale());
    // log everything that goes on here
    Logger.info(SolutionContextListener.class.getName(),
            Messages.getInstance().getString("SolutionContextListener.INFO_INITIALIZING")); //$NON-NLS-1$
    Logger.info(SolutionContextListener.class.getName(),
            Messages.getInstance().getString("SolutionContextListener.INFO_SERVLET_CONTEXT") + context); //$NON-NLS-1$
    SolutionContextListener.contextPath = context.getRealPath(""); //$NON-NLS-1$
    Logger.info(SolutionContextListener.class.getName(),
            Messages.getInstance().getString("SolutionContextListener.INFO_CONTEXT_PATH") //$NON-NLS-1$
                    + SolutionContextListener.contextPath);

    SolutionContextListener.solutionPath = PentahoHttpSessionHelper.getSolutionPath(context);
    if (StringUtils.isEmpty(SolutionContextListener.solutionPath)) {
        String errorMsg = Messages.getInstance()
                .getErrorString("SolutionContextListener.ERROR_0001_NO_ROOT_PATH"); //$NON-NLS-1$
        Logger.error(getClass().getName(), errorMsg);
        /*
         * Since we couldn't find solution repository path there is no point in going forward and the user should know
         * that a major config setting was not found. So we are throwing in a RunTimeException with the requisite message.
         */
        throw new RuntimeException(errorMsg);
    }

    Logger.info(getClass().getName(), Messages.getInstance().getString("SolutionContextListener.INFO_ROOT_PATH")
            + SolutionContextListener.solutionPath); //$NON-NLS-1$

    String fullyQualifiedServerUrl = context.getInitParameter("fully-qualified-server-url"); //$NON-NLS-1$
    if (fullyQualifiedServerUrl == null) {
        // assume this is a demo installation
        // TODO: Create a servlet that's loaded on startup to set this value
        fullyQualifiedServerUrl = "http://localhost:8080/pentaho/"; //$NON-NLS-1$
    }

    IApplicationContext applicationContext = new WebApplicationContext(SolutionContextListener.solutionPath,
            fullyQualifiedServerUrl, context.getRealPath(""), context); //$NON-NLS-1$

    /*
     * Copy out all the initParameter values from the servlet context and put them in the application context.
     */
    Properties props = new Properties();
    Enumeration<?> initParmNames = context.getInitParameterNames();
    String initParmName;
    while (initParmNames.hasMoreElements()) {
        initParmName = (String) initParmNames.nextElement();
        props.setProperty(initParmName, context.getInitParameter(initParmName));
    }
    ((WebApplicationContext) applicationContext).setProperties(props);

    setSystemCfgFile(context);
    setObjectFactory(context);

    boolean initOk = PentahoSystem.init(applicationContext);

    this.showInitializationMessage(initOk, fullyQualifiedServerUrl);
}

From source file:org.red5.net.websocket.WebSocketPlugin.java

/**
 * Returns a new instance of WsServerContainer if one does not already exist.
 * //from www  .j  a v a 2s . 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.wings.session.Session.java

protected void initProps(ServletContext servletContext) {
    Enumeration params = servletContext.getInitParameterNames();
    while (params.hasMoreElements()) {
        String name = (String) params.nextElement();
        props.put(name, servletContext.getInitParameter(name));
    }/*from   ww  w  . ja  v a  2  s  .  co  m*/
}