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:ch.qos.logback.ext.spring.web.WebLogbackConfigurer.java

/**
 * Initialize Logback, including setting the web app root system property.
 *
 * @param servletContext the current ServletContext
 * @see org.springframework.web.util.WebUtils#setWebAppRootSystemProperty
 *///from  ww  w  .jav a  2s  .  c  om
public static void initLogging(ServletContext servletContext) {
    // Expose the web app root system property.
    if (exposeWebAppRoot(servletContext)) {
        WebUtils.setWebAppRootSystemProperty(servletContext);
    }

    // Only perform custom Logback initialization in case of a config file.
    String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
    if (location != null) {
        // Perform actual Logback initialization; else rely on Logback's default initialization.
        try {
            // Resolve system property placeholders before potentially resolving real path.
            location = ServletContextPropertyUtils.resolvePlaceholders(location);
            // Return a URL (e.g. "classpath:" or "file:") as-is;
            // consider a plain file path as relative to the web application root directory.
            if (!ResourceUtils.isUrl(location)) {
                location = WebUtils.getRealPath(servletContext, location);
            }

            // Write log message to server log.
            servletContext.log("Initializing Logback from [" + location + "]");

            // Initialize
            LogbackConfigurer.initLogging(location);
        } catch (FileNotFoundException ex) {
            throw new IllegalArgumentException("Invalid 'logbackConfigLocation' parameter: " + ex.getMessage());
        } catch (JoranException e) {
            throw new RuntimeException("Unexpected error while configuring logback", e);
        }
    }

    //If SLF4J's java.util.logging bridge is available in the classpath, install it. This will direct any messages
    //from the Java Logging framework into SLF4J. When logging is terminated, the bridge will need to be uninstalled
    try {
        Class<?> julBridge = ClassUtils.forName("org.slf4j.bridge.SLF4JBridgeHandler",
                ClassUtils.getDefaultClassLoader());

        Method removeHandlers = ReflectionUtils.findMethod(julBridge, "removeHandlersForRootLogger");
        if (removeHandlers != null) {
            servletContext.log("Removing all previous handlers for JUL to SLF4J bridge");
            ReflectionUtils.invokeMethod(removeHandlers, null);
        }

        Method install = ReflectionUtils.findMethod(julBridge, "install");
        if (install != null) {
            servletContext.log("Installing JUL to SLF4J bridge");
            ReflectionUtils.invokeMethod(install, null);
        }
    } catch (ClassNotFoundException ignored) {
        //Indicates the java.util.logging bridge is not in the classpath. This is not an indication of a problem.
        servletContext.log("JUL to SLF4J bridge is not available on the classpath");
    }
}

From source file:net.ontopia.topicmaps.classify.WebChew.java

/**
 * INTERNAL: Returns the plug-in class instance used by the ontopoly
 * plugin. Used by classify/plugin.jsp./*from   ww  w. j  av a2  s. com*/
 */
public static ClassifyPluginIF getPlugin(HttpServletRequest request) {
    // create plugin by dynamically intantiating plugin class
    HttpSession session = request.getSession(true);
    ServletContext scontext = session.getServletContext();
    String pclass = scontext.getInitParameter("classify_plugin");
    if (pclass == null)
        pclass = "net.ontopia.topicmaps.classify.DefaultPlugin";
    ClassifyPluginIF cp = (ClassifyPluginIF) ObjectUtils.newInstance(pclass);
    if (cp instanceof HttpServletRequestAwareIF)
        ((HttpServletRequestAwareIF) cp).setRequest(request);
    return cp;
}

From source file:org.squale.welcom.taglib.field.util.LayoutUtils.java

/**
 * init/*from w  ww .j a v  a2  s .  c  o  m*/
 * 
 * @param context le servlet context
 */
public static void init(final ServletContext context) {
    if ("noerror".equals(context.getInitParameter("struts-layout-mode"))) {
        noErrorMode = true;
    }

    String l_string = context.getInitParameter("struts-layout-config");

    if (l_string != null) {
        javascript_dir = l_string;
    }

    l_string = context.getInitParameter("struts-layout-image");

    if (l_string != null) {
        image_dir = l_string;
    }

    l_string = context.getInitParameter("struts-layout-skin");

    if (l_string != null) {
        default_skin = l_string;
    }

    if (!default_skin.endsWith(".css")) {
        default_skin += ".css";
    }
}

From source file:org.glite.slcs.config.SLCSServerConfiguration.java

/**
 * Initialize the singleton instance of the SLCSServerConfiguration.<p> 
 * Use the servlet context parameter <code>SLCSServerConfigurationFile</code> to
 * determine the configuration file to load. 
 * <p>Try to configure the log4j engine by loading the config file define in the
 * servlet context parameter <code>Log4JConfigureFile</code>.
 * /*from   w  w  w .  jav a2  s .co  m*/
 * @param ctxt
 *            The ServletContext
 * @throws SLCSConfigurationException
 *             If a configuration error occurs.
 * @see org.glite.slcs.config.Log4JConfiguration#configure(ServletContext)
 */
