Example usage for java.util.concurrent.atomic AtomicReference AtomicReference

List of usage examples for java.util.concurrent.atomic AtomicReference AtomicReference

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicReference AtomicReference.

Prototype

public AtomicReference() 

Source Link

Document

Creates a new AtomicReference with null initial value.

Usage

From source file:test.java.com.spotify.docker.client.DefaultDockerClientTest.java

@Test
public void testBuildImageIdWithAuth() throws Exception {
    final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
    final AtomicReference<String> imageIdFromMessage = new AtomicReference<>();

    final DefaultDockerClient sut2 = DefaultDockerClient.builder().uri(dockerEndpoint).authConfig(authConfig)
            .build();//w w w. j  a  va 2 s .  c  o  m

    final String returnedImageId = sut2.build(Paths.get(dockerDirectory), "test", new ProgressHandler() {
        @Override
        public void progress(ProgressMessage message) throws DockerException {
            final String imageId = message.buildImageId();
            if (imageId != null) {
                imageIdFromMessage.set(imageId);
            }
        }
    });

    assertThat(returnedImageId, is(imageIdFromMessage.get()));
}

From source file:hello.MetricsActivator.java

private MessageHandler handlerInAnonymousWrapper(final Object bean) {
    if (bean != null && bean.getClass().isAnonymousClass()) {
        final AtomicReference<MessageHandler> wrapped = new AtomicReference<MessageHandler>();
        ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {

            @Override/*from   w ww.  j av  a 2 s  . com*/
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                field.setAccessible(true);
                Object handler = field.get(bean);
                if (handler instanceof MessageHandler) {
                    wrapped.set((MessageHandler) handler);
                }
            }
        }, new FieldFilter() {

            @Override
            public boolean matches(Field field) {
                return wrapped.get() == null && field.getName().startsWith("val$");
            }
        });
        return wrapped.get();
    } else {
        return null;
    }
}

From source file:de.schildbach.pte.AbstractEfaProvider.java

protected SuggestLocationsResult mobileStopfinderRequest(final Location constraint) throws IOException {
    final HttpUrl.Builder url = stopFinderEndpoint.newBuilder();
    appendStopfinderRequestParameters(url, constraint, "XML");
    final AtomicReference<SuggestLocationsResult> result = new AtomicReference<>();

    final HttpClient.Callback callback = new HttpClient.Callback() {
        @Override//from   w w  w.  j a v a 2  s  .  com
        public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException {
            try {
                final XmlPullParser pp = parserFactory.newPullParser();
                pp.setInput(body.byteStream(), null); // Read encoding from XML declaration
                final ResultHeader header = enterEfa(pp);

                final List<SuggestedLocation> locations = new ArrayList<>();

                XmlPullUtil.require(pp, "sf");
                if (XmlPullUtil.optEnter(pp, "sf")) {
                    while (XmlPullUtil.optEnter(pp, "p")) {
                        final String name = normalizeLocationName(XmlPullUtil.valueTag(pp, "n"));
                        final String u = XmlPullUtil.valueTag(pp, "u");
                        if (!"sf".equals(u))
                            throw new RuntimeException("unknown usage: " + u);
                        final String ty = XmlPullUtil.valueTag(pp, "ty");
                        final LocationType type;
                        if ("stop".equals(ty))
                            type = LocationType.STATION;
                        else if ("poi".equals(ty))
                            type = LocationType.POI;
                        else if ("loc".equals(ty))
                            type = LocationType.COORD;
                        else if ("street".equals(ty))
                            type = LocationType.ADDRESS;
                        else if ("singlehouse".equals(ty))
                            type = LocationType.ADDRESS;
                        else
                            throw new RuntimeException("unknown type: " + ty);

                        XmlPullUtil.enter(pp, "r");

                        final String id = XmlPullUtil.valueTag(pp, "id");
                        XmlPullUtil.optValueTag(pp, "gid", null);
                        XmlPullUtil.valueTag(pp, "stateless");
                        XmlPullUtil.valueTag(pp, "omc");
                        final String place = normalizeLocationName(XmlPullUtil.optValueTag(pp, "pc", null));
                        XmlPullUtil.valueTag(pp, "pid");
                        final Point coord = parseCoord(XmlPullUtil.optValueTag(pp, "c", null));

                        XmlPullUtil.skipExit(pp, "r");

                        final String qal = XmlPullUtil.optValueTag(pp, "qal", null);
                        final int quality = qal != null ? Integer.parseInt(qal) : 0;

                        XmlPullUtil.skipExit(pp, "p");

                        final Location location = new Location(type, type == LocationType.STATION ? id : null,
                                coord, place, name);
                        final SuggestedLocation locationAndQuality = new SuggestedLocation(location, quality);
                        locations.add(locationAndQuality);
                    }

                    XmlPullUtil.skipExit(pp, "sf");
                }

                result.set(new SuggestLocationsResult(header, locations));
            } catch (final XmlPullParserException x) {
                throw new ParserException("cannot parse xml: " + bodyPeek, x);
            }
        }
    };

    if (httpPost)
        httpClient.getInputStream(callback, url.build(), url.build().encodedQuery(),
                "application/x-www-form-urlencoded", httpReferer);
    else
        httpClient.getInputStream(callback, url.build(), httpReferer);

    return result.get();
}

From source file:brooklyn.util.internal.ssh.sshj.SshjTool.java

