Search in sources :

Example 1 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class LocalDBHealthChecker method doHealthCheck.

public List<HealthRecord> doHealthCheck(final PwmApplication pwmApplication) {
    if (pwmApplication == null) {
        return null;
    }
    final List<HealthRecord> healthRecords = new ArrayList<>();
    final LocalDB localDB = pwmApplication.getLocalDB();
    if (localDB == null) {
        final String detailedError = pwmApplication.getLastLocalDBFailure() == null ? "unknown, check logs" : pwmApplication.getLastLocalDBFailure().toDebugStr();
        healthRecords.add(HealthRecord.forMessage(HealthMessage.LocalDB_BAD, detailedError));
        return healthRecords;
    }
    if (LocalDB.Status.NEW == localDB.status()) {
        healthRecords.add(HealthRecord.forMessage(HealthMessage.LocalDB_NEW));
        return healthRecords;
    }
    if (LocalDB.Status.CLOSED == localDB.status()) {
        healthRecords.add(HealthRecord.forMessage(HealthMessage.LocalDB_CLOSED));
        return healthRecords;
    }
    if (healthRecords.isEmpty()) {
        healthRecords.add(HealthRecord.forMessage(HealthMessage.LocalDB_OK));
    }
    return healthRecords;
}
Also used : ArrayList(java.util.ArrayList) LocalDB(password.pwm.util.localdb.LocalDB)

Example 2 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class PwmLogManager method initializeLocalDBLogger.

public static LocalDBLogger initializeLocalDBLogger(final PwmApplication pwmApplication) {
    final LocalDB localDB = pwmApplication.getLocalDB();
    if (pwmApplication.getApplicationMode() == PwmApplicationMode.READ_ONLY) {
        LOGGER.trace("skipping initialization of LocalDBLogger due to read-only mode");
        return null;
    }
    // initialize the localDBLogger
    final LocalDBLogger localDBLogger;
    final PwmLogLevel localDBLogLevel = pwmApplication.getConfig().getEventLogLocalDBLevel();
    try {
        localDBLogger = initLocalDBLogger(localDB, pwmApplication);
        if (localDBLogger != null) {
            PwmLogger.setLocalDBLogger(localDBLogLevel, localDBLogger);
        }
    } catch (Exception e) {
        LOGGER.warn("unable to initialize localDBLogger: " + e.getMessage());
        return null;
    }
    // add appender for other packages;
    try {
        final LocalDBLog4jAppender localDBLog4jAppender = new LocalDBLog4jAppender(localDBLogger);
        localDBLog4jAppender.setThreshold(localDBLogLevel.getLog4jLevel());
        for (final Package logPackage : LOGGING_PACKAGES) {
            if (logPackage != null && !logPackage.equals(PwmApplication.class.getPackage())) {
                final Logger logger = Logger.getLogger(logPackage.getName());
                logger.addAppender(localDBLog4jAppender);
                logger.setLevel(Level.TRACE);
            }
        }
    } catch (Exception e) {
        LOGGER.warn("unable to initialize localDBLogger/extraAppender: " + e.getMessage());
    }
    return localDBLogger;
}
Also used : PwmApplication(password.pwm.PwmApplication) Logger(org.apache.log4j.Logger) LocalDB(password.pwm.util.localdb.LocalDB) LocalDBException(password.pwm.util.localdb.LocalDBException) IOException(java.io.IOException)

Example 3 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class ExportLocalDBCommand method doCommand.

@Override
void doCommand() throws Exception {
    final LocalDB localDB = cliEnvironment.getLocalDB();
    final File outputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_NEW_OUTPUT_FILE.getName());
    if (outputFile.exists()) {
        out("outputFile for exportLocalDB cannot already exist");
        return;
    }
    final LocalDBUtility localDBUtility = new LocalDBUtility(localDB);
    try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
        localDBUtility.exportLocalDB(fileOutputStream, System.out, true);
    } catch (PwmOperationalException e) {
        out("error during export: " + e.getMessage());
    }
}
Also used : LocalDBUtility(password.pwm.util.localdb.LocalDBUtility) FileOutputStream(java.io.FileOutputStream) LocalDB(password.pwm.util.localdb.LocalDB) File(java.io.File) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 4 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class MainClass method createEnv.

