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:MyServlet.java

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ServletContext context = getServletContext();
    dbName = context.getInitParameter("name");
    dbPassword = context.getInitParameter("password");

}

From source file:com.onlinebusiness.dataaccess.DataFacadeFactory.java

public CRUDFacade getDataFacadeInstance(ServletContext ctx) {
    String contextParamValue = ctx.getInitParameter("projectStage");
    if (contextParamValue.equals("dev")) {
        return crudFacade;
    } else {//from  w ww.  ja  va2 s  . co  m
        return null;
    }

}

From source file:org.geoserver.platform.GeoServerResourceLoader.java

/**
 * Determines the location of the geoserver data directory based on the following lookup
 * mechanism:/*from w w w .  j a va 2s. c o m*/
 *  
 * 1) Java environment variable
 * 2) Servlet context variable
 * 3) System variable 
 *
 * For each of these, the methods checks that
 * 1) The path exists
 * 2) Is a directory
 * 3) Is writable
 * 
 * @param servContext The servlet context.
 * @return String The absolute path to the data directory, or <code>null</code> if it could not
 * be found. 
 */
public static String lookupGeoServerDataDirectory(ServletContext servContext) {

    final String[] typeStrs = { "Java environment variable ", "Servlet context parameter ",
            "System environment variable " };

    String requireFileVar = "GEOSERVER_REQUIRE_FILE";
    requireFile(System.getProperty(requireFileVar), typeStrs[0] + requireFileVar);
    requireFile(servContext.getInitParameter(requireFileVar), typeStrs[1] + requireFileVar);
    requireFile(System.getenv(requireFileVar), typeStrs[2] + requireFileVar);

    final String[] varStrs = { "GEOSERVER_DATA_DIR", "GEOSERVER_DATA_ROOT" };

    String dataDirStr = null;
    String msgPrefix = null;

    // Loop over variable names
    for (int i = 0; i < varStrs.length && dataDirStr == null; i++) {

        // Loop over variable access methods
        for (int j = 0; j < typeStrs.length && dataDirStr == null; j++) {
            String value = null;
            String varStr = varStrs[i];
            String typeStr = typeStrs[j];

            // Lookup section
            switch (j) {
            case 0:
                value = System.getProperty(varStr);
                break;
            case 1:
                value = servContext.getInitParameter(varStr);
                break;
            case 2:
                value = System.getenv(varStr);
                break;
            }

            if (value == null || value.equalsIgnoreCase("")) {
                LOGGER.finer("Found " + typeStr + varStr + " to be unset");
                continue;
            }

            // Verify section
            File fh = new File(value);

            // Being a bit pessimistic here
            msgPrefix = "Found " + typeStr + varStr + " set to " + value;

            if (!fh.exists()) {
                LOGGER.warning(msgPrefix + " , but this path does not exist");
                continue;
            }
            if (!fh.isDirectory()) {
                LOGGER.warning(msgPrefix + " , which is not a directory");
                continue;
            }
            if (!fh.canWrite()) {
                LOGGER.warning(msgPrefix + " , which is not writeable");
                continue;
            }

            // Sweet, we can work with this
            dataDirStr = value;
        }
    }

    // fall back to embedded data dir
    if (dataDirStr == null) {
        dataDirStr = servContext.getRealPath("/data");
        LOGGER.info("Falling back to embedded data directory: " + dataDirStr);
    }

    return dataDirStr;
}

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

/**
 * Set a system property to the web application root directory.
 * The key of the system property can be defined with the "webAppRootKey"
 * context-param in {@code web.xml}. Default is "webapp.root".
 * <p>Can be used for tools that support substition with {@code System.getProperty}
 * values, like log4j's "${key}" syntax within log file locations.
 * @param servletContext the servlet context of the web application
 * @throws IllegalStateException if the system property is already set,
 * or if the WAR file is not expanded//from   w w w. j  a v a  2 s . c  om
 * @see #WEB_APP_ROOT_KEY_PARAM
 * @see #DEFAULT_WEB_APP_ROOT_KEY
 * @see WebAppRootListener
 * @see Log4jWebConfigurer
 */
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
    Assert.notNull(servletContext, "ServletContext must not be null");
    String root = servletContext.getRealPath("/");
    if (root == null) {
        throw new IllegalStateException(
                "Cannot set web app root system property when WAR file is not expanded");
    }
    String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
    String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
    String oldValue = System.getProperty(key);
    if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) {
        throw new IllegalStateException("Web app root system property already set to different value: '" + key
                + "' = [" + oldValue + "] instead of [" + root + "] - "
                + "Choose unique values for the 'webAppRootKey' context-param in your web.xml files!");
    }
    System.setProperty(key, root);
    servletContext.log("Set web app root system property: '" + key + "' = [" + root + "]");
}

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