/**
 * Executes the script in the background (`nohup ... &`), and then executes other ssh commands to poll for the
 * stdout, stderr and exit code of that original process (which will each have been written to separate files).
 * /*from  www  .  jav  a  2  s .com*/
 * The polling is a "long poll". That is, it executes a long-running ssh command to retrieve the stdout, etc.
 * If that long-poll command fails, then we just execute another one to pick up from where it left off.
 * This means we do not need to execute many ssh commands (which are expensive), but can still return promptly
 * when the command completes.
 * 
 * Much of this was motivated by https://issues.apache.org/jira/browse/BROOKLYN-106, which is no longer
 * an issue. The retries (e.g. in the upload-script) are arguably overkill given that {@link #acquire(SshAction)}
 * will already retry. However, leaving this in place as it could prove useful when working with flakey
 * networks in the future.
 * 
 * TODO There are (probably) issues with this method when using {@link ShellTool#PROP_RUN_AS_ROOT}.
 * I (Aled) saw the .pid file having an owner of root:root, and a failure message in stderr of:
 *   -bash: line 3: /tmp/brooklyn-20150113-161203056-XMEo-move_install_dir_from_user_to_.pid: Permission denied
 */
protected int execScriptAsyncAndPoll(final Map<String, ?> props, final List<String> commands,
        final Map<String, ?> env) {
    return new ToolAbstractAsyncExecScript(props) {
        private int maxConsecutiveSshFailures = 3;
        private Duration maxDelayBetweenPolls = Duration.seconds(20);
        private Duration pollTimeout = getOptionalVal(props, PROP_EXEC_ASYNC_POLLING_TIMEOUT,
                Duration.FIVE_MINUTES);
        private int iteration = 0;
        private int consecutiveSshFailures = 0;
        private int stdoutCount = 0;
        private int stderrCount = 0;
        private Stopwatch timer;

        public int run() {
            timer = Stopwatch.createStarted();
            final String scriptContents = toScript(props, commands, env);
            if (LOG.isTraceEnabled())
                LOG.trace("Running shell command at {} as async script: {}", host, scriptContents);

            // Upload script; try repeatedly because have seen timeout intermittently on vcloud-director (BROOKLYN-106 related).
            boolean uploadSuccess = Repeater
                    .create("async script upload on " + SshjTool.this.toString() + " (for " + getSummary()
                            + ")")
                    .backoffTo(maxDelayBetweenPolls).limitIterationsTo(3).rethrowException()
                    .until(new Callable<Boolean>() {
                        @Override
                        public Boolean call() throws Exception {
                            iteration++;
                            if (LOG.isDebugEnabled()) {
                                String msg = "Uploading (iteration=" + iteration + ") for async script on "
                                        + SshjTool.this.toString() + " (for " + getSummary() + ")";
                                if (iteration == 1) {
                                    LOG.trace(msg);
                                } else {
                                    LOG.debug(msg);
                                }
                            }
                            copyToServer(ImmutableMap.of("permissions", "0700"), scriptContents.getBytes(),
                                    scriptPath);
                            return true;
                        }
                    }).run();

            if (!uploadSuccess) {
                // Unexpected! Should have either returned true or have rethrown the exception; should never get false.
                String msg = "Unexpected state: repeated failure for async script upload on "
                        + SshjTool.this.toString() + " (" + getSummary() + ")";
                LOG.warn(msg + "; rethrowing");
                throw new IllegalStateException(msg);
            }

            // Execute script asynchronously
            int execResult = asInt(acquire(new ShellAction(buildRunScriptCommand(), out, err, execTimeout)),
                    -1);
            if (execResult != 0)
                return execResult;

            // Long polling to get the status
            try {
                final AtomicReference<Integer> result = new AtomicReference<Integer>();
                boolean success = Repeater
                        .create("async script long-poll on " + SshjTool.this.toString() + " (for "
                                + getSummary() + ")")
                        .backoffTo(maxDelayBetweenPolls).limitTimeTo(execTimeout)
                        .until(new Callable<Boolean>() {
                            @Override
                            public Boolean call() throws Exception {
                                iteration++;
                                if (LOG.isDebugEnabled())
                                    LOG.debug("Doing long-poll (iteration=" + iteration
                                            + ") for async script to complete on " + SshjTool.this.toString()
                                            + " (for " + getSummary() + ")");
                                Integer exitstatus = longPoll();
                                result.set(exitstatus);
                                return exitstatus != null;
                            }
                        }).run();

                if (!success) {
                    // Timed out
                    String msg = "Timeout for async script to complete on " + SshjTool.this.toString() + " ("
                            + getSummary() + ")";
                    LOG.warn(msg + "; rethrowing");
                    throw new TimeoutException(msg);
                }

                return result.get();

            } catch (Exception e) {
                LOG.debug("Problem polling for async script on " + SshjTool.this.toString() + " (for "
                        + getSummary() + "); rethrowing after deleting temporary files", e);
                throw Exceptions.propagate(e);
            } finally {
                // Delete the temporary files created (and the `tail -c` commands that might have been left behind by long-polls).
                // Using pollTimeout so doesn't wait forever, but waits for a reasonable (configurable) length of time.
                // TODO also execute this if the `buildRunScriptCommand` fails, as that might have left files behind?
                try {
                    int execDeleteResult = asInt(
                            acquire(new ShellAction(deleteTemporaryFilesCommand(), out, err, pollTimeout)), -1);
                    if (execDeleteResult != 0) {
                        LOG.debug("Problem deleting temporary files of async script on "
                                + SshjTool.this.toString() + " (for " + getSummary() + "): exit status "
                                + execDeleteResult);
                    }
                } catch (Exception e) {
                    Exceptions.propagateIfFatal(e);
                    LOG.debug("Problem deleting temporary files of async script on " + SshjTool.this.toString()
                            + " (for " + getSummary() + "); continuing", e);
                }
            }
        }

        Integer longPoll() throws IOException {
            // Long-polling to get stdout, stderr + exit status of async task.
            // If our long-poll disconnects, we will just re-execute.
            // We wrap the stdout/stderr so that we can get the size count. 
            // If we disconnect, we will pick up from that char of the stream.
            // TODO Additional stdout/stderr written by buildLongPollCommand() could interfere, 
            //      causing us to miss some characters.
            Duration nextPollTimeout = Duration.min(pollTimeout,
                    Duration.millis(execTimeout.toMilliseconds() - timer.elapsed(TimeUnit.MILLISECONDS)));
            CountingOutputStream countingOut = (out == null) ? null : new CountingOutputStream(out);
            CountingOutputStream countingErr = (err == null) ? null : new CountingOutputStream(err);
            List<String> pollCommand = buildLongPollCommand(stdoutCount, stderrCount, nextPollTimeout);
            Duration sshJoinTimeout = nextPollTimeout.add(Duration.TEN_SECONDS);
            ShellAction action = new ShellAction(pollCommand, countingOut, countingErr, sshJoinTimeout);

            int longPollResult;
            try {
                longPollResult = asInt(acquire(action, 3, nextPollTimeout), -1);
            } catch (RuntimeTimeoutException e) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Long-poll timed out on " + SshjTool.this.toString() + " (for " + getSummary()
                            + "): " + e);
                return null;
            }
            stdoutCount += (countingOut == null) ? 0 : countingOut.getCount();
            stderrCount += (countingErr == null) ? 0 : countingErr.getCount();

            if (longPollResult == 0) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Long-poll succeeded (exit status 0) on " + SshjTool.this.toString() + " (for "
                            + getSummary() + ")");
                return longPollResult; // success

            } else if (longPollResult == -1) {
                // probably a connection failure; try again
                if (LOG.isDebugEnabled())
                    LOG.debug("Long-poll received exit status -1; will retry on " + SshjTool.this.toString()
                            + " (for " + getSummary() + ")");
                return null;

            } else if (longPollResult == 125) {
                // 125 is the special code for timeout in long-poll (see buildLongPollCommand).
                // However, there is a tiny chance that the underlying command might have returned that exact exit code!
                // Don't treat a timeout as a "consecutiveSshFailure".
                if (LOG.isDebugEnabled())
                    LOG.debug("Long-poll received exit status " + longPollResult
                            + "; most likely timeout; retrieving actual status on " + SshjTool.this.toString()
                            + " (for " + getSummary() + ")");
                return retrieveStatusCommand();

            } else {
                // want to double-check whether this is the exit-code from the async process, or
                // some unexpected failure in our long-poll command.
                if (LOG.isDebugEnabled())
                    LOG.debug("Long-poll received exit status " + longPollResult
                            + "; retrieving actual status on " + SshjTool.this.toString() + " (for "
                            + getSummary() + ")");
                Integer result = retrieveStatusCommand();
                if (result != null) {
                    return result;
                }
            }

            consecutiveSshFailures++;
            if (consecutiveSshFailures > maxConsecutiveSshFailures) {
                LOG.warn("Aborting on " + consecutiveSshFailures
                        + " consecutive ssh connection errors (return -1) when polling for async script to complete on "
                        + SshjTool.this.toString() + " (" + getSummary() + ")");
                return -1;
            } else {
                LOG.info("Retrying after ssh connection error when polling for async script to complete on "
                        + SshjTool.this.toString() + " (" + getSummary() + ")");
                return null;
            }
        }

        Integer retrieveStatusCommand() throws IOException {
            // want to double-check whether this is the exit-code from the async process, or
            // some unexpected failure in our long-poll command.
            ByteArrayOutputStream statusOut = new ByteArrayOutputStream();
            ByteArrayOutputStream statusErr = new ByteArrayOutputStream();
            int statusResult = asInt(
                    acquire(new ShellAction(buildRetrieveStatusCommand(), statusOut, statusErr, execTimeout)),
                    -1);

            if (statusResult == 0) {
                // The status we retrieved really is valid; return it.
                // TODO How to ensure no additional output in stdout/stderr when parsing below?
                String statusOutStr = new String(statusOut.toByteArray()).trim();
                if (Strings.isEmpty(statusOutStr)) {
                    // suggests not yet completed; will retry with long-poll
                    if (LOG.isDebugEnabled())
                        LOG.debug(
                                "Long-poll retrieved status directly; command successful but no result available on "
                                        + SshjTool.this.toString() + " (for " + getSummary() + ")");
                    return null;
                } else {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Long-poll retrieved status directly; returning '" + statusOutStr + "' on "
                                + SshjTool.this.toString() + " (for " + getSummary() + ")");
                    int result = Integer.parseInt(statusOutStr);
                    return result;
                }

            } else if (statusResult == -1) {
                // probably a connection failure; try again with long-poll
                if (LOG.isDebugEnabled())
                    LOG.debug("Long-poll retrieving status directly received exit status -1; will retry on "
                            + SshjTool.this.toString() + " (for " + getSummary() + ")");
                return null;

            } else {
                if (out != null) {
                    out.write(toUTF8ByteArray(
                            "retrieving status failed with exit code " + statusResult + " (stdout follow)"));
                    out.write(statusOut.toByteArray());
                }
                if (err != null) {
                    err.write(toUTF8ByteArray(
                            "retrieving status failed with exit code " + statusResult + " (stderr follow)"));
                    err.write(statusErr.toByteArray());
                }

                if (LOG.isDebugEnabled())
                    LOG.debug("Long-poll retrieving status failed; returning " + statusResult + " on "
                            + SshjTool.this.toString() + " (for " + getSummary() + ")");
                return statusResult;
            }
        }
    }.run();
}

