Skip to content

Commit e7dc8d5

Browse files
author
Yegor Bugayenko
committed
#1 implemented
1 parent b7b8f08 commit e7dc8d5

File tree

5 files changed

+169
-22
lines changed

5 files changed

+169
-22
lines changed

pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,94 @@
194194
</plugin>
195195
</plugins>
196196
</build>
197+
<profiles>
198+
<profile>
199+
<id>pdd</id>
200+
<build>
201+
<plugins>
202+
<plugin>
203+
<groupId>com.rempl.plugins</groupId>
204+
<artifactId>rempl-maven-plugin</artifactId>
205+
<version>1.4</version>
206+
<executions>
207+
<execution>
208+
<phase>generate-resources</phase>
209+
<goals>
210+
<goal>read</goal>
211+
</goals>
212+
<configuration>
213+
<profiles>
214+
<profile>
215+
<type>com.rempl.pdd.PDDProfile</type>
216+
<arguments>
217+
<argument>${basedir}</argument>
218+
<argument>**</argument>
219+
<argument>target/**,**/target,.git/**</argument>
220+
</arguments>
221+
</profile>
222+
</profiles>
223+
</configuration>
224+
</execution>
225+
</executions>
226+
<dependencies>
227+
<dependency>
228+
<groupId>com.rempl.profiles</groupId>
229+
<artifactId>rempl-pdd-profile</artifactId>
230+
<version>1.7</version>
231+
</dependency>
232+
</dependencies>
233+
</plugin>
234+
<plugin>
235+
<artifactId>maven-site-plugin</artifactId>
236+
<executions>
237+
<execution>
238+
<id>pdd-site</id>
239+
<phase>process-resources</phase>
240+
<goals>
241+
<goal>site</goal>
242+
</goals>
243+
<configuration>
244+
<generateProjectInfo>false</generateProjectInfo>
245+
<reportPlugins combine.self="override">
246+
<plugin>
247+
<groupId>com.rempl.plugins</groupId>
248+
<artifactId>rempl-maven-plugin</artifactId>
249+
<configuration>
250+
<reports>
251+
<report>
252+
<type>com.rempl.pdd.SummaryReport</type>
253+
</report>
254+
</reports>
255+
</configuration>
256+
</plugin>
257+
</reportPlugins>
258+
</configuration>
259+
</execution>
260+
</executions>
261+
</plugin>
262+
<plugin>
263+
<groupId>com.bazaarvoice.maven.plugins</groupId>
264+
<artifactId>s3-upload-maven-plugin</artifactId>
265+
<version>1.2</version>
266+
<executions>
267+
<execution>
268+
<id>pdd-deploy</id>
269+
<phase>deploy</phase>
270+
<goals>
271+
<goal>s3-upload</goal>
272+
</goals>
273+
<configuration>
274+
<bucketName>${pdd.s3.bucket}</bucketName>
275+
<accessKey>${pdd.s3.key}</accessKey>
276+
<secretKey>${pdd.s3.secret}</secretKey>
277+
<source>${project.build.directory}/site/rempl/com.rempl.pdd.SummaryReport/index.xml</source>
278+
<destination>${project.artifactId}.xml</destination>
279+
</configuration>
280+
</execution>
281+
</executions>
282+
</plugin>
283+
</plugins>
284+
</build>
285+
</profile>
286+
</profiles>
197287
</project>

src/main/java/co/stateful/Lock.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,22 @@ public interface Lock {
6464
*/
6565
void unlock() throws IOException;
6666

67+
/**
68+
* Lock with label.
69+
* @param label Label to attach
70+
* @return TRUE if success, FALSE otherwise
71+
* @throws IOException If any problem inside
72+
* @since 0.11
73+
*/
74+
boolean lock(String label) throws IOException;
75+
76+
/**
77+
* Unlock, if label matches.
78+
* @param label Label to attach
79+
* @return TRUE if success, FALSE otherwise (label doesn't match)
80+
* @throws IOException If any problem inside
81+
* @since 0.11
82+
*/
83+
boolean unlock(String label) throws IOException;
84+
6785
}

