Example usage for javax.servlet ServletContext getInitParameter

List of usage examples for javax.servlet ServletContext getInitParameter

Introduction

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

Prototype

public String getInitParameter(String name);

Source Link

Document

Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

Usage

From source file:com.betfair.tornjak.monitor.overlay.AuthUtils.java

private static synchronized RolePerms getOrCreateRolePerms(ServletContext servletContext) throws IOException {
    RolePerms rolePerms;/*  www.j  a va  2s.  c o  m*/

    rolePerms = (RolePerms) servletContext.getAttribute(CONTEXT_ATTR_NAME);

    if (rolePerms != null) {
        return rolePerms;
    }

    ApplicationContext context = (ApplicationContext) servletContext
            .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (context == null) {
        throw new ApplicationContextException("Unable to find application context");
    }
    String authFileLocation = servletContext.getInitParameter(CONTEXT_INIT_PARAM_NAME);
    if (StringUtils.isBlank(authFileLocation)) {
        throw new ApplicationContextException(String
                .format("Parameter '%s' not defined, unable to load jmx auth file", CONTEXT_INIT_PARAM_NAME));
    }

    rolePerms = AuthFileReader.load(context.getResource(authFileLocation));
    servletContext.setAttribute(CONTEXT_ATTR_NAME, rolePerms);

    return rolePerms;

}

From source file:com.meltmedia.cadmium.core.util.LogUtils.java

/**
 * <p>Reconfigures the logging context.</p>
 * <p>The LoggerContext gets configured with "/WEB-INF/context-logback.xml". There is a <code>logDir</code> property set here which is expected to be the directory that the log file is written to.</p>
 * <p>The <code>logDir</code> property gets set on the LoggerContext with the following logic.</p>
 * <ul>//from  w w w  .ja  va  2  s .c o m
 *   <li>{@link LOG_DIR_INIT_PARAM} context parameter will be created and checked if it is writable.</li>
 *   <li>The File object passed in is used as a fall-back if it can be created and written to.</li>
 * </ul>    
 * @see {@link LoggerContext}
 * @param servletContext The current servlet context.
 * @param logDirFallback The fall-back directory to log to.
 * @param vHostName
 * @param log
 * @throws FileNotFoundException Thrown if no logDir can be written to.
 * @throws MalformedURLException 
 * @throws IOException
 */
public static void configureLogback(ServletContext servletContext, File logDirFallback, String vHostName,
        Logger log) throws FileNotFoundException, MalformedURLException, IOException {
    log.debug("Reconfiguring Logback!");
    String systemLogDir = System.getProperty(LOG_DIRECTORY_OVERRIDE, System.getProperty(JBOSS_LOG_DIR));
    if (systemLogDir != null) {
        systemLogDir += "/" + vHostName;
    }
    File logDir = FileSystemManager.getWritableDirectoryWithFailovers(systemLogDir,
            servletContext.getInitParameter(LOG_DIR_INIT_PARAM), logDirFallback.getAbsolutePath());
    if (logDir != null) {
        log.debug("Resetting logback context.");
        URL configFile = servletContext.getResource("/WEB-INF/context-logback.xml");
        log.debug("Configuring logback with file, {}", configFile);
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        try {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(context);
            context.stop();
            context.reset();
            context.putProperty("logDir", logDir.getCanonicalPath());
            configurator.doConfigure(configFile);
        } catch (JoranException je) {
            // StatusPrinter will handle this
        } finally {
            context.start();
            log.debug("Done resetting logback.");
        }
        StatusPrinter.printInCaseOfErrorsOrWarnings(context);
    }
}

From source file:org.apache.myfaces.webapp.StartupServletContextListener.java