From source file:org.eclipse.hono.adapter.http.AbstractVertxBasedHttpProtocolAdapter.java

private void doUploadMessage(final RoutingContext ctx, final String tenant, final String deviceId,
        final Buffer payload, final String contentType, final Future<MessageSender> senderTracker,
        final String endpointName) {

    if (!isPayloadOfIndicatedType(payload, contentType)) {
        HttpUtils.badRequest(ctx,/*from   w  w w  .j  av  a2 s .  co m*/
                String.format("Content-Type %s does not match with the payload", contentType));
    } else {
        final Integer qosHeader = getQoSLevel(ctx.request().getHeader(Constants.HEADER_QOS_LEVEL));
        if (contentType == null) {
            HttpUtils.badRequest(ctx, String.format("%s header is missing", HttpHeaders.CONTENT_TYPE));
        } else if (qosHeader != null && qosHeader == HEADER_QOS_INVALID) {
            HttpUtils.badRequest(ctx, "Bad QoS Header Value");
        } else {

            final Device authenticatedDevice = getAuthenticatedDevice(ctx);
            final Future<JsonObject> tokenTracker = getRegistrationAssertion(tenant, deviceId,
                    authenticatedDevice);
            final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant);

            // AtomicBoolean to control if the downstream message was sent successfully
            final AtomicBoolean downstreamMessageSent = new AtomicBoolean(false);
            // AtomicReference to a Handler to be called to close an open command receiver link.
            final AtomicReference<Handler<Void>> closeLinkAndTimerHandlerRef = new AtomicReference<>();

            // Handler to be called with a received command. If the timer expired, null is provided as command.
            final Handler<Message> commandReceivedHandler = commandMessage -> {
                // reset the closeHandler reference, since it is not valid anymore at this time.
                closeLinkAndTimerHandlerRef.set(null);
                if (downstreamMessageSent.get()) {
                    // finish the request, since the response is now complete (command was added)
                    if (!ctx.response().closed()) {
                        ctx.response().end();
                    }
                }
            };

            CompositeFuture.all(tokenTracker, tenantConfigTracker, senderTracker).compose(ok -> {

                if (tenantConfigTracker.result().isAdapterEnabled(getTypeName())) {
                    final MessageSender sender = senderTracker.result();
                    final Message downstreamMessage = newMessage(
                            ResourceIdentifier.from(endpointName, tenant, deviceId),
                            sender.isRegistrationAssertionRequired(), ctx.request().uri(), contentType, payload,
                            tokenTracker.result(), HttpUtils.getTimeTilDisconnect(ctx));
                    customizeDownstreamMessage(downstreamMessage, ctx);

                    // first open the command receiver link (if needed)
                    return openCommandReceiverLink(ctx, tenant, deviceId, commandReceivedHandler)
                            .compose(closeLinkAndTimerHandler -> {
                                closeLinkAndTimerHandlerRef.set(closeLinkAndTimerHandler);

                                if (qosHeader == null) {
                                    return sender.send(downstreamMessage);
                                } else {
                                    return sender.sendAndWaitForOutcome(downstreamMessage);
                                }
                            });
                } else {
                    // this adapter is not enabled for the tenant
                    return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN));
                }
            }).compose(delivery -> {
                LOG.trace(
                        "successfully processed message for device [tenantId: {}, deviceId: {}, endpoint: {}]",
                        tenant, deviceId, endpointName);
                metrics.incrementProcessedHttpMessages(endpointName, tenant);
                ctx.response().setStatusCode(HttpURLConnection.HTTP_ACCEPTED);
                downstreamMessageSent.set(true);

                // if no command timer was created, the request now can be responded
                if (closeLinkAndTimerHandlerRef.get() == null) {
                    ctx.response().end();
                }

                return Future.succeededFuture();

            }).recover(t -> {

                LOG.debug("cannot process message for device [tenantId: {}, deviceId: {}, endpoint: {}]",
                        tenant, deviceId, endpointName, t);

                cancelResponseTimer(closeLinkAndTimerHandlerRef);

                if (ClientErrorException.class.isInstance(t)) {
                    final ClientErrorException e = (ClientErrorException) t;
                    ctx.fail(e.getErrorCode());
                } else {
                    metrics.incrementUndeliverableHttpMessages(endpointName, tenant);
                    HttpUtils.serviceUnavailable(ctx, 2);
                }
                return Future.failedFuture(t);
            });
        }
    }
}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testStart() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();

    URI uri = new URI("ws://localhost:" + port + "/composite");
    WebSocketStompClient stompClient = new WebSocketStompClient(uri, this.headers, sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());

    stompClient.connect(new StompMessageHandler() {

        private StompSession stompSession;

        @Override//from   w  w  w.  j a  v a2s.  c  o  m
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            String topicUuid = simulateJoinEvent();

            this.stompSession.subscribe("/user/queue/device", null);
            this.stompSession.subscribe(String.format("/topic/%s", topicUuid), null);

            try {
                // send a message to the start endpoint
                HashMap<String, Object> start = new HashMap<String, Object>();
                start.put("data", "Some Data");
                start.put("type", "start");
                this.stompSession.send(String.format("/app/%s", topicUuid), start);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").exists(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "start");
                new JsonPathExpectationsHelper("serverTime").exists(json);
            } catch (Throwable t) {
                failure.set(t);
            } finally {
                this.stompSession.disconnect();
                latch.countDown();
            }
        }

        @Override
        public void handleError(Message<byte[]> message) {
            StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
            String error = "[Producer] " + accessor.getShortLogMessage(message.getPayload());
            logger.error(error);
            failure.set(new Exception(error));
        }

        @Override
        public void handleReceipt(String receiptId) {
        }

        @Override
        public void afterDisconnected() {
        }
    });

    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("Start response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}

From source file:io.hummer.util.ws.WebServiceClient.java

private InvocationResult doInvokeGET(String parameters, Map<String, String> httpHeaders, int retries,
        long connectTimeoutMS, long readTimeoutMS, boolean doUseCache) throws Exception {
    if (retries < 0)
        throw new Exception("Invocation to " + endpointURL + " failed: " + xmlUtil.toString(parameters));

    String host = new URL(endpointURL).getHost();
    if (!lastRequestedHosts.containsKey(host)) {
        lastRequestedHosts.put(host, new AtomicLong());
    }/*from  ww  w  . j  a va2  s. c  o m*/
    Object lockForTargetHost = lastRequestedHosts.get(host);

    parameters = parameters.trim();
    String urlString = endpointURL;
    if (!strUtil.isEmpty(parameters)) {
        String separator = endpointURL.contains("?") ? "&" : "?";
        urlString = endpointURL + separator + parameters;
    }

    if (doUseCache) {
        /** retrieve result from document cache */
        CacheEntry existing = cache.get(urlString);
        if (existing != null && !strUtil.isEmpty(existing.value)) {
            String valueShort = (String) existing.value;
            if (valueShort.length() > 200)
                valueShort = valueShort.substring(0, 200) + "...";
            AtomicReference<Element> eRef = new AtomicReference<Element>();
            Parallelization.warnIfNoResultAfter(eRef, "! Client could not convert element ("
                    + existing.value.length() + " bytes) within 15 seconds: " + valueShort, 15 * 1000);
            Parallelization.warnIfNoResultAfter(eRef, "! Client could not convert element ("
                    + existing.value.length() + " bytes) within 40 seconds: " + valueShort, 40 * 1000);
            Element e = xmlUtil.toElement((String) existing.value);
            eRef.set(e);
            logger.info("Result exists in cache for URL " + urlString + " - " + e + " - "
                    + this.xmlUtil.toString().length());
            return new InvocationResult(e);
        }
    }

    pauseToAvoidSpamming();

    URL url = new URL(urlString);
    URLConnection c = url.openConnection();
    c.setConnectTimeout((int) connectTimeoutMS);
    c.setReadTimeout((int) readTimeoutMS);
    logger.info("Retrieving data from service using GET: " + url);

    String tmpID = PerformanceInterceptor.event(EventType.START_HTTP_GET);
    for (String key : httpHeaders.keySet()) {
        c.setRequestProperty(key, httpHeaders.get(key));
    }
    StringBuilder b = new StringBuilder();
    synchronized (lockForTargetHost) {
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream()));
            String temp;
            while ((temp = r.readLine()) != null) {
                b.append(temp);
                b.append("\n");
            }
        } catch (Exception e) {
            logger.info("Could not GET page with regular URLConnection, trying HtmlUnit..: " + e);
            b = getPageUsingHtmlUnit(urlString, httpHeaders, readTimeoutMS, null);
        }
    }

    PerformanceInterceptor.event(EventType.FINISH_HTTP_GET, tmpID);

    String tmpID1 = PerformanceInterceptor.event(EventType.START_RESPONSE_TO_XML);
    String result = b.toString().trim();
    if (!result.startsWith("<") || !result.endsWith(">")) { // wrap non-xml results (e.g., CSV files)
        StringBuilder sb = new StringBuilder("<doc><![CDATA[");
        sb.append(result);
        sb.append("]]></doc>");
        result = sb.toString();
    }

    String tmpID2 = PerformanceInterceptor.event(EventType.START_STRING_TO_XML);
    Element resultElement = xmlUtil.toElement(result);
    PerformanceInterceptor.event(EventType.FINISH_STRING_TO_XML, tmpID2);

    if (doUseCache) {
        /** put result element to document cache */
        cache.putWithoutWaiting(urlString, xmlUtil.toString(resultElement, true));
    }

    InvocationResult invResult = new InvocationResult(resultElement);
    PerformanceInterceptor.event(EventType.FINISH_RESPONSE_TO_XML, tmpID1);

    return invResult;
}

