Example usage for org.apache.commons.lang3.exception ContextedRuntimeException setContextValue

List of usage examples for org.apache.commons.lang3.exception ContextedRuntimeException setContextValue

Introduction

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

Prototype

@Override
public ContextedRuntimeException setContextValue(final String label, final Object value) 

Source Link

Document

Sets information helpful to a developer in diagnosing and correcting the problem.

Usage

From source file:ServerMultipart.java

private static void multipartUpload(ServerSideMultipartManager multipart) {
    String uploadObject = "/" + mantaUsername + "/stor/multipart";

    // We catch network errors and handle them here
    try {//from  ww w . j ava 2s.co m
        ServerSideMultipartUpload upload = multipart.initiateUpload(uploadObject);
        MantaMultipartUploadPart part1 = multipart.uploadPart(upload, 1, RandomUtils.nextBytes(5242880));
        MantaMultipartUploadPart part2 = multipart.uploadPart(upload, 2, RandomUtils.nextBytes(1000000));

        // Complete the process by instructing Manta to assemble the final object from its parts
        MantaMultipartUploadTuple[] parts = new MantaMultipartUploadTuple[] { part1, part2 };
        Stream<MantaMultipartUploadTuple> partsStream = Arrays.stream(parts);
        multipart.complete(upload, partsStream);

        System.out.println(uploadObject + " is now assembled!");
    } catch (IOException e) {
        // This catch block is for general network failures
        // For example, ServerSideMultipartUpload.initiateUpload can throw an IOException

        ContextedRuntimeException exception = new ContextedRuntimeException(
                "A network error occurred when doing a multipart upload to Manta.");
        exception.setContextValue("path", uploadObject);

        throw exception;
    }
}

From source file:ClientEncryptionServerMultipart.java

private static void multipartUpload(EncryptedServerSideMultipartManager multipart) {
    String uploadObject = "/" + mantaUsername + "/stor/multipart";

    // We catch network errors and handle them here
    try {/*from  ww w.j a  va2 s.c o  m*/
        MantaMetadata metadata = new MantaMetadata();
        metadata.put("e-secretkey", "My Secret Value");
        EncryptedMultipartUpload<ServerSideMultipartUpload> upload = multipart.initiateUpload(uploadObject,
                metadata);
        MantaMultipartUploadPart part1 = multipart.uploadPart(upload, 1, RandomUtils.nextBytes(5242880));
        MantaMultipartUploadPart part2 = multipart.uploadPart(upload, 2, RandomUtils.nextBytes(1000000));

        // Complete the process by instructing Manta to assemble the final object from its parts
        MantaMultipartUploadTuple[] parts = new MantaMultipartUploadTuple[] { part1, part2 };
        Stream<MantaMultipartUploadTuple> partsStream = Arrays.stream(parts);
        multipart.complete(upload, partsStream);

        System.out.println(uploadObject + " is now assembled!");
    } catch (IOException e) {
        // This catch block is for general network failures
        // For example, ServerSideMultipartUpload.initiateUpload can throw an IOException

        ContextedRuntimeException exception = new ContextedRuntimeException(
                "A network error occurred when doing a multipart upload to Manta.");
        exception.setContextValue("path", uploadObject);

        throw exception;
    }
}

From source file:JobsBasedMultipart.java

