View Javadoc

1   /**
2    * Copyright (C) 2010-2012 Joerg Bellmann <joerg.bellmann@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.googlecode.t7mp.scanner;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.Set;
23  import java.util.TimerTask;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.commons.io.FilenameUtils;
27  
28  import com.google.common.base.Predicates;
29  import com.google.common.collect.Collections2;
30  import com.googlecode.t7mp.PluginLog;
31  import com.googlecode.t7mp.util.FileUtil;
32  
33  public final class ModifiedFileTimerTask extends TimerTask {
34  
35      private static final String DEF_STATIC = FilenameUtils.separatorsToSystem("src/main/webapp/");
36      private static final String DEF_CLASSES = FilenameUtils.separatorsToSystem("target/classes/");
37      private long lastrun = System.currentTimeMillis();
38      private final File rootDirectory;
39      private final File webappDirectory;
40      private final List<String> suffixe;
41      private final PluginLog log;
42  
43      public ModifiedFileTimerTask(File rootDirectory, File webappDirectory, List<String> suffixe, PluginLog log) {
44          this.rootDirectory = rootDirectory;
45          this.webappDirectory = webappDirectory;
46          this.suffixe = suffixe;
47          this.log = log;
48      }
49  
50      @Override
51      public void run() {
52          long timeStamp = lastrun;
53          lastrun = System.currentTimeMillis();
54          Set<File> fileSet = FileUtil.getAllFiles(rootDirectory);
55          Collection<File> changedFiles = Collections2.filter(fileSet,
56                  Predicates.and(new ModifiedFilePredicate(timeStamp), new FileSuffixPredicate(suffixe)));
57          for (File file : changedFiles) {
58              String absolutePath = file.getAbsolutePath();
59              String def = getResourceDef(absolutePath);
60  
61              int endIndex = absolutePath.lastIndexOf(def);
62              String copyFragment = absolutePath.substring(endIndex + def.length());
63              File copyToFile = new File(webappDirectory, copyFragment);
64              log.debug("CHANGED: " + absolutePath);
65              log.debug("COPY TO : " + copyToFile.getAbsolutePath());
66              try {
67                  FileUtils.copyFile(file, copyToFile);
68                  FileUtils.touch(copyToFile);
69              } catch (IOException e) {
70                  e.printStackTrace();
71              }
72          }
73          log.debug("-----------END SCAN-------------");
74      }
75  
76      private String getResourceDef(String absolutePath) {
77          if (absolutePath.lastIndexOf(DEF_STATIC) != -1) {
78              return DEF_STATIC;
79          } else {
80              return DEF_CLASSES;
81          }
82      }
83  }