public static void initFaces(ServletContext servletContext) {
    try {//  w w  w  .  ja v  a 2  s.c  o m
        Boolean b = (Boolean) servletContext.getAttribute(FACES_INIT_DONE);

        if (b == null || b.booleanValue() == false) {
            log.trace("Initializing MyFaces");

            //Load the configuration
            ExternalContext externalContext = new ServletExternalContextImpl(servletContext, null, null);

            //And configure everything
            new FacesConfigurator(externalContext).configure();

            if ("true".equals(servletContext.getInitParameter(FacesConfigValidator.VALIDATE_CONTEXT_PARAM))
                    || "true".equals(servletContext
                            .getInitParameter(FacesConfigValidator.VALIDATE_CONTEXT_PARAM.toLowerCase()))) {
                List list = FacesConfigValidator.validate(externalContext, servletContext.getRealPath("/"));

                Iterator iterator = list.iterator();

                while (iterator.hasNext())
                    log.warn(iterator.next());

            }

            // parse web.xml
            WebXml.init(externalContext);

            servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE);
        } else {
            log.info("MyFaces already initialized");
        }
    } catch (Exception ex) {
        log.error("Error initializing ServletContext", ex);
        ex.printStackTrace();
    }
    log.info("ServletContext '" + servletContext.getRealPath("/") + "' initialized.");

    if (servletContext.getInitParameter(StateUtils.INIT_SECRET) != null
            || servletContext.getInitParameter(StateUtils.INIT_SECRET.toLowerCase()) != null)
        StateUtils.initSecret(servletContext);
}

From source file:nl.b3p.viewer.components.ComponentRegistryInitializer.java

private static void tryLoadCrossContextComponents() {
    assert (crossContextName != null);

    log.info("Looking at cross context \"" + crossContextName + "\" for components...");
    ServletContext crossContext = servletContext.getContext(crossContextName);
    if (crossContext == null) {
        log.error("Cannot get cross context for name \"" + crossContextName
                + "\", is it deployed? Can't load components from it!");
        retryLoading = true;/*from   ww w .  j av  a2s  .  c om*/
    } else {
        log.debug("Context found, checking context parameters for paths...");

        /* load path init params from cross context or use default */
        String crossContextComponentPath = crossContext.getInitParameter(PARAM_PATH);
        if (crossContextComponentPath == null) {
            crossContextComponentPath = crossContext.getInitParameter(PARAM_PATH_OLD);
        }
        if (crossContextComponentPath == null) {
            crossContextComponentPath = DEFAULT_COMPONENT_PATH;
        }
        log.info("Cross context component path: \"" + crossContextComponentPath + "\"");
        for (String p : crossContextComponentPath.split(",")) {
            try {
                log.info(String.format("Loading component metadata from cross context \"%s\" path \"%s\"",
                        crossContextName, p));

                registry.loadFromPath(crossContext, p);
            } catch (Exception e) {
                log.error(String.format("Error loading components from cross context \"%s\", path \"%s\"",
                        crossContextName, p), e);
            }
        }

        /* only stop retrying if components are actually loaded - sometimes
         * Tomcat erroniously returns the ROOT context!
         */
        if (!registry.getSortedComponentClassNameList().isEmpty()) {
            retryLoading = false;
        }
    }
}

From source file:net.testdriven.psiprobe.tools.ApplicationUtils.java

public static List getApplicationInitParams(Context context) {
    // We'll try to determine if a parameter value comes from a deployment descriptor or a context descriptor.
    // assumption: Context.findParameter() returns only values of parameters that are declared in a deployment descriptor.
    // If a parameter is declared in a context descriptor with override=false and redeclared in a deployment descriptor,
    // Context.findParameter() still returns its value, even though the value is taken from a context descriptor.
    // context.findApplicationParameters() returns all parameters that are declared in a context descriptor regardless
    // of whether they are overridden in a deployment descriptor or not or not.

    // creating a set of parameter names that are declared in a context descriptor
    // and can not be ovevridden in a deployment descriptor.
    Set nonOverridableParams = new HashSet();
    ApplicationParameter[] appParams = context.findApplicationParameters();
    for (int i = 0; i < appParams.length; i++) {
        if (appParams[i] != null && !appParams[i].getOverride()) {
            nonOverridableParams.add(appParams[i].getName());
        }/*w  ww.j  a  v a 2 s .  co m*/
    }

    List initParams = new ArrayList();
    ServletContext servletCtx = context.getServletContext();
    for (Enumeration e = servletCtx.getInitParameterNames(); e.hasMoreElements();) {
        String paramName = (String) e.nextElement();

        ApplicationParam param = new ApplicationParam();
        param.setName(paramName);
        param.setValue(servletCtx.getInitParameter(paramName));
        // if the parameter is declared in a deployment descriptor
        // and it is not declared in a context descriptor with override=false,
        // the value comes from the deployment descriptor
        param.setFromDeplDescr(
                context.findParameter(paramName) != null && !nonOverridableParams.contains(paramName));
        initParams.add(param);
    }

    return initParams;
}

