KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > ant > taskdefs > DependencyTask


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.savant.ant.taskdefs;
8
9
10 import java.io.File JavaDoc;
11
12 import org.apache.tools.ant.BuildException;
13 import org.apache.tools.ant.Project;
14 import org.apache.tools.ant.Task;
15 import org.apache.tools.ant.types.FileSet;
16 import org.apache.tools.ant.types.Path;
17 import org.apache.tools.ant.types.PatternSet;
18
19 import com.inversoft.savant.Artifact;
20 import com.inversoft.savant.ArtifactGroup;
21 import com.inversoft.savant.Dependencies;
22 import com.inversoft.savant.DependencyListener;
23 import com.inversoft.savant.DependencyMediator;
24 import com.inversoft.savant.LocalProject;
25 import com.inversoft.savant.SavantException;
26 import com.inversoft.savant.SavantInternetProcess;
27 import com.inversoft.savant.Workflow;
28 import com.inversoft.savant.ant.AntLocalProjectBuilder;
29 import com.inversoft.savant.ant.AntLogListener;
30 import com.inversoft.savant.ant.types.ArtifactGroupType;
31 import com.inversoft.savant.ant.types.ArtifactType;
32 import com.inversoft.savant.ant.types.LocalProjectType;
33 import com.inversoft.savant.ant.types.WorkflowType;
34 import com.inversoft.savant.log.LogManager;
35
36
37 /**
38  * <p>
39  * This class is an ant task that stores all the necessary
40  * information about project dependencies.
41  * </p>
42  *
43  * @author Brian Pontarelli
44  */

