View Javadoc
1   package org.nuiton.eugene.writer;
2   
3   /*
4    * #%L
5    * EUGene :: EUGene
6    * %%
7    * Copyright (C) 2004 - 2015 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import org.apache.commons.collections4.CollectionUtils;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.codehaus.plexus.util.DirectoryScanner;
29  import org.nuiton.util.FileUtil;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  
38  /**
39   * Created on 5/24/15.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 3.0
43   */
44  public class FileGrabberFromDirectory implements FileGrabber {
45  
46      /** Logger. */
47      private static final Log log = LogFactory.getLog(FileGrabberFromDirectory.class);
48  
49      private final ChainedFileWriterConfiguration configuration;
50  
51      public FileGrabberFromDirectory(ChainedFileWriterConfiguration configuration) {
52          this.configuration = configuration;
53      }
54  
55      @Override
56      public void addFilesToTreate(File extractDirectory,
57                                   String inputDirectory,
58                                   Set<String> includePatterns,
59                                   ChainedFileWriterData result) throws IOException {
60  
61          Map<File, List<File>> filesByRoot = result.getFilesByRoot();
62          Map<File, List<File>> resourcesByFile = result.getResourcesByFile();
63  
64          // final input directory to use
65  
66          File realInputDirectory = new File(inputDirectory);
67  
68          List<File> files = filesByRoot.get(realInputDirectory);
69          if (files == null) {
70              files = new ArrayList<>();
71              filesByRoot.put(realInputDirectory, files);
72          }
73  
74          List<File> newFiles = getFiles(inputDirectory, includePatterns);
75  
76          for (File file : newFiles) {
77  
78              // add the file in reactor
79              files.add(file);
80  
81              // get resources associated with the file
82              File resourceFile = getAssociatedResource(file);
83  
84              if (resourceFile == null) {
85  
86                  // no resource associated with the file
87                  if (log.isDebugEnabled()) {
88                      log.debug("[" + file + "] No resource associated.");
89                  }
90  
91              } else {
92  
93                  if (configuration.isVerbose() && log.isDebugEnabled()) {
94                      log.debug("[" + file + "] Detected resource " + resourceFile);
95                  }
96                  List<File> resources = new ArrayList<>(1);
97                  resources.add(resourceFile);
98  
99                  resourcesByFile.put(file, resources);
100 
101             }
102 
103         }
104 
105     }
106 
107     protected List<File> getFiles(String inputPath, Set<String> includePattern) {
108 
109         if (CollectionUtils.isEmpty(includePattern)) {
110             throw new IllegalArgumentException("Must have at least one include pattern");
111         }
112 
113         List<File> result = new ArrayList<>();
114 
115         DirectoryScanner ds = new DirectoryScanner();
116         File inputDirectory = new File(inputPath);
117         ds.setBasedir(inputDirectory);
118         ds.setIncludes(includePattern.toArray(new String[includePattern.size()]));
119         ds.setExcludes(null);
120         ds.addDefaultExcludes();
121         ds.scan();
122 
123         for (String file : ds.getIncludedFiles()) {
124             File in = new File(inputDirectory, file);
125             result.add(in);
126         }
127 
128         return result;
129 
130     }
131 
132     protected File getAssociatedResource(File file) throws IOException {
133 
134         String extension = "." + FileUtil.extension(file.getName());
135 
136         String path = file.getAbsolutePath();
137 
138         String filename = FileUtil.basename(path, extension).concat(".properties");
139 
140         if (log.isDebugEnabled()) {
141             log.info("path of file : " + path);
142             log.info("path of resource : " + filename);
143         }
144 
145         File propertiesFile = new File(filename);
146 
147         if (!propertiesFile.exists()) {
148             propertiesFile = null;
149         }
150 
151         return propertiesFile;
152 
153     }
154 
155 }