private static void multipartUpload(MantaClient mantaClient) {
    // instantiated with a reference to the class the actually connects to Manta
    JobsMultipartManager multipart = new JobsMultipartManager(mantaClient);

    String uploadObject = "/username/stor/test/file";

    /* I'm using File objects below, but I could be using byte[] arrays,
     * Strings, or InputStreams as well. */
    File part1file = new File("part-1.data");
    File part2file = new File("part-2.data");
    File part3file = new File("part-3.data");

    // We can set any metadata for the final object
    MantaMetadata metadata = new MantaMetadata();
    metadata.put("m-test-metadata", "any value");

    // We can set any header for the final object
    MantaHttpHeaders headers = new MantaHttpHeaders();
    headers.setContentType("text/plain");

    // We catch network errors and handle them here
    try {/*from  ww w.j  a  v  a 2s . co m*/
        // We get a response object
        JobsMultipartUpload upload = multipart.initiateUpload(uploadObject);

        // It contains a UUID transaction id
        UUID id = upload.getId();
        // It also contains the path of the final object
        String uploadPath = upload.getPath();

        // Everywhere below that we specified "upload" we could also just
        // use the upload transaction id

        List<MantaMultipartUploadPart> parts = new ArrayList<>();

        // We can add the parts in any order
        MantaMultipartUploadPart part2 = multipart.uploadPart(upload, 2, part2file);
        // Each put of a part is a synchronous operation
        MantaMultipartUploadPart part1 = multipart.uploadPart(upload, 1, part1file);
        // Although in a later version we could make an async option
        MantaMultipartUploadPart part3 = multipart.uploadPart(upload, 3, part3file);

        parts.add(part1);
        parts.add(part3);
        parts.add(part2);

        // If we want to give up now, we could always abort
        // multipart.abort(upload);

        // We've uploaded all of the parts, now lets join them
        multipart.complete(upload, parts.stream());

        // If we want to pause execution until it is committed
        int timesToPoll = 10;
        multipart.waitForCompletion(upload, Duration.ofSeconds(5), timesToPoll, uuid -> {
            throw new RuntimeException("Multipart completion timed out");
        });

    } catch (MantaClientHttpResponseException e) {
        // This catch block is for when we actually have a response code from Manta

        // We can handle specific HTTP responses here
        if (e.getStatusCode() == 503) {
            System.out.println("Manta is unavailable. Please try again");
            return;
        }

        // We could rethrow as a more detailed exception as below
        throw new RuntimeException(e);
    } catch (IOException e) {
        // This catch block is for general network failures
        // Note: MantaClientHttpResponseException inherits from IOException
        // so if it is not explicitly caught, it would go to this block

        ContextedRuntimeException exception = new ContextedRuntimeException(
                "A network error occurred when doing a multipart upload to"
                        + "Manta. See context for details.");
        // We should all of the diagnostic context that we need
        exception.setContextValue("parts", "[part-1.data, part-2.data, part-3.data]");

        // We rethrow the exception with additional detail
        throw exception;
    }
}

From source file:net.certifi.audittablegen.ChangeSourceFactory.java