static public synchronized void initialize(ServletContext ctxt) throws SLCSConfigurationException {
    // first configure Log4J with the external log4j config file
    Log4JConfiguration.configure(ctxt);
    // and the SLCS server
    LOG.debug("initialize SLCSServerConfiguration(ServletContext)...");
    String filename = DEFAULT_CONFIGURATION_FILE;
    if (ctxt.getInitParameter(CONFIGURATION_FILE_KEY) != null) {
        filename = ctxt.getInitParameter(CONFIGURATION_FILE_KEY);
    } else {
        LOG.warn("Parameter " + CONFIGURATION_FILE_KEY
                + " not found in the Servlet context, using default file: " + filename);
    }
    initialize(filename);
}

From source file:org.wings.session.PortletWingServlet.java

public static void installSession(HttpServletRequest req, HttpServletResponse res) {
    ServletContext context = req.getSession().getServletContext();
    String lookupName = context.getInitParameter("wings.servlet.lookupname");

    if (lookupName == null || lookupName.trim().length() == 0) {
        lookupName = "SessionServlet:" + context.getInitParameter("wings.mainclass");
    }//from  www. j  a  v  a  2s . c  om
    PortletSessionServlet sessionServlet = (PortletSessionServlet) req.getSession().getAttribute(lookupName);
    if (sessionServlet != null) {
        Session session = sessionServlet.getSession();
        session.setServletRequest(req);
        session.setServletResponse(res);
        SessionManager.setSession(session);
    }
}

From source file:com.github.gagu.web.util.Log4jWebConfigurer.java

/**
 * Initialize log4j, including setting the web app root system property.
 * @param servletContext the current ServletContext
 * @see WebUtils#setWebAppRootSystemProperty
 *//*from w  ww . java  2s  .co  m*/
