1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }
85
86 }
87
88
89 }
90 else {
91 log.error("Dir " +d.getAbsolutePath() +" was not found");
92 throw new AlertError("Dir " +d.getAbsolutePath() +" was not found");
93 }
94 }
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 }
105
106
107 DirTest t = new DirTest("main");
108 t.dirs = new Vector();
109 t.dirs.add(dir);
110 t.age= 5*60*1000L;
111 t.testFileAge();
112 } catch (Error e) {
113 System.out.println("Test failed: " + e);
114
115 }
116
117 }
118 }