SolutionContextListener implements ServletContextListener { public void contextInitialized( final ServletContextEvent event ) { context = event.getServletContext(); String encoding = getServerParameter( "encoding" ); if ( encoding != null ) { LocaleHelper.setSystemEncoding( encoding ); } String textDirection = getServerParameter( "text-direction" ); if ( textDirection != null ) { LocaleHelper.setTextDirection( textDirection ); } String localeLanguage = getServerParameter( "locale-language" ); String localeCountry = getServerParameter( "locale-country" ); boolean localeSet = false; if ( !StringUtils.isEmpty( localeLanguage ) && !StringUtils.isEmpty( localeCountry ) ) { 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 ) { LocaleHelper.setLocale( Locale.getDefault() ); } LocaleHelper.setDefaultLocale( LocaleHelper.getLocale() ); logger.info( Messages.getInstance().getString( "SolutionContextListener.INFO_INITIALIZING" ) ); logger.info( Messages.getInstance().getString( "SolutionContextListener.INFO_SERVLET_CONTEXT", context ) ); SolutionContextListener.contextPath = context.getRealPath( "" ); logger .info( Messages.getInstance().getString( "SolutionContextListener.INFO_CONTEXT_PATH", SolutionContextListener.contextPath ) ); SolutionContextListener.solutionPath = PentahoHttpSessionHelper.getSolutionPath( context ); if ( StringUtils.isEmpty( SolutionContextListener.solutionPath ) ) { String errorMsg = Messages.getInstance().getErrorString( "SolutionContextListener.ERROR_0001_NO_ROOT_PATH" ); logger.error( errorMsg ); throw new RuntimeException( errorMsg ); } logger.info( Messages.getInstance().getString( "SolutionContextListener.INFO_ROOT_PATH", SolutionContextListener.solutionPath ) ); String fullyQualifiedServerUrl = getServerParameter( "fully-qualified-server-url" ); if ( fullyQualifiedServerUrl == null ) { fullyQualifiedServerUrl = "http: } IApplicationContext applicationContext = new WebApplicationContext( SolutionContextListener.solutionPath, fullyQualifiedServerUrl, context .getRealPath( "" ), context ); Properties props = new Properties(); ISystemConfig systemConfig = PentahoSystem.get( ISystemConfig.class ); if ( systemConfig != null ) { IConfiguration config = systemConfig.getConfiguration( "server" ); if ( config != null ) { try { props.putAll( config.getProperties() ); } catch ( IOException e ) { logger.error( "Could not find/read the server.properties file." ); } } } Enumeration<?> initParmNames = context.getInitParameterNames(); String initParmName; while ( initParmNames.hasMoreElements() ) { initParmName = (String) initParmNames.nextElement(); props.setProperty( initParmName, getServerParameter( initParmName, true ) ); } ( (WebApplicationContext) applicationContext ).setProperties( props ); setSystemCfgFile( context ); setObjectFactory( context ); serverStatusProvider.setStatus( IServerStatusProvider.ServerStatus.STARTING ); serverStatusProvider.setStatusMessages( new String[] { "Caution, the system is initializing. Do not shut down or restart the system at this time." } ); PeriodicStatusLogger.start(); boolean initOk = false; try { initOk = PentahoSystem.init( applicationContext ); } finally { updateStatusMessages( initOk ); PeriodicStatusLogger.stop(); } this.showInitializationMessage( initOk, fullyQualifiedServerUrl ); }  void contextInitialized( final ServletContextEvent event ); void showInitializationMessage( final boolean initOk, final String fullyQualifiedServerUrl ); void contextDestroyed( final ServletContextEvent event );  }
@Test( expected = RuntimeException.class ) public void testContextInitialized_invalidSolutionPath() throws Exception { when( systemConfig.getProperty( anyString() ) ).thenReturn( null ); File tempFile = File.createTempFile( "SolutionContextListenerTest", ".tmp" ); tempFile.deleteOnExit(); when( context.getRealPath( "" ) ).thenReturn( tempFile.getParent() ); solutionContextListener.contextInitialized( contextEvent ); }
@Test public void testContextInitialized() throws Exception { when( systemConfig.getProperty( "server.encoding" ) ).thenReturn( "UTF-8" ); when( systemConfig.getProperty( "server.text-direction" ) ).thenReturn( "leftToRight" ); when( systemConfig.getProperty( "server.locale-language" ) ).thenReturn( "en" ); when( systemConfig.getProperty( "server.locale-country" ) ).thenReturn( "CA" ); when( systemConfig.getProperty( "server.param 1" ) ).thenReturn( "value 1" ); when( systemConfig.getProperty( "server.pentahoObjectFactory" ) ).thenReturn( objectFactory.getClass().getName() ); Path temp = Files.createTempDirectory( "temp" ); File penSolution = new File( temp.toFile(), "pentaho-solutions/SolutionContextListenerTest.tmp" ); penSolution.mkdirs(); penSolution.deleteOnExit(); when( context.getRealPath( "" ) ).thenReturn( temp.toString() ); temp.toFile().deleteOnExit(); List<String> paramNames = new ArrayList<>(); paramNames.add( "param 1" ); when( context.getInitParameterNames() ).thenReturn( Collections.enumeration( paramNames ) ); solutionContextListener.contextInitialized( contextEvent ); assertEquals( "en_CA", LocaleHelper.getLocale().toString() ); }