public static void initLogging(ServletContext servletContext) {
    // Expose the web app root system property.
    if (exposeWebAppRoot(servletContext)) {
        WebUtils.setWebAppRootSystemProperty(servletContext);
    }

    // Only perform custom log4j initialization in case of a config file.
    // e.g. "/WEB-INF/config/log4j.properties"
    String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
    //TODO load default log4j.xml specified
    if (StringUtils.isEmpty(location)) {
        location = "com/github/gagu/web/util/framework-log4j.xml";
    }

    if (location != null) {
        // Perform actual log4j initialization; else rely on log4j's default initialization.
        try {
            // Resolve property placeholders before potentially resolving a real path.
            location = ServletContextPropertyUtils.resolvePlaceholders(location, servletContext);

            // Leave a URL (e.g. "classpath:" or "file:") as-is.
            if (!ResourceUtils.isUrl(location)) {
                // Consider a plain file path as relative to the web application root directory.
                // e.g. "/Users/hanyosh/git/gagu/gagu-example/target/gagu-example/WEB-INF/config/log4j.properties"
                location = WebUtils.getRealPath(servletContext, location);
            }

            // Write log message to server log.
            servletContext.log("Initializing log4j from [" + location + "]");

            // Check whether refresh interval was specified.
            String intervalString = servletContext.getInitParameter(REFRESH_INTERVAL_PARAM);
            if (StringUtils.hasText(intervalString)) {
                // Initialize with refresh interval, i.e. with log4j's watchdog thread,
                // checking the file in the background.
                try {
                    long refreshInterval = Long.parseLong(intervalString);
                    Log4jConfigurer.initLogging(location, refreshInterval);
                } catch (NumberFormatException ex) {
                    throw new IllegalArgumentException(
                            "Invalid 'log4jRefreshInterval' parameter: " + ex.getMessage());
                }
            } else {
                // Initialize without refresh check, i.e. without log4j's watchdog thread.
                Log4jConfigurer.initLogging(location);
            }
        } catch (FileNotFoundException ex) {
            throw new IllegalArgumentException("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage());
        }
    }
}

From source file:com.facetime.communication.activemq.AmqConsumer.java

protected static synchronized void initConnectionFactory(ServletContext servletContext) {
    if (factory == null) {
        factory = (ConnectionFactory) servletContext.getAttribute(CONNECTION_FACTORY_ATTRIBUTE);
    }/*from   w ww . ja  v  a  2s. c  o  m*/
    if (factory == null) {
        String brokerURL = servletContext.getInitParameter(BROKER_URL_INIT_PARAM);

        BROKER_URL = brokerURL;

        LOG.debug("Value of: " + BROKER_URL_INIT_PARAM + " is: " + brokerURL);

        if (brokerURL == null) {
            throw new IllegalStateException(
                    "missing brokerURL (specified via " + BROKER_URL_INIT_PARAM + " init-Param");
        }

        ActiveMQConnectionFactory amqfactory = new ActiveMQConnectionFactory(brokerURL);

        // Set prefetch policy for factory
        if (servletContext.getInitParameter(CONNECTION_FACTORY_PREFETCH_PARAM) != null) {
            int prefetch = Integer.valueOf(servletContext.getInitParameter(CONNECTION_FACTORY_PREFETCH_PARAM))
                    .intValue();
            amqfactory.getPrefetchPolicy().setAll(prefetch);
        }

        // Set optimize acknowledge setting
        if (servletContext.getInitParameter(CONNECTION_FACTORY_OPTIMIZE_ACK_PARAM) != null) {
            boolean optimizeAck = Boolean
                    .valueOf(servletContext.getInitParameter(CONNECTION_FACTORY_OPTIMIZE_ACK_PARAM))
                    .booleanValue();
            amqfactory.setOptimizeAcknowledge(optimizeAck);
        }

        factory = amqfactory;

        servletContext.setAttribute(CONNECTION_FACTORY_ATTRIBUTE, factory);
    }
}

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

public static RegistrationProfile constructRegistrationProfile(ServletContext servletContext,
        String webAppName) {/*from   ww  w.j  a v a  2  s  . c  om*/
    RegistrationProfile registrationProfile;
    registrationProfile = new RegistrationProfile();
    registrationProfile.setGrantType(
            servletContext.getInitParameter(DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_GRANT_TYPE));
    registrationProfile.setTokenScope(
            servletContext.getInitParameter(DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_TOKEN_SCOPE));
    registrationProfile.setOwner(DynamicClientWebAppRegistrationUtil.getUserName());
    String callbackURL = servletContext
            .getInitParameter(DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_CALLBACK_URL);
    if ((callbackURL != null) && !callbackURL.isEmpty()) {
        registrationProfile.setCallbackUrl(callbackURL);
    } else {
        registrationProfile.setCallbackUrl(DynamicClientWebAppRegistrationUtil.getCallbackUrl(webAppName));
    }
    registrationProfile.setClientName(webAppName);
    registrationProfile.setSaasApp(Boolean.parseBoolean(
            servletContext.getInitParameter(DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_SAAS_APP)));
    return registrationProfile;
}

From source file:net.ontopia.topicmaps.webed.impl.utils.TagUtils.java

public static synchronized NamedLockManager getNamedLockManager(ServletContext servletContext) {
    String identifier = servletContext.getInitParameter("lockmanager");
    if (identifier == null)
        identifier = "";
    return LockManagers.getLockManager(identifier);
}

From source file:org.apache.myfaces.shared_impl.util.StateUtils.java

/**
 * Does nothing if the user has disabled the SecretKey cache. This is
 * useful when dealing with a JCA provider whose SecretKey 
 * implementation is not thread safe.//from  ww w.  j  av a2  s . co  m
 * 
 * Instantiates a SecretKey instance based upon what the user has 
 * specified in the deployment descriptor.  The SecretKey is then 
 * stored in application scope where it can be used for all requests.
 */

public static void initSecret(ServletContext ctx) {

    if (ctx == null)
        throw new NullPointerException("ServletContext ctx");

    String cache = ctx.getInitParameter(INIT_SECRET_KEY_CACHE);

    if (cache == null) {
        cache = ctx.getInitParameter(INIT_SECRET_KEY_CACHE.toLowerCase());
    }

    if ("false".equals(cache))
        return;

    String _secret = ctx.getInitParameter(INIT_SECRET);

    if (_secret == null) {
        _secret = ctx.getInitParameter(INIT_SECRET.toLowerCase());
    }

    String _algorithm = ctx.getInitParameter(INIT_ALGORITHM);

    if (_algorithm == null) {
        _algorithm = ctx.getInitParameter(INIT_ALGORITHM.toLowerCase());
    }

    if (_algorithm == null) {
        if (log.isDebugEnabled()) {
            log.debug("Using default algorithm " + DEFAULT_ALGORITHM);
        }
        _algorithm = DEFAULT_ALGORITHM;
    }

    if (_secret == null)
        throw new NullPointerException("_secret String - '" + INIT_SECRET_KEY_CACHE + "' has been enabled, "
                + "but there is no '" + INIT_SECRET + "'");

    byte[] secret = new Base64().decode(_secret.getBytes());

    // you want to do this as few times as possible
    SecretKey secretKey = new SecretKeySpec(secret, _algorithm);

    if (log.isDebugEnabled())
        log.debug("Storing SecretKey @ " + INIT_SECRET_KEY_CACHE);

    ctx.setAttribute(INIT_SECRET_KEY_CACHE, secretKey);

}