]> 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
1e994e4182fbdf5410844a40f5812c31668a4b80
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / lib / svn / SvnKitDriver.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.slc.lib.svn;
17
18 import java.io.File;
19 import java.io.OutputStream;
20 import java.util.List;
21 import java.util.Vector;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.deploy.VersioningDriver;
27 import org.tmatesoft.svn.core.SVNDepth;
28 import org.tmatesoft.svn.core.SVNException;
29 import org.tmatesoft.svn.core.SVNURL;
30 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
31 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
32 import org.tmatesoft.svn.core.io.SVNRepository;
33 import org.tmatesoft.svn.core.wc.SVNClientManager;
34 import org.tmatesoft.svn.core.wc.SVNRevision;
35 import org.tmatesoft.svn.core.wc.SVNWCUtil;
36 import org.tmatesoft.svn.core.wc.admin.ISVNChangeEntryHandler;
37 import org.tmatesoft.svn.core.wc.admin.SVNChangeEntry;
38
39 /** Versioning driver with a Subversion backen, based on SVNKit */
40 public class SvnKitDriver implements VersioningDriver {
41 private final static Log log = LogFactory.getLog(SvnKitDriver.class);
42
43 private final SVNClientManager manager;
44
45 public SvnKitDriver() {
46 DAVRepositoryFactory.setup();
47 FSRepositoryFactory.setup();
48 manager = SVNClientManager.newInstance();
49 }
50
51 @SuppressWarnings("deprecation")
52 public void updateToHead(File fileOrDir) {
53 try {
54 manager.getUpdateClient().doUpdate(fileOrDir, SVNRevision.HEAD,
55 true);
56 } catch (Exception e) {
57 throw new SlcException("Cannot update " + fileOrDir, e);
58 }
59 }
60
61 @SuppressWarnings("deprecation")
62 public void importFileOrDir(String repositoryUrl, File fileOrDir) {
63 try {
64 manager.getCommitClient().doImport(fileOrDir,
65 SVNURL.parseURIDecoded(repositoryUrl),
66 "Import " + fileOrDir, true);
67 } catch (Exception e) {
68 throw new SlcException("Cannot import " + repositoryUrl + " to "
69 + fileOrDir, e);
70 }
71 }
72
73 public Boolean checkout(String repositoryUrl, File destDir,
74 Boolean recursive) {
75 try {
76 SVNRevision previousRevision = null;
77 if (destDir.exists() && SVNWCUtil.isVersionedDirectory(destDir)) {
78 previousRevision = manager.getWCClient().doInfo(destDir, null)
79 .getRevision();
80 }
81 if (previousRevision == null && log.isDebugEnabled())
82 log.debug("Checking out " + repositoryUrl + " to " + destDir
83 + "...");
84 long revision = manager.getUpdateClient().doCheckout(
85 SVNURL.parseURIDecoded(repositoryUrl), destDir,
86 SVNRevision.UNDEFINED, SVNRevision.HEAD, SVNDepth.INFINITY,
87 recursive);
88 if (previousRevision != null
89 && previousRevision.getNumber() == revision) {
90 if (log.isTraceEnabled())
91 log.trace(destDir + " already at revision " + revision);
92 return false;
93
94 } else {
95 if (log.isDebugEnabled())
96 if (previousRevision != null)
97 log.debug(destDir + " updated to revision " + revision
98 + " from " + previousRevision.getNumber());
99 else
100 log.debug(destDir + " checked out to revision "
101 + revision);
102 return true;
103 }
104 } catch (Exception e) {
105 throw new SlcException("Cannot checkout " + repositoryUrl + " to "
106 + destDir, e);
107 }
108 }
109
110 public void getFileFromRepository(String repositoryBaseUrl,
111 String location, OutputStream out) {
112 try {
113 SVNURL url = SVNURL.parseURIDecoded(repositoryBaseUrl);
114 SVNRepository repo = manager.createRepository(url, true);
115 repo.getFile(location, -1, null, out);
116 } catch (Exception e) {
117 throw new SlcException("Cannot retrieve file " + location
118 + " from " + repositoryBaseUrl, e);
119 }
120
121 }
122
123 public String getRelativePath(String repositoryUrl) {
124 try {
125 SVNURL url = SVNURL.parseURIDecoded(repositoryUrl);
126 SVNRepository repo = manager.createRepository(url, true);
127 return repo.getRepositoryPath("");
128 } catch (Exception e) {
129 throw new SlcException("Cannot get relative path for "
130 + repositoryUrl, e);
131 }
132 }
133
134 public String getRepositoryRoot(String repositoryUrl) {
135 try {
136 SVNURL url = SVNURL.parseURIDecoded(repositoryUrl);
137 SVNRepository repo = manager.createRepository(url, true);
138 return repo.getRepositoryRoot(true).toDecodedString();
139 } catch (Exception e) {
140 throw new SlcException("Cannot get repository root for "
141 + repositoryUrl, e);
142 }
143 }
144
145 public List<String> getChangedPaths(File repositoryRoot, Long revision) {
146 try {
147 final List<String> paths = new Vector<String>();
148 ISVNChangeEntryHandler handler = new ISVNChangeEntryHandler() {
149 public void handleEntry(SVNChangeEntry entry)
150 throws SVNException {
151 paths.add(entry.getPath());
152 }
153 };
154 manager.getLookClient().doGetChanged(repositoryRoot,
155 SVNRevision.create(revision), handler, false);
156 return paths;
157 } catch (Exception e) {
158 throw new SlcException("Cannot get changed paths at "
159 + repositoryRoot + " for revision " + revision, e);
160 }
161 }
162
163 public void createRepository(String filePath) {
164 try {
165 manager.getAdminClient().doCreateRepository(new File(filePath),
166 null, false, false);
167 } catch (Exception e) {
168 throw new SlcException("Cannot create repository " + filePath, e);
169 }
170 }
171
172 @SuppressWarnings("deprecation")
173 public void commit(File fileOrDir, String commitMessage) {
174 try {
175 manager.getCommitClient().doCommit(new File[] { fileOrDir }, true,
176 commitMessage, false, true);
177 } catch (Exception e) {
178 throw new SlcException("Cannot commit " + fileOrDir, e);
179 }
180 }
181
182 }