Skip to content

Commit

Permalink
Version 1.1.1
Browse files Browse the repository at this point in the history
Specify nullable annotations directly, package-info.java annotations are not sufficient
  • Loading branch information
sanyarnd committed May 22, 2019
1 parent 42423aa commit 9950703
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 60 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.1
- Specify nullable annotations directly,
`package-info.java` annotations are not sufficient

# 1.1
- `JSR305` was replaced with `Checker Framework Annotations`
- Cleanup documentation and all textual resources
Expand Down
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,13 @@ Maven:
<dependency>
<groupId>io.github.sanyarnd</groupId>
<artifactId>app-locker</artifactId>
<version>1.1</version>
<version>1.1.1</version>
</dependency>

<repositories>
<repository>
<id>bintray</id>
<name>Bintray Dependency Repository</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
```

Gradle:
```gradle
compile 'io.github.sanyarnd:app-locker:1.1'
repositories {
jcenter()
}
compile 'io.github.sanyarnd:app-locker:1.1.1'
```

Standalone jars are available on [releases](https://github.com/sanyarnd/applocker/releases) page.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sanyarnd</groupId>
<artifactId>app-locker</artifactId>
<version>1.1</version>
<version>1.1.1</version>
<url>https://sanyarnd.github.io/applocker/</url>
<packaging>jar</packaging>

Expand Down
59 changes: 42 additions & 17 deletions src/main/java/io/github/sanyarnd/applocker/AppLocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import lombok.ToString;
Expand All @@ -47,26 +48,33 @@
*/
@ToString(of = {"lockId", "appLock"})
public final class AppLocker {
@NonNull
private final String lockId;
@NonNull
private final Lock gLock;
@NonNull
private final Lock appLock;
@NonNull
private final Path portFile;
@NonNull
private final Runtime runtime;
private final Thread shutdownHook;
@Nullable
private final Server<?, ?> server;
@NonNull
private final Runnable acquiredHandler;
@Nullable
private final BiConsumer<AppLocker, LockingBusyException> busyHandler;
@NonNull
private final Consumer<LockingException> failedHandler;

private AppLocker(final String nameId,
final Path lockPath,
final LockIdEncoder idEncoder,
private AppLocker(@NonNull final String nameId,
@NonNull final Path lockPath,
@NonNull final LockIdEncoder idEncoder,
@Nullable final Server<?, ?> messageServer,
final Runnable onAcquire,
@NonNull final Runnable onAcquire,
@Nullable final BiConsumer<AppLocker, LockingBusyException> onBusy,
final Consumer<LockingException> onFail) {
@NonNull final Consumer<LockingException> onFail) {
lockId = nameId;
server = messageServer;
acquiredHandler = onAcquire;
Expand All @@ -90,7 +98,8 @@ private AppLocker(final String nameId,
* @param id AppLocker unique ID
* @return builder
*/
public static Builder create(final String id) {
@NonNull
public static Builder create(@NonNull final String id) {
return new Builder(id);
}

Expand Down Expand Up @@ -135,7 +144,7 @@ private void lockInternal() {
}
}

private void handleLockBusyException(final LockingBusyException ex) {
private void handleLockBusyException(@NonNull final LockingBusyException ex) {
// if busy != null then prefer busy
if (busyHandler != null) {
try {
Expand Down Expand Up @@ -193,7 +202,8 @@ public boolean isLocked() {
* @return the answer from AppLocker's message messageHandler
* @throws LockingCommunicationException if there's a trouble communicating to other AppLocker instance
*/
public <I extends Serializable, O extends Serializable> O sendMessage(final I message) {
@NonNull
public <I extends Serializable, O extends Serializable> O sendMessage(@NonNull final I message) {
try {
int port = getPortFromFile();
Client<I, O> client = new Client<>(port);
Expand All @@ -205,7 +215,7 @@ public <I extends Serializable, O extends Serializable> O sendMessage(final I me
}
}

private void writeAppLockPortToFile(final Path portFilePath, final int portNumber) throws IOException {
private void writeAppLockPortToFile(@NonNull final Path portFilePath, final int portNumber) throws IOException {
final int bytesInInt = 4;
Files.write(portFilePath, ByteBuffer.allocate(bytesInInt).putInt(portNumber).array());
}
Expand All @@ -220,12 +230,17 @@ private int getPortFromFile() throws IOException {
* @author Alexander Biryukov
*/
public static final class Builder {
@NonNull
private final String id;
@NonNull
private Path path = Paths.get(".");
@NonNull
private LockIdEncoder encoder = new Sha1Encoder();
@Nullable
private MessageHandler<?, ?> messageHandler = null;
@NonNull
private Runnable acquiredHandler = () -> { };
@NonNull
private Consumer<LockingException> failedHandler = ex -> { throw ex; };
@Nullable
private BiConsumer<AppLocker, LockingBusyException> busyHandler = null;
Expand All @@ -239,7 +254,8 @@ public static final class Builder {
* @param storePath storing path
* @return builder
*/
public Builder setPath(final Path storePath) {
@NonNull
public Builder setPath(@NonNull final Path storePath) {
path = storePath;
return this;
}
Expand All @@ -252,7 +268,8 @@ public Builder setPath(final Path storePath) {
* @param handler message handler
* @return builder
*/
public Builder setMessageHandler(final MessageHandler<?, ?> handler) {
@NonNull
public Builder setMessageHandler(@NonNull final MessageHandler<?, ?> handler) {
messageHandler = handler;
return this;
}
Expand All @@ -265,7 +282,8 @@ public Builder setMessageHandler(final MessageHandler<?, ?> handler) {
* @param idEncoder name encoder
* @return builder
*/
public Builder setIdEncoder(final LockIdEncoder idEncoder) {
@NonNull
public Builder setIdEncoder(@NonNull final LockIdEncoder idEncoder) {
encoder = idEncoder;
return this;
}
Expand All @@ -277,7 +295,8 @@ public Builder setIdEncoder(final LockIdEncoder idEncoder) {
* @param callback function to call after successful locking
* @return builder
*/
public Builder onSuccess(final Runnable callback) {
@NonNull
public Builder onSuccess(@NonNull final Runnable callback) {
acquiredHandler = callback;
return this;
}
Expand All @@ -291,7 +310,9 @@ public Builder onSuccess(final Runnable callback) {
* @param <T> answer type
* @return builder
*/
public <T extends Serializable> Builder onBusy(final Serializable message, final Consumer<T> handler) {
@NonNull
public <T extends Serializable> Builder onBusy(@NonNull final Serializable message,
@NonNull final Consumer<T> handler) {
busyHandler = (appLocker, ex) -> {
T answer = appLocker.sendMessage(message);
handler.accept(answer);
Expand All @@ -307,7 +328,8 @@ public <T extends Serializable> Builder onBusy(final Serializable message, final
* @param handler answer processing function
* @return builder
*/
public Builder onBusy(final Serializable message, final Runnable handler) {
@NonNull
public Builder onBusy(@NonNull final Serializable message, @NonNull final Runnable handler) {
busyHandler = (appLocker, ignoredException) -> {
appLocker.sendMessage(message);
handler.run();
Expand All @@ -322,7 +344,8 @@ public Builder onBusy(final Serializable message, final Runnable handler) {
* @param handler error processing function
* @return builder
*/
public Builder onFail(final Consumer<LockingException> handler) {
@NonNull
public Builder onFail(@NonNull final Consumer<LockingException> handler) {
failedHandler = handler;
return this;
}
Expand All @@ -334,7 +357,8 @@ public Builder onFail(final Consumer<LockingException> handler) {
* @param handler error processing function
* @return builder
*/
public Builder onFail(final Runnable handler) {
@NonNull
public Builder onFail(@NonNull final Runnable handler) {
failedHandler = ignoredException -> handler.run();
return this;
}
Expand All @@ -344,6 +368,7 @@ public Builder onFail(final Runnable handler) {
*
* @return AppLocker instance
*/
@NonNull
public AppLocker build() {
@SuppressWarnings("unchecked")
Server<?, ?> server = messageHandler != null ? new Server(messageHandler) : null;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/github/sanyarnd/applocker/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.net.InetAddress;
import java.net.Socket;

import org.checkerframework.checker.nullness.qual.NonNull;

import io.github.sanyarnd.applocker.exceptions.LockingCommunicationException;

/**
Expand All @@ -41,7 +43,8 @@ final class Client<I extends Serializable, O extends Serializable> {
port = portNumber;
}

O send(final I message) {
@NonNull
O send(@NonNull final I message) {
try (Socket socket = new Socket(InetAddress.getLocalHost(), port);
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream())) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/github/sanyarnd/applocker/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import io.github.sanyarnd.applocker.exceptions.LockingBusyException;
Expand All @@ -37,13 +38,14 @@
* @author Alexander Biryukov
*/
final class Lock implements AutoCloseable {
@NonNull
private final Path file;
@Nullable
private FileChannel channel;
@Nullable
private FileLock fileLock;

Lock(final Path underlyingFile) {
Lock(@NonNull final Path underlyingFile) {
file = underlyingFile.toAbsolutePath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package io.github.sanyarnd.applocker;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Provides the safe way to encode application id such that it can be stored
* on filesystem without exceptions: invalid characters, too long etc.
Expand All @@ -30,5 +32,6 @@ public interface LockIdEncoder {
* @param string input string
* @return encoded string
*/
String encode(String string);
@NonNull
String encode(@NonNull String string);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.io.Serializable;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Interface for the function that runs on the server side
* and handles all incoming messages.
Expand All @@ -34,5 +36,6 @@ public interface MessageHandler<I extends Serializable, O extends Serializable>
* @param message input message
* @return result of the message processing
*/
O handleMessage(I message);
@NonNull
O handleMessage(@NonNull I message);
}
6 changes: 5 additions & 1 deletion src/main/java/io/github/sanyarnd/applocker/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import io.github.sanyarnd.applocker.exceptions.LockingCommunicationException;
Expand All @@ -41,15 +42,18 @@
* @author Alexander Biryukov
*/
final class Server<I extends Serializable, O extends Serializable> {
@NonNull
private final MessageHandler<I, O> messageHandler;
@NonNull
private final Runtime runtime;
@Nullable
private Future<?> threadHandle;
@Nullable
private ServerLoop runnable;
@NonNull
private Thread shutdownHook;

Server(final MessageHandler<I, O> handler) {
Server(@NonNull final MessageHandler<I, O> handler) {
messageHandler = handler;
runtime = Runtime.getRuntime();

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/github/sanyarnd/applocker/Sha1Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* SHA-1 string encoder.
*
* @author Alexander Biryukov
*/
final class Sha1Encoder implements LockIdEncoder {
@NonNull
@Override
public String encode(final String string) {
public String encode(@NonNull final String string) {
try {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
sha1.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package io.github.sanyarnd.applocker.exceptions;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Exception indicates that the lock has already been acquired.
*
Expand All @@ -34,7 +36,7 @@ public class LockingBusyException extends LockingException {
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public LockingBusyException(final Throwable cause) {
public LockingBusyException(@NonNull final Throwable cause) {
super(cause);
}
}
Loading

0 comments on commit 9950703

Please sign in to comment.