Example usage for org.apache.commons.lang3.exception ExceptionUtils getMessage

List of usage examples for org.apache.commons.lang3.exception ExceptionUtils getMessage

Introduction

In this page you can find the example usage for org.apache.commons.lang3.exception ExceptionUtils getMessage.

Prototype

public static String getMessage(final Throwable th) 

Source Link

Document

Gets a short message summarising the exception.

Usage

From source file:nl.opengeogroep.filesetsync.client.FilesetSyncer.java

private void deleteLocalFiles() {
    if (Shutdown.isHappening()) {
        return;/*w  w  w.  j a v  a2s.co  m*/
    }

    if (fs.getRegexp() != null) {
        // XXX only delete local files matching regexp, for now don't delete
        // anything
        return;
    }

    if (fileList.size() == 1 && fileList.get(0).getType() == TYPE_FILE) {
        return;
    }

    for (List<FileRecord> dirList : new FileRecordListDirectoryIterator(fileList)) {
        Iterator<FileRecord> it = dirList.iterator();
        FileRecord dir = it.next();
        File localDir = new File(fs.getLocal() + File.separator + dir.getName());

        if (!localDir.exists()) {
            continue;
        }

        List<String> toDelete = new ArrayList(Arrays.asList(localDir.list()));

        while (it.hasNext()) {
            FileRecord fr = it.next();
            String name = dir.getName().equals(".") ? fr.getName()
                    : fr.getName().substring(dir.getName().length() + 1);
            if (toDelete.indexOf(name) != -1) {
                // Don't delete this file -- may need to be overwritten though

                try {
                    // But if is not the same type, do delete it
                    char localType = new File(localDir.getCanonicalPath() + File.separator + name).isDirectory()
                            ? 'd'
                            : 'f';
                    if (localType == fr.getType()) {
                        toDelete.remove(name);
                    }
                } catch (IOException ex) {
                }
            }
        }

        for (String deleteIt : toDelete) {
            if (Shutdown.isHappening()) {
                return;
            }

            File f = new File(localDir + File.separator + deleteIt);
            try {
                if (f.isDirectory()) {
                    log.info("rmdirs    " + f.getCanonicalPath());
                    FileUtils.deleteDirectory(f);
                } else {
                    log.info("delete    " + f.getCanonicalPath());
                    f.delete();
                }
            } catch (Exception e) {
                log.error("Exception deleting file " + f + ": " + ExceptionUtils.getMessage(e));
            }
        }
    }
}

From source file:nl.opengeogroep.filesetsync.client.FilesetSyncer.java

/**
 * Removes files which are locally up-to-date from the list of files to
 * transfer. Updates lastModified date.//w ww.j  ava 2  s.c  om
 */