/**
 * Get names and values for all the init parameters from the specified 
 * context./*from  ww  w . j  ava 2 s  .  c  om*/
 * 
 * @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.glassfish.jersey.server.spring.SpringWebApplicationInitializer.java

@Override
public void onStartup(ServletContext sc) throws ServletException {
    if (sc.getInitParameter(PAR_NAME_CTX_CONFIG_LOCATION) == null) {
        LOGGER.config(LocalizationMessages.REGISTERING_CTX_LOADER_LISTENER());
        sc.setInitParameter(PAR_NAME_CTX_CONFIG_LOCATION, "classpath:applicationContext.xml");
        sc.addListener("org.springframework.web.context.ContextLoaderListener");
        sc.addListener("org.springframework.web.context.request.RequestContextListener");
    } else {//from ww  w.  ja  v a2s  .  c  o m
        LOGGER.config(LocalizationMessages.SKIPPING_CTX_LODAER_LISTENER_REGISTRATION());
    }
}

From source file:org.lightadmin.logging.configurer.LoggingConfigurerSettings.java

public LoggingConfigurerSettings(ServletContext servletContext) {
    this.applicationBaseUrl = servletContext.getInitParameter(LIGHT_CONFIGURER_BASE_URL);
    this.demoMode = Boolean.valueOf(servletContext.getInitParameter(LIGHT_CONFIGURER_DEMO_MODE));
    setBackToSiteUrl(servletContext.getInitParameter(LIGHT_CONFIGURER_BACK_TO_SITE_URL));
}

From source file:it.geosolutions.geoserver.jms.configuration.ApplicationProperties.java

/**
 * Looks up for a named string property into the following contexts (in order):
 * <ul>//from ww  w . j av a 2 s .  co m
 * <li>System Property</li>
 * <li>web.xml init parameters</li>
 * <li>Environment variable</li>
 * </ul>
 * and returns the first non null, non empty value found.
 * @param propertyName The property name to be searched
 * @param context The servlet context used to look into web.xml (may be null)
 * @return The property value, or null if not found
 */
public static String getProperty(String propertyName, ServletContext context) {
    // TODO: this code comes from the data directory lookup and it's useful as 
    // long as we don't provide a way for the user to manually inspect the three contexts
    // (when trying to debug why the variable they thing they've set, and so on, see also
    // http://jira.codehaus.org/browse/GEOS-2343
    // Once that is fixed, we can remove the logging code that makes this method more complex
    // than strictly necessary

    final String[] typeStrs = { "Java environment variable ", "Servlet context parameter ",
            "System environment variable " };

    String result = null;
    for (int j = 0; j < typeStrs.length; j++) {
        // Lookup section
        switch (j) {
        case 0:
            result = System.getProperty(propertyName);
            break;
        case 1:
            if (context != null) {
                result = context.getInitParameter(propertyName);
            }
            break;
        case 2:
            result = System.getenv(propertyName);
            break;
        }

        if (result == null || result.equalsIgnoreCase("")) {
            LOGGER.finer("Found " + typeStrs[j] + ": '" + propertyName + "' to be unset");
        } else {
            break;
        }
    }

    return result;
}

From source file:jease.cms.web.servlet.JeaseServletListener.java

protected void initLocale(ServletContext context) {
    String localeCode = context.getInitParameter(Names.JEASE_DEFAULT_LOCALE);
    if (StringUtils.isNotBlank(localeCode)) {
        Locale locale = new Locale(localeCode);
        I18N.load(locale);//  www  .  j av  a 2s . c  om
        context.setAttribute("org.zkoss.web.preferred.locale", locale);
    }
}

From source file:jease.cms.web.servlet.JeaseServletListener.java

protected void initDatabase(ServletContext context) {
    String databaseName = context.getInitParameter(Names.JEASE_DATABASE_NAME);
    if (databaseName != null) {
        Database.open(databaseName);/*from w  ww  . j  a v a 2 s. com*/
    } else {
        throw new RuntimeException();
    }
}