1 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 ; 40 import java.util.ArrayList ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class SubversionClient { 48 private final L10N L = new L10N(SubversionClient.class); 49 private final Logger 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 host, int port) 60 throws IOException 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 String 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 uuid = _in.readString(); 88 String url = _in.readString(); 89 90 _in.expect(')'); 91 _in.expect(')'); 92 } 93 94 98 public long getLatestRev() 99 throws IOException 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 checkPath(String s) 120 throws IOException 121 { 122 println("( check-path ( " + s.length() + ":" + s + " ( ) ) )"); 123 124 readSuccess(); 125 126 _in.expect('('); 127 expectSuccess(); 128 _in.expect('('); 129 130 String type = _in.readLiteral(); 131 132 _in.expect(')'); 133 _in.expect(')'); 134 135 return type; 136 } 137 138 public Object getDir(String s) 139 throws IOException 140 { 141 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 props = _in.readSexp(); 154 155 ArrayList <SubversionNode> results = new ArrayList <SubversionNode>(); 156 157 _in.expect('('); 158 159 while (true) { 160 _in.skipWhitespace(); 161 162 int ch = _in.read(); 163 164 if (ch == '(') { 165 String name = _in.readString(); 166 String 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 modified = _in.readString(); 173 _in.expect(')'); 174 175 _in.expect('('); 176 String 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 update(long version, String s) 213 throws IOException 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 cmd = _in.readLiteral(); 238 239 Object 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 setPath(long version, String s) 252 throws IOException 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 finishReport() 269 throws IOException 270 { 271 println("( finish-report ( ) )"); 272 273 readSuccess(); 274 275 return _in.readSexp(); 276 } 277 278 public void doMore() 279 throws IOException 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 296 { 297 _in.expect('('); 298 expectSuccess(); 299 _in.expect('('); 300 301 long major = _in.readLong(); 302 long minor = _in.readLong(); 303 304 Object auth = _in.readSexp(); 305 Object cap = _in.readSexp(); 306 307 _in.expect(')'); 308 _in.expect(')'); 309 } 310 311 private void readLoginResponse() 312 throws IOException 313 { 314 _in.expect('('); 315 expectSuccess(); 316 _in.expect('('); 317 318 Object cap = _in.readSexp(); 319 320 String code = _in.readString(); 321 322 _in.expect(')'); 323 _in.expect(')'); 324 } 325 326 public void readSuccess() 327 throws IOException 328 { 329 _in.expect('('); 330 331 expectSuccess(); 332 333 _in.readSexp(); 334 335 _in.expect(')'); 336 } 337 338 public void expectSuccess() 339 throws IOException 340 { 341 String 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 msg) 349 throws IOException 350 { 351 if (log.isLoggable(Level.FINER)) 352 log.finer(msg); 353 354 _os.println(msg); 355 } 356 357 private IOException error(String msg) 358 { 359 return new IOException (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 e) { 376 } 377 } 378 379 if (is != null) { 380 is.close(); 381 } 382 } 383 } 384 | Popular Tags |