-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
577 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,21 @@ | ||
package com.github.kunai.sink; | ||
|
||
import static java.nio.file.LinkOption.NOFOLLOW_LINKS; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.nio.file.spi.FileSystemProvider; | ||
|
||
import com.github.kunai.util.Exceptions; | ||
import com.github.kunai.util.PathHelper; | ||
|
||
public class DirectoryMaker { | ||
private static boolean exists(FileSystemProvider provider, Path path){ | ||
return Exceptions.isThrowed(provider, path, | ||
(givenProvider, givenPath) -> givenProvider.readAttributes( | ||
givenPath, BasicFileAttributes.class, NOFOLLOW_LINKS)); | ||
} | ||
|
||
public static void mkdirs(FileSystem system, Path path){ | ||
try{ | ||
mkdirsImpl(system.provider(), path.getParent()); | ||
} catch(IOException e){ } | ||
public static void mkdirs(Path givenPath, FileSystem givenSystem){ | ||
Exceptions.isThrowed(givenPath, givenSystem, | ||
(path, system) -> mkdirsImpl(path, system)); | ||
} | ||
|
||
private static void mkdirsImpl(FileSystemProvider provider, Path path) throws IOException{ | ||
if(path == null || exists(provider, path)) return; | ||
mkdirsImpl(provider, path.getParent()); | ||
provider.createDirectory(path); | ||
private static void mkdirsImpl(Path path, FileSystem system) throws IOException{ | ||
if(path == null || PathHelper.exists(path, system)) return; | ||
mkdirsImpl(path.getParent(), system); | ||
system.provider().createDirectory(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 3 additions & 17 deletions
20
src/main/java/com/github/kunai/sink/factories/DataSinkFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,11 @@ | ||
package com.github.kunai.sink.factories; | ||
|
||
import java.nio.file.Path; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import com.github.kunai.sink.DataSink; | ||
|
||
class DataSinkFactory { | ||
private Predicate<Path> predicate; | ||
private Function<Path, DataSink> function; | ||
public interface DataSinkFactory { | ||
DataSink create(Path path); | ||
|
||
public DataSinkFactory(Predicate<Path> predicate, Function<Path, DataSink> function){ | ||
this.predicate = predicate; | ||
this.function = function; | ||
} | ||
|
||
public boolean test(Path path){ | ||
return predicate.test(path); | ||
} | ||
|
||
public DataSink create(Path path){ | ||
return function.apply(path); | ||
} | ||
boolean isTarget(Path path); | ||
} |
37 changes: 0 additions & 37 deletions
37
src/main/java/com/github/kunai/sink/factories/DataSinkFactoryBuilder.java
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
src/main/java/com/github/kunai/sink/factories/DefaultDataSinkFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.github.kunai.sink.factories; | ||
|
||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.github.kunai.sink.ClassFileDataSink; | ||
import com.github.kunai.sink.DataSink; | ||
import com.github.kunai.sink.DirectoryDataSink; | ||
import com.github.kunai.sink.JarFileDataSink; | ||
import com.github.kunai.sink.WarFileDataSink; | ||
import com.github.kunai.util.PathHelper; | ||
|
||
public class DefaultDataSinkFactory implements DataSinkFactory{ | ||
private List<DataSinkFactory> factories = new ArrayList<>(); | ||
|
||
public DefaultDataSinkFactory(){ | ||
register(new GenericDataSinkFactory(path -> PathHelper.endsWith(path, ".jar"), path -> new JarFileDataSink(path))); | ||
register(new GenericDataSinkFactory(path -> PathHelper.endsWith(path, ".war"), path -> new WarFileDataSink(path))); | ||
register(new GenericDataSinkFactory(path -> PathHelper.endsWith(path, ".class"), path -> new ClassFileDataSink(path))); | ||
register(new GenericDataSinkFactory(path -> true, path -> new DirectoryDataSink(path))); | ||
} | ||
|
||
public boolean isTarget(Path path){ | ||
return path != null; | ||
} | ||
|
||
public DataSinkFactory factory(Path path){ | ||
return factories.stream() | ||
.filter(factory -> factory.isTarget(path)) | ||
.findFirst().get(); | ||
} | ||
|
||
public DataSink create(Path path){ | ||
return factory(path).create(path); | ||
} | ||
|
||
private void register(GenericDataSinkFactory factory){ | ||
factories.add(factory); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/github/kunai/sink/factories/GenericDataSinkFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.kunai.sink.factories; | ||
|
||
import java.nio.file.Path; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import com.github.kunai.sink.DataSink; | ||
|
||
class GenericDataSinkFactory implements DataSinkFactory{ | ||
private Predicate<Path> predicate; | ||
private Function<Path, DataSink> function; | ||
|
||
public GenericDataSinkFactory(Predicate<Path> predicate, Function<Path, DataSink> function){ | ||
this.predicate = predicate; | ||
this.function = function; | ||
} | ||
|
||
@Override | ||
public boolean isTarget(Path path){ | ||
return predicate.test(path); | ||
} | ||
|
||
@Override | ||
public DataSink create(Path path){ | ||
return function.apply(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.kunai.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.FileVisitor; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
|
||
public class DirectoryRemover implements FileVisitor<Path> { | ||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
Files.delete(file); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult postVisitDirectory(Path dir, IOException exception) throws IOException { | ||
if(exception != null) throw exception; | ||
Files.delete(dir); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.