View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/resolver/src/java/org/astrogrid/community/resolver/ant/CommunityResolverTask.java,v $</cvs:source>
3    * <cvs:author>$Author: dave $</cvs:author>
4    * <cvs:date>$Date: 2004/06/18 13:45:20 $</cvs:date>
5    * <cvs:version>$Revision: 1.6 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: CommunityResolverTask.java,v $
9    *   Revision 1.6  2004/06/18 13:45:20  dave
10   *   Merged development branch, dave-dev-200406081614, into HEAD
11   *
12   *   Revision 1.5.18.1  2004/06/17 13:38:59  dave
13   *   Tidied up old CVS log entries
14   *
15   * </cvs:log>
16   *
17   *
18   */
19  package org.astrogrid.community.resolver.ant ;
20  
21  import java.util.Properties ;
22  import java.util.Enumeration ;
23  
24  import java.io.FileInputStream ;
25  
26  import org.apache.tools.ant.Task ;
27  import org.apache.tools.ant.BuildException ;
28  
29  import org.astrogrid.config.Config ;
30  import org.astrogrid.config.SimpleConfig ;
31  
32  import org.astrogrid.community.common.security.data.SecurityToken ;
33  
34  /***
35   * Base class for our Ant tasks.
36   * 
37   */
38  public class CommunityResolverTask
39      extends Task
40      {
41      /***
42       * Our debug flag.
43       *
44       */
45      private static final boolean DEBUG_FLAG = true ;
46  
47      /***
48       * Public constructor.
49       *
50       */
51      public CommunityResolverTask()
52          {
53          //
54          // Initialise our base class.
55          super() ;
56          }
57  
58      /***
59       * Public constructor.
60       *
61       */
62      public CommunityResolverTask(Task parent)
63          {
64          //
65          // Initialise our base class.
66          super() ;
67          //
68          // Set our project.
69          setProject(parent.getProject()) ;
70          }
71  
72      /***
73       * Initialise our Task.
74       *
75       */
76      public void init()
77          throws BuildException
78          {
79          if (DEBUG_FLAG) System.out.println("----\"----");
80          if (DEBUG_FLAG) System.out.println("CommunityResolverTask.init()");
81          if (DEBUG_FLAG) System.out.println("  Token : " + this.getToken());
82          }
83  
84      /***
85       * The name of our token property.
86       *
87       */
88      public static final String TOKEN_PROPERTY = "org.astrogrid.community.token" ;
89  
90      /***
91       * Get our security token.
92       *
93       */
94      protected SecurityToken getToken()
95          {
96          //
97          // If we have a project
98          if (null != this.getProject())
99              {
100             //
101             // Get the current property.
102             String string = this.getProject().getProperty(TOKEN_PROPERTY) ;
103             //
104             // If we found a property.
105             if (null != string)
106                 {
107                 //
108                 // Create a new token.
109                 return new SecurityToken(string) ;
110                 }
111             }
112         return null ;
113         }
114 
115     /***
116      * Set our security token.
117      *
118      */
119     protected void setToken(SecurityToken token)
120         {
121         //
122         // If we have a project
123         if (null != this.getProject())
124             {
125             //
126             // Set our token property.
127             this.getProject().setProperty(
128                 TOKEN_PROPERTY,
129                 token.toString()
130                 ) ;
131             }
132         }
133 
134     /***
135      * The AstroGrid configuration.
136      *
137      */
138     protected static Config config = SimpleConfig.getSingleton() ;
139 
140     /***
141      * Our registry endpoint.
142      *
143      */
144     private String registry ;
145 
146     /***
147      * Access to our registry endpoint.
148      *
149      */
150     public void setRegistry(String value)
151         {
152         this.registry = value ;
153         }
154 
155     /***
156      * Access to our registry endpoint.
157      *
158      */
159     public String getRegistry()
160         {
161         return this.registry ;
162         }
163 
164     /***
165      * Our properties location.
166      *
167      */
168     private String properties ;
169 
170     /***
171      * Get our properties location.
172      *
173      */
174     public String getProperties()
175         {
176         return this.properties ;
177         }
178 
179     /***
180      * Set our properties location.
181      *
182      */
183     public void setProperties(String value)
184         {
185         this.properties = value ;
186         }
187 
188     /***
189      * Load our config properties.
190      *
191      */
192     public void configure()
193         throws BuildException
194         {
195         if (DEBUG_FLAG) System.out.println("----\"----");
196         if (DEBUG_FLAG) System.out.println("CommunityResolverTask.configure()");
197         //
198         // If our properties are set.
199         if (null != this.properties)
200             {
201             //
202             // Try parse our properties.
203             try {
204                 if (DEBUG_FLAG) System.out.println("  Properties  : " + this.properties);
205                 //
206                 // Load our properties file.
207                 Properties map = new Properties() ;
208                 map.load(
209                     new FileInputStream(
210                         this.properties
211                         )
212                     ) ;
213                 //
214                 // Add the properties to our config.
215                 Enumeration enum = map.keys() ;
216                 while (enum.hasMoreElements())
217                     {
218                     if (DEBUG_FLAG) System.out.println("----");
219                     //
220                     // Get the property name and value.
221                     String name = (String)enum.nextElement() ;
222                     if (DEBUG_FLAG) System.out.println("  Name  : " + name);
223                     String value = map.getProperty(name) ;
224                     if (DEBUG_FLAG) System.out.println("  Value : " + value);
225                     //
226                     // Process the property.
227                     if (null != this.getProject())
228                         {
229                         value = this.getProject().replaceProperties(value) ;
230                         }
231                     if (DEBUG_FLAG) System.out.println("  Value : " + value);
232                     //
233                     // Add the property to our config.
234                     config.setProperty(name, value) ;
235                     if (DEBUG_FLAG) System.out.println("----");
236                     }
237                 }
238             catch (Exception ouch)
239                 {
240                 throw new BuildException(ouch) ;
241                 }
242             }
243         }
244 
245     /***
246      * Execute our Task.
247      *
248      */
249     public void execute()
250         throws BuildException
251         {
252         if (DEBUG_FLAG) System.out.println("----\"----");
253         if (DEBUG_FLAG) System.out.println("CommunityResolverTask.execute()");
254         if (DEBUG_FLAG) System.out.println("  Registry   : " + this.registry);
255         if (DEBUG_FLAG) System.out.println("  Properties : " + this.properties);
256         //
257         // Try loading our config properties.
258         this.configure() ;
259         }
260     }
261