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:org.apache.hadoop.gateway.filter.rewrite.impl.UrlRewriteResponseTest.java

@Test
public void testResolve() throws Exception {

    UrlRewriteProcessor rewriter = EasyMock.createNiceMock(UrlRewriteProcessor.class);

    ServletContext context = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(context.getServletContextName()).andReturn("test-cluster-name").anyTimes();
    EasyMock.expect(context.getInitParameter("test-init-param-name")).andReturn("test-init-param-value")
            .anyTimes();//from w  w  w . j a v  a 2s. c om
    EasyMock.expect(context.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME))
            .andReturn(rewriter).anyTimes();

    FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(config.getInitParameter("test-filter-init-param-name"))
            .andReturn("test-filter-init-param-value").anyTimes();
    EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();

    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);

    EasyMock.replay(rewriter, context, config, request, response);

    UrlRewriteResponse rewriteResponse = new UrlRewriteResponse(config, request, response);

    List<String> names = rewriteResponse.resolve("test-filter-init-param-name");
    assertThat(names.size(), is(1));
    assertThat(names.get(0), is("test-filter-init-param-value"));
}

From source file:com.xhsoft.framework.common.listener.ApplicationContextListener.java

public void contextInitialized(ServletContextEvent event) {
    try {//from  w  w w  .j a v  a 2s . com
        ApplicationContext applicationContext = WebApplicationContextUtils
                .getWebApplicationContext(event.getServletContext());
        ServletContext sc = event.getServletContext();
        String appName = sc.getServletContextName();
        logger.info("[" + appName + "] init context ...");
        AppServiceHelper.setApplicationContext(applicationContext);
        logger.info(" - Put Spring Context to AppServiceHelper");
        String facility = sc.getInitParameter("facility");
        AppContext.addParam(AppContext.FACILITY, facility);
        logger.info(" - Put FACILITY[" + facility + "] to AppContext");
        logger.info("[" + appName + "] init context[done]");
    } catch (Exception e) {
        logger.error("error detail:", e);
    }
}

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);
    }//from  w  w  w. j  a va 2  s .  c  om

    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.pentaho.platform.web.http.context.SolutionContextListener.java

private void setObjectFactory(final ServletContext context) {

    final String SYSTEM_FOLDER = "/system"; //$NON-NLS-1$
    String pentahoObjectFactoryClassName = context.getInitParameter("pentahoObjectFactory"); //$NON-NLS-1$
    String pentahoObjectFactoryConfigFile = context.getInitParameter("pentahoObjectFactoryCfgFile"); //$NON-NLS-1$

    // if web.xml doesnt specify a config file, use the default path.
    if (StringUtils.isEmpty(pentahoObjectFactoryConfigFile)) {
        pentahoObjectFactoryConfigFile = solutionPath + SYSTEM_FOLDER + "/" + DEFAULT_SPRING_CONFIG_FILE_NAME; //$NON-NLS-1$
    } else if (-1 == pentahoObjectFactoryConfigFile.indexOf("/")) { //$NON-NLS-1$
        pentahoObjectFactoryConfigFile = solutionPath + SYSTEM_FOLDER + "/" + pentahoObjectFactoryConfigFile; //$NON-NLS-1$
    }/*from w  w w.  j  a  va  2  s  .  c o m*/
    // else objectFactoryCreatorCfgFile contains the full path.
    IPentahoObjectFactory pentahoObjectFactory;
    try {
        Class<?> classObject = Class.forName(pentahoObjectFactoryClassName);
        pentahoObjectFactory = (IPentahoObjectFactory) classObject.newInstance();
    } catch (Exception e) {
        String msg = Messages.getInstance().getErrorString(
                "SolutionContextListener.ERROR_0002_BAD_OBJECT_FACTORY", pentahoObjectFactoryClassName); //$NON-NLS-1$
        // Cannot proceed without an object factory, so we'll put some context around what
        // we were trying to do throw a runtime exception
        throw new RuntimeException(msg, e);
    }
    pentahoObjectFactory.init(pentahoObjectFactoryConfigFile, context);
    PentahoSystem.registerPrimaryObjectFactory(pentahoObjectFactory);
}

From source file:org.wso2.carbon.discovery.cxf.listeners.TomcatCxfDiscoveryListener.java

private InputStream getConfigLocation(ServletContext context) {

    String configLocation = context.getInitParameter("config-location");
    if (configLocation == null) {
        try {//from w w  w. j a  va 2s .  c  o  m
            InputStream is = context.getResourceAsStream("/WEB-INF/cxf-servlet.xml");
            if (is != null && is.available() > 0) {
                is.close();
                configLocation = "/WEB-INF/cxf-servlet.xml";
            }
        } catch (Exception ex) {
            //ignore
        }
    }

    return context.getResourceAsStream(configLocation);
}