From source file:io.fabric8.maven.core.service.openshift.OpenshiftBuildService.java

private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build)
        throws MojoExecutionException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch logTerminateLatch = new CountDownLatch(1);
    final String buildName = KubernetesHelper.getName(build);

    final AtomicReference<Build> buildHolder = new AtomicReference<>();

    // Don't query for logs directly, Watch over the build pod:
    waitUntilPodIsReady(buildName + "-build", 20, log);
    log.info("Waiting for build " + buildName + " to complete...");
    try (LogWatch logWatch = client.pods().withName(buildName + "-build").watchLog()) {
        KubernetesClientUtil.printLogsAsync(logWatch, "Failed to tail build log", logTerminateLatch, log);
        Watcher<Build> buildWatcher = getBuildWatcher(latch, buildName, buildHolder);
        try (Watch watcher = client.builds().withName(buildName).watch(buildWatcher)) {
            // Check if the build is already finished to avoid waiting indefinitely
            Build lastBuild = client.builds().withName(buildName).get();
            if (Builds.isFinished(KubernetesResourceUtil.getBuildStatusPhase(lastBuild))) {
                log.debug("Build %s is already finished", buildName);
                buildHolder.set(lastBuild);
                latch.countDown();//from  w w w  . j  a  va 2s  .c o  m
            }

            waitUntilBuildFinished(latch);
            logTerminateLatch.countDown();

            build = buildHolder.get();
            if (build == null) {
                log.debug("Build watcher on %s was closed prematurely", buildName);
                build = client.builds().withName(buildName).get();
            }
            String status = KubernetesResourceUtil.getBuildStatusPhase(build);
            if (Builds.isFailed(status) || Builds.isCancelled(status)) {
                throw new MojoExecutionException("OpenShift Build " + buildName + " error: "
                        + KubernetesResourceUtil.getBuildStatusReason(build));
            }

            if (!Builds.isFinished(status)) {
                log.warn(
                        "Could not wait for the completion of build %s. It may be  may be still running (status=%s)",
                        buildName, status);
            } else {
                log.info("Build %s in status %s", buildName, status);
            }
        }
    }
}

