KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > test > TestMonitor


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * TestMonitor.java
20  *
21  * This file contains the definition of the test monitor. It can be setup to
22  * monitor a test such as the loading of a test jar.
23  */

24
25 // The package path
26
package com.rift.coad.lib.deployment.test;
27
28 // java imports
29
import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.Date JavaDoc;
33
34 // coadunation imports
35
import com.rift.coad.lib.configuration.Configuration;
36 import com.rift.coad.lib.configuration.ConfigurationFactory;
37
38 /**
39  * This file contains the definition of the test monitor. It can be setup to
40  * monitor a test such as the loading of a test jar.
41  *
42  * @author Brett Chaldecott
43  */

44 public class TestMonitor {
45     
46     // class static singleton object
47
private static TestMonitor singleton = null;
48     
49     // the class member variables
50
private long timeout = 0;
51     private List JavaDoc entries = null;
52     
53     
54     /**
55      * Creates a new instance of TestMonitor
56      *
57      * @exception TestException
58      */

59     private TestMonitor() throws TestException {
60         try {
61             // init the array list
62
entries = new ArrayList JavaDoc();
63             
64             // retrieve the configuration
65
Configuration config = ConfigurationFactory.getInstance().getConfig(
66                         this.getClass());
67             
68             // create the string tokenizer
69
timeout = config.getLong("Timeout");
70             StringTokenizer JavaDoc stringTok = new StringTokenizer JavaDoc(
71                     config.getString("Monitor"),",");
72             while (stringTok.hasMoreTokens()) {
73                 entries.add(stringTok.nextToken());
74             }
75             
76         } catch (Exception JavaDoc ex) {
77             throw new TestException("Failed to init the test monitor.",ex);
78         }
79     }
80     
81     
82     /**
83      * This method is responsible for initializing the test monitor singleton.
84      *
85      * @exception TestException
86      */

87     public static synchronized void init() throws TestException {
88         singleton = new TestMonitor();
89     }
90     
91     
92     /**
93      * This method returns an instance of the test monitor object.
94      *
95      * @return The reference to the test object.
96      * @exception TestException
97      */

98     public static synchronized TestMonitor getInstance() throws TestException {
99         if (singleton != null) {
100             return singleton;
101         }
102         throw new TestException("The test monitor has not been initialized.");
103     }
104     
105     
106     /**
107      * This method alerts the monitoring thread to the fact that it is complete.
108      *
109      * @param element The element to monitor.
110      * @exception TestException
111      */

112     public synchronized void alert(String JavaDoc element) throws TestException {
113         if (entries.contains(element)) {
114             entries.remove(element);
115             notify();
116         }
117     }
118     
119     
120     /**
121      * This method will be called to monitor the test to determine if the test
122      * was successfull.
123      *
124      * @exception TestException
125      */

126     public synchronized void monitor() throws TestException {
127         try {
128             Date JavaDoc startTime = new Date JavaDoc();
129             while (entries.size() > 0) {
130                 wait(timeout);
131                 Date JavaDoc currentTime = new Date JavaDoc();
132                 if ((currentTime.getTime() - timeout) > startTime.getTime()) {
133                     break;
134                 }
135             }
136         } catch (Exception JavaDoc ex) {
137             throw new TestException("Monitoring failed because : " +
138                     ex.getMessage(),ex);
139         }
140         
141         // the entries to monitor
142
if (entries.size() > 0) {
143             String JavaDoc entryString = new String JavaDoc();
144             String JavaDoc sep = "";
145             for (int count = 0; count < entries.size(); count++) {
146                 entryString += sep + entries.get(count).toString();
147                 sep = ":";
148             }
149             throw new TestException(
150                     "Monitor test failed because not all monitored entries " +
151                     "reported successfully [" + entryString + "]");
152         }
153     }
154 }
155
Popular Tags