From source file:com.github.persapiens.jsfboot.mojarra.MojarraServletContextConfigurerIT.java

public void testConfigure() {
    ServletContext servletContext = new MockServletContext();

    MojarraServletContextConfigurer mojarraServletContextConfigurer = MojarraServletContextConfigurer.builder()
            .mojarraProperties(this.mojarraProperties).servletContext(servletContext).build();

    mojarraServletContextConfigurer.configure();

    assertThat(servletContext.getInitParameter(MojarraServletContextConfigurer.PREFFIX + ".clientStateTimeout"))
            .isEqualTo("10");
}

From source file:org.apereo.portal.jmx.JavaManagementServerListener.java

/**
 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
 *//*from  ww  w . j a v a  2 s . c o m*/
public void contextInitialized(ServletContextEvent event) {
    final ServletContext servletContext = event.getServletContext();

    //Create the bean
    this.javaManagementServerBean = new JavaManagementServerBean();

    //Get the failOnException option
    final String failOnExceptionStr = servletContext.getInitParameter(FAIL_ON_EXCEPTION);
    boolean failOnException = Boolean.parseBoolean(failOnExceptionStr);
    this.javaManagementServerBean.setFailOnException(failOnException);

    final String host = servletContext.getInitParameter(JMX_RMI_HOST);
    this.javaManagementServerBean.setHost(host);

    //Get the base rmi port from the init parameters
    final String portOneStr = servletContext.getInitParameter(JMX_RMI_PORT_1);
    try {
        final int portOne = Integer.parseInt(portOneStr);
        this.javaManagementServerBean.setPortOne(portOne);
    } catch (NumberFormatException nfe) {
        getLogger().warn("init-parameter '" + JMX_RMI_PORT_1 + "' is required and must contain a number. '"
                + portOneStr + "' is not a valid number.", nfe);
    }

    //Get the second rmi port from the init parameters
    final String portTwoStr = servletContext.getInitParameter(JMX_RMI_PORT_2);
    try {
        final int portTwo = Integer.parseInt(portTwoStr);
        this.javaManagementServerBean.setPortTwo(portTwo);
    } catch (NumberFormatException nfe) {
        getLogger().debug("Failed to convert init-parameter '" + JMX_RMI_PORT_2 + "' with value '" + portTwoStr
                + "' to a number, defaulting portTwo to portOne + 1", nfe);
    }

    this.javaManagementServerBean.startServer();
}

From source file:org.joinfaces.mojarra.MojarraServletContextConfigurerIT.java

@Test
public void testConfigure() {
    ServletContext servletContext = new MockServletContext();

    MojarraServletContextConfigurer mojarraServletContextConfigurer = MojarraServletContextConfigurer.builder()
            .mojarraProperties(this.mojarraProperties).servletContext(servletContext).build();

    mojarraServletContextConfigurer.configure();

    assertThat(servletContext.getInitParameter(MojarraServletContextConfigurer.PREFFIX + ".clientStateTimeout"))
            .isEqualTo("10");
}

From source file:com.github.persapiens.jsfboot.myfaces.MyfacesServletContextConfigurerIT.java

public void testConfigure() {
    ServletContext servletContext = new MockServletContext();

    MyfacesServletContextConfigurer myfacesServletContextConfigurer = MyfacesServletContextConfigurer.builder()
            .myfacesProperties(this.myfacesProperties).servletContext(servletContext).build();

    myfacesServletContextConfigurer.configure();

    assertThat(servletContext
            .getInitParameter(MyfacesServletContextConfigurer.PREFFIX + "STRICT_JSF_2_CC_EL_RESOLVER"))
                    .isEqualTo("myElResolver");
}

From source file:org.joinfaces.myfaces.MyfacesServletContextConfigurerIT.java

@Test
public void testConfigure() {
    ServletContext servletContext = new MockServletContext();

    MyfacesServletContextConfigurer myfacesServletContextConfigurer = MyfacesServletContextConfigurer.builder()
            .myfacesProperties(this.myfacesProperties).servletContext(servletContext).build();

    myfacesServletContextConfigurer.configure();

    assertThat(servletContext
            .getInitParameter(MyfacesServletContextConfigurer.PREFFIX + "STRICT_JSF_2_CC_EL_RESOLVER"))
                    .isEqualTo("myElResolver");
}