Example usage for java.util Objects toString

List of usage examples for java.util Objects toString

Introduction

In this page you can find the example usage for java.util Objects toString.

Prototype

public static String toString(Object o) 

Source Link

Document

Returns the result of calling toString for a non- null argument and "null" for a null argument.

Usage

From source file:org.hibernate.validator.internal.util.logging.Log.java

public void debugf(final String pstring, final Object pdescriptor) {
    GWT.log(StringUtils.replace(pstring, "%s", Objects.toString(pdescriptor)));
}

From source file:com.joyent.manta.client.MantaClient.java

/**
 * Gets the high-level job container object for a given id.
 * First, attempts to get the live status object and if it can't be
 * retrieved, then we get the archived status object.
 *
 * @param jobId UUID of the Manta job//from   w w w  .  j a  v a2  s.  c o  m
 * @return Object representing the properties of the job or null if not found
 * @throws IOException thrown when we can't get the job over the network
 */
public MantaJob getJob(final UUID jobId) throws IOException {
    Validate.notNull(jobId, "Manta job id must not be null");
    final String livePath = String.format("%s/jobs/%s/live/status", config.getMantaHomeDirectory(), jobId);

    final CloseableHttpClient client = httpHelper.getConnectionContext().getHttpClient();
    final HttpUriRequest initialRequest = httpHelper.getRequestFactory().get(livePath);
    MantaJob job;
    HttpEntity entity;

    CloseableHttpResponse lastResponse = null;

    try (CloseableHttpResponse initialResponse = client.execute(initialRequest)) {
        lastResponse = initialResponse;
        final StatusLine statusLine = initialResponse.getStatusLine();

        // If we can't get the live status of the job, we try to get the archived
        // status of the job just like the CLI mjob utility.
        if (statusLine.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            final String archivePath = String.format("%s/jobs/%s/job.json", config.getMantaHomeDirectory(),
                    jobId);

            final HttpUriRequest archiveRequest = httpHelper.getRequestFactory().get(archivePath);

            // We close the request that was previously opened, because it
            // didn't have what we need.
            IOUtils.closeQuietly(initialResponse);

            CloseableHttpResponse archiveResponse = client.execute(archiveRequest);
            lastResponse = archiveResponse;
            final StatusLine archiveStatusLine = archiveResponse.getStatusLine();

            // Job wasn't available via live status nor archive
            if (archiveStatusLine.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                String msg = "No record for job in Manta";
                MantaJobException e = new MantaJobException(jobId, msg);
                HttpHelper.annotateContextedException(e, archiveRequest, archiveResponse);
                throw e;
                // There was an undefined problem with pulling the job from the archive
            } else if (archiveStatusLine.getStatusCode() != HttpStatus.SC_OK) {
                String msg = "Unable to get job data from archive";
                MantaIOException ioe = new MantaIOException(msg);
                HttpHelper.annotateContextedException(ioe, archiveRequest, archiveResponse);
                ioe.setContextValue("jobId", Objects.toString(jobId));
                throw ioe;
                // The job was pulled without problems from the archive
            } else {
                entity = archiveResponse.getEntity();
            }

            // There was an undefined problem with pulling the job from the live status
        } else if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
            String msg = "Unable to get job data from live status";
            MantaIOException ioe = new MantaIOException(msg);
            HttpHelper.annotateContextedException(ioe, initialRequest, initialResponse);
            ioe.setContextValue("jobId", Objects.toString(jobId));
            throw ioe;
            // The job was pulled without problems from the live status
        } else {
            entity = initialResponse.getEntity();
        }

        try (InputStream in = entity.getContent()) {
            job = MantaObjectMapper.INSTANCE.readValue(in, MantaJob.class);
        } catch (IOException e) {
            String msg = "Unable to deserialize job data";
            MantaIOException ioe = new MantaIOException(msg, e);
            HttpHelper.annotateContextedException(ioe, initialRequest, initialResponse);
            ioe.setContextValue("jobId", Objects.toString(jobId));
            throw ioe;
        }
        // We sweep up any uncaught io exceptions and wrap them with details
    } catch (IOException e) {
        if (e instanceof MantaIOException) {
            throw e;
        }

        String msg = "Unable to get job data";
        MantaIOException ioe = new MantaIOException(msg, e);
        HttpHelper.annotateContextedException(ioe, initialRequest, lastResponse);
        ioe.setContextValue("jobId", Objects.toString(jobId));
        throw ioe;
        // Likewise we sweep up any uncaught runtime exceptions and wrap them
    } catch (RuntimeException e) {
        if (e instanceof MantaException) {
            throw e;
        }

        String msg = "Unexpected error when getting job data";
        MantaJobException je = new MantaJobException(jobId, msg, e);
        HttpHelper.annotateContextedException(je, initialRequest, lastResponse);
        throw je;
    } finally {
        if (lastResponse != null) {
            IOUtils.closeQuietly(lastResponse);
        }
    }

    Validate.notNull(job, "Job returned must not be null");
    return job;
}

From source file:com.joyent.manta.client.MantaClient.java

/**
 * Gets all of the Manta jobs as a real-time {@link Stream} from
 * the Manta API. <strong>Make sure to close this stream when you are done with
 * otherwise the HTTP socket will remain open.</strong>
 *
 * @return a stream with all of the jobs
 *///w  ww.  j  a  v  a  2s  .  c o  m
public Stream<MantaJob> getAllJobs() {
    return getAllJobIds().map(id -> {
        Validate.notNull(id, "Job ids must not be null");

        try {
            return getJob(id);
        } catch (IOException e) {
            String msg = "Error processing job object stream";
            MantaJobException jobException = new MantaJobException(id, msg, e);
            jobException.setContextValue("failedJobId", Objects.toString(id));
            throw jobException;
        }
    });
}

From source file:com.joyent.manta.client.MantaClient.java

/**
 * Gets all of the Manta jobs' IDs as a real-time {@link Stream} from
 * the Manta API. <strong>Make sure to close this stream when you are done with
 * otherwise the HTTP socket will remain open.</strong>
 *
 * @return a stream with all of the job IDs (actually all that Manta will give us)
 *///from  ww  w.j  ava  2  s.co m
public Stream<UUID> getAllJobIds() {
    final String path = String.format("%s/jobs", config.getMantaHomeDirectory());

    final MantaDirectoryListingIterator itr = new MantaDirectoryListingIterator(path, httpHelper, MAX_RESULTS);

    danglingStreams.add(itr);

    Stream<Map<String, Object>> backingStream = StreamSupport
            .stream(Spliterators.spliteratorUnknownSize(itr, Spliterator.ORDERED | Spliterator.NONNULL), false);

    return backingStream.map(item -> {
        final String id = Objects.toString(item.get("name"));
        return UUID.fromString(id);
    });
}