01: /**
02: * JOnAS: Java(TM) Open Application Server
03: * Copyright (C) 2004 Bull S.A.
04: * Contact: jonas-team@objectweb.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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 6353 2005-03-02 16:40:27Z benoitf $
24: * --------------------------------------------------------------------------
25: */package org.objectweb.jonas.ant;
26:
27: import org.apache.tools.ant.BuildException;
28: import org.apache.tools.ant.taskdefs.Property;
29:
30: /**
31: * Allow to define property with the value of my.${name}.property
32: * @author Florent Benoit
33: */
34: public class JProperty extends Property {
35:
36: /**
37: * Internal value (to evaluate)
38: */
39: private String internalValue = null;
40:
41: /**
42: * Default value if the key is not found
43: */
44: private String defaultValue = null;
45:
46: /**
47: * The value of the property to set
48: * @param value value to set
49: */
50: public void setValue(String value) {
51: this .internalValue = value;
52: }
53:
54: /**
55: * The default value if the property cannot be found
56: * @param defaultValue value to set
57: */
58: public void setDefaultValue(String defaultValue) {
59: this .defaultValue = defaultValue;
60: }
61:
62: /**
63: * Execute the task. It sets the value by evaluating variable name
64: * @throws BuildException if value is not set
65: * @see org.apache.tools.ant.Task#execute()
66: */
67: public void execute() throws BuildException {
68:
69: if (internalValue == null) {
70: throw new BuildException("The property '" + internalValue
71: + "' was not set.");
72: }
73:
74: String valueEvaluated = getProject().getProperty(internalValue);
75:
76: // set default value
77: if (valueEvaluated == null && defaultValue != null) {
78: valueEvaluated = defaultValue;
79: }
80:
81: // No value ? error
82: if (valueEvaluated == null) {
83: throw new BuildException("The property '" + internalValue
84: + "' cannot be evaluated in the current project.");
85: }
86:
87: // Set the value with the value evaluated
88: super .setValue(valueEvaluated);
89:
90: // Now execute the super method
91: super.execute();
92: }
93:
94: }
|