KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > MavenSnapshotDependency


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import java.io.File JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Date JavaDoc;
43 import java.util.Hashtable JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 import net.sourceforge.cruisecontrol.CruiseControlException;
49 import net.sourceforge.cruisecontrol.Modification;
50 import net.sourceforge.cruisecontrol.SourceControl;
51 import net.sourceforge.cruisecontrol.util.ValidationHelper;
52
53 import org.apache.log4j.Logger;
54 import org.jdom.Element;
55 import org.jdom.JDOMException;
56 import org.jdom.input.SAXBuilder;
57
58 /**
59  * Checks binary dependencies listed in a Maven project rather than in a
60  * repository.
61  *
62  * <at> author Tim Shadel
63  */

64 public class MavenSnapshotDependency implements SourceControl {
65
66     private Hashtable JavaDoc properties = new Hashtable JavaDoc();
67     private String JavaDoc property;
68     private List JavaDoc modifications;
69     private File JavaDoc projectFile;
70     private File JavaDoc localRepository = new File JavaDoc(System.getProperty("user.home") + "/.maven/repository/");
71     private String JavaDoc user;
72
73     /** enable logging for this class */
74     private static Logger log = Logger.getLogger(MavenSnapshotDependency.class);
75
76     /**
77      * Set the root folder of the directories that we are going to scan
78      */

79     public void setProjectFile(String JavaDoc s) {
80         projectFile = new File JavaDoc(s);
81     }
82
83     /**
84      * Set the path for the local Maven repository
85      */

86     public void setLocalRepository(String JavaDoc s) {
87         localRepository = new File JavaDoc(s);
88     }
89
90     /**
91      * Set the username listed with changes found in binary dependencies
92      */

93     public void setUser(String JavaDoc s) {
94         user = s;
95     }
96
97     public void setProperty(String JavaDoc property) {
98         this.property = property;
99     }
100
101     public Map JavaDoc getProperties() {
102         return properties;
103     }
104
105     public void validate() throws CruiseControlException {
106         ValidationHelper.assertIsSet(projectFile, "projectFile", this.getClass());
107         ValidationHelper.assertTrue(projectFile.exists(),
108             "Project file '" + projectFile.getAbsolutePath() + "' does not exist.");
109         ValidationHelper.assertFalse(projectFile.isDirectory(),
110             "The directory '" + projectFile.getAbsolutePath()
111             + "' cannot be used as the projectFile for MavenSnapshotDependency.");
112
113         ValidationHelper.assertTrue(localRepository.exists(),
114             "Local Maven repository '" + localRepository.getAbsolutePath() + "' does not exist.");
115         ValidationHelper.assertTrue(localRepository.isDirectory(),
116             "Local Maven repository '" + localRepository.getAbsolutePath()
117             + "' must be a directory.");
118     }
119
120     /**
121      * The quiet period is ignored. All dependencies changed since the last
122      * build trigger a modification.
123      *
124      * <at> param lastBuild
125      * date of last build
126      * <at> param now
127      * IGNORED
128      */

129     public List JavaDoc getModifications(Date JavaDoc lastBuild, Date JavaDoc now) {
130         modifications = new ArrayList JavaDoc();
131
132         checkProjectDependencies(projectFile, lastBuild.getTime());
133
134         return modifications;
135     }
136
137     /**
138      * Add a Modification to the list of modifications. All modifications are
139      * listed as "change" and all have the same comment.
140      */

141     private void addRevision(File JavaDoc dependency) {
142         Modification mod = new Modification("maven");
143         Modification.ModifiedFile modfile = mod.createModifiedFile(dependency.getName(), dependency.getParent());
144         modfile.action = "change";
145
146         mod.userName = user;
147         mod.modifiedTime = new Date JavaDoc(dependency.lastModified());
148         mod.comment = "Maven project dependency: timestamp change detected.";
149         modifications.add(mod);
150
151         if (property != null) {
152             properties.put(property, "true");
153         }
154     }
155
156     /**
157      * Use Maven library to open project file and check for newer dependencies.
158      * Do not download them, only list them as modifications.
159      */

160     private void checkProjectDependencies(File JavaDoc projectFile, long lastBuild) {
161         List JavaDoc filenames = getSnapshotFilenames(projectFile);
162         Iterator JavaDoc itr = filenames.iterator();
163         while (itr.hasNext()) {
164             String JavaDoc filename = (String JavaDoc) itr.next();
165             File JavaDoc dependency = new File JavaDoc(filename);
166             checkFile(dependency, lastBuild);
167         }
168     }
169
170     /**
171      * Parse the Maven project file, and file names
172      */

173     List JavaDoc getSnapshotFilenames(File JavaDoc mavenFile) {
174         List JavaDoc filenames = new ArrayList JavaDoc();
175         Element mavenElement;
176         SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
177         try {
178             mavenElement = builder.build(mavenFile).getRootElement();
179         } catch (JDOMException e) {
180             log.error("failed to load project file ["
181                 + (mavenFile != null ? mavenFile.getAbsolutePath() : "")
182                 + "]", e);
183             return filenames;
184         } catch (IOException JavaDoc e) {
185             log.error("failed to load project file ["
186                 + (mavenFile != null ? mavenFile.getAbsolutePath() : "")
187                 + "]", e);
188             return filenames;
189         }
190         Element depsRoot = mavenElement.getChild("dependencies");
191
192         // No dependencies listed at all
193
if (depsRoot == null) {
194             return filenames;
195         }
196         List JavaDoc dependencies = depsRoot.getChildren();
197         Iterator JavaDoc itr = dependencies.iterator();
198         while (itr.hasNext()) {
199             Element dependency = (Element) itr.next();
200             String JavaDoc versionText = dependency.getChildText("version");
201             if (versionText != null && versionText.endsWith("SNAPSHOT")) {
202                 String JavaDoc groupId = dependency.getChildText("groupId");
203                 String JavaDoc artifactId = dependency.getChildText("artifactId");
204                 String JavaDoc id = dependency.getChildText("id");
205                 String JavaDoc type = dependency.getChildText("type");
206                 if (type == null) {
207                     type = "jar";
208                 }
209
210                 // Format:
211
// ${repo}/${groupId}/${type}s/${artifactId}-${version}.${type}
212
StringBuffer JavaDoc fileName = new StringBuffer JavaDoc();
213                 fileName.append(localRepository.getAbsolutePath());
214                 fileName.append('/');
215                 if (groupId != null) {
216                     fileName.append(groupId);
217                 } else {
218                     fileName.append(id);
219                 }
220                 fileName.append('/');
221                 fileName.append(type);
222                 fileName.append('s');
223                 fileName.append('/');
224                 if (artifactId != null) {
225                     fileName.append(artifactId);
226                 } else {
227                     fileName.append(id);
228                 }
229                 fileName.append('-');
230                 fileName.append(versionText);
231                 fileName.append('.');
232                 if ("uberjar".equals(type) || "ejb".equals(type)
233                         || "plugin".equals(type)) {
234                     fileName.append("jar");
235                 } else {
236                     fileName.append(type);
237                 }
238                 File JavaDoc file = new File JavaDoc(fileName.toString());
239                 filenames.add(file.getAbsolutePath());
240             }
241         }
242         return filenames;
243     }
244
245     /** Check for newer timestamps */
246     private void checkFile(File JavaDoc file, long lastBuild) {
247         if ((!file.isDirectory()) && (file.lastModified() > lastBuild)) {
248             addRevision(file);
249         }
250     }
251 }
252
Popular Tags