45 public class DependencyTask extends Task {
46
47     /**
48      * Since there should only ever be one dependencies defintion, this is the
49      * reference ID that the one dependency is stored under.
50      */

51     public static final String JavaDoc REFERENCE_ID = "com.inversoft.ant.Dependencies";
52
53     private String JavaDoc group;
54     private DependencyMediator mediator = new DependencyMediator();
55     private Dependencies dependencies = new Dependencies();
56
57
58     /**
59      * Constructs a new <code>DependencyTask</code>.
60      */

61     public DependencyTask() {
62         mediator.setDependencies(dependencies);
63     }
64
65
66     /**
67      * Retrieves the group of this project, which is used when publishing the
68      * artifacts that this project publishes.
69      *
70      * @return The group name
71      */

72     public String JavaDoc getGroup() {
73         return group;
74     }
75
76     /**
77      * Sets the group of this project, which is used when publishing the artifacts
78      * that this project publishes.
79      *
80      * @param group The group name
81      */

82     public void setGroup(String JavaDoc group) {
83         this.group = group;
84     }
85
86     /**
87      * Retrieves the location of the localCache on the file system where dependencies
88      * and artifacts are stored.
89      *
90      * @return The local cache location
91      */

92     public File JavaDoc getLocalcache() {
93         return mediator.getLocalCacheDir();
94     }
95
96     /**
97      * Sets the location of the localCache on the file system where the dependencies
98      * and artifacts are stored.
99      *
100      * @param localCache The local cache location
101      */

102     public void setLocalcache(File JavaDoc localCache) {
103         if (localCache != null) {
104             log("Using local cache location of [" + localCache.getAbsolutePath() + "]");
105         }
106
107         try {
108             mediator.setLocalCacheDir(localCache);
109         } catch (SavantException se) {
110             log("Invalid local cache location [" + localCache.getAbsolutePath() +
111                 "]");
112             throw new BuildException(se);
113         }
114     }
115
116     /**
117      * Adds a new project dependency to this project.
118      *
119      * @param project The project dependency type
120      */

121     public void addConfiguredProject(LocalProjectType project) {
122         if (project.getGroup() == null) {
123             if (group == null) {
124                 throw new BuildException("Either local projects must define" +
125                     " a group or the dependency must");
126             }
127
128             project.setGroup(group);
129         }
130
131         // Setup the builder as an ant builder for this local project
132
project.getProxy().setBuilder(new AntLocalProjectBuilder(getProject()));
133
134         try {
135             dependencies.addProject(project.getProxy());
136             log("Added project [" + project.getProxy().getID() + "]", Project.MSG_DEBUG);
137         } catch (SavantException se) {
138             log("Invalid local project definition");
139             throw new BuildException(se);
140         }
141     }
142
143     /**
144      * Adds a new artifact group to this project.
145      *
146      * @param group The new group
147      */

148     public void addConfiguredArtifactGroup(ArtifactGroupType group) {
149         try {
150             dependencies.addArtifactGroup(group.getProxy());
151         } catch (SavantException se) {
152             log("Invalid artifact group definition");
153             throw new BuildException(se);
154         }
155     }
156
157     /**
158      * Adds a new artifact to this project.
159      *
160      * @param artifact The artifact to add
161      */

162     public void addConfiguredArtifact(ArtifactType artifact) {
163         try {
164             dependencies.addArtifact(artifact.getProxy());
165         } catch (SavantException se) {
166             log("Invalid artifact definition");
167             throw new BuildException(se);
168         }
169     }
170
171     /**
172      * Adds a new WorkflowType to this project.
173      *
174      * @param workflow The new workflow
175      */

176     public void addWorkflow(WorkflowType workflow) {
177         if (mediator.getWorkflow() != null) {
178             throw new BuildException("Only one workflow per project");
179         }
180
181         mediator.setWorkflow(workflow.getProxy());
182     }
183
184     /**
185      * Executes this task that builds up the dependencies and such.
186      *
187      * @throws BuildException If the task fails
188      */

189     public void execute() throws BuildException {
190         validate();
191
192         // Setup the default workflow if one was not specified
193
if (mediator.getWorkflow() == null) {
194             initWorkflow();
195         }
196
197         LogManager.getInstance().addListener(new AntLogListener(getProject()));
198         
199         mediator.addListener(new AntDependencyListener());
200
201         try {
202             mediator.mediate();
203         } catch (SavantException se) {
204             throw new BuildException(se);
205         }
206     }
207
208     /**
209      * Initializes the dependecy task by validating parameters, setting defaults
210      * and possibly creating directories.
211      */

212     protected void validate() {
213         if (mediator.getLocalCacheDir() == null) {
214             setLocalcache(null);
215         }
216     }
217
218     /**
219      * Sets up the default workflow if one was never set.
220      */

221     protected void initWorkflow() {
222         Workflow workflow = new Workflow();
223
224         Project project = getProject();
225         String JavaDoc repositoryDomain = project.getProperty("domain.savant.repository");
226         String JavaDoc mappingFile = project.getProperty("file.savant.mapping");
227         if (repositoryDomain != null || mappingFile != null) {
228             File JavaDoc mapping = new File JavaDoc(mappingFile);
229             if (!mapping.exists() || mapping.isDirectory()) {
230                 mapping = null;
231             }
232
233             SavantInternetProcess sip = new SavantInternetProcess();
234             sip.setDefaultdomain(repositoryDomain);
235             sip.setMapping(mapping);
236             workflow.addProcess(sip);
237         }
238
239         mediator.setWorkflow(workflow);
240     }
241
242
243     /**
244      * The listener class that handles the callbacks from the mediator.
245      */

246     public class AntDependencyListener implements DependencyListener {
247         // Load the defaults
248
FileSet defaultFS = findFileSet(ArtifactGroup.DEFAULT_FS);
249         Path defaultPath = findClassPath(ArtifactGroup.DEFAULT_PATH);
250
251
252         /**
253          * Handles when a project is built by sending a log message to the user.
254          */

255         public void projectBuilt(LocalProject project) {
256             log("Built project [" + project.getGroup() + ", " + project.getName() + "]");
257         }
258
259         /**
260          * Handles when an artifact is found by resolving the class path and file
261          * set to add the artifact to. If the group passed in is not null and has
262          * a file set or class path specified, that is used. Otherwise the defaults
263          * are used.
264          */

265         public void artifactFound(File JavaDoc file, Artifact artifact, ArtifactGroup group) {
266             log("Handling artifact [" + artifact + "] group [" + group + "]",
267                 Project.MSG_DEBUG);
268
269             FileSet fileSet = defaultFS;
270             if (group != null) {
271                 String JavaDoc fileSetId = group.getFilesetid();
272                 if (fileSetId != null) {
273                     fileSet = findFileSet(fileSetId);
274                 }
275             }
276
277             Path classPath = defaultPath;
278             if (group != null) {
279                 String JavaDoc classPathId = group.getClasspathid();
280                 if (classPathId != null) {
281                     classPath = findClassPath(classPathId);
282                 }
283             }
284
285             if (classPath != null) {
286                 Path.PathElement element = classPath.createPathElement();
287                 element.setLocation(file);
288             }
289
290             if (fileSet != null) {
291                 // Only add the part of the path that is not the localCache
292
// directory path
293
PatternSet.NameEntry ne = fileSet.createInclude();
294                 int length = mediator.getLocalCacheDir().getAbsolutePath().length();
295                 String JavaDoc artPath = file.getAbsolutePath();
296                 ne.setName(artPath.substring(length + 1));
297             }
298
299             log("Loaded artifact [" + artifact + "]");
300         }
301
302         /**
303          * Locates the class path with the given id. If it doesn't exists it is created,
304          * stored in the project as a reference and then returned.
305          *
306          * @param classPathId The ID of the classpath
307          * @return The classpath and never null
308          */

309         private Path findClassPath(String JavaDoc classPathId) {
310             Path path = (Path) getProject().getReference(classPathId);
311             if (path == null) {
312                 path = new Path(getProject());
313                 getProject().addReference(classPathId, path);
314             }
315
316             return path;
317         }
318
319         /**
320          * Locates the file set with the given id. If it doesn't exists it is created,
321          * stored in the project as a reference and then returned.
322          *
323          * @param fileSetId The ID of the fileset
324          * @return The fileset and never null
325          */

326         private FileSet findFileSet(String JavaDoc fileSetId) {
327             FileSet fileSet = (FileSet) getProject().getReference(fileSetId);
328             if (fileSet == null) {
329                 fileSet = new FileSet();
330                 fileSet.setDir(mediator.getLocalCacheDir());
331                 fileSet.setProject(getProject());
332                 getProject().addReference(fileSetId, fileSet);
333             }
334
335             return fileSet;
336         }
337     }
338 }
Popular Tags