KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ownhelpers > ScopedProperties


1 /**
2  * $Id: ScopedProperties.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 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://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.ownhelpers;
30
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.PropertyHelper;
37 import org.apache.tools.ant.taskdefs.Property;
38 import org.apache.tools.ant.types.PropertySet;
39
40 import com.idaremedia.antx.AntX;
41 import com.idaremedia.antx.FixtureExaminer;
42 import com.idaremedia.antx.apis.Nameable;
43 import com.idaremedia.antx.apis.ProblemHandler;
44 import com.idaremedia.antx.apis.ProjectDependent;
45 import com.idaremedia.antx.helpers.GenericParameters;
46
47 /**
48  * Common implementation of the {@linkplain org.apache.tools.ant.DynamicConfigurator
49  * DynamicConfigurator} interface.
50  *
51  * @since JWare/AntX 0.4
52  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
53  * @version 0.5
54  * @.safety guarded
55  * @.group impl,helper
56  * @see com.idaremedia.antx.solo.LocalFixtureTaskSet LocalFixtureTaskSet
57  **/

58
59 public final class ScopedProperties extends Hashtable JavaDoc
60     implements ProjectDependent, Nameable
61 {
62     private static final String JavaDoc IAM_= AntX.utilities+"ScopedProperties:";
63
64
65     /**
66      * Initializes a new supplemental properties dictionary. This
67      * dictionary's project must be defined before it is installed.
68      * @param commandProperties <i>true</i> if this helper's properties
69      * act like command properties
70      **/

71     public ScopedProperties(boolean commandProperties)
72     {
73         super();
74         m_commandProperties = commandProperties;
75     }
76
77
78
79     /**
80      * Initializes a new project-bound supplemental properties dictionary.
81      * @param commandProperties <i>true</i> if this helper's properties
82      * act like command properties
83      **/

84     public ScopedProperties(Project project, boolean commandProperties)
85     {
86         super();
87         m_commandProperties = commandProperties;
88         setProject(project);
89     }
90
91
92
93     /**
94      * Initializes this dictionary's enclosing project. Must be
95      * called by this dictionary's controlling task <em>before</em>
96      * {@linkplain #install install}.
97      **/

98     public void setProject(Project project)
99     {
100         m_project = project;
101     }
102
103
104     /**
105      * Returns this dictionary's enclosing project. Will return
106      * <i>null</i> if never set.
107      **/

108     public Project getProject()
109     {
110         return m_project;
111     }
112
113
114
115     /**
116      * Initializes this dictionary's diagnostic label.
117      * @param name the label (non-null)
118      **/

119     public void setName(String JavaDoc name)
120     {
121         m_label = name;
122     }
123
124
125
126     /**
127      * Returns this dictionary's diagnostic label. Will
128      * return the empty string if never defined explicitly.
129      **/

130     public String JavaDoc getName()
131     {
132         return m_label;
133     }
134
135
136
137     /**
138      * Tells this dictionary whether <em>standalone</em> keys
139      * should be filtered and any underscores replaced with periods.
140      * @param swapEm <i>true</i> to swap underscores.
141      * @since JWare/AntX 0.5
142      **/

143     public final void swapUnderscores(boolean swapEm)
144     {
145         m_swapUnderscores = swapEm;
146     }
147
148
149
150     /**
151      * Installs this dictionary as a supplemental set of project
152      * properties until uninstalled. When this method returns, this
153      * dictionary will be at the front of the hook chain.
154      **/

155     public final void install()
156     {
157         AntX.verify_(m_conduit==null,IAM_,"install- not installed");
158         AntX.verify_(getProject()!=null,IAM_,"install- in project");
159
160         if (!isEmpty()) {
161             PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject());
162             Conduit c = new Conduit(m_commandProperties);
163             synchronized(ph) {
164                 c.setNext(ph.getNext());
165                 ph.setNext(c);
166             }
167             m_conduit = c;
168         }
169     }
170
171
172
173     /**
174      * Uninstalls this dictionary as a supplemental set of project
175      * properties. This method works even if other hooks have been
176      * installed ahead of this dictionary in the hook chain.
177      * @param notInstalled [optional] error handler
178      **/

179     public final void uninstall(ProblemHandler notInstalled)
180     {
181         if (!isEmpty() && m_conduit!=null) {
182             Conduit c = (Conduit)m_conduit;
183             m_conduit = null;
184             PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject());
185             synchronized(ph) {
186                 PropertyHelper prevPh=null;
187                 do {
188                     if (ph.getNext()==c) { break; }
189                     prevPh = ph;
190                     ph = ph.getNext();
191                 } while(ph!=null);
192                 if (ph!=null) {
193                     ph.setNext(c.getNext());
194                     if (prevPh!=null) {
195                         prevPh.setNext(ph);
196                     }
197                 } else {
198                     String JavaDoc warning= AntX.uistrs().get("fixture.supPh.not.instald",getName());
199                     if (notInstalled!=null) {
200                         notInstalled.problem(warning,Project.MSG_WARN);
201                     } else {
202                         getProject().log(warning,Project.MSG_WARN);
203                     }
204                 }
205                 c.setNext(null);
206             }
207         }
208     }
209
210
211
212     /**
213      * Adds the given attribute to this dictionary ensuring all
214      * underscores in <span class="src">key</span> have been
215      * replaced by dots.
216      **/

