KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > changelog > CvsChangelog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * CvsChangelog.java
22  *
23  * Created on January 28, 2003, 4:31 PM
24  */

25
26 package org.netbeans.nbbuild.changelog;
27
28
29 import org.apache.tools.ant.*;
30 import org.apache.tools.ant.types.*;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.text.ParseException JavaDoc;
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.util.Date JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39 import java.util.LinkedList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Vector JavaDoc;
43 import org.netbeans.lib.cvsclient.Client;
44 import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
45 import org.netbeans.lib.cvsclient.command.GlobalOptions;
46 import org.netbeans.lib.cvsclient.command.log.*;
47 import org.netbeans.lib.cvsclient.connection.*;
48 import org.netbeans.lib.cvsclient.event.*;
49
50 /**
51  * @author rbalada
52  */

53 public class CvsChangelog extends Task {
54
55     
56     private File JavaDoc topDir; // toplevel directory, where to start changelogging
57
private Date JavaDoc fromDate;
58     private Date JavaDoc toDate;
59     private String JavaDoc branch = "";
60     private Vector JavaDoc modules = new Vector JavaDoc();
61     private GlobalOptions globalOptions = new GlobalOptions();
62     private CVSRoot cvsRoot;
63
64     public void setTopDir(File JavaDoc f) {
65         log("Setting topDir to: "+f.getAbsolutePath(), Project.MSG_VERBOSE);
66         topDir = new File JavaDoc(f.getAbsolutePath());
67     }
68
69     public void setFromDate(String JavaDoc fd) {
70         if ( fd != null ) {
71             if ( ! fd.equals("")) {
72                 SimpleDateFormat JavaDoc parser = new SimpleDateFormat JavaDoc("YYYY-MM-DD HH:mm:ss");
73                 try {
74                     fromDate = parser.parse(fd);
75                 } catch (ParseException JavaDoc jpe) {
76                     throw new BuildException("Wrong date format of fromdate attribute: \""+fd+"\"", jpe, location);
77                 }
78             }
79         }
80     }
81     
82     public void setToDate(String JavaDoc td) {
83         if ( td != null ) {
84             if ( ! td.equals("")) {
85                 SimpleDateFormat JavaDoc parser = new SimpleDateFormat JavaDoc("YYYY-MM-DD HH:mm:ss");
86                 try {
87                     toDate = parser.parse(td);
88                 } catch (ParseException JavaDoc jpe) {
89                     throw new BuildException("Wrong date format of todate attribute: \""+td+"\"", jpe, location);
90                 }
91             }
92         }
93     }
94
95     public void setBranch(String JavaDoc s) {
96         if (! (s.equalsIgnoreCase("trunk") || s.equalsIgnoreCase("dev"))) {
97             branch = s;
98         }
99     }
100     
101     public void setModules(String JavaDoc s) {
102         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ",");
103         modules = new Vector JavaDoc();
104         while (st.hasMoreTokens()) {
105             modules.addElement((String JavaDoc) st.nextToken().trim());
106         }
107         if (modules.isEmpty()) {
108             throw new BuildException("No modules specified for changelog");
109         }
110     }
111  
112     /**
113      * Lookup the password in the .cvspass file. This file is looked for
114      * in the user.home directory if the option cvs.passfile is not set
115      * @param CVSRoot the CVS root for which the password is being searched
116      * @return the password, scrambled
117      */

