3232import com .jcabi .aspects .Immutable ;
3333import com .jcabi .aspects .Loggable ;
3434import com .jcabi .http .Request ;
35- import com .jcabi .http .response .RestResponse ;
3635import com .jcabi .log .Logger ;
3736import com .jcabi .manifests .Manifests ;
3837import java .io .IOException ;
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" })
5756final 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}
0 commit comments