private void compareFilesetList() throws IOException {

    MutableLong hashTime = new MutableLong();
    long hashBytes = 0;
    long startTime = System.currentTimeMillis();
    long progressTime = startTime;
    int processed = 0;
    int newerLocalFiles = 0;

    boolean setLastModifiedToServer = "true".equals(fs.getProperty("setLastModifiedToServer"));

    for (int index = 0; index < fileList.size(); index++) {
        FileRecord fr = fileList.get(index);
        if (Shutdown.isHappening()) {
            return;
        }

        File localFile;
        if (fileList.size() == 1 && fr.getType() == TYPE_FILE) {
            localFile = new File(fs.getLocal());
        } else {
            localFile = new File(fs.getLocal() + File.separator + fr.getName());
        }
        if (fr.getType() == TYPE_DIRECTORY && localFile.exists()) {
            if (!localFile.isDirectory()) {
                log.error("Local file in is the way for remote directory: " + localFile.getCanonicalPath());
            }
            if (fr.getLastModified() != localFile.lastModified()) {
                log.trace(String.format("later updating last modified for directory %s",
                        localFile.getCanonicalPath()));
                directoriesLastModifiedTimes.add(Pair.of(localFile, fr.getLastModified()));
            }
            fileList.set(index, null);
            alreadyLocal++;
        }
        if (fr.getType() == TYPE_FILE && localFile.exists()) {
            if (!localFile.isFile()) {
                log.error("Local non-file is in the way for remote file: " + localFile.getCanonicalPath());
            }
            if (fs.isHash()) {
                try {
                    String hash = FileRecord.calculateHash(localFile, hashTime);
                    //localFilesByHash.put(hash, localFile.getCanonicalPath());
                    hashBytes += localFile.length();
                    if (hash.equals(fr.getHash())) {
                        if (log.isTraceEnabled()) {
                            log.trace("Same hash for " + fr.getName());
                        }
                        if (fr.getLastModified() > localFile.lastModified()) {
                            if (log.isTraceEnabled()) {
                                log.trace("Same hash, updating last modified for " + fr.getName());
                            }
                            localFile.setLastModified(fr.getLastModified());
                        }
                        fileList.set(index, null);
                        alreadyLocal++;
                    } else {
                        if (log.isTraceEnabled()) {
                            log.trace("Hash mismatch for " + fr.getName());
                        }
                    }
                } catch (Exception e) {
                    log.error("Error hashing " + localFile.getCanonicalPath() + ": "
                            + ExceptionUtils.getMessage(e));
                }
            } else {
                if (fr.getLastModified() > localFile.lastModified()) {
                    if (log.isTraceEnabled()) {
                        log.trace("Remote file newer: " + fr.getName());
                    }
                } else if (fr.getLastModified() < localFile.lastModified()) {
                    if (setLastModifiedToServer) {
                        localFile.setLastModified(fr.getLastModified());
                    } else {
                        if (log.isTraceEnabled()) {
                            log.trace(String.format(
                                    "Keeping local file last modified at %s, later than remote file at %s: ",
                                    dateToString(new Date(localFile.lastModified())),
                                    dateToString(new Date(fr.getLastModified())), fr.getName()));
                        }
                    }
                    newerLocalFiles++;
                    fileList.set(index, null);
                    alreadyLocal++;
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace("Local file unmodified: " + fr.getName());
                    }
                    fileList.set(index, null);
                    alreadyLocal++;
                }
            }
        }

        processed++;
        long time = System.currentTimeMillis();
        if (time - progressTime > 30000) {
            log.info(String.format("Still comparing files, processed %d files", processed));
            progressTime = time;
        }
    }

    // TODO: if file in file list already in localFilesByHash OR, remove them
    // Also remove duplicate hashes in fileList

    String hashInfo;
    if (fs.isHash()) {
        hashInfo = String.format(", hashed hashed %d KB, hash speed %s", hashBytes / 1024,
                (hashTime.getValue() < 100 ? "n/a"
                        : Math.round(hashBytes / 1024.0 / (hashTime.getValue() / 1000.0)) + " KB/s"));
    } else {
        hashInfo = "";
    }
    log.info(String.format("Compared file list to local files in %s, %d files up-to-date%s",
            DurationFormatUtils.formatDurationWords(System.currentTimeMillis() - startTime, true, false),
            alreadyLocal, hashInfo));
    if (newerLocalFiles != 0) {
        log.warn(String.format(
                "Not overwriting %d local files with newer local last modified date compared to files on server",
                newerLocalFiles));
    }
}

From source file:nl.opengeogroep.filesetsync.client.FilesetSyncer.java

