KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > Problems


1 /**
2  * $Id: Problems.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;
30
31 import org.apache.tools.ant.Location;
32 import org.apache.tools.ant.Task;
33
34 import com.idaremedia.antx.apis.ProblemHandler;
35 import com.idaremedia.antx.apis.Requester;
36 import com.idaremedia.antx.apis.ScriptLocatable;
37
38 /**
39  * Administers problem handlers and other error-related fixture information. Usually
40  * a default fixture problem handler is installed by an early initialization build
41  * target. Local (scoped) problem handlers are not addressed because the application
42  * can install scoped handlers via the AntX <span class="src">&lt;protect&gt;</span>
43  * flowcontrol taskset.
44  *
45  * @since JWare/AntX 0.3
46  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
47  * @version 0.5
48  * @.safety guarded
49  * @.group impl,infra
50  * @see ProblemHandler
51  * @see com.idaremedia.antx.apis.Responses
52  **/

53
54 public class Problems implements FixtureCore, FixtureAdministrator
55 {
56     /**
57      * The fixture component id for all problem administration.
58      **/

59     public static final String JavaDoc FXID= FixtureIds.PROBLEM_ADMINISTRATION;
60
61
62
63     /**
64      * Problem handler that silently ignores the problem. Not null.
65      **/

66     public static final ProblemHandler IgnoringHandler=
67         new ProblemHandler() {
68                 public void problem(Object JavaDoc nugget, int nl) {
69                     //nada
70
}
71             };
72
73
74
75
76     /**
77      * Returns the location of a helper component or its client if
78      * helper has no location. Never returns <i>null</i>.
79      * @param calr requester (non-null)
80      * @param impl helper's location if known
81      * @since JWare/AntX 0.5
82      **/

83     public static final Location getHelperLocation(Task impl, Requester calr)
84     {
85         Location l = impl.getLocation();
86         if (l==Location.UNKNOWN_LOCATION) {
87             l = calr.getLocation();
88         }
89         return l==null ? Location.UNKNOWN_LOCATION : l;
90     }
91
92
93
94     /**
95      * Returns the location of a calling client or the service if
96      * client is anonymous. Never returns <i>null</i>.
97      * @param impl the helper (non-null)
98      * @param calr the client (non-null)
99      * @since JWare/AntX 0.5
100      **/

101     public static final Location getCallerLocation(Requester calr, ScriptLocatable impl)
102     {
103         Location l = calr.getLocation();
104         if (l==Location.UNKNOWN_LOCATION) {
105             l = impl.getLocation();
106         }
107         return l==null ? Location.UNKNOWN_LOCATION : l;
108     }
109
110
111
112     /**
113      * Returns the current thread's ProblemHandler if one exists. Returns
114      * <i>null</i> if no ProblemHandler explicitly installed for thread.
115      **/

116     public static ProblemHandler getDefaultHandler()
117     {
118         return (ProblemHandler)FixtureOverlays.getContextInstance().nearest(FXID);
119     }
120
121
122
123     /**
124      * Helper to {@linkplain #getDefaultHandler getDefaultHandler} that
125      * returns the {@linkplain #IgnoringHandler} if the current thread
126      * has no explicit handler. Never returns <i>null</i>.
127      **/

128     public static ProblemHandler getDefaultHandlerNoNull()
129     {
130         ProblemHandler ph = getDefaultHandler();
131         return (ph!=null) ? ph : IgnoringHandler;
132     }
133
134
135
136     /**
137      * Installs a new default ProblemHandler for the current thread. This handler
138      * becomes the active (and only administered) problem handler until it is
139      * unwound or another handler is installed.
140      * @param ph the new (default) problem handler (non-null)
141      * @param noInstallHandler [optional] used to notify if cannot install
142      * @return previous problem handler if any (can be <i>null</i>)
143      * @throws BuildException if incoming handler already on iteration stack
144      **/

145     public static ProblemHandler installDefaultHandler(ProblemHandler ph,
146                                                   ProblemHandler noInstallHandler)
147     {
148         if (ph==null) {
149             throw new IllegalArgumentException JavaDoc
150                 (AntX.uistrs().get("problems.null.handlr"));
151         }
152         return (ProblemHandler)FixtureOverlays.installIfNot
153                             (FXID, ph, noInstallHandler, "ProblemsHandler");
154     }
155
156
157
158     /**
159      * Removes the most recently installed ProblemHandler for the current
160      * thread. The previous installation is reactivated, or if this was
161      * the only problem handler, the current thread's uncaught problem
162      * handler is not undefined.
163      * @param noUninstallHandler [optional]used to notify if cannot uninstall
164      * @see com.idaremedia.antx.apis.Responses
165      **/

166     public static void unwindDefaultHandler(ProblemHandler noUninstallHandler)
167     {
168         FixtureOverlays.uninstallIfIs(FXID,noUninstallHandler,"ProblemsHandler");
169     }
170
171
172
173     /**
174      * Installs test-harness cleanup method to clear up the various problem
175      * handling fixture components. Your application should never use this
176      * helper directly; it is provided so that test harnesses can reset the
177      * environment to a known state.
178      * @since JWare/AntX 0.4
179      **/

180     static {
181         AntXFixture.setKillMethod
182             (FXID,
183              new String JavaDoc[]{"problems","problemhandlers"},
184              new KillMethodSkeleton(FXID, "ProblemsHandler"));
185     }
186
187
188     /** Disallow; only public static utility methods. **/
189     private Problems()
190     { }
191 }
192
193 /* end-of-Problems.java */
194
Popular Tags