KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > feedback > EmitContext


1 /**
2  * $Id: EmitContext.java 186 2007-03-16 13:42:35Z 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.feedback;
30
31 import com.idaremedia.antx.AntX;
32 import com.idaremedia.antx.AntXFixture;
33 import com.idaremedia.antx.FixtureAdministrator;
34 import com.idaremedia.antx.FixtureCore;
35 import com.idaremedia.antx.FixtureOverlays;
36 import com.idaremedia.antx.FixtureIds;
37 import com.idaremedia.antx.Iteration;
38 import com.idaremedia.antx.KillMethodSkeleton;
39 import com.idaremedia.antx.apis.ProblemHandler;
40
41 /**
42  * Manages iteration-based EmitConfiguration for the active thread. All context-aware
43  * emit configurables should use this class instead of directly accessing FixtureOverlays\
44  * stacks. The default emit configurations are bound to the current iteration; if the
45  * iteration is replaced, a new (empty) default configuration is installed.
46  *
47  * @since JWare/AntX 0.1
48  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
49  * @version 0.5
50  * @.safety guarded
51  * @.group impl,infra
52  * @see EmitTask
53  * @see EmitConfigureTask
54  **/

55
56 public final class EmitContext implements FixtureCore, FixtureAdministrator
57 {
58     /**
59      * The fixture component id for all EmitContext information.
60      **/

61     public static final String JavaDoc FXID= FixtureIds.EMIT_CONFIGURATION;
62
63
64
65     /**
66      * Returns the current thread's EmitConfiguration instance if one exists.
67      * Returns <i>null</i> if no configuration installed for current thread.
68      **/

69     public static EmitConfiguration getConfiguration()
70     {
71         return (EmitConfiguration)FixtureOverlays.getContextInstance().nearest(FXID);
72     }
73
74
75
76     /**
77      * Helper to {@linkplain #getConfiguration getConfiguration} that returns
78      * the {@linkplain #getDefaultConfiguration default configuration} if the
79      * current thread has no explicit configuration. Never returns <i>null</i>.
80      **/

81     public static EmitConfiguration getConfigurationNoNull()
82     {
83         EmitConfiguration ec = getConfiguration();
84         return (ec!=null) ? ec : getDefaultConfiguration();
85     }
86
87
88
89     /**
90      * Installs a new EmitConfiguration instance for the current thread. This
91      * configuration becomes the active (and only visible) configuration until
92      * it is unwound or another configuration is installed.
93      * @param ec the new configuration (non-null)
94      * @param noInstallHandler [optional] used in event of error
95      * @return previous configuration if any (can be null)
96      * @throws BuildException if incoming configuration already on iteration stack
97      **/

98     public static EmitConfiguration installConfiguration(EmitConfiguration ec,
99                                                    ProblemHandler noInstallHandler)
100     {
101         if (ec==null) {
102             throw new IllegalArgumentException JavaDoc(AntX.uistrs().get("emit.null.ecfg"));
103         }
104         String JavaDoc whoami = AntX.uistrs().dget("emit.whoami","EmitConfiguration");
105         return (EmitConfiguration)FixtureOverlays.installIfNot(FXID,ec,noInstallHandler,
106                                                          whoami);
107     }
108
109
110
111     /**
112      * Removes the most recently installed EmitConfiguration for the current
113      * thread. The previous installation is reactivated, or if this was
114      * the only configuration, the current thread's emit configuration is
115      * undefined (should use default if necessary).
116      * @param noUninstallHandler used in event of already unwound stack
117      **/

118     public static void unwindConfiguration(ProblemHandler noUninstallHandler)
119     {
120         String JavaDoc whoami = AntX.uistrs().dget("emit.whoami","EmitConfiguration");
121         FixtureOverlays.uninstallIfIs(FXID,noUninstallHandler,whoami);
122     }
123
124
125
126     /**
127      * Returns the default EmitConfiguration used by this context. Never
128      * returns <i>null</i>.
129      * @see DefaultEmitConfiguration#INSTANCE
130      * @see #setDefaultConfiguration
131      * @since JWare/AntX 0.3
132      **/

133     public static EmitConfiguration getDefaultConfiguration()
134     {
135         EmitConfiguration ec = (EmitConfiguration)Iteration.getProperty(FXID);
136         return (ec==null) ? DefaultEmitConfiguration.INSTANCE : ec;
137     }
138
139
140
141     /**
142      * Initializes the default EmitConfiguration returned by this context. Not
143      * necessary since by default, is set to the predefined {@linkplain
144      * DefaultEmitConfiguration#INSTANCE DefaultEmitConfiguration}. Should not
145      * be set to a configuration that already exists on current thread's iteration
146      * stack. This method <em>replaces</em> any pre-existing default
147      * configuration-- the current object is lost unless the caller maintains it.
148      * @since JWare/AntX 0.3
149      * @.safety guarded
150      **/

151     public static EmitConfiguration setDefaultConfiguration(EmitConfiguration ec)
152     {
153         if (ec==null) {
154             throw new IllegalArgumentException JavaDoc
155                 (AntX.uistrs().get("emit.null.ecfg"));
156         }
157         return (EmitConfiguration)Iteration.setProperty(FXID,ec);
158     }
159
160
161
162     /**
163      * Returns <i>true</i> if the default EmitConfiguration is defined as
164      * the empty placeholder.
165      * @see #setDefaultConfiguration setDefaultConfiguration()
166      * @since JWare/AntX 0.4
167      **/

168     public static final boolean isDefaultUndefined()
169     {
170         return getDefaultConfiguration()==DefaultEmitConfiguration.INSTANCE;
171     }
172
173
174
175     /**
176      * Resets the default EmitConfiguration to the empty placeholder. Your
177      * scripts and application should never need to do this. This method is
178      * provided for test scripts and harnesses.
179      * @.safety guarded
180      * @since JWare/AntX 0.4
181      **/

182     static final void unsetDefaultConfiguration()
183     {
184         Iteration.removeProperty(FXID);
185     }
186
187
188
189     /**
190      * Installs a test-harness helper that clears up the various EmitContext
191      * fixture components. Your application should never use this helper
192      * directly; it is provided so that test harnesses can reset the environment
193      * to a known state.
194      * @since JWare/AntX 0.4
195      **/

196     static {
197         AntXFixture.setKillMethod
198             (FXID,
199              new String JavaDoc[]{"emitconfigurations"},
200              new KillMethodSkeleton() {
201                      protected boolean killDefault(ProblemHandler from) {
202                          EmitContext.unsetDefaultConfiguration();
203                          return true;
204                      }
205                      protected String JavaDoc getComponentId() {
206                          return EmitContext.FXID;
207                      }
208                      protected String JavaDoc getComponentName() {
209                          return AntX.uistrs().dget
210                              ("emit.whoami", "EmitConfiguration");
211                      }
212                  }
213              );
214     }
215
216
217     /** Disallow; only static helpers. **/
218     private EmitContext()
219     { }
220 }
221
222 /* end-of-EmitContext.java */
223
Popular Tags