From 94105d790e8b06aa8fac252bcd94be63ee356638 Mon Sep 17 00:00:00 2001 From: terminator48 Date: Fri, 8 Apr 2016 23:32:54 +0600 Subject: [PATCH] BuildFile and FS modules implementation [commit 2] --- build_files/pom.xml | 21 ++ .../system/build_sequence/Activator.java | 44 +++ .../system/build_sequence/CommandLine.java | 250 ++++++++++++++++++ .../build_sequence/api/IBuildCommand.java | 32 +++ .../build_sequence/api/IBuildEnvironment.java | 10 + .../system/build_sequence/api/IBuildFile.java | 10 + .../build_sequence/api/IBuildFileFactory.java | 20 ++ .../build_sequence/api/IBuildSequence.java | 14 + .../system/build_sequence/api/IBuildStep.java | 12 + .../implementation/BuildEnvironment.java | 24 ++ .../implementation/BuildFileFactory.java | 66 +++++ .../implementation/BuildSequence.java | 47 ++++ .../implementation/BuildStep.java | 82 ++++++ .../MalformedBuildFileException.java | 10 + core/pom.xml | 6 +- .../eu/arcadex/system/database/Activator.java | 1 + filesystem/pom.xml | 23 +- .../arcadex/system/file_system/Activator.java | 24 ++ .../system/file_system}/FileSystemLib.java | 17 +- .../system/file_system}/UnzipUtility.java | 2 + .../system/file_system/api/IFileSystem.java | 71 +++++ web_api/pom.xml | 8 +- 22 files changed, 784 insertions(+), 10 deletions(-) create mode 100644 build_files/src/main/java/eu/arcadex/system/build_sequence/Activator.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/CommandLine.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildCommand.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildEnvironment.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFile.java create mode 100644 build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFileFactory.java create mode 100644 build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildSequence.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildStep.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildEnvironment.java create mode 100644 build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildFileFactory.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildSequence.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildStep.java create mode 100755 build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/MalformedBuildFileException.java create mode 100644 filesystem/src/main/java/eu/arcadex/system/file_system/Activator.java rename filesystem/src/main/java/{ => eu/arcadex/system/file_system}/FileSystemLib.java (92%) rename filesystem/src/main/java/{ => eu/arcadex/system/file_system}/UnzipUtility.java (98%) create mode 100644 filesystem/src/main/java/eu/arcadex/system/file_system/api/IFileSystem.java diff --git a/build_files/pom.xml b/build_files/pom.xml index 8236080..ccd3e41 100644 --- a/build_files/pom.xml +++ b/build_files/pom.xml @@ -19,4 +19,25 @@ ${global.version} + + + ${groupId}.${artifactId}-${version} + + + org.apache.felix + maven-bundle-plugin + 2.3.5 + true + + + Arcadex System: ${name} ${version} + ${groupId}.${artifactId} + eu.arcadex.system.build_sequence.Activator + eu.arcadex.system.build_sequence.api + * + + + + + \ No newline at end of file diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/Activator.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/Activator.java new file mode 100644 index 0000000..4a49ff1 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/Activator.java @@ -0,0 +1,44 @@ +package eu.arcadex.system.build_sequence; + +import eu.arcadex.system.build_sequence.api.IBuildFileFactory; +import eu.arcadex.system.build_sequence.implementation.BuildFileFactory; +import eu.arcadex.system.file_system.api.IFileSystem; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.util.tracker.ServiceTracker; + +/** + * @author Daniil on 08.04.16. + */ +public class Activator implements BundleActivator { + private ServiceTracker fileSystemServiceTracker; + private ServiceRegistration service; + + @Override + public void start(BundleContext bundleContext) throws Exception { + fileSystemServiceTracker = new ServiceTracker(bundleContext, IFileSystem.class.getName(), null); + fileSystemServiceTracker.open(); + + service = bundleContext.registerService(IBuildFileFactory.class.getName(), new BuildFileFactory(this), null); + } + + @Override + public void stop(BundleContext bundleContext) throws Exception { + service.unregister(); + } + + public IFileSystem getFileSystem() { + IFileSystem db = null; + try { + db = fileSystemServiceTracker.waitForService(5000); + } catch (InterruptedException ignore) { + // ignore + } + + if (db == null) { + throw new RuntimeException("Service \"IFileSystem\" not found!"); + } + return db; + } +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/CommandLine.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/CommandLine.java new file mode 100755 index 0000000..e5fced3 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/CommandLine.java @@ -0,0 +1,250 @@ +package eu.arcadex.system.build_sequence; + +import java.util.*; + + +/** + * Commandline objects help handling command lines specifying processes to + * execute. + *

+ * The class can be used to define a command line as nested elements or as a + * helper to define a command line by an application. + *

+ * + * <someelement>
+ *   <acommandline executable="/executable/to/run">
+ *     <argument value="argument 1" />
+ *     <argument line="argument_1 argument_2 argument_3" />
+ *     <argument value="argument 4" />
+ *   </acommandline>
+ * </someelement>
+ *
+ * The element someelement must provide a method + * createAcommandline which returns an instance of this class. + */ +public class CommandLine implements Cloneable { + /** win9x uses a (shudder) bat file (antRun.bat) for executing commands */ + + /** + * The arguments of the command + */ + private List arguments = new ArrayList(); + + /** + * the program to execute + */ + private String executable = null; + + /** + * Put quotes around the given String if necessary. + *

+ *

If the argument doesn't include spaces or quotes, return it + * as is. If it contains double quotes, use single quotes - else + * surround the argument by double quotes.

+ * + * @param argument the argument to quote if necessary. + * @return the quoted argument. + */ + public static String quoteArgument(String argument) { + if (argument.contains("\"")) { + if (argument.contains("\'")) { + throw new RuntimeException("Can\'t handle single and double" + + " quotes in same argument"); + } else { + return '\'' + argument + '\''; + } + } else if (argument.contains("\'") + || argument.contains(" ")) { + return '\"' + argument + '\"'; + } else { + return argument; + } + } + + /** + * Quote the parts of the given array in way that makes them + * usable as command line arguments. + * + * @param line the list of arguments to quote. + * @return empty string for null or no command, else every argument split + * by spaces and quoted by quoting rules. + */ + public static String toString(String[] line) { + // empty path return empty string + if (line == null || line.length == 0) { + return ""; + } + // path containing one or more elements + final StringBuilder result = new StringBuilder(); + for (int i = 0; i < line.length; i++) { + if (i > 0) { + result.append(' '); + } + result.append(quoteArgument(line[i])); + } + return result.toString(); + } + + /** + * Crack a command line. + * + * @param toProcess the command line to process. + * @return the command line broken into strings. + * An empty or null toProcess parameter results in a zero sized array. + */ + public static String[] translateCommandline(String toProcess) { + if (toProcess == null || toProcess.length() == 0) { + //no command? no string + return new String[0]; + } + // parse with a simple finite state machine + + final int normal = 0; + final int inQuote = 1; + final int inDoubleQuote = 2; + int state = normal; + final StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true); + final ArrayList result = new ArrayList<>(); + final StringBuilder current = new StringBuilder(); + boolean lastTokenHasBeenQuoted = false; + + while (tok.hasMoreTokens()) { + String nextTok = tok.nextToken(); + switch (state) { + case inQuote: + if ("\'".equals(nextTok)) { + lastTokenHasBeenQuoted = true; + state = normal; + } else { + current.append(nextTok); + } + break; + case inDoubleQuote: + if ("\"".equals(nextTok)) { + lastTokenHasBeenQuoted = true; + state = normal; + } else { + current.append(nextTok); + } + break; + default: + if ("\'".equals(nextTok)) { + state = inQuote; + } else if ("\"".equals(nextTok)) { + state = inDoubleQuote; + } else if (" ".equals(nextTok)) { + if (lastTokenHasBeenQuoted || current.length() != 0) { + result.add(current.toString()); + current.setLength(0); + } + } else { + current.append(nextTok); + } + lastTokenHasBeenQuoted = false; + break; + } + } + if (lastTokenHasBeenQuoted || current.length() != 0) { + result.add(current.toString()); + } + if (state == inQuote || state == inDoubleQuote) { + throw new RuntimeException("unbalanced quotes in " + toProcess); + } + return result.toArray(new String[result.size()]); + } + + /** + * Return the executable and all defined arguments. + * + * @return the commandline as an array of strings. + */ + public String[] getCommandline() { + final List commands = new LinkedList(); + addCommandToList(commands.listIterator()); + return commands.toArray(new String[commands.size()]); + } + + /** + * Add the entire command, including (optional) executable to a list. + * + * @param list the list to add to. + * @since Ant 1.6 + */ + public void addCommandToList(ListIterator list) { + if (executable != null) { + list.add(executable); + } + addArgumentsToList(list); + } + + /** + * Append all the arguments to the tail of a supplied list. + * + * @param list the list of arguments. + * @since Ant 1.6 + */ + public void addArgumentsToList(ListIterator list) { + for (Argument arg : arguments) { + String[] s = arg.getParts(); + if (s != null) { + for (String value : s) { + list.add(value); + } + } + } + } + + /** + * Return the command line as a string. + * + * @return the command line. + */ + public String toString() { + return toString(getCommandline()); + } + + /** + * Generate a deep clone of the contained object. + * + * @return a clone of the contained object + */ + public Object clone() { + try { + CommandLine c = (CommandLine) super.clone(); + c.arguments = new ArrayList<>(arguments); + return c; + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + } + + /** + * Used for nested xml command line definitions. + */ + public static class Argument { + + private String[] parts; + + private String prefix = ""; + private String suffix = ""; + + + /** + * Return the constituent parts of this Argument. + * + * @return an array of strings. + */ + public String[] getParts() { + if (parts == null || parts.length == 0 + || (prefix.length() == 0 && suffix.length() == 0)) { + return parts; + } + String[] fullParts = new String[parts.length]; + for (int i = 0; i < fullParts.length; ++i) { + fullParts[i] = prefix + parts[i] + suffix; + } + return fullParts; + } + } + +} \ No newline at end of file diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildCommand.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildCommand.java new file mode 100755 index 0000000..329c51d --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildCommand.java @@ -0,0 +1,32 @@ +package eu.arcadex.system.build_sequence.api; + + +/** + * Created by Daniil on 08.02.2016. + */ +public enum IBuildCommand { + INH(false, 1), // Disable or enable Build file inheritance + MKDIRS(true, 1), // + COPY(true, 2), + LNK(true, 2), + UNZIP(true, 2); + + + // Флаг, указывающий на то, стоит ли рассматривать + // команду как призыв к действию + private boolean execute = false; + private int argumentCount = 0; + + IBuildCommand(boolean execute, int argumentCount) { + this.execute = execute; + this.argumentCount = argumentCount; + } + + public boolean isExecute() { + return execute; + } + + public int getArgumentCount() { + return argumentCount; + } +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildEnvironment.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildEnvironment.java new file mode 100755 index 0000000..a899755 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildEnvironment.java @@ -0,0 +1,10 @@ +package eu.arcadex.system.build_sequence.api; + +/** + * Created by Daniil on 08.02.2016. + */ +public interface IBuildEnvironment { + void add(String from, String to); + + String process(String message); +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFile.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFile.java new file mode 100755 index 0000000..9a34ba2 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFile.java @@ -0,0 +1,10 @@ +package eu.arcadex.system.build_sequence.api; + +import java.util.LinkedList; + +/** + * Created by Daniil on 08.02.2016. + */ +public interface IBuildFile { + LinkedList getCommands(); +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFileFactory.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFileFactory.java new file mode 100644 index 0000000..a93a4e9 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildFileFactory.java @@ -0,0 +1,20 @@ +package eu.arcadex.system.build_sequence.api; + +import eu.arcadex.system.build_sequence.implementation.MalformedBuildFileException; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +/** + * @author Daniil on 08.04.16. + */ +public interface IBuildFileFactory { + IBuildFile getFromFile(File file) throws IOException, MalformedBuildFileException; + + IBuildFile getFromStringList(List list) throws MalformedBuildFileException; + + IBuildEnvironment createEnvironment(); + + IBuildSequence createSequence(); +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildSequence.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildSequence.java new file mode 100644 index 0000000..524fa7d --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildSequence.java @@ -0,0 +1,14 @@ +package eu.arcadex.system.build_sequence.api; + +/** + * @author Daniil on 08.04.16. + */ +public interface IBuildSequence { + void setInheritance(boolean inheritance); + + void add(IBuildStep step); + + void include(IBuildFile buildFile); + + void execute(IBuildEnvironment environment) throws Exception; +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildStep.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildStep.java new file mode 100755 index 0000000..2d677b5 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/api/IBuildStep.java @@ -0,0 +1,12 @@ +package eu.arcadex.system.build_sequence.api; + +/** + * Created by Daniil on 08.02.2016. + */ +public interface IBuildStep { + void execute(IBuildEnvironment impl) throws Exception; + + IBuildCommand getCommand(); + + String[] getArgs(); +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildEnvironment.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildEnvironment.java new file mode 100755 index 0000000..dc51771 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildEnvironment.java @@ -0,0 +1,24 @@ +package eu.arcadex.system.build_sequence.implementation; + +import eu.arcadex.system.build_sequence.api.IBuildEnvironment; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Daniil on 08.02.2016. + */ +public class BuildEnvironment implements IBuildEnvironment { + private Map replaces = new HashMap<>(); + + public void add(String from, String to) { + replaces.put(from, to); + } + + public String process(String message) { + for (Map.Entry entry : replaces.entrySet()) { + message = message.replaceAll("%" + entry.getKey() + "%", entry.getValue()); + } + return message; + } +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildFileFactory.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildFileFactory.java new file mode 100644 index 0000000..fd52454 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildFileFactory.java @@ -0,0 +1,66 @@ +package eu.arcadex.system.build_sequence.implementation; + +import eu.arcadex.system.build_sequence.Activator; +import eu.arcadex.system.build_sequence.api.*; +import eu.arcadex.system.file_system.api.IFileSystem; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +/** + * @author Daniil on 08.04.16. + */ +public class BuildFileFactory implements IBuildFileFactory { + private Activator parent; + + public BuildFileFactory(Activator parent) { + this.parent = parent; + } + + public IBuildFile getFromFile(File file) throws IOException, MalformedBuildFileException { + IFileSystem fs = parent.getFileSystem(); + BufferedReader bufferedReader = new BufferedReader(fs.getReader(file)); + String line; + + BuildFile bf = new BuildFile(); + while ((line = bufferedReader.readLine()) != null) + bf.addCommand(BuildStep.deserialize(line, parent)); + bufferedReader.close(); + + return bf; + } + + public IBuildFile getFromStringList(List list) throws MalformedBuildFileException { + BuildFile bf = new BuildFile(); + + for (String s : list) { + bf.addCommand(BuildStep.deserialize(s, parent)); + } + + return bf; + } + + public IBuildEnvironment createEnvironment() { + return new BuildEnvironment(); + } + + public IBuildSequence createSequence() { + return new BuildSequence(); + } + + public class BuildFile implements IBuildFile { + private LinkedList commands = new LinkedList<>(); + + private void addCommand(IBuildStep b) { + commands.add(b); + } + + @Override + public LinkedList getCommands() { + return commands; + } + } +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildSequence.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildSequence.java new file mode 100755 index 0000000..c2fe6c8 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildSequence.java @@ -0,0 +1,47 @@ +package eu.arcadex.system.build_sequence.implementation; + + +import eu.arcadex.system.build_sequence.api.*; + +import java.util.LinkedList; + +/** + * Created by Daniil on 08.02.2016. + */ +public class BuildSequence implements IBuildSequence { + private LinkedList buildSteps; + private boolean inheritance = true; + + public BuildSequence() { + this.buildSteps = new LinkedList<>(); + } + + @Override + public void setInheritance(boolean inheritance) { + if (!inheritance) + buildSteps.clear(); + this.inheritance = inheritance; + } + + @Override + public void add(IBuildStep step) { + if (step.getCommand() == IBuildCommand.INH) + setInheritance(step.getArgs()[0].equalsIgnoreCase("1")); + + buildSteps.add(step); + } + + @Override + public void include(IBuildFile buildFile) { + if (!inheritance) + buildSteps.clear(); + buildFile.getCommands().forEach(this::add); + } + + @Override + public void execute(IBuildEnvironment environment) throws Exception { + for (IBuildStep buildStep : buildSteps) { + buildStep.execute(environment); + } + } +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildStep.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildStep.java new file mode 100755 index 0000000..f77a3c0 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/BuildStep.java @@ -0,0 +1,82 @@ +package eu.arcadex.system.build_sequence.implementation; + + +import eu.arcadex.system.build_sequence.Activator; +import eu.arcadex.system.build_sequence.CommandLine; +import eu.arcadex.system.build_sequence.api.IBuildCommand; +import eu.arcadex.system.build_sequence.api.IBuildEnvironment; +import eu.arcadex.system.build_sequence.api.IBuildStep; +import eu.arcadex.system.file_system.api.IFileSystem; + +import java.io.File; +import java.util.Arrays; + +/** + * Created by Daniil on 08.02.2016. + */ +public class BuildStep implements IBuildStep { + private IBuildCommand command; + private String[] args; + private Activator parent; + + public BuildStep(IBuildCommand command, String[] args, Activator parent) { + this.command = command; + this.args = args; + this.parent = parent; + } + + private static IBuildCommand getByName(String name) throws IllegalArgumentException { + name = name.trim().toLowerCase(); + switch (name) { + case "cp": + case "copy": + return IBuildCommand.COPY; + case "mkdir": + case "mkdirs": + return IBuildCommand.MKDIRS; + case "link": + case "lnk": + return IBuildCommand.LNK; + case "unzip": + return IBuildCommand.UNZIP; + } + throw new IllegalArgumentException("Unknown command " + name); + } + + public static IBuildStep deserialize(String string, Activator parent) throws MalformedBuildFileException { + String myArgs[] = CommandLine.translateCommandline(string); + IBuildCommand cmd = getByName(myArgs[0]); + if (myArgs.length - 1 < cmd.getArgumentCount()) + throw new MalformedBuildFileException("Missing parameters for this procedure"); + + return new BuildStep(cmd, Arrays.copyOfRange(myArgs, 1, myArgs.length), parent); + } + + public void execute(IBuildEnvironment environment) throws Exception { + IFileSystem fileSystem = parent.getFileSystem(); + switch (command) { + case COPY: + fileSystem.copy(new File(environment.process(args[0])).getAbsoluteFile().toPath(), new File(environment.process(args[1])).getAbsoluteFile().toPath(), true); + break; + case MKDIRS: + fileSystem.mkdirs(new File(environment.process(args[0])).getAbsoluteFile()); + break; + case LNK: + fileSystem.createSymlink(new File(environment.process(args[1])).getAbsoluteFile().toPath(), new File(environment.process(args[0])).getAbsoluteFile().toPath()); + break; + case UNZIP: + fileSystem.unzip(new File(environment.process(args[0])).getAbsolutePath(), new File(environment.process(args[1])).getAbsolutePath()); + break; + } + } + + @Override + public IBuildCommand getCommand() { + return command; + } + + @Override + public String[] getArgs() { + return args; + } +} diff --git a/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/MalformedBuildFileException.java b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/MalformedBuildFileException.java new file mode 100755 index 0000000..84cc0b0 --- /dev/null +++ b/build_files/src/main/java/eu/arcadex/system/build_sequence/implementation/MalformedBuildFileException.java @@ -0,0 +1,10 @@ +package eu.arcadex.system.build_sequence.implementation; + +/** + * Created by Daniil on 08.02.2016. + */ +public class MalformedBuildFileException extends Exception { + public MalformedBuildFileException(String message) { + super(message); + } +} diff --git a/core/pom.xml b/core/pom.xml index 194b27b..67e4d71 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -1,13 +1,13 @@ - 4.0.0 Core core - 0.1-SNAPSHOT + ${global.version} bundle diff --git a/database/src/main/java/eu/arcadex/system/database/Activator.java b/database/src/main/java/eu/arcadex/system/database/Activator.java index 98f21fc..8ce0e10 100644 --- a/database/src/main/java/eu/arcadex/system/database/Activator.java +++ b/database/src/main/java/eu/arcadex/system/database/Activator.java @@ -25,4 +25,5 @@ public class Activator implements BundleActivator { service.unregister(); database.save(); } + } diff --git a/filesystem/pom.xml b/filesystem/pom.xml index f9fe83c..8a224db 100644 --- a/filesystem/pom.xml +++ b/filesystem/pom.xml @@ -10,7 +10,26 @@ 4.0.0 filesystem - jar - + bundle + + ${groupId}.${artifactId}-${version} + + + org.apache.felix + maven-bundle-plugin + 2.3.5 + true + + + Arcadex System: ${name} ${version} + ${groupId}.${artifactId} + eu.arcadex.system.file_system.Activator + eu.arcadex.system.file_system.api + * + + + + + \ No newline at end of file diff --git a/filesystem/src/main/java/eu/arcadex/system/file_system/Activator.java b/filesystem/src/main/java/eu/arcadex/system/file_system/Activator.java new file mode 100644 index 0000000..d08c469 --- /dev/null +++ b/filesystem/src/main/java/eu/arcadex/system/file_system/Activator.java @@ -0,0 +1,24 @@ +package eu.arcadex.system.file_system; + +import eu.arcadex.system.file_system.api.IFileSystem; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +/** + * @author Daniil on 08.04.16. + */ +public class Activator implements BundleActivator { + private ServiceRegistration service; + + @Override + public void start(BundleContext bundleContext) throws Exception { + FileSystemLib lib = new FileSystemLib(); + service = bundleContext.registerService(IFileSystem.class.getName(), lib, null); + } + + @Override + public void stop(BundleContext bundleContext) throws Exception { + service.unregister(); + } +} diff --git a/filesystem/src/main/java/FileSystemLib.java b/filesystem/src/main/java/eu/arcadex/system/file_system/FileSystemLib.java similarity index 92% rename from filesystem/src/main/java/FileSystemLib.java rename to filesystem/src/main/java/eu/arcadex/system/file_system/FileSystemLib.java index ba9bfdc..4494423 100644 --- a/filesystem/src/main/java/FileSystemLib.java +++ b/filesystem/src/main/java/eu/arcadex/system/file_system/FileSystemLib.java @@ -1,3 +1,7 @@ +package eu.arcadex.system.file_system; + +import eu.arcadex.system.file_system.api.IFileSystem; + import java.io.*; import java.nio.file.Files; import java.nio.file.Path; @@ -8,7 +12,7 @@ import java.nio.file.StandardCopyOption; *

* Created by daniil on 02.04.16. */ -public class FileSystemLib { +public class FileSystemLib implements IFileSystem { /** * Copy data from InputStream to OutputStream * @@ -42,6 +46,7 @@ public class FileSystemLib { * @param replace replace the destination file if present? * @throws IOException */ + @Override public void copy(Path from, Path to, boolean replace) throws IOException { assert from != null; assert to != null; @@ -58,6 +63,7 @@ public class FileSystemLib { * @param file file to delete * @throws IOException, FileNotFoundException */ + @Override public void delete(File file) throws IOException { assert file != null; @@ -74,6 +80,7 @@ public class FileSystemLib { * @param actualFile Where should it point to * @throws IOException */ + @Override public void createSymlink(Path linkFile, Path actualFile) throws IOException { assert linkFile != null; assert actualFile != null; @@ -84,6 +91,7 @@ public class FileSystemLib { Files.createSymbolicLink(linkFile, actualFile); } + @Override public void restoreFromResources(String resourceName, File destination, boolean replace) throws IOException { assert resourceName != null; assert destination != null; @@ -112,6 +120,7 @@ public class FileSystemLib { * @return FileReader * @throws FileNotFoundException */ + @Override public Reader getReader(File file) throws FileNotFoundException { assert file != null; @@ -127,6 +136,7 @@ public class FileSystemLib { * @return FileWriter * @throws IOException */ + @Override public Writer getWriter(File file) throws IOException { return new FileWriter(file); } @@ -138,8 +148,13 @@ public class FileSystemLib { * @param directory destination * @throws IOException */ + @Override public void unzip(String archive, String directory) throws IOException { UnzipUtility zip = new UnzipUtility(); zip.unzip(archive, directory); } + + public boolean mkdirs(File file) { + return file.mkdirs(); + } } diff --git a/filesystem/src/main/java/UnzipUtility.java b/filesystem/src/main/java/eu/arcadex/system/file_system/UnzipUtility.java similarity index 98% rename from filesystem/src/main/java/UnzipUtility.java rename to filesystem/src/main/java/eu/arcadex/system/file_system/UnzipUtility.java index a3a37ec..ab33326 100755 --- a/filesystem/src/main/java/UnzipUtility.java +++ b/filesystem/src/main/java/eu/arcadex/system/file_system/UnzipUtility.java @@ -1,3 +1,5 @@ +package eu.arcadex.system.file_system; + import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; diff --git a/filesystem/src/main/java/eu/arcadex/system/file_system/api/IFileSystem.java b/filesystem/src/main/java/eu/arcadex/system/file_system/api/IFileSystem.java new file mode 100644 index 0000000..0e535cb --- /dev/null +++ b/filesystem/src/main/java/eu/arcadex/system/file_system/api/IFileSystem.java @@ -0,0 +1,71 @@ +package eu.arcadex.system.file_system.api; + +import java.io.*; +import java.nio.file.Path; + +/** + * @author Daniil on 08.04.16. + */ +public interface IFileSystem { + /** + * Copy file or directory from one direction to another + * + * @param from File to copy + * @param to Destination + * @param replace replace the destination file if present? + * @throws IOException + */ + void copy(Path from, Path to, boolean replace) throws IOException; + + /** + * Delete file + * + * @param file file to delete + * @throws IOException, FileNotFoundException + */ + void delete(File file) throws IOException; + + /** + * Create a symlink + * + * @param linkFile Where symlink should be created + * @param actualFile Where should it point to + * @throws IOException + */ + void createSymlink(Path linkFile, Path actualFile) throws IOException; + + void restoreFromResources(String resourceName, File destination, boolean replace) throws IOException; + + /** + * Get FileReader for the file + *

+ * We need this to make the code testable + * + * @param file file to read + * @return FileReader + * @throws FileNotFoundException + */ + Reader getReader(File file) throws FileNotFoundException; + + /** + * Get FileWriter for the file + *

+ * We need this to make the code testable + * + * @param file file to write to + * @return FileWriter + * @throws IOException + */ + Writer getWriter(File file) throws IOException; + + /** + * Unzip archive to directory + * + * @param archive archive to extract + * @param directory destination + * @throws IOException + */ + void unzip(String archive, String directory) throws IOException; + + boolean mkdirs(File file); +} diff --git a/web_api/pom.xml b/web_api/pom.xml index dd6bf48..382c615 100644 --- a/web_api/pom.xml +++ b/web_api/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 @@ -20,12 +20,12 @@ eu.arcadex.system core - 0.1-SNAPSHOT + ${global.version} eu.arcadex.system database - 0.1-SNAPSHOT + ${global.version} org.eclipse.jetty