]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/svn/SvnKitDriver.java
f1361ec69fbb1f7ca403b4afa2e3cf2a0dbac424
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / lib / svn / SvnKitDriver.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.lib.svn;
18
19 import java.io.File;
20 import java.io.OutputStream;
21 import java.util.List;
22 import java.util.Vector;
23
24 import org.argeo.slc.SlcException;
25 import org.argeo.slc.deploy.VersioningDriver;
26 import org.tmatesoft.svn.core.SVNException;
27 import org.tmatesoft.svn.core.SVNURL;
28 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
29 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
30 import org.tmatesoft.svn.core.io.SVNRepository;
31 import org.tmatesoft.svn.core.wc.SVNClientManager;
32 import org.tmatesoft.svn.core.wc.SVNRevision;
33 import org.tmatesoft.svn.core.wc.admin.ISVNChangeEntryHandler;
34 import org.tmatesoft.svn.core.wc.admin.SVNChangeEntry;
35
36 public class SvnKitDriver implements VersioningDriver {
37 private final SVNClientManager manager;
38
39 public SvnKitDriver() {
40 DAVRepositoryFactory.setup();
41 FSRepositoryFactory.setup();
42 manager = SVNClientManager.newInstance();
43 }
44
45 @SuppressWarnings("deprecation")
46 public void updateToHead(File fileOrDir) {
47 try {
48 manager.getUpdateClient().doUpdate(fileOrDir, SVNRevision.HEAD,
49 true);
50 } catch (Exception e) {
51 throw new SlcException("Cannot update " + fileOrDir, e);
52 }
53 }
54
55 @SuppressWarnings("deprecation")
56 public void importFileOrDir(String repositoryUrl, File fileOrDir) {
57 try {
58 manager.getCommitClient().doImport(fileOrDir,
59 SVNURL.parseURIDecoded(repositoryUrl),
60 "Import " + fileOrDir, true);
61 } catch (Exception e) {
62 throw new SlcException("Cannot import " + repositoryUrl + " to "
63 + fileOrDir, e);
64 }
65 }
66
67 @SuppressWarnings("deprecation")
68 public void checkout(String repositoryUrl, File destDir, Boolean recursive) {
69 try {
70 manager.getUpdateClient().doCheckout(
71 SVNURL.parseURIDecoded(repositoryUrl), destDir,
72 SVNRevision.UNDEFINED, SVNRevision.HEAD, recursive);
73 } catch (Exception e) {
74 throw new SlcException("Cannot checkout " + repositoryUrl + " to "
75 + destDir, e);
76 }
77 }
78
79 public void getFileFromRepository(String repositoryBaseUrl,
80 String location, OutputStream out) {
81 try {
82 SVNURL url = SVNURL.parseURIDecoded(repositoryBaseUrl);
83 SVNRepository repo = manager.createRepository(url, true);
84 repo.getFile(location, -1, null, out);
85 } catch (Exception e) {
86 throw new SlcException("Cannot retrieve file " + location
87 + " from " + repositoryBaseUrl, e);
88 }
89
90 }
91
92 public String getRelativePath(String repositoryUrl) {
93 try {
94 SVNURL url = SVNURL.parseURIDecoded(repositoryUrl);
95 SVNRepository repo = manager.createRepository(url, true);
96 return repo.getRepositoryPath("");
97 } catch (Exception e) {
98 throw new SlcException("Cannot get relative path for "
99 + repositoryUrl, e);
100 }
101 }
102
103 public String getRepositoryRoot(String repositoryUrl) {
104 try {
105 SVNURL url = SVNURL.parseURIDecoded(repositoryUrl);
106 SVNRepository repo = manager.createRepository(url, true);
107 return repo.getRepositoryRoot(true).toDecodedString();
108 } catch (Exception e) {
109 throw new SlcException("Cannot get repository root for "
110 + repositoryUrl, e);
111 }
112 }
113
114 public List<String> getChangedPaths(File repositoryRoot, Long revision) {
115 try {
116 final List<String> paths = new Vector<String>();
117 ISVNChangeEntryHandler handler = new ISVNChangeEntryHandler() {
118 public void handleEntry(SVNChangeEntry entry)
119 throws SVNException {
120 paths.add(entry.getPath());
121 }
122 };
123 manager.getLookClient().doGetChanged(repositoryRoot,
124 SVNRevision.create(revision), handler, false);
125 return paths;
126 } catch (Exception e) {
127 throw new SlcException("Cannot get changed paths at "
128 + repositoryRoot + " for revision " + revision, e);
129 }
130 }
131
132 public void createRepository(String filePath) {
133 try {
134 manager.getAdminClient().doCreateRepository(new File(filePath),
135 null, false, false);
136 } catch (Exception e) {
137 throw new SlcException("Cannot create repository " + filePath, e);
138 }
139 }
140
141 @SuppressWarnings("deprecation")
142 public void commit(File fileOrDir, String commitMessage) {
143 try {
144 manager.getCommitClient().doCommit(new File[] { fileOrDir }, true,
145 commitMessage, false, true);
146 } catch (Exception e) {
147 throw new SlcException("Cannot commit " + fileOrDir, e);
148 }
149 }
150
151 }