From source file:org.glite.authz.pap.common.PAPConfiguration.java

/**
 * Initializes the configuration for this PAP.
 * /*from w  w  w  .  j  a va  2 s .  c  o m*/
 * @param context, a ServletContext that defines suitable defaults for configuration and repository directories 
 * @return the active instance of the PAPConfiguration class
 */
public static PAPConfiguration initialize(ServletContext context) {

    if (context == null)
        throw new NullArgumentException(
                "Please provide a value for the 'context' argument! null is not a valid value in this context.");

    if (instance == null) {

        String papConfDir = context.getInitParameter("papConfigurationDir");

        return initialize(papConfDir);
    }

    return instance;
}

From source file:org.apache.hadoop.fs.webdav.WebdavServlet.java

private static Configuration getConf(ServletContext application) {
    Configuration conf = (Configuration) application.getAttribute("dfs.servlet.conf.key");

    if (conf == null) {
        conf = hadoopConfig;/*from w w w .  java2s .c om*/
        Enumeration e = application.getInitParameterNames();
        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();
            conf.set(name, application.getInitParameter(name));
        }
        application.setAttribute("dfs.servlet.conf.key", conf);
    }

    if (currentUserName != null) {
        WebAppContext webapp = (WebAppContext) application.getAttribute(WebdavServer.WEB_APP_CONTEXT);
        WebdavHashUserRealm userRealm = (WebdavHashUserRealm) webapp.getSecurityHandler().getUserRealm();
        List<String> userRoles = userRealm.getUserRoles(currentUserName);
        currentUserRoles = userRoles;

        UnixUserGroupInformation ugi = new UnixUserGroupInformation(currentUserName,
                userRoles.toArray(new String[0]));
        UnixUserGroupInformation.saveToConf(conf, UnixUserGroupInformation.UGI_PROPERTY_NAME, ugi);
    }
    return conf;
}

From source file:org.jruby.rack.mock.WebUtils.java

/**
 * Return whether default HTML escaping is enabled for the web application,
 * i.e. the value of the "defaultHtmlEscape" context-param in {@code web.xml}
 * (if any).//from w  w w  .  j  a v a 2 s  . c om
 * <p>This method differentiates between no param specified at all and
 * an actual boolean value specified, allowing to have a context-specific
 * default in case of no setting at the global level.
 * @param servletContext the servlet context of the web application
 * @return whether default HTML escaping is enabled (null = no explicit default)
 */
public static Boolean getDefaultHtmlEscape(ServletContext servletContext) {
    if (servletContext == null) {
        return null;
    }
    String param = servletContext.getInitParameter(HTML_ESCAPE_CONTEXT_PARAM);
    return (StringUtils.hasText(param) ? Boolean.valueOf(param) : null);
}

From source file:org.jruby.rack.mock.WebUtils.java

/**
 * Remove the system property that points to the web app root directory.
 * To be called on shutdown of the web application.
 * @param servletContext the servlet context of the web application
 * @see #setWebAppRootSystemProperty//  ww w . j  a  va  2s . c  o m
 */
public static void removeWebAppRootSystemProperty(ServletContext servletContext) {
    Assert.notNull(servletContext, "ServletContext must not be null");
    String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
    String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
    System.getProperties().remove(key);
}

From source file:org.jruby.rack.mock.WebUtils.java

/**
 * Return whether default HTML escaping is enabled for the web application,
 * i.e. the value of the "defaultHtmlEscape" context-param in {@code web.xml}
 * (if any). Falls back to {@code false} in case of no explicit default given.
 * @param servletContext the servlet context of the web application
 * @return whether default HTML escaping is enabled (default is false)
 *///from w w  w  . j a  v a2s  .  co m
public static boolean isDefaultHtmlEscape(ServletContext servletContext) {
    if (servletContext == null) {
        return false;
    }
    String param = servletContext.getInitParameter(HTML_ESCAPE_CONTEXT_PARAM);
    return Boolean.valueOf(param);
}