1 19 20 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 ; 32 import java.io.File ; 33 import java.io.FileReader ; 34 import java.io.IOException ; 35 import java.text.ParseException ; 36 import java.text.SimpleDateFormat ; 37 import java.util.Date ; 38 import java.util.StringTokenizer ; 39 import java.util.LinkedList ; 40 import java.util.List ; 41 import java.util.Iterator ; 42 import java.util.Vector ; 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 53 public class CvsChangelog extends Task { 54 55 56 private File topDir; private Date fromDate; 58 private Date toDate; 59 private String branch = ""; 60 private Vector modules = new Vector (); 61 private GlobalOptions globalOptions = new GlobalOptions(); 62 private CVSRoot cvsRoot; 63 64 public void setTopDir(File f) { 65 log("Setting topDir to: "+f.getAbsolutePath(), Project.MSG_VERBOSE); 66 topDir = new File (f.getAbsolutePath()); 67 } 68 69 public void setFromDate(String fd) { 70 if ( fd != null ) { 71 if ( ! fd.equals("")) { 72 SimpleDateFormat parser = new SimpleDateFormat ("YYYY-MM-DD HH:mm:ss"); 73 try { 74 fromDate = parser.parse(fd); 75 } catch (ParseException jpe) { 76 throw new BuildException("Wrong date format of fromdate attribute: \""+fd+"\"", jpe, location); 77 } 78 } 79 } 80 } 81 82 public void setToDate(String td) { 83 if ( td != null ) { 84 if ( ! td.equals("")) { 85 SimpleDateFormat parser = new SimpleDateFormat ("YYYY-MM-DD HH:mm:ss"); 86 try { 87 toDate = parser.parse(td); 88 } catch (ParseException jpe) { 89 throw new BuildException("Wrong date format of todate attribute: \""+td+"\"", jpe, location); 90 } 91 } 92 } 93 } 94 95 public void setBranch(String s) { 96 if (! (s.equalsIgnoreCase("trunk") || s.equalsIgnoreCase("dev"))) { 97 branch = s; 98 } 99 } 100 101 public void setModules(String s) { 102 StringTokenizer st = new StringTokenizer (s, ","); 103 modules = new Vector (); 104 while (st.hasMoreTokens()) { 105 modules.addElement((String ) st.nextToken().trim()); 106 } 107 if (modules.isEmpty()) { 108 throw new BuildException("No modules specified for changelog"); 109 } 110 } 111 112 118 private String lookupPassword(String CVSRoot) throws BuildException { 119 File passFile = new File (System.getProperty("cvs.passfile", 120 System.getProperty("user.home") + 121 "/.cvspass")); 122 123 BufferedReader reader = null; 124 String password = null; 125 126 try { 127 reader = new BufferedReader (new FileReader (passFile)); 128 String 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 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 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 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 e) { 159 throw new BuildException("Incorrect CVSROOT value: " + s, e, location); 160 } 161 } 162 163 167 private static class CVSRoot 168 { 169 public String connectionType; 170 public String user; 171 public String host; 172 public String repository; 173 174 public CVSRoot(String root) throws BuildException { 176 if (!root.startsWith(":")) 177 throw new BuildException("CVSROOT doesn't start with a colon"); 179 int oldColonPosition = 0; 180 int colonPosition = root.indexOf(':', 1); 181 if (colonPosition==-1) 182 throw new BuildException("CVSROOT doesn't contain connection type"); 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"); 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"); 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"); } 199 } 200 201 public PServerConnection connectCVS() throws BuildException { 202 PServerConnection c = new PServerConnection(); 203 c.setUserName(cvsRoot.user); 204 String 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 228 private final StringBuffer taggedLine = new StringBuffer (); 229 230 236 public void messageSent(MessageEvent e) 237 { 238 String line = e.getMessage(); 239 240 if (e.isTagged()) 242 { 243 String message = e.parseTaggedMessage(taggedLine, line); 244 if (message != null) 248 { 249 if (e.isError()) { 250 log("TERROR: " + message); } else { 252 log("TINFO: " + message); 253 } 254 } 255 } 256 else 257 { 258 if (e.isError()) { 259 log("LERROR: " + line); } else { 261 log("LINFO: " + line); 262 } 263 } 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 PServerConnection connection = connectCVS(); 281 Client client = new Client(connection, new StandardAdminHandler()); 283 client.setLocalPath(topDir.getName()); 284 client.getEventManager().addCVSListener(new BasicListener()); 285 LogCommand command = new LogCommand(); 287 command.setNoTags(((branch == null) || (branch.equals("")))); 288 Vector fmodules = new Vector (); 290 for (int i = 0; i < modules.size(); i++) { 291 final String module = (String ) modules.elementAt(i); 292 File fmodule = new File (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 [] absmodules = (File []) fmodules.toArray(new File [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 e) { 311 throw new BuildException("CVS command has failed", e, location); 312 } 313 log("cvs command: " + command.getCVSCommand(), Project.MSG_VERBOSE); 315 } 316 } | Popular Tags |