View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.task;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.apache.commons.jelly.JellyContext;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.tools.ant.Project;
27  
28  /*** <p><code>AntJellyContext</code> represents the Jelly context from inside Ant.</p>
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 155420 $
32    */
33  
34  public class AntJellyContext extends JellyContext {
35  
36      /*** The Ant project which contains the variables */
37      private Project project;
38  
39      /*** The Log to which logging calls will be made. */
40      private Log log = LogFactory.getLog(AntJellyContext.class);
41  
42      public AntJellyContext(Project project, JellyContext parentJellyContext) {
43          super( parentJellyContext );
44          this.project = project;
45      }
46  
47      /*** @return the value of the given variable name */
48      public Object getVariable(String name) {
49          // look in parent first
50          Object answer = super.getVariable(name);
51          if (answer == null) {
52              answer = project.getProperty(name);
53          }
54  
55          if ( log.isDebugEnabled() ) {
56              log.debug( "Looking up variable: " + name + " answer: " + answer );
57          }
58  
59          return answer;
60      }
61  
62      /*** Sets the value of the given variable name */
63      public void setVariable(String name, Object value) {
64          if ( log.isDebugEnabled() ) {
65              log.debug( "Setting variable: " + name + " to: " + value );
66          }
67  
68          super.setVariable( name, value );
69  
70          // only export string values back to Ant?
71          if ( value instanceof String ) {
72              project.setProperty(name, (String) value);
73          }
74      }
75  
76      /*** Removes the given variable */
77      public void removeVariable(String name) {
78          super.removeVariable( name );
79          project.setProperty(name, null);
80      }
81  
82      /***
83       * @return an Iterator over the current variable names in this
84       * context
85       */
86      public Iterator getVariableNames() {
87          return getVariables().keySet().iterator();
88      }
89  
90      /***
91       * @return the Map of variables in this scope
92       */
93      public Map getVariables() {
94          // we should add all the Project's properties
95          Map map = new HashMap( project.getProperties() );
96  
97          // override any local properties
98          map.putAll( super.getVariables() );
99          return map;
100     }
101 
102     /***
103      * Sets the Map of variables to use
104      */
105 
106     public void setVariables(Map variables) {
107         super.setVariables(variables);
108 
109         // export any Ant properties
110         for ( Iterator iter = variables.entrySet().iterator(); iter.hasNext(); ) {
111             Map.Entry entry = (Map.Entry) iter.next();
112             String key = (String) entry.getKey();
113             Object value = entry.getValue();
114             if ( value instanceof String ) {
115                 project.setProperty(key, (String)value);
116             }
117         }
118     }
119 
120 
121     // Implementation methods
122     //-------------------------------------------------------------------------
123 
124     /***
125      * Factory method to create a new child of this context
126      */
127     protected JellyContext createChildContext() {
128         return new AntJellyContext(project, this);
129     }
130 
131 }