List<DBChangeUnit> getDBChangeList(TableDef baseTableDef) {

    List<DBChangeUnit> tableChangeUnits = new ArrayList();
    List<DBChangeUnit> renameColumnChangeUnits = new ArrayList();
    List<DBChangeUnit> alterTableChangeUnits = new ArrayList();

    DBChangeUnit workUnit;/*  w  w w . j  a v  a2s  .co  m*/
    String baseTableName = baseTableDef.getName();

    String newColumnName;

    if (baseTableDef == null) {
        logger.error("Invalid input. null TableDef");
        return tableChangeUnits;
    }

    if (baseTableDef.getColumns().isEmpty()) {
        ContextedRuntimeException e = new ContextedRuntimeException();
        e.setContextValue("tableName", baseTableDef.getName());
        logger.error("Invalid Input. TableDef has no columns.", e);
        return tableChangeUnits;
    }

    String auditTableName = tablePrefix + baseTableName + tablePostfix;

    String auditActionColumn = columnPrefix + "action" + columnPostfix;

    String auditTimeStampColumn = columnPrefix + "ts" + columnPostfix;

    String auditUserColumn = columnPrefix + "userId" + columnPostfix;

    String sessionUserColumn = columnPrefix + "sessionUser" + columnPostfix;

    TableDef auditTableDef = null;
    if (auditTablesMap.containsKey(auditTableName)) {
        auditTableDef = auditTablesMap.get(auditTableName);
    }

    if (isTableExcluded(baseTableName)) {
        //drop all audit triggers
        tableChangeUnits.add(new DBChangeUnit(DBChangeType.begin));
        workUnit = new DBChangeUnit(DBChangeType.dropTriggers);
        workUnit.setTableName(baseTableName);
        tableChangeUnits.add(workUnit);
        tableChangeUnits.add(new DBChangeUnit(DBChangeType.end));

        //done.  We don't want to alter any exising audit table
        return tableChangeUnits;
    }

    //create or alter audit table
    if (null == auditTableDef) {
        //create table
        tableChangeUnits.add(new DBChangeUnit(DBChangeType.begin));
        workUnit = new DBChangeUnit(DBChangeType.createTable);
        workUnit.tableName = auditTableName;
        tableChangeUnits.add(workUnit);

        //create id column
        workUnit = new DBChangeUnit(DBChangeType.addColumn);
        workUnit.setColumnName(auditTableName + "Id");
        workUnit.setTableName(auditTableName);
        workUnit.setTypeName(auditIdTypeName);
        workUnit.setIdentity(Boolean.TRUE);
        tableChangeUnits.add(workUnit);

        //create all columns on the base table
        for (ColumnDef baseColumn : baseTableDef.getColumns()) {
            workUnit = new DBChangeUnit(DBChangeType.addColumn);
            workUnit.setColumnName(baseColumn.getName());
            workUnit.setTableName(auditTableName);
            workUnit.setTypeName(baseColumn.getTypeName());
            workUnit.setSize(baseColumn.getSize());
            workUnit.setDecimalSize(baseColumn.getDecimalSize());
            tableChangeUnits.add(workUnit);
        }

        //create the audit tracking columns
        //action
        workUnit = new DBChangeUnit(DBChangeType.addColumn);
        workUnit.setColumnName(auditActionColumn);
        workUnit.setTableName(auditTableName);
        workUnit.setTypeName(auditActionTypeName); //insert, update, or delete
        workUnit.setSize(6);
        workUnit.setDecimalSize(0);
        tableChangeUnits.add(workUnit);

        //user
        workUnit = new DBChangeUnit(DBChangeType.addColumn);
        workUnit.setColumnName(auditUserColumn);
        workUnit.setTableName(auditTableName);
        workUnit.setTypeName(auditUserTypeName);
        workUnit.setSize(configSource.getMaxUserNameLength());
        workUnit.setDecimalSize(0);
        tableChangeUnits.add(workUnit);

        //timestamp
        workUnit = new DBChangeUnit(DBChangeType.addColumn);
        workUnit.setColumnName(auditTimeStampColumn);
        workUnit.setTableName(auditTableName);
        workUnit.setTypeName(auditTimeStampTypeName);
        workUnit.setSize(0);
        workUnit.setDecimalSize(0);
        tableChangeUnits.add(workUnit);

        //sessionUser
        if (!sessionUserSQL.isEmpty()) {
            workUnit = new DBChangeUnit(DBChangeType.addColumn);
            workUnit.setColumnName(sessionUserColumn);
            workUnit.setTableName(auditTableName);
            workUnit.setTypeName(sessionUserTypeName);
            workUnit.setSize(sessionUserDataSize);
            workUnit.setDecimalSize(0);
            tableChangeUnits.add(workUnit);
        }

        //end of table
        tableChangeUnits.add(new DBChangeUnit(DBChangeType.end));
    } else {
        //alter table
        //there might not be any changes, so store up any changes in 
        //a temporary list, and evaluate.
        alterTableChangeUnits.add(new DBChangeUnit(DBChangeType.begin));
        workUnit = new DBChangeUnit(DBChangeType.alterTable);
        workUnit.setTableName(auditTableName);
        alterTableChangeUnits.add(workUnit);

        //to make this a little easier, get a map for the column list
        Map<String, ColumnDef> auditColumnMap = new CaseInsensitiveMap();
        for (ColumnDef auditColumn : auditTableDef.getColumns()) {
            auditColumnMap.put(auditColumn.getName(), auditColumn);
        }

        //make sure the audit columns and the id exist
        //create the audit tracking columns
        //action
        if (!auditColumnMap.containsKey(auditActionColumn)) {
            logger.warn("Existing audit table {} does not contain column {}. Creating", auditTableName,
                    auditActionColumn);
            workUnit = new DBChangeUnit(DBChangeType.addColumn);
            workUnit.setColumnName(auditActionColumn);
            workUnit.setTableName(auditTableName);
            workUnit.setTypeName(auditActionTypeName); //insert, update, or delete
            workUnit.setSize(6);
            workUnit.setDecimalSize(0);
            alterTableChangeUnits.add(workUnit);
        }

        //user
        if (!auditColumnMap.containsKey(auditUserColumn)) {
            logger.warn("Existing audit table {} does not contain column {}. Creating", auditTableName,
                    auditUserColumn);
            workUnit = new DBChangeUnit(DBChangeType.addColumn);
            workUnit.setColumnName(auditUserColumn);
            workUnit.setTableName(auditTableName);
            workUnit.setTypeName(auditUserTypeName);
            workUnit.setSize(configSource.getMaxUserNameLength());
            workUnit.setDecimalSize(0);
            alterTableChangeUnits.add(workUnit);
        }

        //timestamp
        if (!auditColumnMap.containsKey(auditTimeStampColumn)) {
            logger.warn("Existing audit table {} does not contain column {}. Creating", auditTableName,
                    auditTimeStampColumn);
            workUnit = new DBChangeUnit(DBChangeType.addColumn);
            workUnit.setColumnName(auditTimeStampColumn);
            workUnit.setTableName(auditTableName);
            workUnit.setTypeName(auditTimeStampTypeName);
            workUnit.setSize(0);
            workUnit.setDecimalSize(0);
            alterTableChangeUnits.add(workUnit);
        }

        //seesionuser
        if (!sessionUserSQL.isEmpty()) {
            if (!auditColumnMap.containsKey(sessionUserColumn)) {
                logger.warn("Existing audit table {} does not contain column {}. Creating", auditTableName,
                        sessionUserColumn);
                workUnit = new DBChangeUnit(DBChangeType.addColumn);
                workUnit.setColumnName(sessionUserColumn);
                workUnit.setTableName(auditTableName);
                workUnit.setTypeName(sessionUserTypeName);
                workUnit.setSize(sessionUserDataSize);
                workUnit.setDecimalSize(0);
                alterTableChangeUnits.add(workUnit);
            }
        }

        //add or alter columns
        for (ColumnDef baseColumn : baseTableDef.getColumns()) {
            if (auditColumnMap.containsKey(baseColumn.name)) {
                //existing column
                ColumnDef auditColumn = auditColumnMap.get(baseColumn.name);
                if (auditColumn.getTypeName().equalsIgnoreCase(baseColumn.getTypeName())
                        && auditColumn.getSize() >= baseColumn.getSize()
                        && auditColumn.getDecimalSize() >= baseColumn.getDecimalSize()) {
                    //nothing to do
                } else if (auditColumn.getTypeName().equalsIgnoreCase(baseColumn.getTypeName())
                        && (auditColumn.getSize() < baseColumn.getSize()
                                || auditColumn.getDecimalSize() < baseColumn.getDecimalSize())) {
                    //type is the same, but size increased
                    workUnit = new DBChangeUnit(DBChangeType.alterColumnSize);
                    workUnit.setTableName(auditTableName);
                    workUnit.setColumnName(baseColumn.getName());
                    workUnit.setTypeName(baseColumn.getTypeName());
                    workUnit.setSize(baseColumn.getSize());
                    workUnit.setDecimalSize(baseColumn.getDecimalSize());
                    alterTableChangeUnits.add(workUnit);
                } else {
                    //type changes or size shrunk. Rename existing column
                    //and create new column in its place.  This requires
                    //the column rename to be done as a sepereate command
                    //from the other column changes.  (At least it does on
                    //postgres.
                    int i = 1;
                    do {
                        newColumnName = String.format("%s_prev%d", auditColumn.getName(), i);
                        i++;
                    } while (auditColumnMap.containsKey(newColumnName));
                    //rename the old version of the audit column
                    renameColumnChangeUnits.add((new DBChangeUnit(DBChangeType.begin)));
                    workUnit = new DBChangeUnit(DBChangeType.alterTable);
                    workUnit.setTableName(auditTableName);
                    renameColumnChangeUnits.add(workUnit);
                    workUnit = new DBChangeUnit(DBChangeType.alterColumnName);
                    workUnit.setTableName(auditTableName);
                    workUnit.setColumnName(auditColumn.getName());
                    workUnit.setNewColumnName(newColumnName);
                    workUnit.setTypeName(auditColumn.getTypeName());
                    workUnit.setSize(auditColumn.getSize());
                    workUnit.setDecimalSize(auditColumn.getDecimalSize());
                    renameColumnChangeUnits.add(workUnit);
                    renameColumnChangeUnits.add((new DBChangeUnit(DBChangeType.end)));

                    //now add the new version of the column
                    workUnit = new DBChangeUnit(DBChangeType.addColumn);
                    workUnit.setTableName(auditTableName);
                    workUnit.setColumnName(baseColumn.getName());
                    workUnit.setTypeName(baseColumn.getTypeName());
                    workUnit.setSize(baseColumn.getSize());
                    workUnit.setDecimalSize(baseColumn.getDecimalSize());
                    alterTableChangeUnits.add(workUnit);
                }
            } else {
                //new column
                workUnit = new DBChangeUnit(DBChangeType.addColumn);
                workUnit.setTableName(auditTableName);
                workUnit.setColumnName(baseColumn.getName());
                workUnit.setTypeName(baseColumn.getTypeName());
                workUnit.setSize(baseColumn.getSize());
                workUnit.setDecimalSize(baseColumn.getDecimalSize());
                alterTableChangeUnits.add(workUnit);
            }
        }

        //end of table
        alterTableChangeUnits.add(new DBChangeUnit(DBChangeType.end));

        //add the workUnits to the return value
        if (!renameColumnChangeUnits.isEmpty()) {
            tableChangeUnits.addAll(renameColumnChangeUnits);
        }

        if (alterTableChangeUnits.size() > 3) {
            tableChangeUnits.addAll(alterTableChangeUnits);
        }

    }

    //begin trigger changes
    tableChangeUnits.add(new DBChangeUnit(DBChangeType.begin));
    workUnit = new DBChangeUnit(DBChangeType.createTriggers);
    workUnit.setTableName(baseTableName);
    workUnit.setAuditTableName(auditTableName);
    tableChangeUnits.add(workUnit);

    //insert trigger
    workUnit = new DBChangeUnit(DBChangeType.fireOnInsert);
    workUnit.setTableName(baseTableName);
    workUnit.setAuditTableName(auditTableName);
    workUnit.setFiresTrigger(hasTriggerType(baseTableName, ConfigAttributeTypes.auditinsert));
    tableChangeUnits.add(workUnit);

    //update trigger
    workUnit = new DBChangeUnit(DBChangeType.fireOnUpdate);
    workUnit.setTableName(baseTableName);
    workUnit.setAuditTableName(auditTableName);
    workUnit.setFiresTrigger(hasTriggerType(baseTableName, ConfigAttributeTypes.auditupdate));
    tableChangeUnits.add(workUnit);

    //delete trigger
    workUnit = new DBChangeUnit(DBChangeType.fireOnDelete);
    workUnit.setTableName(baseTableName);
    workUnit.setAuditTableName(auditTableName);
    workUnit.setFiresTrigger(hasTriggerType(baseTableName, ConfigAttributeTypes.auditdelete));
    tableChangeUnits.add(workUnit);

    //now add the columns that will be included in the trigger
    //and set whether or not they will cause it to fire (default is true)
    for (ColumnDef baseColumnDef : baseTableDef.getColumns()) {
        workUnit = new DBChangeUnit(DBChangeType.addTriggerColumn);
        workUnit.setTableName(baseTableName);
        workUnit.setAuditTableName(auditTableName);
        workUnit.setColumnName(baseColumnDef.getName());
        if (isColumnExcluded(baseTableDef.getName(), baseColumnDef.getName())) {
            workUnit.setFiresTrigger(Boolean.FALSE);
        }
        tableChangeUnits.add(workUnit);
    }

    //add the audit tracking columns

    //action
    workUnit = new DBChangeUnit(DBChangeType.addTriggerAction);
    workUnit.setColumnName(auditActionColumn);
    workUnit.setTableName(baseTableName);
    workUnit.setAuditTableName(auditTableName);
    tableChangeUnits.add(workUnit);

    //user
    workUnit = new DBChangeUnit(DBChangeType.addTriggerUser);
    workUnit.setColumnName(auditUserColumn);
    workUnit.setTableName(baseTableName);
    workUnit.setAuditTableName(auditTableName);
    tableChangeUnits.add(workUnit);

    //timestamp
    workUnit = new DBChangeUnit(DBChangeType.addTriggerTimeStamp);
    workUnit.setColumnName(auditTimeStampColumn);
    workUnit.setTableName(baseTableName);
    workUnit.setAuditTableName(auditTableName);
    tableChangeUnits.add(workUnit);

    //sessionuser
    if (!sessionUserSQL.isEmpty()) {
        workUnit = new DBChangeUnit(DBChangeType.addTriggerSessionUser);
        workUnit.setColumnName(sessionUserColumn);
        workUnit.setTableName(baseTableName);
        workUnit.setAuditTableName(auditTableName);
        tableChangeUnits.add(workUnit);
    }

    //end trigger changes
    tableChangeUnits.add(new DBChangeUnit(DBChangeType.end));

    return tableChangeUnits;

}