118     private String JavaDoc lookupPassword(String JavaDoc CVSRoot) throws BuildException {
119         File JavaDoc passFile = new File JavaDoc(System.getProperty("cvs.passfile",
120                                                     System.getProperty("user.home") +
121                                                     "/.cvspass"));
122
123         BufferedReader JavaDoc reader = null;
124         String JavaDoc password = null;
125
126         try {
127             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(passFile));
128             String JavaDoc line;
129             while ((line = reader.readLine()) != null) {
130                 if (line.startsWith(CVSRoot)) {
131                     password = line.substring(CVSRoot.length() + 1);
132                     break;
133                 }
134             }
135         }
136         catch (IOException JavaDoc e) {
137             log("Could not read password for host: " + e, Project.MSG_ERR);
138             throw new BuildException("Could not read password for host. Check passfile \"" + passFile.getName() + "\"", this.location);
139         }
140         finally {
141             if (reader != null) {
142                 try {
143                     reader.close();
144                 }
145                 catch (IOException JavaDoc e) {
146                     log("Warning: could not close password file.", Project.MSG_WARN);
147                 }
148             }
149         }
150         return password;
151     }
152
153     public void setCvsRoot(String JavaDoc s) {
154         log("Trying to set CVSROOT: " + s, Project.MSG_VERBOSE);
155         try {
156             cvsRoot = new CVSRoot(s);
157             globalOptions.setCVSRoot(":"+cvsRoot.connectionType+":"+cvsRoot.user+"@"+cvsRoot.host+":"+cvsRoot.repository);
158         } catch (Exception JavaDoc e) {
159             throw new BuildException("Incorrect CVSROOT value: " + s, e, location);
160         }
161     }
162     
163     /**
164      * A struct containing the various bits of information in a CVS root
165      * string, allowing easy retrieval of individual items of information
166      */

167     private static class CVSRoot
168     {
169         public String JavaDoc connectionType;
170         public String JavaDoc user;
171         public String JavaDoc host;
172         public String JavaDoc repository;
173         
174         public CVSRoot(String JavaDoc root) throws BuildException //IllegalArgumentException
175
{
176             if (!root.startsWith(":"))
177                 throw new BuildException("CVSROOT doesn't start with a colon"); //IllegalArgumentException();
178

179             int oldColonPosition = 0;
180             int colonPosition = root.indexOf(':', 1);
181             if (colonPosition==-1)
182                 throw new BuildException("CVSROOT doesn't contain connection type"); //IllegalArgumentException();
183
connectionType = root.substring(oldColonPosition + 1, colonPosition);
184             oldColonPosition = colonPosition;
185             colonPosition = root.indexOf('@', colonPosition+1);
186             if (colonPosition==-1)
187                 throw new BuildException("CVSROOT doesn't have a username"); //IllegalArgumentException();
188
user = root.substring(oldColonPosition+1, colonPosition);
189             oldColonPosition = colonPosition;
190             colonPosition = root.indexOf(':', colonPosition+1);
191             if (colonPosition==-1)
192                 throw new BuildException("CVSROOT doesn't have a hostname"); //IllegalArgumentException();
193
host = root.substring(oldColonPosition+1, colonPosition);
194             repository = root.substring(colonPosition+1);
195             if (connectionType==null || user==null || host==null ||
196                 repository==null)
197                 throw new BuildException("CVSROOT doesn't have a repository path"); //IllegalArgumentException();
198
}
199     }
200     
201     public PServerConnection connectCVS() throws BuildException {
202         PServerConnection c = new PServerConnection();
203         c.setUserName(cvsRoot.user);
204         String JavaDoc pwd = lookupPassword(globalOptions.getCVSRoot());
205         log("Encoded CVS password for CVSROOT \"" + globalOptions.getCVSRoot() + "\" is \"" + pwd + "\"", Project.MSG_VERBOSE);
206         if ( pwd != null ) {
207             c.setEncodedPassword(pwd);
208         } else {
209             c.setEncodedPassword("A");
210         }
211         c.setHostName(cvsRoot.host);
212         c.setRepository(cvsRoot.repository);
213         log("Connecting to repository", Project.MSG_VERBOSE);
214         try {
215             c.open();
216         } catch (org.netbeans.lib.cvsclient.connection.AuthenticationException ae) {
217             log("Incorrect login/password for \""+globalOptions.getCVSRoot()+"\". Do login from command line first.", Project.MSG_ERR);
218             throw new BuildException(ae.getMessage(), ae, this.location);
219         }
220         return c;
221     }
222     
223     public class BasicListener extends CVSAdapter
224     {
225         /**
226          * Stores a tagged line
227          */

228         private final StringBuffer JavaDoc taggedLine = new StringBuffer JavaDoc();
229
230         /**
231          * Called when the server wants to send a message to be displayed to
232          * the user. The message is only for information purposes and clients
233          * can choose to ignore these messages if they wish.
234          * @param e the event
235          */

236         public void messageSent(MessageEvent e)
237         {
238             String JavaDoc line = e.getMessage();
239
240 // int logmask = e.isError()?Project.MSG_WARN:Project.MSG_INFO;
241
if (e.isTagged())
242             {
243                 String JavaDoc message = e.parseTaggedMessage(taggedLine, line);
244             // if we get back a non-null line, we have something
245
// to output. Otherwise, there is more to come and we
246
// should do nothing yet.
247
if (message != null)
248                 {
249                     if (e.isError()) {
250                         log("TERROR: " + message);//, logmask);
251
} else {
252                         log("TINFO: " + message);
253                     }
254                 }
255             }
256             else
257             {
258                 if (e.isError()) {
259                     log("LERROR: " + line);//, logmask);
260
} else {
261                     log("LINFO: " + line);
262                 }
263 // log(line);//, logmask);
264
}
265         }
266         public void commandTerminated(TerminationEvent e)
267         {
268             log("Command terminated.",Project.MSG_ERR);
269         }
270         public void fileInfoGenerated(FileInfoEvent e)
271         {
272             LogInformation li = (LogInformation) e.getInfoContainer();
273             log("FileInfoGenerated for "+li.getRepositoryFilename(), Project.MSG_VERBOSE);
274             log("Data could be: "+li.toString(), Project.MSG_VERBOSE);
275         }
276     }
277
278     public void execute() throws BuildException {
279         // connect to CVS server
280
PServerConnection connection = connectCVS();
281         // create new client instance
282
Client client = new Client(connection, new StandardAdminHandler());
283         client.setLocalPath(topDir.getName());
284         client.getEventManager().addCVSListener(new BasicListener());
285         // create new command
286
LogCommand command = new LogCommand();
287         command.setNoTags(((branch == null) || (branch.equals(""))));
288         // get file array for modules being changelogged
289
Vector JavaDoc fmodules = new Vector JavaDoc();
290         for (int i = 0; i < modules.size(); i++) {
291             final String JavaDoc module = (String JavaDoc) modules.elementAt(i);
292             File JavaDoc fmodule = new File JavaDoc(topDir, module);;
293             if (fmodule.exists()) {
294                 fmodules.addElement(fmodule);
295                 log("Info: Adding \""+module+"\" ("+fmodule.getAbsolutePath()+") to an array", Project.MSG_VERBOSE);
296             } else {
297                 log("Warning: \""+module+"\" ("+fmodule.getAbsolutePath()+") is not valid filesystem object", Project.MSG_WARN);
298             }
299         }
300         log("Got "+fmodules.size()+" objects for changelog", Project.MSG_INFO);
301         File JavaDoc[] absmodules = (File JavaDoc[]) fmodules.toArray(new File JavaDoc[fmodules.size()]);
302         command.setFiles(absmodules);
303         LogBuilder logbld = new LogBuilder(client.getEventManager(), command);
304         command.setBuilder(logbld);
305         log("cvs command: " + command.getCVSCommand(), Project.MSG_VERBOSE);
306         log("in command's local directory: "+command.getLocalDirectory(), Project.MSG_VERBOSE);
307         log("in client's local path: "+client.getLocalPath(), Project.MSG_VERBOSE);
308         try {
309             client.executeCommand(command, globalOptions);
310         } catch (Exception JavaDoc e) {
311             throw new BuildException("CVS command has failed", e, location);
312         }
313 // logbld.ii
314
log("cvs command: " + command.getCVSCommand(), Project.MSG_VERBOSE);
315     }
316 }
Popular Tags