private void transferChunk(List<FileRecord> chunkList) throws IOException {
    boolean verbose = "true".equals(fs.getProperty("verbose"));

    try (CloseableHttpClient httpClient = HttpClientUtil.get()) {
        HttpPost post = new HttpPost(serverUrl + "get/" + fs.getRemote());

        ByteArrayOutputStream b = new ByteArrayOutputStream();
        new BufferedFileListEncoder(new GZIPOutputStream(b)).writeAll(chunkList).close();

        post.setEntity(new ByteArrayEntity(b.toByteArray()));
        post.setHeader(HttpHeaders.CONTENT_TYPE, Protocol.FILELIST_MIME_TYPE);
        post.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
        addExtraHeaders(post);/*from  w  ww  .j a va2s .  c  o  m*/

        log.info("> " + post.getRequestLine());
        try (CloseableHttpResponse response = httpClient.execute(post)) {
            log.info("< " + response.getStatusLine());

            if (Shutdown.isHappening()) {
                return;
            }

            int status = response.getStatusLine().getStatusCode();
            if (status < 200 || status >= 300) {
                throw new IOException(String.format("Server returned \"%s\" for request \"%s\", body: %s",
                        response.getStatusLine(), post.getRequestLine(),
                        EntityUtils.toString(response.getEntity())));
            }

            try (MultiFileDecoder decoder = new MultiFileDecoder(response.getEntity().getContent())) {
                int i = 0;
                for (MultiFileHeader mfh : decoder) {
                    if (Shutdown.isHappening()) {
                        post.abort();
                        return;
                    }

                    log.trace(String.format("File #%3d: %8d bytes, %s, %s", ++i, mfh.getContentLength(),
                            mfh.getContentType(), mfh.getFilename()));
                    File local;
                    if (mfh.getFilename().equals(".")) {
                        if (mfh.isDirectory()) {
                            // skip root directory
                            continue;
                        } else {
                            // single file sync, write to local file
                            local = new File(fs.getLocal());
                        }
                    } else {
                        local = new File(fs.getLocal() + File.separator + mfh.getFilename());
                        // Detect if server tries to overwrite file in parent of local path
                        if (!local.getCanonicalPath().startsWith(localCanonicalPath)) {
                            throw new IOException("Server returned invalid filename: " + mfh.getFilename());
                        }
                    }

                    if (mfh.isDirectory()) {
                        if (local.exists() && local.isDirectory()) {
                            continue;
                        }
                        if (verbose) {
                            log.info("mkdir     " + mfh.getFilename());
                        }
                        local.mkdirs();
                        directoriesLastModifiedTimes.add(Pair.of(local, mfh.getLastModified().getTime()));
                        continue;
                    }

                    if (local.exists()) {
                        if (verbose) {
                            log.info("overwrite " + mfh.getFilename());
                        }
                    } else {
                        if (verbose) {
                            log.info("write     " + mfh.getFilename());
                        }
                        local.getParentFile().mkdirs();
                    }
                    try (FileOutputStream out = new FileOutputStream(local)) {
                        IOUtils.copy(mfh.getBody(), out);
                        totalBytes += mfh.getContentLength();
                        filesUpdated = true;
                    } catch (IOException e) {
                        log.error(String.format("Error writing to local file \"%s\": %s", fs.getLocal(),
                                ExceptionUtils.getMessage(e)));
                        throw e;
                    }
                    local.setLastModified(mfh.getLastModified().getTime());
                }
                if (decoder.getIOException() != null) {
                    throw decoder.getIOException();
                }
            }
        }
    }
}

From source file:nl.opengeogroep.filesetsync.server.FileHashCache.java

@Override
public void contextDestroyed(ServletContextEvent sce) {
    log.info("*** destroying ***");

    for (Map.Entry<String, CacheManager> entry : cacheManagers.entrySet()) {
        String name = entry.getKey();
        CacheManager manager = entry.getValue();
        Cache cache = manager.getCache(name);
        log.info(String.format("Evicting expired elements from cache \"%s\"", name));
        cache.evictExpiredElements();// www .  j  a v  a2s  . c  o m
        log.info(String.format("Persisting cache \"%s\" (size %d)...", name, cache.getSize()));
        long startTime = System.currentTimeMillis();
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new GZIPOutputStream(new FileOutputStream(getPersistedCacheFile(name)))))) {
            for (Object key : cache.getKeys()) {
                Element e = cache.get(key);
                if (e != null) {
                    writer.write(key + ":" + e.getObjectValue());
                    writer.newLine();
                }
            }
            log.info("Done persisting cache in " + DurationFormatUtils
                    .formatDurationWords(System.currentTimeMillis() - startTime, true, false));
        } catch (IOException e) {
            log.error(String.format("Error persisting cache \"%s\": %s", name, ExceptionUtils.getMessage(e)),
                    e);
        }
        manager.shutdown();
    }
}

From source file:op.OPDE.java

public static void warn(Logger classLogger, Throwable message) {
    classLogger.warn(message);/* ww w .  ja  v a 2  s  .co m*/
    SyslogTools.warn(ExceptionUtils.getMessage(message) + ": " + ExceptionUtils.getStackTrace(message));
}

From source file:org.apache.eagle.common.rest.RESTResponse.java

public RESTResponse(Throwable throwable) {
    if (throwable.getMessage() == null || throwable.getMessage().isEmpty()) {
        this.setMessage(throwable.getMessage());
    } else {/*from  w w  w . j  a  v  a 2  s  . c  om*/
        this.setMessage(ExceptionUtils.getMessage(throwable));
    }
    this.setException(ExceptionUtils.getStackTrace(throwable));
}

From source file:org.apache.phoenix.mapreduce.index.IndexScrutinyTool.java