private static CliEnvironment createEnv(final CliParameters parameters, final List<String> args) throws Exception {
    final Map<String, Object> options = parseCommandOptions(parameters, args);
    final File applicationPath = figureApplicationPath(mainOptions);
    out("applicationPath=" + applicationPath.getAbsolutePath());
    PwmEnvironment.verifyApplicationPath(applicationPath);
    final File configurationFile = locateConfigurationFile(applicationPath);
    final ConfigurationReader configReader = loadConfiguration(configurationFile);
    final Configuration config = configReader.getConfiguration();
    final PwmApplication pwmApplication;
    final LocalDB localDB;
    if (parameters.needsPwmApplication) {
        pwmApplication = loadPwmApplication(applicationPath, mainOptions.getApplicationFlags(), config, configurationFile, parameters.readOnly);
        localDB = pwmApplication.getLocalDB();
    } else if (parameters.needsLocalDB) {
        pwmApplication = null;
        localDB = loadPwmDB(config, parameters.readOnly, applicationPath);
    } else {
        pwmApplication = null;
        localDB = null;
    }
    out("environment initialized");
    out("");
    final Writer outputStream = new OutputStreamWriter(System.out, PwmConstants.DEFAULT_CHARSET);
    return CliEnvironment.builder().configurationReader(configReader).configurationFile(configurationFile).config(config).applicationPath(applicationPath).pwmApplication(pwmApplication).localDB(localDB).debugWriter(outputStream).options(options).mainOptions(mainOptions).build();
}
Also used : PwmApplication(password.pwm.PwmApplication) Configuration(password.pwm.config.Configuration) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) LocalDB(password.pwm.util.localdb.LocalDB) ConfigurationReader(password.pwm.config.stored.ConfigurationReader) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 5 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class ExportLogsCommand method doCommand.

@Override
void doCommand() throws Exception {
    final LocalDB localDB = this.cliEnvironment.getLocalDB();
    final LocalDBStoredQueue logQueue = LocalDBStoredQueue.createLocalDBStoredQueue(null, localDB, LocalDB.DB.EVENTLOG_EVENTS);
    if (logQueue.isEmpty()) {
        out("no logs present");
        return;
    }
    final File outputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_NEW_OUTPUT_FILE.getName());
    out("outputting " + logQueue.size() + " log events to " + outputFile.getAbsolutePath() + "....");
    try (Writer outputWriter = new OutputStreamWriter(new FileOutputStream(outputFile), PwmConstants.DEFAULT_CHARSET)) {
        for (final Iterator<String> iter = logQueue.descendingIterator(); iter.hasNext(); ) {
            final String loopString = iter.next();
            final PwmLogEvent logEvent = PwmLogEvent.fromEncodedString(loopString);
            if (logEvent != null) {
                outputWriter.write(logEvent.toLogString());
                outputWriter.write("\n");
            }
        }
    }
    out("output complete");
}
Also used : LocalDBStoredQueue(password.pwm.util.localdb.LocalDBStoredQueue) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) LocalDB(password.pwm.util.localdb.LocalDB) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) PwmLogEvent(password.pwm.util.logging.PwmLogEvent)

Aggregations

LocalDB (password.pwm.util.localdb.LocalDB)10 File (java.io.File)5 PwmApplication (password.pwm.PwmApplication)3 LocalDBUtility (password.pwm.util.localdb.LocalDBUtility)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 Configuration (password.pwm.config.Configuration)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 LocalDBException (password.pwm.util.localdb.LocalDBException)2 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)1 InputStream (java.io.InputStream)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Logger (org.apache.log4j.Logger)1 ConfigurationReader (password.pwm.config.stored.ConfigurationReader)1