View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: DirTest.java,v 1.1.1.1 2004/05/19 12:33:41 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.alert.agent;
21  import java.util.List;
22  import java.util.Iterator;
23  import java.util.Vector;
24  import java.util.Date;
25  import java.io.File;
26  import org.backsource.alert.AlertError;
27  import org.backsource.alert.PropertyUtil;
28  import org.backsource.alert.PropertyConstants;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /***
34   * Test if files in directories are older than a certains amount of milliseconds.
35   *
36   * <p>Used to check that amsterdam channels are alive.</p>
37   <pre>
38   * Properties examples:
39   tests.dir.fileage=300000
40   tests.dir.dirs=/home/pra/tmp/testdir
41   </pre>
42   * <p>It uses two properties {@link org.backsource.alert.PropertyConstants#DIR_DIRS} and {@link org.backsource.alert.PropertyConstants#DIR_FILE_AGE}.</p>
43   *
44   *
45   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
46   * @version $Revision: 1.1.1.1 $
47   */
48  
49  public class DirTest extends BaseCase{
50     static final Log log = LogFactory.getLog(DirTest.class);
51     List dirs=null;
52     long age = -1;
53     public DirTest (String name){
54        super(name);
55     }
56  
57     protected void setUp() throws Exception{
58        super.setUp();
59        String dirsP = prop.getProperty(PropertyConstants.DIR_DIRS);
60        dirs = PropertyUtil.getValues(dirsP);
61        age = Long.parseLong(prop.getProperty(PropertyConstants.DIR_FILE_AGE));
62        
63     }
64  
65     /***
66      * Agent/test method.
67      *
68      * <p>Check if any of the directories given in  {@link org.backsource.alert.PropertyConstants#DIR_DIRS} contains files older the value given in {@link org.backsource.alert.PropertyConstants#DIR_FILE_AGE}.</p>
69      */
70     public void testFileAge() {
71        for (Iterator  i = dirs.iterator(); i.hasNext();){
72           File d = new File((String)i.next());
73           if ( d.exists() && d.isDirectory()) {
74              
75              
76              File[] files = d.listFiles();
77              long now = new Date().getTime();
78              for (int  j = 0; j < files.length; j++) {
79                 long mod = files[j].lastModified();
80                 log.debug("Checking file " + files[j] + " in " +d.getAbsolutePath());
81                 if ( (now - mod) > age) {
82                    log.error("Channel seems to be dead. File " + files[j] + " in " + d + " is older than " + age);
83                    throw new AlertError("Channel seems to be dead. File " + files[j] + " in " + d + " is older than " + age);
84                 } // end of if ()
85                 
86              } // end of for (int  = 0;  < ; ++)
87              
88              
89           } // end of if ()
90           else {
91              log.error("Dir " +d.getAbsolutePath() +" was not found");
92              throw new AlertError("Dir " +d.getAbsolutePath() +" was not found");
93           }
94        } // end of for (Iterator  = .iterator(); .hasNext();)
95        
96  
97     }
98     
99     public static void main(String[] args){
100       try {
101          String dir = ".";
102          if (args.length > 0) {
103             dir = args[0];
104          } // end of if ()
105          
106 
107          DirTest t = new DirTest("main");
108          t.dirs = new Vector();
109          t.dirs.add(dir);
110          t.age= 5*60*1000L;//5 minutes
111          t.testFileAge();
112       } catch (Error e) {
113          System.out.println("Test failed: " + e);
114       
115       } // end of try-catch
116       
117    }
118 } // DirTest