@Override
public int run(String[] args) throws Exception {
    Connection connection = null;
    try {//from   w  w w  .j  ava2  s.  c o m
        /** start - parse command line configs **/
        CommandLine cmdLine = null;
        try {
            cmdLine = parseOptions(args);
        } catch (IllegalStateException e) {
            printHelpAndExit(e.getMessage(), getOptions());
        }
        final Configuration configuration = HBaseConfiguration.addHbaseResources(getConf());
        boolean useTenantId = cmdLine.hasOption(TENANT_ID_OPTION.getOpt());
        String tenantId = null;
        if (useTenantId) {
            tenantId = cmdLine.getOptionValue(TENANT_ID_OPTION.getOpt());
            configuration.set(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
            LOG.info(String.format("IndexScrutinyTool uses a tenantId %s", tenantId));
        }
        connection = ConnectionUtil.getInputConnection(configuration);
        final String schemaName = cmdLine.getOptionValue(SCHEMA_NAME_OPTION.getOpt());
        final String dataTable = cmdLine.getOptionValue(DATA_TABLE_OPTION.getOpt());
        String indexTable = cmdLine.getOptionValue(INDEX_TABLE_OPTION.getOpt());
        final String qDataTable = SchemaUtil.getQualifiedTableName(schemaName, dataTable);
        String basePath = cmdLine.getOptionValue(OUTPUT_PATH_OPTION.getOpt());
        boolean isForeground = cmdLine.hasOption(RUN_FOREGROUND_OPTION.getOpt());
        boolean useSnapshot = cmdLine.hasOption(SNAPSHOT_OPTION.getOpt());
        boolean outputInvalidRows = cmdLine.hasOption(OUTPUT_INVALID_ROWS_OPTION.getOpt());
        SourceTable sourceTable = cmdLine.hasOption(SOURCE_TABLE_OPTION.getOpt())
                ? SourceTable.valueOf(cmdLine.getOptionValue(SOURCE_TABLE_OPTION.getOpt()))
                : SourceTable.BOTH;

        long batchSize = cmdLine.hasOption(BATCH_SIZE_OPTION.getOpt())
                ? Long.parseLong(cmdLine.getOptionValue(BATCH_SIZE_OPTION.getOpt()))
                : PhoenixConfigurationUtil.DEFAULT_SCRUTINY_BATCH_SIZE;

        long ts = cmdLine.hasOption(TIMESTAMP.getOpt())
                ? Long.parseLong(cmdLine.getOptionValue(TIMESTAMP.getOpt()))
                : EnvironmentEdgeManager.currentTimeMillis() - 60000;

        if (indexTable != null) {
            if (!IndexTool.isValidIndexTable(connection, qDataTable, indexTable, tenantId)) {
                throw new IllegalArgumentException(
                        String.format(" %s is not an index table for %s ", indexTable, qDataTable));
            }
        }

        String outputFormatOption = cmdLine.getOptionValue(OUTPUT_FORMAT_OPTION.getOpt());
        OutputFormat outputFormat = outputFormatOption != null
                ? OutputFormat.valueOf(outputFormatOption.toUpperCase())
                : OutputFormat.TABLE;
        long outputMaxRows = cmdLine.hasOption(OUTPUT_MAX.getOpt())
                ? Long.parseLong(cmdLine.getOptionValue(OUTPUT_MAX.getOpt()))
                : 1000000L;
        /** end - parse command line configs **/

        if (outputInvalidRows && OutputFormat.TABLE.equals(outputFormat)) {
            // create the output table if it doesn't exist
            try (Connection outputConn = ConnectionUtil.getOutputConnection(configuration)) {
                outputConn.createStatement().execute(IndexScrutinyTableOutput.OUTPUT_TABLE_DDL);
                outputConn.createStatement().execute(IndexScrutinyTableOutput.OUTPUT_METADATA_DDL);
            }
        }

        LOG.info(String.format(
                "Running scrutiny [schemaName=%s, dataTable=%s, indexTable=%s, useSnapshot=%s, timestamp=%s, batchSize=%s, outputBasePath=%s, outputFormat=%s, outputMaxRows=%s]",
                schemaName, dataTable, indexTable, useSnapshot, ts, batchSize, basePath, outputFormat,
                outputMaxRows));
        JobFactory jobFactory = new JobFactory(connection, configuration, batchSize, useSnapshot, ts,
                outputInvalidRows, outputFormat, basePath, outputMaxRows, tenantId);
        // If we are running the scrutiny with both tables as the source, run two separate jobs,
        // one for each direction
        if (SourceTable.BOTH.equals(sourceTable)) {
            jobs.add(jobFactory.createSubmittableJob(schemaName, indexTable, dataTable,
                    SourceTable.DATA_TABLE_SOURCE));
            jobs.add(jobFactory.createSubmittableJob(schemaName, indexTable, dataTable,
                    SourceTable.INDEX_TABLE_SOURCE));
        } else {
            jobs.add(jobFactory.createSubmittableJob(schemaName, indexTable, dataTable, sourceTable));
        }

        if (!isForeground) {
            LOG.info("Running Index Scrutiny in Background - Submit async and exit");
            for (Job job : jobs) {
                job.submit();
            }
            return 0;
        }
        LOG.info(
                "Running Index Scrutiny in Foreground. Waits for the build to complete. This may take a long time!.");
        boolean result = true;
        for (Job job : jobs) {
            result = result && job.waitForCompletion(true);
        }

        // write the results to the output metadata table
        if (outputInvalidRows && OutputFormat.TABLE.equals(outputFormat)) {
            LOG.info("Writing results of jobs to output table "
                    + IndexScrutinyTableOutput.OUTPUT_METADATA_TABLE_NAME);
            IndexScrutinyTableOutput.writeJobResults(connection, args, jobs);
        }

        if (result) {
            return 0;
        } else {
            LOG.error("IndexScrutinyTool job failed! Check logs for errors..");
            return -1;
        }
    } catch (Exception ex) {
        LOG.error("An exception occurred while performing the indexing job: " + ExceptionUtils.getMessage(ex)
                + " at:\n" + ExceptionUtils.getStackTrace(ex));
        return -1;
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException sqle) {
            LOG.error("Failed to close connection ", sqle.getMessage());
            throw new RuntimeException("Failed to close connection");
        }
    }
}

