KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jcr > svn > SubversionClient


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jcr.svn;
31
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.vfs.ReadWritePair;
36 import com.caucho.vfs.Vfs;
37 import com.caucho.vfs.WriteStream;
38
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Subversion Client class.
46  */

47 public class SubversionClient {
48   private final L10N L = new L10N(SubversionClient.class);
49   private final Logger JavaDoc log
50     = Logger.getLogger(SubversionClient.class.getName());;
51
52   private Path _path;
53   private ReadStream _is;
54   private WriteStream _os;
55   
56   private SubversionInput _in;
57   private long _rev = 1;
58
59   public SubversionClient(String JavaDoc host, int port)
60     throws IOException JavaDoc
61   {
62     _path = Vfs.lookup("tcp://" + host + ":" + port);
63
64     ReadWritePair pair = _path.openReadWrite();
65
66     _is = pair.getReadStream();
67     _os = pair.getWriteStream();
68
69     _in = new SubversionInput(_is);
70
71     readHello();
72
73     // anonymous login (?)
74
String JavaDoc svnurl = "svn://" + host + ":" + port;
75     println("( 2 ( edit-pipeline ) "+svnurl.length()+":"+svnurl+ " )");
76
77     readLoginResponse();
78     
79     println("( ANONYMOUS ( 0: ) )");
80
81     readSuccess();
82
83     _in.expect('(');
84     expectSuccess();
85     _in.expect('(');
86
87     String JavaDoc uuid = _in.readString();
88     String JavaDoc url = _in.readString();
89     
90     _in.expect(')');
91     _in.expect(')');
92   }
93
94   /**
95    * Sends a get-latest-rev request to the client, returning the latest
96    * version.
97    */

98   public long getLatestRev()
99     throws IOException JavaDoc
100   {
101     println("( get-latest-rev ( ) )");
102     
103     readSuccess();
104
105     _in.expect('(');
106     expectSuccess();
107     _in.expect('(');
108
109     long value = _in.readLong();
110
111     _rev = value;
112     
113     _in.expect(')');
114     _in.expect(')');
115     
116     return value;
117   }
118
119   public String JavaDoc checkPath(String JavaDoc s)
120     throws IOException JavaDoc
121   {
122     println("( check-path ( " + s.length() + ":" + s + " ( ) ) )");
123     
124     readSuccess();
125
126     _in.expect('(');
127     expectSuccess();
128     _in.expect('(');
129
130     String JavaDoc type = _in.readLiteral();
131     
132     _in.expect(')');
133     _in.expect(')');
134     
135     return type;
136   }
137
138   public Object JavaDoc getDir(String JavaDoc s)
139     throws IOException JavaDoc
140   {
141     // wantProps, wantContents
142
println("( get-dir ( " + s.length() + ":" + s + " ( " + _rev + " ) false true ) )");
143     
144     readSuccess();
145
146     _in.expect('(');
147     expectSuccess();
148
149     _in.expect('(');
150
151     long dirVersion = _in.readLong();
152
153     Object JavaDoc props = _in.readSexp();
154
155     ArrayList JavaDoc<SubversionNode> results = new ArrayList JavaDoc<SubversionNode>();
156
157     _in.expect('(');
158
159     while (true) {
160       _in.skipWhitespace();
161
162       int ch = _in.read();
163
164       if (ch == '(') {
165     String JavaDoc name = _in.readString();
166     String JavaDoc type = _in.readLiteral();
167     long length = _in.readLong();
168     boolean bValue = ! "false".equals(_in.readLiteral());
169     long version = _in.readLong();
170
171     _in.expect('(');
172     String JavaDoc modified = _in.readString();
173     _in.expect(')');
174     
175     _in.expect('(');
176     String JavaDoc user = _in.readString();
177     _in.expect(')');
178     _in.expect(')');
179
180     SubversionNode node;
181
182     if ("dir".equals(type))
183       node = new SubversionFolder(name);
184     else if ("file".equals(type)) {
185       SubversionFile file = new SubversionFile(name);
186       file.setLength(length);
187       node = file;
188     }
189     else
190       node = new SubversionNode(name);
191     
192     node.setVersion(version);
193     node.setUser(user);
194
195     results.add(node);
196       }
197       else if (ch == ')')
198     break;
199       else {
200     throw error(L.l("Expected '(' at {0} (0x{1})",
201             String.valueOf(ch),
202             Integer.toHexString(ch)));
203       }
204     }
205       
206     _in.expect(')');
207     _in.expect(')');
208     
209     return results;
210   }
211
212   public Object JavaDoc update(long version, String JavaDoc s)
213     throws IOException JavaDoc
214   {
215     boolean recurse = true;
216     
217     println("( update ( " +
218         " ( " + version + " ) " +
219         s.length() + ":" + s + " " +
220         recurse + " ) )");
221     
222     readSuccess();
223     
224     boolean startEmpty = true;
225     
226     println("( set-path ( " +
227         s.length() + ":" + s + " " +
228         version + " " +
229         startEmpty + " ) )");
230     
231     println("( finish-report ( ) )");
232     
233     readSuccess();
234
235     while (true) {
236       _in.expect('(');
237       String JavaDoc cmd = _in.readLiteral();
238
239       Object JavaDoc arg = _in.readSexp();
240       _in.expect(')');
241
242       System.out.println("CMD: " + cmd);
243
244       if ("close-edit".equals(cmd))
245     break;
246     }
247     
248     return "ok";
249   }
250
251   public Object JavaDoc setPath(long version, String JavaDoc s)
252     throws IOException JavaDoc
253   {
254     boolean startEmpty = true;
255     
256     println("( set-path ( " +
257         s.length() + ":" + s + " " +
258         version + " " +
259         startEmpty + " ) )");
260     
261     println("( finish-report ( ) )");
262     
263     readSuccess();
264
265     return _in.readSexp();
266   }
267
268   public Object JavaDoc finishReport()
269     throws IOException JavaDoc
270   {
271     println("( finish-report ( ) )");
272     
273     readSuccess();
274
275     return _in.readSexp();
276   }
277
278   public void doMore()
279     throws IOException JavaDoc
280   {
281     println("( check-path ( 0: ( ) ) )");
282     
283     readSuccess();
284     
285     System.out.println(_in.readSexp());
286
287     println("( get-dir ( 0: ( 1 ) false true ) )");
288     
289     readSuccess();
290     
291     System.out.println(_in.readSexp());
292   }
293
294   private void readHello()
295     throws IOException JavaDoc
296   {
297     _in.expect('(');
298     expectSuccess();
299     _in.expect('(');
300
301     long major = _in.readLong();
302     long minor = _in.readLong();
303
304     Object JavaDoc auth = _in.readSexp();
305     Object JavaDoc cap = _in.readSexp();
306
307     _in.expect(')');
308     _in.expect(')');
309   }
310
311   private void readLoginResponse()
312     throws IOException JavaDoc
313   {
314     _in.expect('(');
315     expectSuccess();
316     _in.expect('(');
317
318     Object JavaDoc cap = _in.readSexp();
319
320     String JavaDoc code = _in.readString();
321
322     _in.expect(')');
323     _in.expect(')');
324   }
325
326   public void readSuccess()
327     throws IOException JavaDoc
328   {
329     _in.expect('(');
330     
331     expectSuccess();
332
333     _in.readSexp();
334     
335     _in.expect(')');
336   }
337
338   public void expectSuccess()
339     throws IOException JavaDoc
340   {
341     String JavaDoc token = _in.readLiteral();
342
343     if (! "success".equals(token))
344       throw error(L.l("Expected 'success' at {0}",
345               token));
346   }
347
348   private void println(String JavaDoc msg)
349     throws IOException JavaDoc
350   {
351     if (log.isLoggable(Level.FINER))
352       log.finer(msg);
353     
354     _os.println(msg);
355   }
356
357   private IOException JavaDoc error(String JavaDoc msg)
358   {
359     return new IOException JavaDoc(msg);
360   }
361
362   public void close()
363   {
364     ReadStream is = _is;
365     _is = null;
366     
367     WriteStream os = _os;
368     _os = null;
369
370     _in.close();
371
372     if (os != null) {
373       try {
374     os.close();
375       } catch (IOException JavaDoc e) {
376       }
377     }
378
379     if (is != null) {
380       is.close();
381     }
382   }
383 }
384
Popular Tags