Skip to content

Commit

Permalink
Version 1.0.1
Browse files Browse the repository at this point in the history
- Removed unnecessary classes
- Improved test coverage
  • Loading branch information
sanyarnd committed May 21, 2019
1 parent b497466 commit 60d65d4
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 248 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.1

- Removed unnecessary builders
- Improved test coverage

# 1.0

- Initial commit
Expand Down
4 changes: 1 addition & 3 deletions 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.0</version>
<version>1.0.1</version>
<url>https://github.com/sanyarnd/applocker</url>
<packaging>jar</packaging>

Expand All @@ -33,7 +33,6 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java-version>1.8</java-version>
<!--<maven.compiler.release>${java-version}</maven.compiler.release>-->
<maven.compiler.source>${java-version}</maven.compiler.source>
<maven.compiler.target>${java-version}</maven.compiler.target>
<maven.compiler.useIncrementalCompilation>true</maven.compiler.useIncrementalCompilation>
Expand Down Expand Up @@ -147,7 +146,6 @@
<analyzer>
<outputType>xml</outputType>
<outputFile>target/reports/pvs-output.xml</outputFile>
<!--<sonarQubeData>sonar-project.properties</sonarQubeData>-->
</analyzer>
</configuration>
</plugin>
Expand Down
134 changes: 124 additions & 10 deletions src/main/java/io/github/sanyarnd/applocker/AppLocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand All @@ -32,14 +33,14 @@
import lombok.Getter;
import lombok.ToString;

import io.github.sanyarnd.applocker.builder.Builder;
import io.github.sanyarnd.applocker.builder.BuilderArgs;
import io.github.sanyarnd.applocker.exceptions.LockingBusyException;
import io.github.sanyarnd.applocker.exceptions.LockingCommunicationException;
import io.github.sanyarnd.applocker.exceptions.LockingException;
import io.github.sanyarnd.applocker.exceptions.LockingFailedException;
import io.github.sanyarnd.applocker.filesystem.LockNameProvider;
import io.github.sanyarnd.applocker.filesystem.Sha1Provider;
import io.github.sanyarnd.applocker.messaging.Client;
import io.github.sanyarnd.applocker.messaging.MessageHandler;
import io.github.sanyarnd.applocker.messaging.Server;

/**
Expand Down Expand Up @@ -75,13 +76,13 @@ public final class AppLocker {
@Nonnull
private final Consumer<LockingException> failedHandler;

public AppLocker(@Nonnull String id,
@Nonnull Path lockPath,
@Nonnull LockNameProvider provider,
@Nullable Server<?, ?> server,
@Nonnull Runnable acquiredHandler,
@Nullable BiConsumer<AppLocker, LockingBusyException> busyHandler,
@Nonnull Consumer<LockingException> failedHandler) {
private AppLocker(@Nonnull String id,
@Nonnull Path lockPath,
@Nonnull LockNameProvider provider,
@Nullable Server<?, ?> server,
@Nonnull Runnable acquiredHandler,
@Nullable BiConsumer<AppLocker, LockingBusyException> busyHandler,
@Nonnull Consumer<LockingException> failedHandler) {
this.id = id;
this.server = server;
this.acquiredHandler = acquiredHandler;
Expand All @@ -105,7 +106,7 @@ public AppLocker(@Nonnull String id,
* @param id AppLocker unique ID
* @return builder
*/
public static BuilderArgs create(@Nonnull String id) {
public static Builder create(@Nonnull String id) {
return new Builder(id);
}

Expand Down Expand Up @@ -221,5 +222,118 @@ private int getPortFromFile() throws IOException {
return ByteBuffer.wrap(Files.readAllBytes(portFile)).getInt();
}

/**
* AppLocker builder
*
* @author Alexander Biryukov
*/
public static final class Builder {
@Nonnull
private final String id;
@Nonnull
private Path path = Paths.get(".");
@Nonnull
private LockNameProvider provider = new Sha1Provider();
@Nullable
private MessageHandler<?, ?> handler = null;
@Nonnull
private Runnable acquiredHandler = () -> {};
@Nonnull
private Consumer<LockingException> failedHandler = ex -> { throw ex; };
@Nullable
private BiConsumer<AppLocker, LockingBusyException> busyHandler;

public Builder(@Nonnull String id) { this.id = id; }

/**
* Sets the path where the lock file will be stored<br/>
* Default value is "." (relative)
*
* @param path store path
* @return builder
*/
public Builder setPath(@Nonnull Path path) {
this.path = path;
return this;
}

/**
* Sets the message handler.<br/>
* If not set, AppLocker won't support communication features <br/>
* Default value is null
*
* @param handler message handler
* @return builder
*/
public Builder setMessageHandler(@Nonnull MessageHandler<?, ?> handler) {
this.handler = handler;
return this;
}

/**
* Sets the name provider.<br/>
* Provider encodes lock id to filesystem-friendly entry<br/>
* Default value is {@link Sha1Provider}
*
* @param provider name provider
* @return builder
*/
public Builder setNameProvider(@Nonnull LockNameProvider provider) {
this.provider = provider;
return this;
}

/**
* Successful locking callback.<br/>
* By default does nothing
*
* @param callback the function to call after successful locking
* @return builder
*/
public Builder acquired(@Nonnull Runnable callback) {
acquiredHandler = callback;
return this;
}

/**
* Lock is already taken callback.<br/>
* By default does nothing (null)
*
* @param message message for lock holder
* @param handler answer processing function
* @param <T> answer type
* @return builder
*/
public <T extends Serializable> Builder busy(@Nonnull Serializable message, @Nonnull Consumer<T> handler) {
busyHandler = (appLocker, ex) -> {
T answer = appLocker.sendMessage(message);
handler.accept(answer);
};
return this;
}

/**
* Unable to lock for unknown reasons callback.<br/>
* By default re-throws the exception
*
* @param handler error processing function
* @return builder
*/
public Builder failed(@Nonnull Consumer<LockingException> handler) {
failedHandler = handler;
return this;
}

/**
* Build AppLocker
*
* @return AppLocker instance
*/
public AppLocker build() {
@SuppressWarnings("unchecked")
Server<?, ?> server = handler != null ? new Server(handler) : null;

return new AppLocker(id, path, provider, server, acquiredHandler, busyHandler, failedHandler);
}
}
}
107 changes: 0 additions & 107 deletions src/main/java/io/github/sanyarnd/applocker/builder/Builder.java

This file was deleted.

Loading

0 comments on commit 60d65d4

Please sign in to comment.