217     public final Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
218     {
219         AntX.require_((key instanceof String JavaDoc),IAM_,"put- nonzro name");
220         String JavaDoc s = (String JavaDoc)key;
221         if (m_swapUnderscores) {
222             s = s.replace('_','.');
223         }
224         return super.put(s,value);
225     }
226
227
228
229     /**
230      * Convenient variant of {@linkplain #put(Object,Object)
231      * put(&#8230;)} that processes a standard Ant property set.
232      **/

233     public final void put(PropertySet ps)
234     {
235         AntX.require_(ps!=null,IAM_,"put- nonzro propertyset");
236         Iterator JavaDoc itr= ps.getProperties().entrySet().iterator();
237         while (itr.hasNext()) {
238             Map.Entry JavaDoc e= (Map.Entry JavaDoc)itr.next();
239             super.put(e.getKey(),e.getValue());//NB:skip name swapping
240
}
241     }
242
243
244
245     /**
246      * Convenient variant of {@linkplain #put(Object,Object)
247      * put(&#8230;)} that processes a standard Ant property declaration.
248      * Only works for simple, single-value properties.
249      **/

250     public final void put(Property p)
251     {
252         AntX.require_(p!=null,IAM_,"put- nonzro property");
253         String JavaDoc name = p.getName();
254         String JavaDoc value = p.getValue();
255         if (name!=null && value!=null) {
256             super.put(name,value);//NB:skip name swapping
257
}
258     }
259
260
261
262     /**
263      * Convenient variant of {@linkplain #put(Object,Object)
264      * put(&#8230;)} that processes a standard AntX property list.
265      * @param params generic parameter name-value pairs
266      * @since JWare/AntX 0.5
267      **/

268     public final void put(GenericParameters params)
269     {
270         AntX.require_(params!=null,IAM_,"put- nonzro propertyset");
271         Iterator JavaDoc itr= params.copyOfSimpleKeyValues(getProject(),true)
272             .entrySet().iterator();
273         while (itr.hasNext()) {
274             Map.Entry JavaDoc e= (Map.Entry JavaDoc)itr.next();
275             super.put(e.getKey(),e.getValue());
276         }
277     }
278
279
280
281     /**
282      * Ensure a "get" handles the AntX data-fetch URL format for values.
283      * @param name the property to lookup (non-null)
284      **/

285     private Object JavaDoc lookup(String JavaDoc name)
286     {
287         String JavaDoc s = (String JavaDoc)get(name);
288         if (s!=null) {
289             String JavaDoc actual = FixtureExaminer.findValue(getProject(),s,name);
290             if (actual!=null) {
291                 s = actual;
292             }
293         }
294         return s;
295     }
296
297
298
299     /**
300      * ScopedProperties hook into the standard Ant project property framework.
301      * This class must play nice with other installed hooks.
302      *
303      * @since JWare/AntX 0.4
304      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
305      * @version 0.5
306      * @.safety guarded (inherited)
307      * @.group impl,helper
308      */

309     private class Conduit extends PropertyHelper
310     {
311         private final boolean m_userOnly;
312
313         Conduit(boolean userOnly) {
314             m_userOnly= userOnly;
315         }
316
317         /**
318          * Returns our overlay properties if necessary. Will delegate
319          * to next in hook chain if necessary.
320          **/

321         public Object JavaDoc getPropertyHook(String JavaDoc ns, String JavaDoc name,
322                                       boolean userOnly) {
323             Object JavaDoc o;
324             if (userOnly && !m_userOnly) {
325                 o= null;
326             } else {
327                 o= ScopedProperties.this.lookup(name);
328             }
329             if (o==null && getNext()!=null) {
330                 o = getNext().getPropertyHook(ns,name,userOnly);
331             }
332             return o;
333         }
334
335         /**
336          * Updates our overlay properties if necessary. Handles only our
337          * properties. Will delegate to next in hook chain if necessary.
338          * @.impl The standard Ant PropertyHelper is _BROKEN_ in how it
339          * calls setPropertyHook from 'set[New]Property' (it does not
340          * call the get hook to test if property already exists
341          * before setting unconditionally). We have to compensate
342          * for this. AntX's own net implementation does not need
343          * this work around.
344          **/

345         public boolean setPropertyHook(String JavaDoc ns, String JavaDoc name, Object JavaDoc value,
346                                        boolean inherited, boolean userOnly,
347                                        boolean isNew) {
348
349             if (ScopedProperties.this.containsKey(name)) {
350                 if ((m_userOnly && (inherited || userOnly)) ||
351                     (!m_userOnly && !inherited && !userOnly)) {
352                     ScopedProperties.this.put(name,value);
353                 }
354                 return true;//@.impl fixes problem (even if not altered)
355
}
356             if (getNext()!=null) {
357                 return getNext().setPropertyHook(ns,name,value,inherited,
358                                                  userOnly,isNew);
359             }
360             return false;
361         }
362     }
363
364
365     private Project m_project;
366     private boolean m_commandProperties;
367     private Object JavaDoc m_conduit;
368     private String JavaDoc m_label="n/d";
369     private boolean m_swapUnderscores;//NB: NO (as of AntX 0.5!)
370
}
371
372 /* end-of-ScopedProperties.java */
373
Popular Tags