branch develop updated (2790fd3 -> d3afd29)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository eugene. See http://git.nuiton.org/eugene.git from 2790fd3 fixes #3693: Do generate if model properties file is modify Merge branch 'feature/3693' into develop new 738ce09 introduce FileGrabber API (refs #2937) new a868000 Use FileGrabber API and remove some logic from ChainedFileWriter (refs #2937) new d3afd29 fixes #2937: Plugin fail to generate data if project is in a path containing a space Merge branch 'feature/2937' into develop The 3 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit d3afd29495e23beb65164ac7ee0b6b956faafc4b Merge: 2790fd3 a868000 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun May 24 15:13:40 2015 +0200 fixes #2937: Plugin fail to generate data if project is in a path containing a space Merge branch 'feature/2937' into develop commit a86800025483678f25a6592c4c0b4674d1a6fec5 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun May 24 15:12:20 2015 +0200 Use FileGrabber API and remove some logic from ChainedFileWriter (refs #2937) commit 738ce0998d0f751142911b30c924126c202bdc89 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun May 24 15:11:45 2015 +0200 introduce FileGrabber API (refs #2937) Summary of changes: .../org/nuiton/eugene/plugin/GenerateMojo.java | 5 +- .../plugin/writer/BaseChainedFileWriter.java | 139 -------- eugene/pom.xml | 5 + .../nuiton/eugene/writer/ChainedFileWriter.java | 53 ++- .../nuiton/eugene/writer/ChainedWriterEngine.java | 13 +- .../eugene/writer/DefaultChainedWriterEngine.java | 368 ++++++--------------- .../java/org/nuiton/eugene/writer/FileGrabber.java | 22 ++ .../eugene/writer/FileGrabberFromClassPath.java | 285 ++++++++++++++++ .../eugene/writer/FileGrabberFromDirectory.java | 133 ++++++++ 9 files changed, 580 insertions(+), 443 deletions(-) create mode 100644 eugene/src/main/java/org/nuiton/eugene/writer/FileGrabber.java create mode 100644 eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromClassPath.java create mode 100644 eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromDirectory.java -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository eugene. See http://git.nuiton.org/eugene.git commit 738ce0998d0f751142911b30c924126c202bdc89 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun May 24 15:11:45 2015 +0200 introduce FileGrabber API (refs #2937) --- .../java/org/nuiton/eugene/writer/FileGrabber.java | 22 ++ .../eugene/writer/FileGrabberFromClassPath.java | 285 +++++++++++++++++++++ .../eugene/writer/FileGrabberFromDirectory.java | 133 ++++++++++ 3 files changed, 440 insertions(+) diff --git a/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabber.java b/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabber.java new file mode 100644 index 0000000..12f929d --- /dev/null +++ b/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabber.java @@ -0,0 +1,22 @@ +package org.nuiton.eugene.writer; + +import java.io.File; +import java.io.IOException; +import java.util.Set; + +/** + * To grab files to treate. + * + * Created on 5/24/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public interface FileGrabber { + + void addFilesToTreate(File extractDirectory, + String inputDirectory, + Set<String> includePatterns, + ChainedFileWriterData result) throws IOException; + +} diff --git a/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromClassPath.java b/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromClassPath.java new file mode 100644 index 0000000..9437d1a --- /dev/null +++ b/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromClassPath.java @@ -0,0 +1,285 @@ +package org.nuiton.eugene.writer; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.FileUtil; +import org.nuiton.util.Resource; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Created on 5/24/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class FileGrabberFromClassPath implements FileGrabber { + + /** Logger. */ + private static final Log log = LogFactory.getLog(FileGrabberFromClassPath.class); + + private final ChainedFileWriterConfiguration configuration; + + public FileGrabberFromClassPath(ChainedFileWriterConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public void addFilesToTreate(File extractDirectory, + String inputDirectory, + Set<String> includePatterns, + ChainedFileWriterData result) throws IOException { + + Map<File, List<File>> filesByRoot = result.getFilesByRoot(); + Map<File, List<File>> resourcesByFile = result.getResourcesByFile(); + + // final input directory to use + + File realInputDirectory; + + // use the extracted path as input directory, otherwise there + // will have a problem : the incoming inputPath will not be + // an ancestor of his resources, so prefer to use the extracted + // path which fix this problem. + String inputPath = inputDirectory; + if (inputPath.equals("/")) { + realInputDirectory = extractDirectory; + } else { + realInputDirectory = new File(extractDirectory, inputPath.substring(1)); + } + + List<URL> newUrls = getFiles(inputDirectory, includePatterns); + + List<File> files = filesByRoot.get(realInputDirectory); + if (files == null) { + files = new ArrayList<File>(); + filesByRoot.put(realInputDirectory, files); + } + + for (URL url : newUrls) { + + // get the file + File file = extractFileFromClassPath(extractDirectory, url); + + // add the file in reactor + files.add(file); + + // get resources associated with the file + URL resourceFileUrl = getAssociatedResource(url); + + if (resourceFileUrl == null) { + + // no resource associated with the file + if (log.isDebugEnabled()) { + log.debug("[" + file + "] No resource associated."); + } + + } else { + + // get the resource file + File resourceFile = extractFileFromClassPath(extractDirectory, resourceFileUrl); + if (log.isDebugEnabled()) { + log.debug("[" + file + "] Detected resource " + resourceFile); + } + resourcesByFile.put(file, Collections.singletonList(resourceFile)); + + } + + } + + if (CollectionUtils.isNotEmpty(files)) { + + // check that extracted directory exists, or creates it + boolean b = extractDirectory.exists() || extractDirectory.mkdirs(); + if (!b) { + throw new IOException("Could not create directory " + extractDirectory); + } + + } + + } + + protected List<URL> getFiles(String inputPath, Set<String> includePattern) { + + if (CollectionUtils.isEmpty(includePattern)) { + throw new IllegalArgumentException("Must have at least one include pattern"); + } + + List<URL> result = new ArrayList<URL>(); + + // search in class-path + + ClassLoader loader = configuration.getClassLoader(); + + for (String pattern : includePattern) { + + String path = inputPath; + + //FIXME must change the file.separator to / + if (!path.endsWith("/")) { + path += "/"; + } + path += pattern; + + if (path.startsWith("/")) { + path = path.substring(1); + } + + if (log.isDebugEnabled()) { + log.debug("Try to seek class-path file " + path); + } + + if (pattern.contains("*")) { + + // this is a multi-files to search + List<URL> urlList = Resource.getURLs(path, (URLClassLoader) loader); + if (CollectionUtils.isEmpty(urlList)) { + + log.warn("Could not find in class-path files " + path); + } else { + for (URL url : urlList) { + + if (configuration.isVerbose()) { + log.info("Detected class-path file " + url); + } + result.add(url); + } + } + } else { + + // this is a simple unique search, improve performance + // by searching directly in classloader the resource + URL url = loader.getResource(path); + if (url == null) { + + log.warn("Could not find in class-path the file " + path); + } else { + + if (configuration.isVerbose()) { + log.info("Detected class-path file " + url); + } + result.add(url); + } + } + } + return result; + } + + protected URL getAssociatedResource(URL file) throws IOException { + + // obtain the properties files associated with the file + String path = file.toString(); + + String extension = "." + FileUtil.extension(path); + + String filename = FileUtil.basename(path, extension).concat(".properties"); + + if (log.isDebugEnabled()) { + log.info("path of file : " + path); + log.info("path of resource : " + filename); + } + + URL result; + + URL propertiesFile = URI.create(filename).toURL(); + + if (path.startsWith("file:")) { + + //FIXME-tchemit-2015-05-24 Does this case happen here ? + // local file (not from class-path) + // can test directly on resource if it exists + File file1 = new File(propertiesFile.getFile()); + if (file1.exists()) { + + // resource exist, keep it + result = propertiesFile; + + } else { + + result = null; + + } + + } else { + + InputStream in = null; + try { + in = propertiesFile.openStream(); + + // resource exist, keep it + result = propertiesFile; + + } catch (IOException eee) { + + // resource does not exists + log.warn("Could not find resource " + propertiesFile); + result = null; + + } finally { + + if (in != null) { + in.close(); + } + + } + + } + + return result; + + } + + protected File extractFileFromClassPath(File extractDirectory, URL url) throws IOException { + + String path = url.getPath(); + + // case where file is extracted from jar, "!" found into url, ex: + // url: /home/.../agrosyst-api/target/agrosyst-api-1.0.1-SNAPSHOT.jar!/agrosyst.objectmodel + // path: /home/.../agrosyst-services/target/extracted-sources/model/agrosyst.objectmodel + + // case where file is no extracted from jar, "!" not found into url, ex: + // url: /home/.../agrosyst-api/target/classes/agrosyst.objectmodel + // path: /home/.../agrosyst-services/target/extracted-sources/model/agrosyst.objectmodel + + int index = path.indexOf("!"); + if (index == -1) { + // case where file is no extracted from jar: + index = path.lastIndexOf("/") - 1; // -1: because we need to keep the last "/" from path + } + String relativePath = path.substring(index + 1); + + File f = new File(extractDirectory, relativePath); + if (log.isDebugEnabled()) { + log.debug("extract " + url + " to " + f); + } + File parentFile = f.getParentFile(); + + boolean b = parentFile.exists() || parentFile.mkdirs(); + if (!b) { + throw new IOException("Could not create directory " + f); + } + + FileOutputStream out = new FileOutputStream(f); + try { + IOUtils.copy(url.openStream(), out); + } finally { + out.close(); + } + return f; + + } + +} diff --git a/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromDirectory.java b/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromDirectory.java new file mode 100644 index 0000000..3981dfc --- /dev/null +++ b/eugene/src/main/java/org/nuiton/eugene/writer/FileGrabberFromDirectory.java @@ -0,0 +1,133 @@ +package org.nuiton.eugene.writer; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codehaus.plexus.util.DirectoryScanner; +import org.nuiton.util.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Created on 5/24/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class FileGrabberFromDirectory implements FileGrabber { + + /** Logger. */ + private static final Log log = LogFactory.getLog(FileGrabberFromDirectory.class); + + private final ChainedFileWriterConfiguration configuration; + + public FileGrabberFromDirectory(ChainedFileWriterConfiguration configuration) { + this.configuration = configuration; + } + + @Override + public void addFilesToTreate(File extractDirectory, + String inputDirectory, + Set<String> includePatterns, + ChainedFileWriterData result) throws IOException { + + Map<File, List<File>> filesByRoot = result.getFilesByRoot(); + Map<File, List<File>> resourcesByFile = result.getResourcesByFile(); + + // final input directory to use + + File realInputDirectory = new File(inputDirectory); + + List<File> files = filesByRoot.get(realInputDirectory); + if (files == null) { + files = new ArrayList<File>(); + filesByRoot.put(realInputDirectory, files); + } + + List<File> newFiles = getFiles(inputDirectory, includePatterns); + + for (File file : newFiles) { + + // add the file in reactor + files.add(file); + + // get resources associated with the file + File resourceFile = getAssociatedResource(file); + + if (resourceFile == null) { + + // no resource associated with the file + if (log.isDebugEnabled()) { + log.debug("[" + file + "] No resource associated."); + } + + } else { + + if (configuration.isVerbose() && log.isDebugEnabled()) { + log.debug("[" + file + "] Detected resource " + resourceFile); + } + List<File> resources = new ArrayList<File>(1); + resources.add(resourceFile); + + resourcesByFile.put(file, resources); + + } + + } + + } + + protected List<File> getFiles(String inputPath, Set<String> includePattern) { + + if (CollectionUtils.isEmpty(includePattern)) { + throw new IllegalArgumentException("Must have at least one include pattern"); + } + + List<File> result = new ArrayList<File>(); + + DirectoryScanner ds = new DirectoryScanner(); + File inputDirectory = new File(inputPath); + ds.setBasedir(inputDirectory); + ds.setIncludes(includePattern.toArray(new String[includePattern.size()])); + ds.setExcludes(null); + ds.addDefaultExcludes(); + ds.scan(); + + for (String file : ds.getIncludedFiles()) { + File in = new File(inputDirectory, file); + result.add(in); + } + + return result; + + } + + protected File getAssociatedResource(File file) throws IOException { + + String extension = "." + FileUtil.extension(file.getName()); + + String path = file.getAbsolutePath(); + + String filename = FileUtil.basename(path, extension).concat(".properties"); + + if (log.isDebugEnabled()) { + log.info("path of file : " + path); + log.info("path of resource : " + filename); + } + + File propertiesFile = new File(filename); + + if (!propertiesFile.exists()) { + propertiesFile = null; + } + + return propertiesFile; + + } + +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository eugene. See http://git.nuiton.org/eugene.git commit a86800025483678f25a6592c4c0b4674d1a6fec5 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun May 24 15:12:20 2015 +0200 Use FileGrabber API and remove some logic from ChainedFileWriter (refs #2937) --- .../org/nuiton/eugene/plugin/GenerateMojo.java | 5 +- .../plugin/writer/BaseChainedFileWriter.java | 139 -------- eugene/pom.xml | 5 + .../nuiton/eugene/writer/ChainedFileWriter.java | 53 ++- .../nuiton/eugene/writer/ChainedWriterEngine.java | 13 +- .../eugene/writer/DefaultChainedWriterEngine.java | 368 ++++++--------------- 6 files changed, 140 insertions(+), 443 deletions(-) diff --git a/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateMojo.java b/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateMojo.java index 4e600ae..305e893 100644 --- a/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateMojo.java +++ b/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateMojo.java @@ -408,9 +408,8 @@ public class GenerateMojo extends AbstractPlugin implements ChainedFileWriterCon ); } //FIXME-TC20091217 use a configurator in plexus ? - // Actually we obtain a different instance of the mojo conflit with - // mojo and plexus :) - engine.setConfiguration(this); + // Actually we obtain a different instance of the mojo conflit with mojo and plexus :) + engine.init(this); Set<ChainedFileWriter> availableWriters = engine.getAvailableWriters(); diff --git a/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/writer/BaseChainedFileWriter.java b/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/writer/BaseChainedFileWriter.java index 8ca67b4..ac1d9a8 100644 --- a/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/writer/BaseChainedFileWriter.java +++ b/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/writer/BaseChainedFileWriter.java @@ -25,24 +25,15 @@ package org.nuiton.eugene.plugin.writer; import org.apache.commons.collections.CollectionUtils; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.SystemStreamLog; -import org.codehaus.plexus.util.DirectoryScanner; import org.codehaus.plexus.util.FileUtils; import org.nuiton.eugene.models.object.ObjectModel; import org.nuiton.eugene.models.state.StateModel; import org.nuiton.eugene.writer.AbstractChainedFileWriter; import org.nuiton.eugene.writer.ChainedFileWriterConfiguration; import org.nuiton.util.FileUtil; -import org.nuiton.util.Resource; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; @@ -76,136 +67,6 @@ public abstract class BaseChainedFileWriter extends AbstractChainedFileWriter { } @Override - public final List<URL> getFiles(ChainedFileWriterConfiguration configuration, - String inputPath, - List<String> includePattern, - boolean inClassPath) throws MalformedURLException, IllegalArgumentException { - - if (CollectionUtils.isEmpty(includePattern)) { - throw new IllegalArgumentException("Must have at least one include pattern"); - } - - List<URL> result = new ArrayList<URL>(); - - if (!inClassPath) { - DirectoryScanner ds = new DirectoryScanner(); - File inputDirectory = new File(inputPath); - ds.setBasedir(inputDirectory); - ds.setIncludes(includePattern.toArray(new String[includePattern.size()])); - ds.setExcludes(null); - ds.addDefaultExcludes(); - ds.scan(); - for (String file : ds.getIncludedFiles()) { - File in = new File(inputDirectory, file); - result.add(in.toURI().toURL()); - } - return result; - } - - // search in class-path - - ClassLoader loader = configuration.getClassLoader(); - - for (String pattern : includePattern) { - - String path = inputPath; - - //FIXME must change the file.separator to / - if (!path.endsWith("/")) { - path += "/"; - } - path += pattern; - - if (path.startsWith("/")) { - path = path.substring(1); - } - - if (getLog().isDebugEnabled()) { - getLog().debug("Try to seek class-path file " + path); - } - - if (pattern.contains("*")) { - - // this is a multi-files to search - List<URL> urlList = Resource.getURLs(path, (URLClassLoader) loader); - if (CollectionUtils.isEmpty(urlList)) { - - getLog().warn("Could not find in class-path files " + path); - } else { - for (URL url : urlList) { - - if (configuration.isVerbose()) { - getLog().info("Detected class-path file " + url); - } - result.add(url); - } - } - } else { - - // this is a simple unique search, improve performance - // by searching directly in classloader the resource - URL url = loader.getResource(path); - if (url == null) { - - getLog().warn("Could not find in class-path the file " + path); - } else { - - if (configuration.isVerbose()) { - getLog().info("Detected class-path file " + url); - } - result.add(url); - } - } - } - return result; - } - - @Override - public final List<URL> getResources(URL file) throws IOException { - - // obtain the properties files associated with the file - String path = file.toString(); - - String extension = "." + FileUtil.extension(path); - - String filename = FileUtil.basename(path, extension).concat(".properties"); - - if (getLog().isDebugEnabled()) { - getLog().info("path of file : " + path); - getLog().info("path of resource : " + filename); - } - - URL propertiesFile = URI.create(filename).toURL(); - - if (path.startsWith("file:")) { - - // local file (not from class-path) - // can test directly on resource if it exists - if (!new File(propertiesFile.getFile()).exists()) { - return null; - } - // resource exist, keep it - return Arrays.asList(propertiesFile); - } - InputStream in = null; - try { - in = propertiesFile.openStream(); - - // resource exist, keep it - return Arrays.asList(propertiesFile); - } catch (IOException eee) { - - // resource does not exists - getLog().warn("Could not find resource " + propertiesFile); - return null; - } finally { - if (in != null) { - in.close(); - } - } - } - - @Override protected void initWriter(ChainedFileWriterConfiguration configuration) { super.initWriter(configuration); diff --git a/eugene/pom.xml b/eugene/pom.xml index ab0ec94..15debd8 100644 --- a/eugene/pom.xml +++ b/eugene/pom.xml @@ -121,6 +121,11 @@ <artifactId>snakeyaml</artifactId> </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + </dependency> + </dependencies> <build> diff --git a/eugene/src/main/java/org/nuiton/eugene/writer/ChainedFileWriter.java b/eugene/src/main/java/org/nuiton/eugene/writer/ChainedFileWriter.java index a7f2176..ea68f6a 100644 --- a/eugene/src/main/java/org/nuiton/eugene/writer/ChainedFileWriter.java +++ b/eugene/src/main/java/org/nuiton/eugene/writer/ChainedFileWriter.java @@ -24,7 +24,6 @@ package org.nuiton.eugene.writer; import java.io.File; import java.io.IOException; -import java.net.URL; import java.util.List; import java.util.Map; @@ -139,32 +138,32 @@ public interface ChainedFileWriter { ChainedFileWriterData data) throws IOException; - /** - * Obtain for a given {@code inputDirectory}, all files to treate. - * - * @param configuration the shared configuration - * @param inputPath the input path (can be a directory or a classpath path) - * @param includePattern the include pattern separated by comma - * @param inClassPath a flag to say if we should search in classpath - * @return the list of resources detected - * @throws IOException if any IO pb while searching resources - * @throws IllegalArgumentException if no include pattern given - * @since 2.1.3 - */ - List<URL> getFiles(ChainedFileWriterConfiguration configuration, - String inputPath, - List<String> includePattern, - boolean inClassPath) throws IOException, IllegalArgumentException; - - /** - * Obtain the optional resource files associated to the given file to react. - * - * @param file the file to react - * @return the array of resources associated to the file - * @throws IOException if could not get resources - * @since 2.1.3 - */ - List<URL> getResources(URL file) throws IOException; +// /** +// * Obtain for a given {@code inputDirectory}, all files to treate. +// * +// * @param configuration the shared configuration +// * @param inputPath the input path (can be a directory or a classpath path) +// * @param includePattern the include pattern separated by comma +// * @param inClassPath a flag to say if we should search in classpath +// * @return the list of resources detected +// * @throws IOException if any IO pb while searching resources +// * @throws IllegalArgumentException if no include pattern given +// * @since 2.1.3 +// */ +// List<URL> getFiles(ChainedFileWriterConfiguration configuration, +// String inputPath, +// List<String> includePattern, +// boolean inClassPath) throws IOException, IllegalArgumentException; +// +// /** +// * Obtain the optional resource files associated to the given file to react. +// * +// * @param file the file to react +// * @return the array of resources associated to the file +// * @throws IOException if could not get resources +// * @since 2.1.3 +// */ +// List<URL> getResources(URL file) throws IOException; /** Clear all internal states */ void clear(); diff --git a/eugene/src/main/java/org/nuiton/eugene/writer/ChainedWriterEngine.java b/eugene/src/main/java/org/nuiton/eugene/writer/ChainedWriterEngine.java index 6b9317e..d4f4cce 100644 --- a/eugene/src/main/java/org/nuiton/eugene/writer/ChainedWriterEngine.java +++ b/eugene/src/main/java/org/nuiton/eugene/writer/ChainedWriterEngine.java @@ -42,6 +42,9 @@ import java.util.Set; * @since 2.0.0 */ public interface ChainedWriterEngine { + + void init(ChainedFileWriterConfiguration configuration); + /** * Register in engine a new input source. * <p/> @@ -52,15 +55,6 @@ public interface ChainedWriterEngine { */ void registerInclude(String include); - /** @return the common configuration of engine */ - ChainedFileWriterConfiguration getConfiguration(); - - /** - * Sets the common configuration. - * - * @param configuration the new configuration ot use. - */ - void setConfiguration(ChainedFileWriterConfiguration configuration); /** @return the set of all available writers discovered at runtime */ Set<ChainedFileWriter> getAvailableWriters(); @@ -145,4 +139,5 @@ public interface ChainedWriterEngine { /** clean all internal states */ void clear(); + } diff --git a/eugene/src/main/java/org/nuiton/eugene/writer/DefaultChainedWriterEngine.java b/eugene/src/main/java/org/nuiton/eugene/writer/DefaultChainedWriterEngine.java index cb262f9..18d26d2 100644 --- a/eugene/src/main/java/org/nuiton/eugene/writer/DefaultChainedWriterEngine.java +++ b/eugene/src/main/java/org/nuiton/eugene/writer/DefaultChainedWriterEngine.java @@ -22,18 +22,16 @@ package org.nuiton.eugene.writer; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.io.IOUtils; +import com.google.common.base.Preconditions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -65,20 +63,18 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { /** selected writers obtain while the register phase. */ protected List<ChainedFileWriter> selectedWriters; - @Override + private FileGrabberFromDirectory fileGrabberFromDirectory; + + private FileGrabberFromClassPath fileGrabberFromClassPath; + public ChainedFileWriterConfiguration getConfiguration() { return configuration; } @Override - public void setConfiguration(ChainedFileWriterConfiguration configuration) { - this.configuration = configuration; - } - - @Override public List<ChainedFileWriter> getSelectedWriters() { if (selectedWriters == null) { - checkConfiguration("getSelectedWriters"); + checkInit("getSelectedWriters"); selectedWriters = new ArrayList<ChainedFileWriter>(); } return selectedWriters; @@ -97,7 +93,7 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { @Override public Set<ChainedFileWriter> getAvailableWriters() { if (availableWriters == null) { - checkConfiguration("getAvailableWriters"); + checkInit("getAvailableWriters"); availableWriters = filterWriterForModelType( getConfiguration().getWriters(), getConfiguration().getModelType() @@ -108,7 +104,7 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { @Override public void registerInclude(String include) { - checkConfiguration("registerInclude"); + checkInit("registerInclude"); List<ChainedFileWriter> selectedWriters = getSelectedWriters(); @@ -222,6 +218,7 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { @Override public void clear() { + if (selectedWriters != null) { selectedWriters.clear(); selectedWriters = null; @@ -233,7 +230,22 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { availableWriters.clear(); availableWriters = null; } - setConfiguration(null); + configuration = null; + + fileGrabberFromDirectory = null; + fileGrabberFromClassPath = null; + + } + + @Override + public void init(ChainedFileWriterConfiguration configuration) { + + Preconditions.checkNotNull(configuration, "Configuration can not be null"); + + this.configuration = configuration; + fileGrabberFromDirectory = new FileGrabberFromDirectory(configuration); + fileGrabberFromClassPath = new FileGrabberFromClassPath(configuration); + } @Override @@ -280,45 +292,40 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { @Override public ChainedFileWriterData getData(ChainedFileWriter writer) throws IOException { - checkConfiguration("getData"); - File outputDir = writer.getOutputDirectory( - configuration.getOutputDirectory(), - configuration.isTestPhase() - ); + checkInit("getData"); + File outputDir = writer.getOutputDirectory(configuration.getOutputDirectory(), configuration.isTestPhase()); if (!outputDir.exists()) { if (log.isDebugEnabled()) { - log.debug("[" + writer.getInputProtocol() + - "] Create output directory " + outputDir); + log.debug("[" + writer.getInputProtocol() + "] Create output directory " + outputDir); } boolean b = outputDir.mkdirs(); if (!b) { - throw new IOException("Could not create directory " + - outputDir); + throw new IOException("Could not create directory " + outputDir); } } - // first thing is to merge entries (normal ones and classpath ones) - Map<String, List<String>> normalEntries = new HashMap<String, List<String>>(); - Map<String, List<String>> classpathEntries = new HashMap<String, List<String>>(); + // split directory and classpath entries + Map<String, Set<String>> directoryEntries = new HashMap<String, Set<String>>(); + Map<String, Set<String>> classpathEntries = new HashMap<String, Set<String>>(); for (ChainedFileWriterEntry e : writer.getEntries()) { - Map<String, List<String>> currentMap; - if (e.isUseClassPath()) { + Map<String, Set<String>> currentMap; + if (e.isUseClassPath()) { currentMap = classpathEntries; } else { - currentMap = normalEntries; + currentMap = directoryEntries; } String input = e.getInputPath(); - List<String> includes = currentMap.get(input); + Set<String> includes = currentMap.get(input); if (includes == null) { - includes = new ArrayList<String>(); - currentMap.put(input, includes); + currentMap.put(input, includes = new LinkedHashSet<String>()); } includes.add(e.getIncludePattern()); + } ChainedFileWriterData result = new ChainedFileWriterData(); @@ -326,221 +333,80 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { result.setResourcesByFile(new HashMap<File, List<File>>()); result.setOutputDirectory(outputDir); - // search normal files - addFilesToTreate(writer, normalEntries, result, false); + File extractDirectory = writer.getExtractDirectory(configuration.getExtractDirectory(), + configuration.isTestPhase()); + + // search from directory files + grabFiles(extractDirectory, fileGrabberFromDirectory, directoryEntries, result); // search class-path files - addFilesToTreate(writer, classpathEntries, result, true); + grabFiles(extractDirectory, fileGrabberFromClassPath, classpathEntries, result); return result; - } - - protected void addFilesToTreate(ChainedFileWriter writer, - Map<String, List<String>> entries, - ChainedFileWriterData result, - boolean useClassPath) throws IOException { - - File extractDirectory = writer.getExtractDirectory( - configuration.getExtractDirectory(), - configuration.isTestPhase() - ); - - Map<File, List<File>> filesByRoot = result.getFilesByRoot(); - Map<File, List<File>> resourcesByFile = result.getResourcesByFile(); - - for (Map.Entry<String, List<String>> entry : entries.entrySet()) { - String inputDirectory = entry.getKey(); - - // final input directory to use (for class-path entries, we will - // replace the class-path path by the extracted path - File realInputDirectory = new File(inputDirectory); - - if (useClassPath) { - - // use the extracted path as input directory, otherwise there - // will have a problem : the incoming inputPath will not be - // an ancestor of his resources, so prefer to use the extracted - // path which fix this problem. - String inputPath = inputDirectory; - if (inputPath.equals("/")) { - realInputDirectory = extractDirectory; - } else { - realInputDirectory = - new File(extractDirectory, inputPath.substring(1)); - } - } - - List<String> includePatterns = entry.getValue(); - - List<URL> newUrls = writer.getFiles(configuration, - inputDirectory, - includePatterns, - useClassPath - ); - - List<File> files = filesByRoot.get(realInputDirectory); - if (files == null) { - files = new ArrayList<File>(); - filesByRoot.put(realInputDirectory, files); - } - - for (URL url : newUrls) { - - // get the file - File file = extractFile(extractDirectory, - url, - useClassPath - ); - - // add the file in reactor - files.add(file); - - // get resources associated with the file - List<URL> newResources = writer.getResources(url); - - if (CollectionUtils.isEmpty(newResources)) { - - // no resource associated with the file - if (log.isDebugEnabled()) { - log.debug("[" + file + "] No resource associated."); - } - continue; - } - - // get resources for this file - List<File> resources = new ArrayList<File>(newResources.size()); - for (URL resource : newResources) { - - // get the resource file - File r = extractFile( - extractDirectory, - resource, - useClassPath - ); - if (log.isDebugEnabled()) { - log.debug("[" + file + "] Detected resource " + r); - } - resources.add(r); - } - - resourcesByFile.put(file, resources); - } - - if (useClassPath && CollectionUtils.isNotEmpty(files)) { - - // check that extracted direcotry exists - // create extractDirectory - boolean b = extractDirectory.exists() || - extractDirectory.mkdirs(); - if (!b) { - throw new IOException( - "Could not create directory " + extractDirectory); - } - } - } } - protected File extractFile(File extractDirectory, - URL url, - boolean useClassPath) throws IOException { - - if (!useClassPath) { - return new File(url.getFile()); - } + protected void grabFiles(File extractDirectory, FileGrabber grabber, + Map<String, Set<String>> entries, + ChainedFileWriterData result) throws IOException { - String path = url.getPath(); + for (Map.Entry<String, Set<String>> entry : entries.entrySet()) { - // case where file is extracted from jar, "!" found into url, ex: - // url: /home/.../agrosyst-api/target/agrosyst-api-1.0.1-SNAPSHOT.jar!/agrosyst.objectmodel - // path: /home/.../agrosyst-services/target/extracted-sources/model/agrosyst.objectmodel - - // case where file is no extracted from jar, "!" not found into url, ex: - // url: /home/.../agrosyst-api/target/classes/agrosyst.objectmodel - // path: /home/.../agrosyst-services/target/extracted-sources/model/agrosyst.objectmodel - - int index = path.indexOf("!"); - if (index == -1) { - // case where file is no extracted from jar: - index = path.lastIndexOf("/") - 1; // -1: because we need to keep the last "/" from path - } - String relativePath = path.substring(index + 1); + String inputDirectory = entry.getKey(); + Set<String> includePatterns = entry.getValue(); + grabber.addFilesToTreate(extractDirectory, inputDirectory, includePatterns, result); - File f = new File(extractDirectory, relativePath); - if (log.isDebugEnabled()) { - log.debug("extract " + url + " to " + f); } - File parentFile = f.getParentFile(); - boolean b = parentFile.exists() || parentFile.mkdirs(); - if (!b) { - throw new IOException("Could not create directory " + f); - } - FileOutputStream out = new FileOutputStream(f); - try { - IOUtils.copy(url.openStream(), out); - } finally { - out.close(); - } - return f; } - protected void checkConfiguration(String method) - throws IllegalStateException { - if (getConfiguration() == null) { - throw new IllegalStateException("can not acces to " + method + - "before configuration is set!"); + protected void checkInit(String method) throws IllegalStateException { + if (configuration == null) { + throw new IllegalStateException("Engine was not initialized! Can not access to " + method + + " before the init method was invoked!"); } } - /** - * - */ public enum ModelFileWriterEntryType { ONLY_PROTOCOL_PATTERN("^([a-zA-Z]+)$") { @Override - public ChainedFileWriter getWriter(ChainedWriterEngine engine, + public ChainedFileWriter getWriter(DefaultChainedWriterEngine engine, String include, Matcher matcher) { - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); Set<ChainedFileWriter> universe = engine.getAvailableWriters(); String protocol = matcher.group(1).toLowerCase(); ChainedFileWriter writer = engine.getWriterForInputProtocol( universe, protocol, - configuration.getModelType() + conf.getModelType() ); if (writer == null) { throw new IllegalArgumentException( - "could not find the writer named '" + protocol + - "', use one of " + universe); + "could not find the writer named '" + protocol + "', use one of " + universe); } if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "writer = (" + writer + - ")"); + log.debug("[" + include + "] " + "writer = (" + writer + ")"); } return writer; } @Override public ChainedFileWriterEntry newEntry( - ChainedWriterEngine engine, + DefaultChainedWriterEngine engine, String include, Matcher matcher, ChainedFileWriter writer) { - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "detected pattern (" + - name() + ")"); + log.debug("[" + include + "] " + "detected pattern (" + name() + ")"); } ChainedFileWriterEntry writerEntry = new ChainedFileWriterEntry( - new File(configuration.getBasedir(), - configuration.isTestPhase() ? + new File(conf.getBasedir(), + conf.isTestPhase() ? writer.getDefaultTestInputDirectory() : writer.getDefaultInputDirectory() ).getAbsolutePath(), @@ -552,34 +418,29 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { NO_PROTOCOL_PATTERN_WITH_CLASSPATH("^classpath:([^:]+):([^:]+)$") { @Override - public ChainedFileWriter getWriter(ChainedWriterEngine engine, + public ChainedFileWriter getWriter(DefaultChainedWriterEngine engine, String include, Matcher matcher) { - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); Set<ChainedFileWriter> universe = engine.getAvailableWriters(); - String modelType = configuration.getModelType(); + String modelType = conf.getModelType(); // with no protocol pattern // pattern is discover from the includes // discover the writer from the given pattern - ChainedFileWriter writer = engine.getWriterForInclude( - universe, include, modelType - ); + ChainedFileWriter writer = engine.getWriterForInclude(universe, include, modelType); if (writer == null) { - throw new IllegalArgumentException( - "could not find a writer for include " + include); + throw new IllegalArgumentException("could not find a writer for include " + include); } if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "writer = (" + - writer + ")"); + log.debug("[" + include + "] " + "writer = (" + writer + ")"); } return writer; } @Override - public ChainedFileWriterEntry newEntry(ChainedWriterEngine engine, + public ChainedFileWriterEntry newEntry(DefaultChainedWriterEngine engine, String include, Matcher matcher, ChainedFileWriter writer) { @@ -587,69 +448,57 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { // with no protocol pattern // pattern is discover from the includes if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "detected pattern (" + - name() + ")"); + log.debug("[" + include + "] " + "detected pattern (" + name() + ")"); } String inputPath = matcher.group(1); String includes = matcher.group(2); - ChainedFileWriterEntry writerEntry = new ChainedFileWriterEntry( - inputPath, - includes, - true - ); + ChainedFileWriterEntry writerEntry = new ChainedFileWriterEntry(inputPath, includes, true); return writerEntry; } }, NO_PROTOCOL_PATTERN("^([^:]+):([^:]+)$") { @Override - public ChainedFileWriter getWriter(ChainedWriterEngine engine, + public ChainedFileWriter getWriter(DefaultChainedWriterEngine engine, String include, Matcher matcher) { - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); Set<ChainedFileWriter> universe = engine.getAvailableWriters(); - String modelType = configuration.getModelType(); + String modelType = conf.getModelType(); // with no protocol pattern // pattern is discover from the includes // discover the writer from the given pattern - ChainedFileWriter writer = engine.getWriterForInclude( - universe, include, modelType - ); + ChainedFileWriter writer = engine.getWriterForInclude(universe, include, modelType); if (writer == null) { - throw new IllegalArgumentException( - "could not find a writer for include " + include); + throw new IllegalArgumentException("could not find a writer for include " + include); } if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "writer = (" + - writer + ")"); + log.debug("[" + include + "] " + "writer = (" + writer + ")"); } return writer; } @Override - public ChainedFileWriterEntry newEntry(ChainedWriterEngine engine, + public ChainedFileWriterEntry newEntry(DefaultChainedWriterEngine engine, String include, Matcher matcher, ChainedFileWriter writer) { - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); // with no protocol pattern // pattern is discover from the includes if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "detected pattern (" + - name() + ")"); + log.debug("[" + include + "] " + "detected pattern (" + name() + ")"); } String inputPath = matcher.group(1); String includes = matcher.group(2); ChainedFileWriterEntry writerEntry = new ChainedFileWriterEntry( - new File(configuration.getBasedir(), inputPath).getAbsolutePath(), + new File(conf.getBasedir(), inputPath).getAbsolutePath(), includes ); return writerEntry; @@ -658,41 +507,36 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { FULL_PATTERN_WITH_CLASSPATH("^classpath:(\\w+):([^:]+):([^:]+)$") { @Override - public ChainedFileWriter getWriter(ChainedWriterEngine engine, + public ChainedFileWriter getWriter(DefaultChainedWriterEngine engine, String include, Matcher matcher) { // with full pattern to search in class path (protocol + directory + includes) // pattern is discover from the includes - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); Set<ChainedFileWriter> universe = engine.getAvailableWriters(); String protocol = matcher.group(1).toLowerCase(); - ChainedFileWriter writer = engine.getWriterForInputProtocol( - universe, protocol, configuration.getModelType()); + ChainedFileWriter writer = engine.getWriterForInputProtocol(universe, protocol, conf.getModelType()); if (writer == null) { throw new IllegalArgumentException( - "could not find the writer named '" + protocol + - "', use one of " + universe); + "could not find the writer named '" + protocol + "', use one of " + universe); } if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "writer = (" + writer + - ")"); + log.debug("[" + include + "] " + "writer = (" + writer + ")"); } return writer; } @Override - public ChainedFileWriterEntry newEntry(ChainedWriterEngine engine, + public ChainedFileWriterEntry newEntry(DefaultChainedWriterEngine engine, String include, Matcher matcher, ChainedFileWriter writer) { // with full pattern (protocol + directory + includes) // pattern is discover from the includes if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "detected pattern (" + - name() + ")"); + log.debug("[" + include + "] " + "detected pattern (" + name() + ")"); } String inputPath = matcher.group(2); @@ -709,49 +553,43 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { }, FULL_PATTERN("^(\\w+):([^:]+):([^:]+)$") { @Override - public ChainedFileWriter getWriter(ChainedWriterEngine engine, + public ChainedFileWriter getWriter(DefaultChainedWriterEngine engine, String include, Matcher matcher) { // with full pattern (protocol + directory + includes) // pattern is discover from the includes - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); Set<ChainedFileWriter> universe = engine.getAvailableWriters(); String protocol = matcher.group(1).toLowerCase(); - ChainedFileWriter writer = engine.getWriterForInputProtocol( - universe, protocol, configuration.getModelType()); + ChainedFileWriter writer = engine.getWriterForInputProtocol(universe, protocol, conf.getModelType()); if (writer == null) { throw new IllegalArgumentException( - "could not find the writer named '" + protocol + - "', use one of " + universe); + "could not find the writer named '" + protocol + "', use one of " + universe); } if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "writer = (" + writer + - ")"); + log.debug("[" + include + "] " + "writer = (" + writer + ")"); } return writer; } @Override - public ChainedFileWriterEntry newEntry(ChainedWriterEngine engine, + public ChainedFileWriterEntry newEntry(DefaultChainedWriterEngine engine, String include, Matcher matcher, ChainedFileWriter writer) { - ChainedFileWriterConfiguration configuration = - engine.getConfiguration(); + ChainedFileWriterConfiguration conf = engine.getConfiguration(); // with full pattern (protocol + directory + includes) // pattern is discover from the includes if (log.isDebugEnabled()) { - log.debug("[" + include + "] " + "detected pattern (" + - name() + ")"); + log.debug("[" + include + "] " + "detected pattern (" + name() + ")"); } String inputPath = matcher.group(2); String includes = matcher.group(3); ChainedFileWriterEntry writerEntry = new ChainedFileWriterEntry( - new File(configuration.getBasedir(), inputPath).getAbsolutePath(), + new File(conf.getBasedir(), inputPath).getAbsolutePath(), includes ); return writerEntry; @@ -774,12 +612,12 @@ public class DefaultChainedWriterEngine implements ChainedWriterEngine { } public abstract ChainedFileWriterEntry newEntry( - ChainedWriterEngine engine, + DefaultChainedWriterEngine engine, String include, Matcher matcher, ChainedFileWriter writer); public abstract ChainedFileWriter getWriter( - ChainedWriterEngine engine, + DefaultChainedWriterEngine engine, String include, Matcher matcher); } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository eugene. See http://git.nuiton.org/eugene.git commit d3afd29495e23beb65164ac7ee0b6b956faafc4b Merge: 2790fd3 a868000 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun May 24 15:13:40 2015 +0200 fixes #2937: Plugin fail to generate data if project is in a path containing a space Merge branch 'feature/2937' into develop .../org/nuiton/eugene/plugin/GenerateMojo.java | 5 +- .../plugin/writer/BaseChainedFileWriter.java | 139 -------- eugene/pom.xml | 5 + .../nuiton/eugene/writer/ChainedFileWriter.java | 53 ++- .../nuiton/eugene/writer/ChainedWriterEngine.java | 13 +- .../eugene/writer/DefaultChainedWriterEngine.java | 368 ++++++--------------- .../java/org/nuiton/eugene/writer/FileGrabber.java | 22 ++ .../eugene/writer/FileGrabberFromClassPath.java | 285 ++++++++++++++++ .../eugene/writer/FileGrabberFromDirectory.java | 133 ++++++++ 9 files changed, 580 insertions(+), 443 deletions(-) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm