| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RtCounter |
|
| 1.0;1 | ||||
| RtCounter$AjcClosure1 |
|
| 1.0;1 | ||||
| RtCounter$AjcClosure3 |
|
| 1.0;1 | ||||
| RtCounter$AjcClosure5 |
|
| 1.0;1 |
| 1 | 2 | /** |
| 2 | * Copyright (c) 2014-2016, stateful.co | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the stateful.co nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package co.stateful; | |
| 31 | ||
| 32 | import com.jcabi.aspects.Immutable; | |
| 33 | import com.jcabi.aspects.Loggable; | |
| 34 | import com.jcabi.http.Request; | |
| 35 | import com.jcabi.http.response.RestResponse; | |
| 36 | import com.jcabi.http.response.XmlResponse; | |
| 37 | import com.jcabi.log.Logger; | |
| 38 | import java.io.IOException; | |
| 39 | import java.net.HttpURLConnection; | |
| 40 | import javax.ws.rs.core.HttpHeaders; | |
| 41 | import javax.ws.rs.core.MediaType; | |
| 42 | import lombok.EqualsAndHashCode; | |
| 43 | import lombok.ToString; | |
| 44 | ||
| 45 | /** | |
| 46 | * Counter. | |
| 47 | * | |
| 48 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
| 49 | * @version $Id$ | |
| 50 | * @since 0.1 | |
| 51 | * @checkstyle MultipleStringLiteralsCheck (500 lines) | |
| 52 | */ | |
| 53 | @Immutable | |
| 54 | @Loggable(Loggable.DEBUG) | |
| 55 | 0 | @ToString(of = "label", includeFieldNames = false) |
| 56 | 0 | @EqualsAndHashCode(of = { "label", "request" }) |
| 57 | final class RtCounter implements Counter { | |
| 58 | ||
| 59 | /** | |
| 60 | * Its name. | |
| 61 | */ | |
| 62 | private final transient String label; | |
| 63 | ||
| 64 | /** | |
| 65 | * Home request. | |
| 66 | */ | |
| 67 | private final transient Request request; | |
| 68 | ||
| 69 | /** | |
| 70 | * Ctor. | |
| 71 | * @param name Name of it | |
| 72 | * @param req Home page request | |
| 73 | */ | |
| 74 | 2 | RtCounter(final String name, final Request req) { |
| 75 | 2 | this.label = name; |
| 76 | 2 | this.request = req; |
| 77 | 2 | } |
| 78 | ||
| 79 | @Override | |
| 80 | public String name() { | |
| 81 | 0 | return this.label; |
| 82 | } | |
| 83 | ||
| 84 | @Override | |
| 85 | public void set(final long value) throws IOException { | |
| 86 | 2 | final long start = System.currentTimeMillis(); |
| 87 | 1 | this.front("set").method(Request.PUT) |
| 88 | .uri().queryParam("value", value).back() | |
| 89 | .fetch() | |
| 90 | .as(RestResponse.class) | |
| 91 | .assertStatus(HttpURLConnection.HTTP_OK); | |
| 92 | 0 | Logger.info( |
| 93 | this, "counter \"%s\" set to %d in %[ms]s", | |
| 94 | this.label, value, | |
| 95 | System.currentTimeMillis() - start | |
| 96 | ); | |
| 97 | 0 | } |
| 98 | ||
| 99 | @Override | |
| 100 | public long incrementAndGet(final long delta) throws IOException { | |
| 101 | 2 | final long start = System.currentTimeMillis(); |
| 102 | 1 | final long value = Long.parseLong( |
| 103 | this.front("increment").method(Request.GET) | |
| 104 | .uri().queryParam("value", delta).back() | |
| 105 | .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) | |
| 106 | .fetch() | |
| 107 | .as(RestResponse.class) | |
| 108 | .assertStatus(HttpURLConnection.HTTP_OK) | |
| 109 | .body() | |
| 110 | ); | |
| 111 | 0 | Logger.info( |
| 112 | this, "counter \"%s\" incremented by %d to %d in %[ms]s", | |
| 113 | this.label, delta, value, | |
| 114 | System.currentTimeMillis() - start | |
| 115 | ); | |
| 116 | 0 | return value; |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Get front request. | |
| 121 | * @param ops Operation | |
| 122 | * @return Request | |
| 123 | * @throws IOException If fails | |
| 124 | */ | |
| 125 | private Request front(final String ops) throws IOException { | |
| 126 | 2 | return this.request.fetch() |
| 127 | .as(RestResponse.class) | |
| 128 | .assertStatus(HttpURLConnection.HTTP_OK) | |
| 129 | .as(XmlResponse.class) | |
| 130 | .rel( | |
| 131 | String.format( | |
| 132 | // @checkstyle LineLength (1 line) | |
| 133 | "/page/counters/counter[name='%s']/links/link[@rel='%s']/@href", | |
| 134 | this.label, ops | |
| 135 | ) | |
| 136 | ); | |
| 137 | } | |
| 138 | ||
| 139 | } |