View Javadoc

1   package org.csc.phynixx.loggersystem.logger.channellogger;
2   
3   /*
4    * #%L
5    * phynixx-common
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import java.io.File;
25  
26  /**
27   * traverses all logfile an notifies a given callback. This callback can collect any information.
28   */
29  class LogFileTraverser {
30  
31      public interface ICollectorCallback {
32          void match(File file, LogFilenameMatcher.LogFilenameParts parts);
33      }
34  
35      private LogFilenameMatcher logFileMatcher = null;
36  
37  
38      LogFileTraverser(LogFilenameMatcher logFileMatcher, File startDirectory, ICollectorCallback cb) {
39          super();
40          this.logFileMatcher = logFileMatcher;
41          if (startDirectory.exists() && startDirectory.isDirectory()) {
42              this.iterateDirectory(startDirectory, cb);
43          }
44      }
45  
46  
47      private void iterateDirectory(File startDirectory, ICollectorCallback cb) {
48          File[] files = startDirectory.listFiles();
49          if (files == null || files.length == 0) {
50              return;
51          }
52          for (int i = 0; i < files.length; i++) {
53              if (files[i].isDirectory()) {
54                  iterateDirectory(files[i], cb);
55              } else {
56                  LogFilenameMatcher.LogFilenameParts parts =
57                          this.logFileMatcher.matches(files[i].getName());
58                  if (parts != null) {
59                      cb.match(files[i], parts);
60                  }
61              }
62          }
63  
64      }
65  
66  
67  }