KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > IsReference


1 /**
2  * $Id: IsReference.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 of the License, or (at your option) any later
9  * 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.condition;
30
31 import org.apache.tools.ant.taskdefs.condition.Condition;
32
33 import com.idaremedia.antx.AssertableProjectComponent;
34 import com.idaremedia.antx.FixtureExaminer;
35 import com.idaremedia.antx.helpers.Tk;
36
37 /**
38  * Simple Is-A-Reference condition check. Example usage:
39  * <pre>
40  * &lt;isref name="the.err"/&gt;
41  * &lt;isref name="module.classes" class="org.apache.tools.ant.types.DirSet"/&gt;
42  * &lt;isref name="last.err" class="java.lang.Throwable" kindof="true"/&gt;
43  *</pre>
44  *
45  * @since JWare/AntX 0.1
46  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
47  * @version 0.5
48  * @.safety single
49  * @.group api,infra
50  **/

51
52 public class IsReference extends AssertableProjectComponent
53     implements Condition, URIable
54 {
55     /**
56      * Creates new IsReference condition; defaults
57      * to returning <i>true</i>.
58      **/

59     public IsReference()
60     {
61     }
62
63
64     /**
65      * Creates pre-defined IsReference condition.
66      * @param refid the refid against which condition checks
67      **/

68     public IsReference(String JavaDoc refid)
69     {
70         setName(refid);
71     }
72
73
74     /**
75      * Sets reference name used by evaluation method.
76      * @param refId the reference's name (non-null)
77      **/

78     public void setName(String JavaDoc refId)
79     {
80         m_refId = refId;
81     }
82
83
84     /**
85      * Returns reference name used by evaluation method.
86      **/

87     public final String JavaDoc getName()
88     {
89         return m_refId;
90     }
91
92
93
94     /**
95      * Sets this condition's reference name as part of
96      * a value URI.
97      * @param fragment the value uri bits (non-null)
98      * @since JWare/AntX 0.5
99      */

100     public void xsetFromURI(String JavaDoc fragment)
101     {
102         setName(fragment);
103     }
104
105
106
107     /**
108      * Tells this condition that the reference shoudld <em>not</em>
109      * exist in order for condition to pass.
110      * @param noexist <i>true</i> if reference should not exist.
111      * @since JWare/AntX 0.5
112      **/

113     public void xsetNoExist(boolean noexist)
114     {
115         m_noexist = noexist;
116     }
117
118
119
120     /**
121      * Returns current referenced this condition would use
122      * (will return <i>null</i> if never set or reference is
123      * undefined or reference is out-of-scope (defined in a
124      * target that has yet to execute).
125      **/

126     public Object JavaDoc getReferencedObject()
127     {
128         require_(getProject()!=null,"getRefObj- in project");
129         String JavaDoc refid= Tk.resolveString(getProject(),getName());
130         Object JavaDoc ref = null;
131         if (refid!=null) {
132             ref = FixtureExaminer.trueReference(getProject(),refid);
133             if (ref==FixtureExaminer.IGNORED_REFERENCE) {
134                 ref = null;
135             }
136         }
137         return ref;
138     }
139
140
141     /**
142      * Sets whether reference must be of an exact class
143      * match or just a "kind-of" the class.
144      **/

145     public void setKindOf(boolean kindOf)
146     {
147         m_kindOf = kindOf;
148     }
149
150
151     /**
152      * Returns <i>true</i> if reference only has to be a
153      * "kind-of" class match and not an exact class match.
154      **/

155     public final boolean isKindOf()
156     {
157         return m_kindOf;
158     }
159
160
161     /**
162      * Sets the class the reference be an instance of.
163      * @since JWare/AntX 0.5
164      **/

165     public void setClassName(String JavaDoc clazzname)
166     {
167         m_requiredClass = clazzname;
168     }
169
170
171     /**
172      * (Older)Synonym for {@linkplain #setClassName setClassName()}.
173      **/

174     public final void setClass(String JavaDoc clazzname)
175     {
176         setClassName(clazzname);
177     }
178
179
180     /**
181      * Returns required instance-of class class name for
182      * this condition.
183      * Returns <i>null</i> if never set.
184      * @since JWare/AntX 0.5
185      **/

186     public final String JavaDoc getClassName()
187     {
188         return m_requiredClass;
189     }
190
191
192
193     /**
194      * Returns <i>true</i> if the condtion's property has not
195      * been explicitly set. This condition should behave reasonably
196      * even if it was loaded by a classloader different from
197      * referenced object.
198      **/

199     public boolean eval()
200     {
201         verifyInProject_("eval");
202
203         boolean result = false;
204         Object JavaDoc object = getReferencedObject();
205
206         if (object!=null) {
207             if (!m_noexist) {
208                 final String JavaDoc classname = getClassName();
209                 if (classname!=null) {
210                     if (object==FixtureExaminer.PROBLEM_REFERENCE) {
211                         result= false;
212                     } else {
213                         result= object.getClass().getName().equals(classname);
214                         if (result==false && isKindOf()) {
215                             result = isCompatible(object,classname);
216                         }
217                     }
218                 } else {
219                     result= true;//=>just need to exist!
220
}
221             }
222         } else if (m_noexist) {
223             result = true;
224         }
225
226         return result;
227     }
228
229
230
231     /**
232      * Tries to determine if object is assignment compatible with
233      * the named class. Has to work w/ names to avoid issues when condition
234      * and object loaded by different classloaders.
235      * @param object the referenced object
236      * @param classname class we're interested in.
237      * @return <i>true</i> if (sortof) compatible.
238      * @since JWare/AntX 0.5
239      **/

240     private boolean isCompatible(Object JavaDoc object, String JavaDoc classname)
241     {
242         boolean compatible = false;
243         ClassLoader JavaDoc cl = object.getClass().getClassLoader();
244         if (cl!=null) {
245             try {
246                 Class JavaDoc c = cl.loadClass(classname);
247                 compatible = c.isInstance(object);
248             } catch(ClassNotFoundException JavaDoc cnfx) {
249                 /*burp*/
250             }
251         } else {
252             try {
253                 Class JavaDoc c = Class.forName(classname);
254                 compatible = c.isInstance(object);
255             }catch(ClassNotFoundException JavaDoc cnfx) {
256                 /*burp*/
257             }
258         }
259         return compatible;
260     }
261
262
263     private String JavaDoc m_refId;//required
264
private boolean m_kindOf;//NB:=>*exact* match
265
private String JavaDoc m_requiredClass;//NB:most flex to support diff classpaths
266
private boolean m_noexist;
267 }
268
269 /* end-of-IsReference.java */
270
Popular Tags