View Javadoc

1   package org.apache.commons.jelly.tags.ant;
2   
3   /*
4    * Copyright 1999-2001,2004 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.util.Hashtable;
21  
22  import org.apache.tools.ant.BuildException;
23  import org.apache.tools.ant.Project;
24  import org.apache.tools.ant.ProjectHelper;
25  
26  /*** A subclass of an ant <code>Project</code> which allows
27   *  installation of delegators for particular functions.
28   *
29   *  <p>
30   *  Current delegation points include:
31   *
32   *  <ul>
33   *    <li>Properties</li>
34   *  </ul>
35   *
36   *  @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
37   */
38  public class GrantProject extends Project {
39  
40      // ------------------------------------------------------------
41      //     Instance members
42      // ------------------------------------------------------------
43  
44      /*** Properties delegate. */
45      private PropsHandler propsHandler;
46  
47      // ------------------------------------------------------------
48      //     Constructors
49      // ------------------------------------------------------------
50  
51      /*** Construct a new, empty <code>GrantProject</code>.
52       *
53       *  <p>
54       *  Immediately after initialization, a <code>GrantProject</code>
55       *  delegates <b>all</b> calls to the normal ant <code>Project</code>
56       *  super class.   Only after installing delegators will custom
57       *  behavious be acheived.
58       *  </p>
59       */
60      public GrantProject() {
61          this.propsHandler = null;
62      }
63  
64      // ------------------------------------------------------------
65      //     Instance methods
66      // ------------------------------------------------------------
67  
68      /*** Install a <code>PropsHandler</code> delegate.
69       *
70       *  @param propsHandler The <code>PropsHandler</code> to install,
71       *         or <code>null</code> to remove any currently installed
72       *         <code>PropsHandler</code>.
73       */
74      public void setPropsHandler(PropsHandler propsHandler) {
75          this.propsHandler = propsHandler;
76      }
77  
78      /*** Retrieve the currently installed <code>PropsHandler</code>.
79       *
80       *  @return The currently installed <code>PropsHandler</code>,
81       *          or <code>null</code> if no <code>PropsHandler</code>
82       *          had yet to be installed.
83       */
84      public PropsHandler getPropsHandler() {
85          return this.propsHandler;
86      }
87  
88      // ------------------------------------------------------------
89      //     org.apache.tools.ant.Project implementation
90      // ------------------------------------------------------------
91  
92      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
93      //         properties delegators:
94      //
95      //         If a PropsHandler is not installed, delegate
96      //         up the hierarchy to the ant.Project parent
97      //         class.  Otherwise, delegate outwards using
98      //         the PropsHandler.
99      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
100 
101     public String replaceProperties(String value) throws BuildException {
102         return ProjectHelper.replaceProperties(this, value, getProperties());
103     }
104 
105     public synchronized void setProperty(String key, String value) {
106         if (this.propsHandler == null) {
107             super.setProperty(key, value);
108         }
109         else {
110             this.propsHandler.setProperty(key, value);
111         }
112     }
113 
114     public synchronized void setUserProperty(String key, String value) {
115         if (this.propsHandler == null) {
116             super.setUserProperty(key, value);
117         }
118         else {
119             this.propsHandler.setUserProperty(key, value);
120         }
121     }
122 
123     public synchronized void setNewProperty(String key, String value) {
124         if (this.propsHandler == null) {
125             super.setNewProperty(key, value);
126         }
127         else {
128             this.propsHandler.setNewProperty(key, value);
129         }
130     }
131 
132     public void setInheritedProperty(String key, String value) {
133         if (this.propsHandler == null) {
134             super.setInheritedProperty(key, value);
135         }
136         else {
137             this.propsHandler.setInheritedProperty(key, value);
138         }
139     }
140 
141     public String getProperty(String key) {
142         if (this.propsHandler == null) {
143             return super.getProperty(key);
144         }
145 
146         return this.propsHandler.getProperty(key);
147     }
148 
149     public String getUserProperty(String key) {
150         if (this.propsHandler == null) {
151             return super.getUserProperty(key);
152         }
153 
154         return this.propsHandler.getUserProperty(key);
155     }
156 
157     public Hashtable getUserProperties() {
158         if (this.propsHandler == null) {
159             return super.getUserProperties();
160         }
161 
162         return this.propsHandler.getUserProperties();
163     }
164 
165     public Hashtable getProperties() {
166         if (this.propsHandler == null) {
167             return super.getProperties();
168         }
169 
170         return this.propsHandler.getProperties();
171     }
172 
173     public void copyUserProperties(Project other) {
174         if (this.propsHandler == null) {
175             super.copyUserProperties(other);
176         }
177         else {
178             this.propsHandler.copyUserProperties(other);
179         }
180     }
181 
182     public void copyInheritedProperties(Project other) {
183         if (this.propsHandler == null) {
184             super.copyInheritedProperties(other);
185         }
186         else {
187             this.propsHandler.copyInheritedProperties(other);
188         }
189     }
190 
191     public void setSystemProperties() {
192         if (this.propsHandler == null) {
193             super.setSystemProperties();
194         }
195         else {
196             this.propsHandler.setSystemProperties();
197         }
198     }
199 
200     public void setJavaVersionProperty() throws BuildException {
201         // Always call the super, as they do some sanity checks
202         super.setJavaVersionProperty();
203 
204         if (this.propsHandler != null) {
205             this.propsHandler.setJavaVersionProperty();
206         }
207     }
208 
209     public void setBaseDir(File baseDir) throws BuildException {
210         super.setBaseDir(baseDir);
211 
212         if (this.propsHandler != null) {
213             this.propsHandler.setPropertyIfUndefinedByUser(
214                 "basedir",
215                 baseDir.getPath());
216         }
217     }
218 }