From source file:de.schildbach.pte.AbstractHafasLegacyProvider.java

protected final QueryDeparturesResult xmlStationBoard(final HttpUrl url, final String stationId)
        throws IOException {
    final String normalizedStationId = normalizeStationId(stationId);
    final AtomicReference<QueryDeparturesResult> result = new AtomicReference<>();

    httpClient.getInputStream(new HttpClient.Callback() {

        @Override/*from  w  ww. j  a va 2s .c  o  m*/
        public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException {
            StringReplaceReader reader = null;
            String firstChars = null;

            // work around unparsable XML
            reader = new StringReplaceReader(body.charStream(), " & ", " &amp; ");
            reader.replace("<b>", " ");
            reader.replace("</b>", " ");
            reader.replace("<u>", " ");
            reader.replace("</u>", " ");
            reader.replace("<i>", " ");
            reader.replace("</i>", " ");
            reader.replace("<br />", " ");
            reader.replace(" ->", " &#x2192;"); // right arrow
            reader.replace(" <-", " &#x2190;"); // left arrow
            reader.replace(" <> ", " &#x2194; "); // left-right arrow
            addCustomReplaces(reader);

            try {
                final XmlPullParserFactory factory = XmlPullParserFactory
                        .newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
                final XmlPullParser pp = factory.newPullParser();
                pp.setInput(reader);

                pp.nextTag();

                final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT);
                final QueryDeparturesResult r = new QueryDeparturesResult(header);

                if (XmlPullUtil.test(pp, "Err")) {
                    final String code = XmlPullUtil.attr(pp, "code");
                    final String text = XmlPullUtil.attr(pp, "text");

                    if (code.equals("H730")) {
                        result.set(new QueryDeparturesResult(header,
                                QueryDeparturesResult.Status.INVALID_STATION));
                        return;
                    }
                    if (code.equals("H890")) {
                        r.stationDepartures.add(
                                new StationDepartures(new Location(LocationType.STATION, normalizedStationId),
                                        Collections.<Departure>emptyList(), null));
                        result.set(r);
                        return;
                    }
                    throw new IllegalArgumentException("unknown error " + code + ", " + text);
                }

                String[] stationPlaceAndName = null;

                if (stationBoardHasStationTable)
                    XmlPullUtil.enter(pp, "StationTable");
                else
                    checkState(!XmlPullUtil.test(pp, "StationTable"));

                if (stationBoardHasLocation) {
                    XmlPullUtil.require(pp, "St");

                    final String evaId = XmlPullUtil.attr(pp, "evaId");
                    if (evaId != null) {
                        if (!evaId.equals(normalizedStationId))
                            throw new IllegalStateException(
                                    "stationId: " + normalizedStationId + ", evaId: " + evaId);

                        final String name = XmlPullUtil.attr(pp, "name");
                        if (name != null)
                            stationPlaceAndName = splitStationName(name.trim());
                    }
                    XmlPullUtil.requireSkip(pp, "St");
                } else {
                    checkState(!XmlPullUtil.test(pp, "St"));
                }

                while (XmlPullUtil.test(pp, "Journey")) {
                    final String fpTime = XmlPullUtil.attr(pp, "fpTime");
                    final String fpDate = XmlPullUtil.attr(pp, "fpDate");
                    final String delay = XmlPullUtil.attr(pp, "delay");
                    final String eDelay = XmlPullUtil.optAttr(pp, "e_delay", null);
                    final String platform = XmlPullUtil.optAttr(pp, "platform", null);
                    // TODO newpl
                    final String targetLoc = XmlPullUtil.optAttr(pp, "targetLoc", null);
                    // TODO hafasname
                    final String dirnr = XmlPullUtil.optAttr(pp, "dirnr", null);
                    final String prod = XmlPullUtil.attr(pp, "prod");
                    final String classStr = XmlPullUtil.optAttr(pp, "class", null);
                    final String dir = XmlPullUtil.optAttr(pp, "dir", null);
                    final String capacityStr = XmlPullUtil.optAttr(pp, "capacity", null);
                    final String depStation = XmlPullUtil.optAttr(pp, "depStation", null);
                    final String delayReason = XmlPullUtil.optAttr(pp, "delayReason", null);
                    // TODO is_reachable
                    // TODO disableTrainInfo
                    // TODO lineFG/lineBG (ZVV)
                    final String administration = normalizeLineAdministration(
                            XmlPullUtil.optAttr(pp, "administration", null));

                    if (!"cancel".equals(delay) && !"cancel".equals(eDelay)) {
                        final Calendar plannedTime = new GregorianCalendar(timeZone);
                        plannedTime.clear();
                        parseXmlStationBoardDate(plannedTime, fpDate);
                        parseXmlStationBoardTime(plannedTime, fpTime);

                        final Calendar predictedTime;
                        if (eDelay != null) {
                            predictedTime = new GregorianCalendar(timeZone);
                            predictedTime.setTimeInMillis(plannedTime.getTimeInMillis());
                            predictedTime.add(Calendar.MINUTE, Integer.parseInt(eDelay));
                        } else if (delay != null) {
                            final Matcher m = P_XML_STATION_BOARD_DELAY.matcher(delay);
                            if (m.matches()) {
                                if (m.group(1) != null) {
                                    predictedTime = new GregorianCalendar(timeZone);
                                    predictedTime.setTimeInMillis(plannedTime.getTimeInMillis());
                                    predictedTime.add(Calendar.MINUTE, Integer.parseInt(m.group(1)));
                                } else {
                                    predictedTime = null;
                                }
                            } else {
                                throw new RuntimeException("cannot parse delay: '" + delay + "'");
                            }
                        } else {
                            predictedTime = null;
                        }

                        final Position position = parsePosition(ParserUtils.resolveEntities(platform));

                        final String destinationName;
                        if (dir != null)
                            destinationName = dir.trim();
                        else if (targetLoc != null)
                            destinationName = targetLoc.trim();
                        else
                            destinationName = null;

                        final Location destination;
                        if (dirnr != null) {
                            final String[] destinationPlaceAndName = splitStationName(destinationName);
                            destination = new Location(LocationType.STATION, dirnr, destinationPlaceAndName[0],
                                    destinationPlaceAndName[1]);
                        } else {
                            destination = new Location(LocationType.ANY, null, null, destinationName);
                        }

                        final Line prodLine = parseLineAndType(prod);
                        final Line line;
                        if (classStr != null) {
                            final Product product = intToProduct(Integer.parseInt(classStr));
                            if (product == null)
                                throw new IllegalArgumentException();
                            // could check for type consistency here
                            final Set<Attr> attrs = prodLine.attrs;
                            if (attrs != null)
                                line = newLine(administration, product, prodLine.label, null,
                                        attrs.toArray(new Line.Attr[0]));
                            else
                                line = newLine(administration, product, prodLine.label, null);
                        } else {
                            final Set<Attr> attrs = prodLine.attrs;
                            if (attrs != null)
                                line = newLine(administration, prodLine.product, prodLine.label, null,
                                        attrs.toArray(new Line.Attr[0]));
                            else
                                line = newLine(administration, prodLine.product, prodLine.label, null);
                        }

                        final int[] capacity;
                        if (capacityStr != null && !"0|0".equals(capacityStr)) {
                            final String[] capacityParts = capacityStr.split("\\|");
                            capacity = new int[] { Integer.parseInt(capacityParts[0]),
                                    Integer.parseInt(capacityParts[1]) };
                        } else {
                            capacity = null;
                        }

                        final String message;
                        if (delayReason != null) {
                            final String msg = delayReason.trim();
                            message = msg.length() > 0 ? msg : null;
                        } else {
                            message = null;
                        }

                        final Departure departure = new Departure(plannedTime.getTime(),
                                predictedTime != null ? predictedTime.getTime() : null, line, position,
                                destination, capacity, message);

                        final Location location;
                        if (!stationBoardCanDoEquivs || depStation == null) {
                            location = new Location(LocationType.STATION, normalizedStationId,
                                    stationPlaceAndName != null ? stationPlaceAndName[0] : null,
                                    stationPlaceAndName != null ? stationPlaceAndName[1] : null);
                        } else {
                            final String[] depPlaceAndName = splitStationName(depStation);
                            location = new Location(LocationType.STATION, null, depPlaceAndName[0],
                                    depPlaceAndName[1]);
                        }

                        StationDepartures stationDepartures = findStationDepartures(r.stationDepartures,
                                location);
                        if (stationDepartures == null) {
                            stationDepartures = new StationDepartures(location, new ArrayList<Departure>(8),
                                    null);
                            r.stationDepartures.add(stationDepartures);
                        }

                        stationDepartures.departures.add(departure);
                    }

                    XmlPullUtil.requireSkip(pp, "Journey");
                }

                if (stationBoardHasStationTable)
                    XmlPullUtil.exit(pp, "StationTable");

                XmlPullUtil.requireEndDocument(pp);

                // sort departures
                for (final StationDepartures stationDepartures : r.stationDepartures)
                    Collections.sort(stationDepartures.departures, Departure.TIME_COMPARATOR);

                result.set(r);
            } catch (final XmlPullParserException x) {
                throw new ParserException("cannot parse xml: " + firstChars, x);
            }
        }
    }, url);

    return result.get();
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.localworkspace.LocalWorkspaceScanner.java

