private void initServices() throws PwmUnrecoverableException { for (final Class<? extends PwmService> serviceClass : PWM_SERVICE_CLASSES) { final Date startTime = new Date(); final PwmService newServiceInstance; try { final Object newInstance = serviceClass.newInstance(); newServiceInstance = (PwmService) newInstance; } catch (Exception e) { final String errorMsg = "unexpected error instantiating service class '" + serviceClass.getName() + "', error: " + e.toString(); LOGGER.fatal(errorMsg, e); throw new PwmUnrecoverableException( new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, errorMsg)); } try { LOGGER.debug("initializing service " + serviceClass.getName()); newServiceInstance.init(this); LOGGER.debug( "completed initialization of service " + serviceClass.getName() + " in " + TimeDuration.fromCurrent(startTime).asCompactString() + ", status=" + newServiceInstance.status()); } catch (PwmException e) { LOGGER.warn( "error instantiating service class '" + serviceClass.getName() + "', service will remain unavailable, error: " + e.getMessage()); } catch (Exception e) { String errorMsg = "unexpected error instantiating service class '" + serviceClass.getName() + "', cannot load, error: " + e.getMessage(); if (e.getCause() != null) { errorMsg += ", cause: " + e.getCause(); } LOGGER.fatal(errorMsg); throw new PwmUnrecoverableException( new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, errorMsg)); } pwmServices.put(serviceClass, newServiceInstance); } }
public void shutdown() { LOGGER.warn("shutting down"); { // send system audit event final SystemAuditRecord auditRecord = SystemAuditRecord.create(AuditEvent.SHUTDOWN, null, getInstanceID()); try { getAuditManager().submit(auditRecord); } catch (PwmException e) { LOGGER.warn("unable to submit alert event " + JsonUtil.serialize(auditRecord)); } } { final List<Class> reverseServiceList = new ArrayList<Class>(PWM_SERVICE_CLASSES); Collections.reverse(reverseServiceList); for (final Class serviceClass : reverseServiceList) { if (pwmServices.containsKey(serviceClass)) { LOGGER.trace("closing service " + serviceClass.getName()); final PwmService loopService = pwmServices.get(serviceClass); LOGGER.trace("successfully closed service " + serviceClass.getName()); try { loopService.close(); } catch (Exception e) { LOGGER.error( "error closing " + loopService.getClass().getSimpleName() + ": " + e.getMessage(), e); } } } } if (localDBLogger != null) { try { localDBLogger.close(); } catch (Exception e) { LOGGER.error("error closing localDBLogger: " + e.getMessage(), e); } localDBLogger = null; } if (localDB != null) { try { localDB.close(); } catch (Exception e) { LOGGER.fatal("error closing localDB: " + e, e); } localDB = null; } LOGGER.info( PwmConstants.PWM_APP_NAME + " " + PwmConstants.SERVLET_VERSION + " closed for bidness, cya!"); }
private PwmApplication(final PwmEnvironment pwmEnvironment) throws PwmUnrecoverableException { verifyIfApplicationPathIsSetProperly(pwmEnvironment); this.configuration = pwmEnvironment.config; this.applicationMode = pwmEnvironment.applicationMode; this.applicationPath = pwmEnvironment.applicationPath; this.configurationFile = pwmEnvironment.configurationFile; this.webInfPath = pwmEnvironment.webInfPath; try { initialize(pwmEnvironment.initLogging); } catch (PwmUnrecoverableException e) { LOGGER.fatal(e.getMessage()); throw e; } }
private void initialize(final boolean initLogging) throws PwmUnrecoverableException { final Date startTime = new Date(); // initialize log4j if (initLogging) { final String log4jFileName = configuration.readSettingAsString(PwmSetting.EVENTS_JAVA_LOG4JCONFIG_FILE); final File log4jFile = Helper.figureFilepath(log4jFileName, applicationPath); final String consoleLevel, fileLevel; switch (getApplicationMode()) { case ERROR: case NEW: consoleLevel = PwmLogLevel.TRACE.toString(); fileLevel = PwmLogLevel.TRACE.toString(); break; default: consoleLevel = configuration.readSettingAsString(PwmSetting.EVENTS_JAVA_STDOUT_LEVEL); fileLevel = configuration.readSettingAsString(PwmSetting.EVENTS_FILE_LEVEL); break; } PwmLogManager.initializeLogger( this, configuration, log4jFile, consoleLevel, applicationPath, fileLevel); switch (getApplicationMode()) { case RUNNING: break; case ERROR: LOGGER.fatal( "starting up in ERROR mode! Check log or health check information for cause"); break; default: LOGGER.trace( "setting log level to TRACE because application mode is " + getApplicationMode()); break; } } LOGGER.info( "initializing, application mode=" + getApplicationMode() + ", applicationPath=" + (applicationPath == null ? "null" : applicationPath.getAbsolutePath()) + ", configurationFile=" + (configurationFile == null ? "null" : configurationFile.getAbsolutePath())); this.localDB = Initializer.initializeLocalDB(this); this.localDBLogger = PwmLogManager.initializeLocalDBLogger(this); // log the loaded configuration LOGGER.info("configuration load completed"); // read the pwm servlet instance id instanceID = fetchInstanceID(localDB, this); LOGGER.info("using '" + getInstanceID() + "' for instance's ID (instanceID)"); // read the pwm installation date installTime = fetchInstallDate(startupTime); LOGGER.debug( "this application instance first installed on " + PwmConstants.DEFAULT_DATETIME_FORMAT.format(installTime)); initServices(); final TimeDuration totalTime = TimeDuration.fromCurrent(startTime); LOGGER.info( PwmConstants.PWM_APP_NAME + " " + PwmConstants.SERVLET_VERSION + " open for bidness! (" + totalTime.asCompactString() + ")"); StatisticsManager.incrementStat(this, Statistic.PWM_STARTUPS); LOGGER.debug( "buildTime=" + PwmConstants.BUILD_TIME + ", javaLocale=" + Locale.getDefault() + ", DefaultLocale=" + PwmConstants.DEFAULT_LOCALE); final Thread postInitThread = new Thread() { @Override public void run() { postInitTasks(); } }; postInitThread.setDaemon(true); postInitThread.setName(Helper.makeThreadName(this, PwmApplication.class)); postInitThread.start(); }