1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 $RCSfile: CvsPlugin.java,v $
19 Created on $Date: 2003/09/11 10:16:22 $
20 */
21 package net.sf.bloof.scm.cvsplugin;
22
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.io.UnsupportedEncodingException;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31 import java.util.Locale;
32 import java.util.logging.Logger;
33
34 import net.sf.bloof.scm.ScmAccess;
35 import net.sf.bloof.scm.ScmAccessException;
36 import net.sf.bloof.scm.ScmPlugin;
37 import net.sf.bloof.scm.ScmRevisionIterator;
38 import net.sf.bloof.testdata.TestData;
39
40 /***
41 * SCM plugin for CVS
42 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
43 * @version $Id: CvsPlugin.java,v 1.19 2003/09/11 10:16:22 pekacki Exp $
44 */
45 public class CvsPlugin implements ScmPlugin {
46 /***
47 * Returns a date from a given modTime String of a cvs logfile
48 * @param aModTime String of a cvs logfile
49 * @return Date date from a given modTime String of a cvs logfile
50 */
51 protected static Date convertFromLogTime(String aModTime) throws ParseException {
52 SimpleDateFormat format = new SimpleDateFormat(LOG_TIMESTAMP_FORMAT, LOG_TIMESTAMP_LOCALE);
53 return format.parse(aModTime);
54 }
55 /***
56 * Converts the connection method string into the
57 * Integer constant
58 * @param aConnMethodString named by connection method
59 * @return String[] connection methods
60 */
61 public static CvsConnectionMethod getConnectionMethod(String aConnMethodString) {
62 if (aConnMethodString.equals(CvsConnectionMethod.PSERVER.toString())) {
63 return CvsConnectionMethod.PSERVER;
64 }
65 if (aConnMethodString.equals(CvsConnectionMethod.SSH.toString())) {
66 return CvsConnectionMethod.SSH;
67 }
68 return CvsConnectionMethod.PSERVER;
69 }
70
71 /***
72 * Returns valid connection methods
73 * @return String[] connection methods
74 */
75 public static String[] getConnectionMethods() {
76 return CONN_METHDOS;
77 }
78
79 /***
80 * Returns the sample CvsAccess objects for example usage of the
81 * bloof application
82 * @return CvsAccess[]
83 */
84 public static CvsAccess[] getSamples() {
85 return CVS_SAMPLES;
86 }
87 /***
88 * Normalizes pathname by replacing all seperators to '/'
89 * @param aString input path
90 * @return String normalized path
91 */
92 public static String normalizePath(String aString) {
93 return aString.replace('//', NORMALZIED_PATH_SEPERATOR);
94
95 }
96
97 /***
98 * Connect to CVS Server and get the log file for the module
99 * @return Reader on the log file
100 * @throws CvsConnectionException on connection error
101 */
102 private Reader getLogReader() throws CvsConnectionException {
103 if (isConnectionToRealCvsServer()) {
104 CvsConnection cvsConn = null;
105 if (mAccessInformation.getConnectionMethod() == CvsConnectionMethod.PSERVER) {
106 try {
107 cvsConn = new PServerConnection(mAccessInformation);
108 } catch (IOException e) {
109 throw new CvsConnectionException("Could not open pserver connection. "+e.toString());
110 }
111 } else if (mAccessInformation.getConnectionMethod() == CvsConnectionMethod.SSH) {
112 try {
113 cvsConn = new MindBrightSSHConnection(mAccessInformation);
114 } catch (IOException e) {
115 throw new CvsConnectionException(e);
116 }
117 } else if (mAccessInformation.getConnectionMethod() == CvsConnectionMethod.EXT) {
118 cvsConn = new ExternalProcessServerConnection(mAccessInformation);
119 } else {
120 throw new CvsConnectionException(
121 "The specified connection method "
122 + mAccessInformation.getConnectionMethod()
123 + " is invalid");
124 }
125 mCvsConnection = cvsConn;
126 return new InputStreamReader(cvsConn.getInputStream());
127 } else {
128 Reader r = null;
129 if (mAccessInformation.getConnectionMethod() == CvsConnectionMethod.LOGFILE) {
130 try {
131 r = new java.io.FileReader(mAccessInformation.getLogfilePath());
132 } catch (java.io.FileNotFoundException e) {
133 sLogger.warning(
134 "Could not open local log file at:" + mAccessInformation.getLogfilePath());
135 }
136 } else if (mAccessInformation.getConnectionMethod() == CvsConnectionMethod.SAMPLE) {
137 r =
138 new InputStreamReader(
139 TestData.class.getResourceAsStream(mAccessInformation.getLogfilePath()));
140 } else {
141 throw new CvsConnectionException(
142 "The specified connection method "
143 + mAccessInformation.getConnectionMethod()
144 + "is invalid");
145 }
146 return r;
147 }
148
149 }
150 /***
151 * @see net.sf.bloof.scm.ScmPlugin#getRevisions( ScmAccess )
152 */
153 public ScmRevisionIterator getRevisions(ScmAccess aAccessInformation)
154 throws ScmAccessException {
155 mAccessInformation = (CvsAccess) aAccessInformation;
156 // connect & get output stream
157 Reader read = getLogReader();
158 if (isConnectionToRealCvsServer()) {
159 try {
160 sendLogRequest();
161 } catch (IOException e) {
162 throw new ScmAccessException(e.toString());
163 }
164 }
165 RevisionIterator revIter = new RevisionIterator(read);
166 return revIter;
167 }
168
169 /***
170 * @see net.sf.bloof.scm.ScmPlugin#getRevisionsUpdate(net.sf.bloof.scm.ScmAccess, java.util.Date)
171 */
172 public ScmRevisionIterator getRevisionsUpdate(ScmAccess aAccessInformation, Date aFromDate)
173 throws ScmAccessException {
174 mAccessInformation = (CvsAccess) aAccessInformation;
175 // connect & get output stream
176 Reader read = getLogReader();
177 if (isConnectionToRealCvsServer()) {
178 try {
179 sendUpdateRequest(aFromDate);
180 } catch (IOException e) {
181 throw new ScmAccessException(e.toString());
182 }
183 }
184 RevisionIterator revIter = new RevisionIterator(read);
185 return revIter;
186 }
187
188 /***
189 * Method isConnectionToRealCvsServer.
190 * @return boolean
191 */
192 private boolean isConnectionToRealCvsServer() {
193 return (
194 mAccessInformation.getConnectionMethod() != CvsConnectionMethod.LOGFILE
195 && mAccessInformation.getConnectionMethod() != CvsConnectionMethod.SAMPLE);
196 }
197
198 private void sendLine(OutputStream aOut, String aLogCommand)
199 throws UnsupportedEncodingException, IOException {
200 sLogger.fine("Send:" + aLogCommand);
201 aLogCommand += "\n";
202 byte[] outBytes = null;
203 outBytes = aLogCommand.getBytes("ASCII");
204 aOut.write(outBytes);
205 aOut.flush();
206 }
207
208 private void sendLogRequest() throws IOException {
209 OutputStream out = mCvsConnection.getOutputStream();
210 sendLine(out, "Root " + mAccessInformation.getRepositoryRoot());
211 sendLine(out, "Argument " + mAccessInformation.getModuleName());
212 sendLine(out, "rlog \n");
213
214 }
215
216 private void sendUpdateRequest(Date aFromDate) throws IOException {
217 OutputStream out = mCvsConnection.getOutputStream();
218 sendLine(out, "Root " + mAccessInformation.getRepositoryRoot());
219 sendLine(out,"Argument -d");
220 sendLine(out,"Argument >" + mDateFormat.format(aFromDate));
221 sendLine(out, "Argument " + mAccessInformation.getModuleName());
222 sendLine(out, "rlog \n");
223
224 }
225
226 private static final String[] CONN_METHDOS =
227 { CvsConnectionMethod.ID_PSERVER, CvsConnectionMethod.ID_SSH };
228 private final static String CVS_DATE_FORMAT = "yyyy/MM/dd";
229 private static final CvsAccess[] CVS_SAMPLES =
230 {
231 new CvsAccess(
232 RepositoryLocation.createValidLocation(
233 CvsConnectionMethod.SAMPLE,
234 "nouser",
235 "nohost",
236 "/cvsroot/b/bl/bloof"),
237 "bloof.log",
238 "bloof"),
239 new CvsAccess(
240 RepositoryLocation.createValidLocation(
241 CvsConnectionMethod.SAMPLE,
242 "nouser",
243 "nohost",
244 "/cvsroot/j/ju/junit"),
245 "junit.log",
246 "junit"),
247 new CvsAccess(
248 RepositoryLocation.createValidLocation(
249 CvsConnectionMethod.SAMPLE,
250 "nouser",
251 "nohost",
252 "/cvsroot/c/ch/checkstyle"),
253 "checkstyle.log",
254 "checkstyle")};
255 /***
256 * constants for date functions
257 */
258 private static final String LOG_TIMESTAMP_FORMAT = "yyyy/MM/dd HH:mm:ss zzz";
259 private static final Locale LOG_TIMESTAMP_LOCALE = Locale.US;
260 /*** mini log file for testing purposes */
261 public static final CvsAccess MINI_LOG =
262 new CvsAccess(
263 RepositoryLocation.createValidLocation(
264 CvsConnectionMethod.SAMPLE,
265 "nouser",
266 "nohost",
267 "/rep"),
268 "einfach.log",
269 "mini example");
270
271 /*** Normalized path seperator */
272 public static final char NORMALZIED_PATH_SEPERATOR = '/';
273
274 private static Logger sLogger = Logger.getLogger("net.sf.bloof.cvsplugin.CvsPlugin");
275 private CvsAccess mAccessInformation;
276 private CvsConnection mCvsConnection;
277 private SimpleDateFormat mDateFormat = new SimpleDateFormat(CVS_DATE_FORMAT);
278
279 }
This page was automatically generated by Maven