private void scanPartTwo() {
    // The items in this set may be folders or files. They have local
    // version rows, but no local item on disk. We will mark the local
    // version row as MissingOnDisk. The row will persist until the
    // reconcile before the next Get. If it is still MissingOnDisk then, we
    // will remove the row and reconcile that delete to the server so that
    // the item will come back.
    for (final WorkspaceLocalItem lvEntry : markForRemoval) {
        // If the item is in the exclusion list for this scanner, do not
        // process the item.
        if (skippedItems.contains(lvEntry.getLocalItem())) {
            continue;
        }/*from w  ww .j a  v a 2 s  .com*/

        final LocalPendingChange pcEntry = pc.getByLocalVersion(lvEntry);

        // Pending adds do not have their local version row removed marked
        // as MissingFromDisk.
        if (null == pcEntry || !pcEntry.isAdd()) {
            if (!lvEntry.isMissingOnDisk()) {
                lv.removeByServerItem(lvEntry.getServerItem(), lvEntry.isCommitted(), false);
                lvEntry.setMissingOnDisk(true);
                lv.add(lvEntry);
            }

            final String targetServerItem = pc.getTargetServerItemForLocalVersion(lvEntry);

            if (lvEntry.isCommitted() && null == pcEntry
                    && null != pc.getByTargetServerItem(targetServerItem)) {
                // if we don't have a pending change against the item, but
                // have a pending change against the target i.e. add, branch
                // or rename we don't want mark this as a candidate as there
                // is no way to actually pend the delete without moving the
                // item out of the way, we will let check-in take care of it
                // with a namespace conflict
                continue;
            }

            final LocalPendingChange newChange = new LocalPendingChange(lvEntry, targetServerItem,
                    ChangeType.DELETE);
            newChange.setCandidate(true);
            addCandidateChange(newChange);
        }
    }

    // The items in this set may be folders or files. They have local
    // version rows, and were previously marked as MissingOnDisk. However,
    // their local item has reappeared. We will remove the MissingOnDisk
    // bit.
    for (final WorkspaceLocalItem lvEntry : reappearedOnDisk) {
        // If the item is in the exclusion list for this scanner, do not
        // process the item.
        if (skippedItems.contains(lvEntry.getLocalItem())) {
            continue;
        }

        lv.removeByServerItem(lvEntry.getServerItem(), lvEntry.isCommitted(), false);
        lvEntry.setMissingOnDisk(false);
        lv.add(lvEntry);
    }

    // The items in this set are all files. They were identified as having
    // identical content as the workspace version, but a different
    // timestamp. We will update the local version table to contain the new
    // timestamp. Additionally, if the item has a pending edit, we will
    // selective-undo the edit.

    for (final KeyValuePair<String, Long> undoOp : toUndo) {
        final String localItem = undoOp.getKey();
        final long onDiskModifiedTime = undoOp.getValue();

        if (skippedItems.contains(localItem)) {
            continue;
        }

        final WorkspaceLocalItem lvEntry = lv.getByLocalItem(localItem);

        // Bring the last-modified time on the item forward to match the
        // latest scan.
        if (lvEntry.getLastModifiedTime() != onDiskModifiedTime) {
            lvEntry.setLastModifiedTime(onDiskModifiedTime);
            lv.setDirty(true);
        }

        final LocalPendingChange pcEntry = pc.getByLocalVersion(lvEntry);

        // If the item has a pending edit, undo the pending edit, because
        // the content is identical to the workspace version. The only
        // uncommitted server items which can have their pending change
        // undone are those with changetype "branch, edit".
        if (null != pcEntry && pcEntry.isEdit() && !pcEntry.isAdd()
                && (!pcEntry.isEncoding() || pcEntry.getEncoding() == lvEntry.getEncoding())) {
            final AtomicReference<Failure[]> outFailures = new AtomicReference<Failure[]>();
            final AtomicBoolean outOnlineOperationRequired = new AtomicBoolean();
            final List<LocalPendingChange> pcEntries = new ArrayList<LocalPendingChange>(1);
            pcEntries.add(pcEntry);

            // The GetOperations returned are not processed.
            final GetOperation[] getOps = LocalDataAccessLayer.undoPendingChanges(
                    LocalWorkspaceTransaction.getCurrent().getWorkspace(), wp, lv, pc, pcEntries,
                    ChangeType.EDIT, outFailures, outOnlineOperationRequired);

            // No renames or locks are being undone,
            Check.isTrue(!outOnlineOperationRequired.get() && (1 == getOps.length),
                    "!outOnlineOperationRequired.get() && (1 == getOps.length)"); //$NON-NLS-1$

            // Since we've modified the pending changes table in a silent
            // way, we want to set the flag on the transaction we're a part
            // of that indicates the PendingChangesChanged event should be
            // raised for this workspace, once the transaction completes.
            LocalWorkspaceTransaction.getCurrent().setRaisePendingChangesChanged(true);
        }
    }
}