From source file:org.apache.syncope.core.misc.ExceptionUtil.java

/**
 * Uses commons lang's ExceptionUtils to provide a representation of the full stack trace of the given throwable.
 *
 * @param t throwable to build stack trace from
 * @return a string representation of full stack trace of the given throwable
 *///from   w w  w . j  ava  2s .  c om
public static String getFullStackTrace(final Throwable t) {
    StringBuilder result = new StringBuilder();

    for (Throwable throwable : ExceptionUtils.getThrowableList(t)) {
        result.append(ExceptionUtils.getMessage(throwable)).append('\n')
                .append(ExceptionUtils.getStackTrace(throwable)).append("\n\n");
    }

    return result.toString();
}

From source file:org.finra.herd.service.activiti.HerdCommandInvoker.java

@Override
public <T> T execute(CommandConfig config, Command<T> command) {
    LOGGER.debug("command=\"{}\"", command.getClass().getName());
    try {//from  w w w  .  jav a 2s.c o m
        // Perform the normal execution.
        return super.execute(config, command);
    } catch (Exception e) {
        LOGGER.debug(String.format("HerdCommandInvoker caught an exception."), e);
        /*
         * Attempt to handle exception based on the command.
         * If we bubble the exception up here, the transaction will be rolled back and all variables which were not committed will not be persisted.
         * The problem with swallowing the exception, however, is that the exception message is not persisted automatically. To get around it, we must save
         * the exception message and stacktrace into a JobEntity which is associated with the current execution.
         */

        if (command instanceof ExecuteAsyncJobCmd) {
            /*
             * ExecuteAsyncJobCmd is executed when a task is asynchronous.
             * Save the exception information in the command's JobEntity
             */
            ExecuteAsyncJobCmd executeAsyncJobCmd = (ExecuteAsyncJobCmd) command;
            JobEntity jobEntity = getJobEntity(executeAsyncJobCmd);
            jobEntity.setExceptionMessage(ExceptionUtils.getMessage(e));
            jobEntity.setExceptionStacktrace(ExceptionUtils.getStackTrace(e));
            return null;
        } else {
            /*
             * We do not know how to handle any other commands, so just bubble it up and let Activiti's default mechanism kick in.
             */
            throw e;
        }
    }
}

From source file:org.jmingo.parser.xml.dom.DocumentBuilderFactoryCreator.java

/**
 * Creates schema from list of {@link Source} objects.
 *
 * @param sources list of {@link Source}
 * @return immutable in-memory representation of grammar
 * @throws ParserConfigurationException {@link ParserConfigurationException}
 *//*from w  w w.  ja v a  2 s .  c o m*/
private static Schema createSchema(List<Source> sources) throws ParserConfigurationException {
    Schema schema;
    try {
        schema = SCHEMA_FACTORY.newSchema(sources.toArray(new Source[sources.size()]));
    } catch (SAXException e) {
        throw new ParserConfigurationException(ExceptionUtils.getMessage(e));
    }
    return schema;
}