src/main/java/co/stateful/RtLock.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.jcabi.aspects.Immutable;
3333
import com.jcabi.aspects.Loggable;
3434
import com.jcabi.http.Request;
35-
import com.jcabi.http.response.RestResponse;
3635
import com.jcabi.log.Logger;
3736
import com.jcabi.manifests.Manifests;
3837
import java.io.IOException;
@@ -52,14 +51,14 @@
5251
*/
5352
@Immutable
5453
@Loggable(Loggable.DEBUG)
55-
@ToString(of = "label", includeFieldNames = false)
56-
@EqualsAndHashCode(of = { "lrequest", "urequest" })
54+
@ToString(of = "lock", includeFieldNames = false)
55+
@EqualsAndHashCode(of = { "lock", "lrequest", "urequest" })
5756
final class RtLock implements Lock {
5857

5958
/**
6059
* Its name.
6160
*/
62-
private final transient String label;
61+
private final transient String lock;
6362

6463
/**
6564
* Lock request.
@@ -78,51 +77,63 @@ final class RtLock implements Lock {
7877
* @param ureq Unlock request
7978
*/
8079
RtLock(final String name, final Request lreq, final Request ureq) {
81-
this.label = name;
80+
this.lock = name;
8281
this.lrequest = lreq;
8382
this.urequest = ureq;
8483
}
8584

8685
@Override
8786
public String name() {
88-
return this.label;
87+
return this.lock;
8988
}
9089

9190
@Override
9291
public boolean lock() throws IOException {
93-
final String marker = String.format(
94-
"co.stateful/java-sdk %s/%s; %s; Java %s; %s %s",
95-
Manifests.read("Sttc-Version"),
96-
Manifests.read("Sttc-Revision"),
97-
DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date()),
98-
System.getProperty("java.version"),
99-
System.getProperty("os.name"),
100-
System.getProperty("os.version")
92+
return this.lock(
93+
String.format(
94+
"co.stateful/java-sdk %s/%s; %s; Java %s; %s %s",
95+
Manifests.read("Sttc-Version"),
96+
Manifests.read("Sttc-Revision"),
97+
DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date()),
98+
System.getProperty("java.version"),
99+
System.getProperty("os.name"),
100+
System.getProperty("os.version")
101+
)
101102
);
103+
}
104+
105+
@Override
106+
public void unlock() throws IOException {
107+
this.unlock("");
108+
}
109+
110+
@Override
111+
public boolean lock(final String label) throws IOException {
102112
final long start = System.currentTimeMillis();
103113
final boolean locked = this.lrequest
104-
.body().formParam("label", marker).back()
114+
.body().formParam("label", label).back()
105115
.fetch()
106116
.status() == HttpURLConnection.HTTP_SEE_OTHER;
107117
Logger.info(
108118
this, "lock of \"%s\" is %s in %[ms]s",
109-
this.label, Boolean.toString(locked),
119+
this.lock, Boolean.toString(locked),
110120
System.currentTimeMillis() - start
111121
);
112122
return locked;
113123
}
114124

115125
@Override
116-
public void unlock() throws IOException {
126+
public boolean unlock(final String label) throws IOException {
117127
final long start = System.currentTimeMillis();
118-
this.urequest
128+
final boolean unlocked = this.urequest
129+
.uri().queryParam("label", label).back()
119130
.fetch()
120-
.as(RestResponse.class)
121-
.assertStatus(HttpURLConnection.HTTP_SEE_OTHER);
131+
.status() == HttpURLConnection.HTTP_SEE_OTHER;
122132
Logger.info(
123-
this, "unlocked \"%s\" in %[ms]s",
124-
this.label, System.currentTimeMillis() - start
133+
this, "unlocked \"%s\" in %[ms]s: %B",
134+
this.lock, System.currentTimeMillis() - start, unlocked
125135
);
136+
return unlocked;
126137
}
127138

128139
}

src/main/java/co/stateful/mock/MkLock.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,14 @@ public boolean lock() {
6262
public void unlock() {
6363
// nothing to do here
6464
}
65+
66+
@Override
67+
public boolean lock(final String label) {
68+
return true;
69+
}
70+
71+
@Override
72+
public boolean unlock(final String label) {
73+
return true;
74+
}
6575
}

src/main/java/co/stateful/retry/ReLock.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,22 @@ public boolean lock() throws IOException {
9191
public void unlock() throws IOException {
9292
this.origin.unlock();
9393
}
94+
95+
@Override
96+
@RetryOnFailure(
97+
verbose = false, attempts = Tv.TWENTY,
98+
delay = Tv.FIVE, unit = TimeUnit.SECONDS
99+
)
100+
public boolean lock(final String label) throws IOException {
101+
return this.origin.lock(label);
102+
}
103+
104+
@Override
105+
@RetryOnFailure(
106+
verbose = false, attempts = Tv.TWENTY,
107+
delay = Tv.FIVE, unit = TimeUnit.SECONDS
108+
)
109+
public boolean unlock(final String label) throws IOException {
110+
return this.origin.unlock(label);
111+
}
94112
}

0 commit comments

Comments
 (0)