KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > capture > CapturedLogs


1 /**
2  * $Id: CapturedLogs.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.capture;
30
31 import com.idaremedia.antx.AntX;
32 import com.idaremedia.antx.AntXFixture;
33 import com.idaremedia.antx.FixtureAdministrator;
34 import com.idaremedia.antx.FixtureOverlays;
35 import com.idaremedia.antx.FixtureIds;
36 import com.idaremedia.antx.KillMethod;
37 import com.idaremedia.antx.KillMethodSkeleton;
38 import com.idaremedia.antx.apis.ProblemHandler;
39 import com.idaremedia.antx.helpers.Strings;
40
41 /**
42  * Manages iteration-based log recorders for the active thread. The {@linkplain
43  * AssertLoggedTask &lt;assertlogged&gt;} task uses this class to determine source of
44  * logs to be verified.
45  *
46  * @since JWare/AntX 0.2
47  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
48  * @version 0.5
49  * @.safety single
50  * @.group impl,infra
51  * @see AssertLoggedTask
52  * @see CaptureLogsTask
53  **/

54
55 public final class CapturedLogs implements FixtureAdministrator
56 {
57     private static final String JavaDoc WHOAMI_ = "CapturedLogs";
58
59
60     /**
61      * The iteration category for all captured logs's information.
62      **/

63     public static final String JavaDoc ANTLOG=
64         FixtureIds.ANT_LOGS_RECORDER;
65
66
67     /**
68      * The iteration category for all captured stdio information.
69      **/

70     public static final String JavaDoc SYSTEM=
71         FixtureIds.SYSTEM_STREAMS_RECORDER;
72
73
74
75     /**
76      * Returns the current thread's frontmost LogsRecorder instance
77      * if one exists. Returns <i>null</i> if no recorder installed
78      * for executing thread.
79      **/

80     public static LogsRecorder getRecorder()
81     {
82         FixtureOverlays context = FixtureOverlays.getContextInstance();
83         return (LogsRecorder)context.nearest(ANTLOG);
84     }
85
86
87
88     /**
89      * Helper to {@linkplain #getRecorder getRecorder} that returns
90      * the LogsRecorder {@linkplain EmptyLogs placeholder} if the current
91      * thread has no installed recorders. Never returns <i>null</i>.
92      **/

93     public static LogsRecorder getRecorderNoNull()
94     {
95         LogsRecorder r = getRecorder();
96         return (r!=null) ? r : EmptyLogs.INSTANCE;
97     }
98
99
100
101     /**
102      * Installs a new LogsRecorder instance for the current thread. This
103      * recorder becomes the active (and only visible) recorder until it
104      * is unwound or another recorder is installed.
105      * @param lr the new logs recorder (non-null)
106      * @param noInstallHandler [optional] used to if unable to install recorder
107      * @return previous recorder if any (can be <i>null</i>)
108      * @throws BuildException if incoming recorder already on iteration stack
109      **/

110     public static LogsRecorder installRecorder(LogsRecorder lr,
111                                                ProblemHandler noInstallHandler)
112     {
113         if (lr==null) {
114             throw new IllegalArgumentException JavaDoc
115                 (AntX.uistrs().get("brul.logs.nul.recorder"));
116         }
117         return (LogsRecorder)FixtureOverlays.installIfNot
118             (ANTLOG, lr, noInstallHandler, WHOAMI_);
119     }
120
121
122
123     /**
124      * Removes the most recently installed LogsRecorder instance for the
125      * current thread. The previous recorder is reactivated, or if this was
126      * the only recorder, the current thread's logs recorder becomes
127      * undefined (should use EmptyLogs placeholder if necessary).
128      * @param noUninstallHandler [optional] used to if unable to uninstall
129      **/

130     public static void unwindRecorder(ProblemHandler noUninstallHandler)
131     {
132         FixtureOverlays.uninstallIfIs(ANTLOG,noUninstallHandler,WHOAMI_);
133     }
134
135
136
137     /**
138      * Returns the current thread's frontmost stdio LogsRecorder
139      * instance if one exists. Returns <i>null</i> if no recorder installed
140      * for executing thread.
141      * @since JWare/AntX 0.3
142      **/

143     public static LogsRecorder getStdIORecorder()
144     {
145         FixtureOverlays context = FixtureOverlays.getContextInstance();
146         return (LogsRecorder)context.nearest(SYSTEM);
147     }
148
149
150
151     /**
152      * Helper to {@linkplain #getStdIORecorder getStdIORecorder} that
153      * returns the LogsRecorder {@linkplain EmptyLogs placeholder} if
154      * the current thread has no installed stdio recorders. Never
155      * returns <i>null</i>.
156      * @since JWare/AntX 0.3
157      **/

158     public static LogsRecorder getStdIORecorderNoNull()
159     {
160         LogsRecorder r = getStdIORecorder();
161         return (r!=null) ? r : EmptyLogs.INSTANCE;
162     }
163
164
165
166     /**
167      * Installs a new stdio streams LogsRecorder instance for the current
168      * thread. This recorder becomes the active (and only visible) stdio
169      * recorder until it is unwound or another recorder is installed.
170      * @param lr the new stdio output recorder (non-null)
171      * @param noInstallHandler [optional] used to if unable to install recorder
172      * @return previous recorder if any (can be <i>null</i>)
173      * @throws BuildException if incoming recorder already on iteration stack
174      * @since JWare/AntX 0.3
175      **/

176     public static LogsRecorder installStdIORecorder(LogsRecorder lr,
177                                               ProblemHandler noInstallHandler)
178     {
179         if (lr==null) {
180             throw new IllegalArgumentException JavaDoc
181                 (AntX.uistrs().get("brul.logs.nul.recorder"));
182         }
183         return (LogsRecorder)FixtureOverlays.installIfNot
184             (SYSTEM, lr, noInstallHandler, WHOAMI_);
185     }
186
187
188
189     /**
190      * Removes the most recently installed stdio stream LogsRecorder instance
191      * for the current thread. The previous recorder is reactivated, or if this
192      * was the only recorder, the current thread's logs recorder becomes
193      * undefined (should use EmptyLogs placeholder if necessary).
194      * @param noUninstallHandler [optional] used to if unable to uninstall
195      * @since JWare/AntX 0.3
196      **/

197     public static void unwindStdIORecorder(ProblemHandler noUninstallHandler)
198     {
199         FixtureOverlays.uninstallIfIs(SYSTEM,noUninstallHandler,WHOAMI_);
200     }
201
202
203
204     /** Disallow; only public static utility methods. **/
205     private CapturedLogs()
206     { }
207
208
209
210     /**
211      * Installs test-harness cleanup method to clear up the various output
212      * recorder components. Your application should never use this helper
213      * directly; it is provided so that test harnesses can reset the environment
214      * to a known state.
215      * @since JWare/AntX 0.4
216      **/

217     static {
218         AntXFixture.setKillMethod
219             (SYSTEM,
220              new String JavaDoc[] {"stdio","stdout","stderr"},
221              new KillMethodSkeleton(SYSTEM, WHOAMI_));
222
223         AntXFixture.setKillMethod
224             (ANTLOG,
225              new String JavaDoc[] {"antlogs"},
226              new KillMethodSkeleton(ANTLOG, WHOAMI_));
227
228
229         //Both
230
AntXFixture.setKillMethod
231             (FixtureIds.CAPTURED_LOG_OUTPUTS,
232              new String JavaDoc[] {"iocapture", "outputrecorders"},
233              new KillMethod() {
234                      public boolean kill(ProblemHandler from) {
235                          FixtureOverlays iteration = FixtureOverlays.getContextInstance();
236                          synchronized(iteration) {
237                              iteration.clear(CapturedLogs.ANTLOG);
238                              iteration.clear(CapturedLogs.SYSTEM);
239                          }
240                          return true;
241                      }
242                      public boolean kill(String JavaDoc target, ProblemHandler from) {
243                          if (Strings.ALL.equals(target)) {
244                              return kill(from);
245                          }
246                          return true;
247                      }
248                  }
249              );
250     }
251 }
252
253 /* end-of-CapturedLogs.java */
254
Popular Tags