KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Set JavaDoc;
12
13 import org.apache.tools.ant.BuildException;
14 import org.apache.tools.ant.Task;
15
16 import com.inversoft.savant.ArtifactTools;
17 import com.inversoft.savant.LocalCacheStore;
18 import com.inversoft.savant.SavantException;
19 import com.inversoft.savant.ant.types.ArtifactType;
20 import com.inversoft.savant.ant.types.DependenciesType;
21
22
23 /**
24  * <p>
25  * This class is an ant taskdef that publishes a local artifact
26  * to the local cache store.
27  * </p>
28  *
29  * @author Brian Pontarelli
30  */

31 public class PublishTask extends Task {
32
33     private File JavaDoc from;
34     private File JavaDoc localcache;
35     private ArtifactType artifact;
36     private DependenciesType deps;
37
38
39     public File JavaDoc getFrom() {
40         return from;
41     }
42
43     public void setFrom(File JavaDoc from) {
44         this.from = from;
45     }
46
47     public File JavaDoc getLocalcache() {
48         return localcache;
49     }
50
51     public void setLocalcache(File JavaDoc localcache) {
52         this.localcache = localcache;
53     }
54
55     /**
56      * Adds the artifact to the publish.
57      *
58      * @param artifact The artifact
59      * @throws BuildException If the artifact was already set
60      */

61     public void addArtifact(ArtifactType artifact) {
62         if (this.artifact != null) {
63             throw new BuildException("Only one artifact per publish tag");
64         }
65
66         this.artifact = artifact;
67     }
68
69     /**
70      * Adds the list of artifact dependencies. This will generate the XML dependency
71      * file and publish it to the LocalCacheStore.
72      *
73      * @param deps The dependencies.
74      * @throws BuildException If the dependencies were already set.
75      */

76     public void addDependencies(DependenciesType deps) {
77         if (this.deps != null) {
78             throw new BuildException("Only one artifact per publish tag");
79         }
80
81         this.deps = deps;
82     }
83
84     /**
85      * Publishes the artifact specified from the file specified.
86      *
87      * @throws BuildException If the publish fails
88      */

89     public void execute() throws BuildException {
90         validate();
91
92         LocalCacheStore lcs;
93         try {
94             lcs = new LocalCacheStore(localcache);
95         } catch (SavantException se) {
96             log("Invalid local cache location [" + localcache.getAbsolutePath() + "]");
97             throw new BuildException(se);
98         }
99
100         try {
101             lcs.store(artifact.getProxy(), from);
102         } catch (SavantException se) {
103             throw new BuildException(se);
104         }
105
106         log("Published artifact [" + artifact.getProxy() + "]");
107
108         // Publish the XML file
109
if (deps != null) {
110             Set JavaDoc artifacts = deps.getAllArtifacts();
111             if (artifacts.size() > 0) {
112                 File JavaDoc file = null;
113                 try {
114                     file = ArtifactTools.generateXML(artifacts);
115                     lcs.storeDeps(artifact.getProxy(), file);
116                 } catch (SavantException se) {
117                     throw new BuildException(se);
118                 } finally {
119                     if (file != null) {
120                         file.delete();
121                     }
122                 }
123             }
124         }
125     }
126
127     /**
128      * Validates the parameters.
129      */

130     private void validate() {
131         if (from == null) {
132             throw new BuildException("from attribute required for publish task");
133         }
134
135         if (!from.exists() || from.isDirectory()) {
136             throw new BuildException("Invalid from file [" + from.getAbsolutePath() +
137                 "]");
138         }
139     }
140 }
Popular Tags