KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > JProperty


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JProperty.java,v 1.2 2005/03/02 16:40:27 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.ant;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.taskdefs.Property;
31
32 /**
33  * Allow to define property with the value of my.${name}.property
34  * @author Florent Benoit
35  */

36 public class JProperty extends Property {
37
38     /**
39      * Internal value (to evaluate)
40      */

41     private String JavaDoc internalValue = null;
42
43     /**
44      * Default value if the key is not found
45      */

46     private String JavaDoc defaultValue = null;
47
48     /**
49      * The value of the property to set
50      * @param value value to set
51      */

52     public void setValue(String JavaDoc value) {
53         this.internalValue = value;
54     }
55
56     /**
57      * The default value if the property cannot be found
58      * @param defaultValue value to set
59      */

60     public void setDefaultValue(String JavaDoc defaultValue) {
61         this.defaultValue = defaultValue;
62     }
63
64     /**
65      * Execute the task. It sets the value by evaluating variable name
66      * @throws BuildException if value is not set
67      * @see org.apache.tools.ant.Task#execute()
68      */

69     public void execute() throws BuildException {
70
71         if (internalValue == null) {
72             throw new BuildException("The property '" + internalValue
73                     + "' was not set.");
74         }
75
76         String JavaDoc valueEvaluated = getProject().getProperty(internalValue);
77
78         // set default value
79
if (valueEvaluated == null && defaultValue != null) {
80             valueEvaluated = defaultValue;
81         }
82
83         // No value ? error
84
if (valueEvaluated == null) {
85             throw new BuildException("The property '" + internalValue
86                     + "' cannot be evaluated in the current project.");
87         }
88
89         // Set the value with the value evaluated
90
super.setValue(valueEvaluated);
91
92         // Now execute the super method
93
super.execute();
94     }
95
96 }
Popular Tags