KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: AntXFixture.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2004-2005,2007 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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.util.FileUtils;
37
38 import com.idaremedia.apis.FixtureStarter;
39
40 import com.idaremedia.antx.apis.BuildError;
41 import com.idaremedia.antx.apis.ProblemHandler;
42 import com.idaremedia.antx.helpers.Empties;
43
44 /**
45  * The AntX global fixture. The AntXFixture serves as a factory for standard Java and Ant
46  * data types as well as a controller that resets (or recreates) various core AntX fixture
47  * components in between execution iterations. Every administrator of fixture data must
48  * define a iteration-bound clean-up method by supplying a {@linkplain KillMethod}
49  * callback.
50  * <p/>
51  * The AntXFixture differs from the {@linkplain Iteration} in that it is global,
52  * always exists, and is <em>independent</em> of any particular execution iteration.
53  * For instance, kill methods are registered at the class level, not the iteration level.
54  * Multiple iterations will use the same kill method to reset certain components.
55  *
56  * @since JWare/AntX 0.4
57  * @author ssmc, &copy;2004-2005,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
58  * @version 0.5.1
59  * @.safety guarded
60  * @.group impl,infra
61  * @.pattern GoF.Facade
62  * @see Iteration
63  **/

64
65 public final class AntXFixture extends FixtureStarter implements FixtureCore
66 {
67     private static final String JavaDoc IAM_= "AntX.Fixture:";
68
69
70
71     /**
72      * Returns a shareable <span class="src">FileUtils</span>
73      * helper instance.
74      * @since JWare/AntX 0.4
75      **/

76     public static FileUtils fileUtils()
77     {
78         return FileUtils.getFileUtils();
79     }
80
81
82
83     /**
84      * Returns the kill method associated with target. Will return
85      * <i>null</i> if nothing registered for target.
86      **/

87     static KillMethod getKillMethod(String JavaDoc target)
88     {
89         return (KillMethod)m_killMethods.get(target);
90     }
91
92
93
94     /**
95      * Defines the fixture cleanup method for a specific administrator.
96      * This method is used by the fixture component class when it is loaded.
97      * @param component fixture administrator or component (non-null)
98      * @param method cleanup method (non-null)
99      * @throws BuildError if component already installed
100      **/

101     public static void setKillMethod(String JavaDoc component, KillMethod method)
102     {
103         setKillMethod(component, Empties.EMPTY_STRING_ARRAY, method);
104     }
105
106
107
108     /**
109      * Defines the fixture cleanup method for a specific administrator
110      * and its common script-facing aliases. This method is used by the
111      * fixture component class when it is loaded.
112      * @param component fixture administrator or component (non-null)
113      * @param aliases component aliases (non-null)
114      * @param method cleanup method (non-null)
115      * @throws BuildError if component already installed
116      * @throws IllegalArgumentException if any alias is already registered
117      **/

118     public static void setKillMethod(String JavaDoc component, String JavaDoc[] aliases,
119                                      KillMethod method)
120     {
121         AntX.require_(component!=null,IAM_,"setKillMeth- nonzro id");
122         AntX.require_(method!=null,IAM_,"setKillMeth- nonzro method");
123         AntX.require_(aliases!=null,IAM_,"setKillMeth- nonzro aliases");
124
125         KillMethod method0 = getKillMethod(component);
126         if (method0!=null) {
127             throw new BuildError
128                 (AntX.uistrs().get("fixture.cleanup.instald",component));
129         }
130
131         m_killMethods.put(component,method);
132
133         for (int i=0;i<aliases.length;i++) {
134             AntX.require_(m_killMethods.put(aliases[i],method)==null, IAM_,
135                           "setKillMeth- unique component alias("+aliases[i]+")");
136         }
137
138         m_FXIDs.add(component);
139     }
140
141
142
143     /**
144      * Resets a particular aspect of a fixture component. If there is
145      * no cleanup for the component registered, this method will return
146      * <i>true</i>.
147      * @param component fixture component to be reset (non-null)
148      * @param aspect aspect to be reset
149      * @param from controlling task or test (non-null)
150      * @return <i>false</i> if there was a problem with the cleanup
151      **/

152     public static boolean reset(String JavaDoc component, String JavaDoc aspect,
153                                 ProblemHandler from)
154     {
155         AntX.require_(from!=null,IAM_,"reset- nonzro source");
156         AntX.require_(component!=null,IAM_,"reset- nonzro id");
157
158         KillMethod method0 = getKillMethod(component);
159         if (method0!=null) {
160             boolean ok = method0.kill(aspect, from);
161             if (!ok) {
162                 String JavaDoc warning = AntX.uistrs().get
163                     ("fixture.errs.killin.comp.aspect", component,aspect);
164                 from.problem(warning, Project.MSG_WARN);
165             }
166             return ok;
167         }
168         return true;
169     }
170
171
172
173     /**
174      * Resets a particular aspect of a fixture component. If there is
175      * no cleanup for the component registered, this method will use the
176      * supplied fall back cleanup method.
177      * @param component fixture component to be reset (non-null)
178      * @param aspect aspect to be reset
179      * @param fallback fall back cleanup method (non-null)
180      * @param from controlling task or test (non-null)
181      * @return <i>false</i> if there was a problem with the cleanup
182      **/

183     public static boolean reset(String JavaDoc component, String JavaDoc aspect,
184                                 KillMethod fallback, ProblemHandler from)
185     {
186         AntX.require_(from!=null,IAM_,"reset- nonzro source");
187         AntX.require_(component!=null,IAM_,"reset- nonzro id");
188         AntX.require_(fallback!=null,IAM_,"reset- nonzro fallback");
189
190         KillMethod method0 = getKillMethod(component);
191         if (method0==null) {
192             method0= fallback;
193         }
194         boolean ok = method0.kill(aspect, from);
195         if (!ok) {
196             String JavaDoc warning = AntX.uistrs().get
197                 ("fixture.errs.killin.comp.aspect", component,aspect);
198             from.problem(warning, Project.MSG_WARN);
199         }
200         return ok;
201     }
202
203
204
205     /**
206      * Resets <em>all</em> fixture components. Your application should
207      * never use this method directly unless it is part of a execution
208      * harness system. Call this method before resetting the iteration
209      * to a new instance.
210      * @param from controlling task or test (non-null)
211      **/

212     public static void reset(ProblemHandler from)
213     {
214         AntX.require_(from!=null,IAM_,"reset- nonzro source");
215
216         List JavaDoc l= FixtureStarter.newListCopy(m_FXIDs);
217
218         Iterator JavaDoc itr= l.iterator();
219         while (itr.hasNext()) {
220             String JavaDoc component = itr.next().toString();
221             KillMethod method = getKillMethod(component);
222             if (!method.kill(from)) {
223                 String JavaDoc warning = AntX.uistrs().get
224                     ("fixture.errs.killin.comp", component);
225                 from.problem(warning, Project.MSG_WARN);
226             }
227         }
228
229         l=null;
230     }
231
232
233
234     /**
235      * Disallow; only public static utility methods.
236      **/

237     private AntXFixture()
238     {
239     }
240
241
242     private static final List JavaDoc m_FXIDs= FixtureStarter.newSynchronizedList();
243     private static final Map JavaDoc m_killMethods= FixtureStarter.newSynchronizedMap();
244 }
245
246 /* end-of-AntXFixture.java */
247
Popular Tags