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.util.Timer;
19  
20  import com.googlecode.t7mp.PluginLog;
21  import com.googlecode.t7mp.Stoppable;
22  
23  /**
24   * Scanner that creates a Timer with a TimerTask.
25   * 
26   * @author Joerg Bellmann
27   *
28   */
29  public final class Scanner implements Stoppable {
30  
31      private static final int DEFAULT_DELAY = 10000;
32      private static final int MILLIS = 1000;
33  
34      private final ScannerConfiguration scannerConfiguration;
35      private Timer timer;
36      private final PluginLog log;
37  
38      public Scanner(ScannerConfiguration scannerConfiguration, PluginLog log) {
39          this.scannerConfiguration = scannerConfiguration;
40          this.log = log;
41      }
42  
43      public void start() {
44          log.info("Starting Scanner ....");
45          this.timer = new Timer();
46          this.timer.scheduleAtFixedRate(new ModifiedFileTimerTask(scannerConfiguration.getRootDirectory(),
47                  scannerConfiguration.getWebappDirectory(), scannerConfiguration.getEndingsAsList(), log),
48                  DEFAULT_DELAY, scannerConfiguration.getInterval() * MILLIS);
49          log.info("Scanner started");
50      }
51  
52      public void stop() {
53          log.info("Stopping Scanner ...");
54          if (this.timer == null) {
55              return;
56          }
57          this.timer.cancel();
58          log.info("Scanner stopped");
59      }
60  
61  }