1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.connector.svn;
25
26 import java.util.Collection;
27 import java.util.Collections;
28 import org.modeshape.graph.connector.RepositorySourceException;
29 import org.modeshape.graph.request.InvalidWorkspaceException;
30 import org.tmatesoft.svn.core.SVNDirEntry;
31 import org.tmatesoft.svn.core.SVNErrorCode;
32 import org.tmatesoft.svn.core.SVNErrorMessage;
33 import org.tmatesoft.svn.core.SVNException;
34 import org.tmatesoft.svn.core.SVNNodeKind;
35 import org.tmatesoft.svn.core.SVNURL;
36 import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
37 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
38 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
39 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
40 import org.tmatesoft.svn.core.io.SVNRepository;
41 import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
42 import org.tmatesoft.svn.core.wc.SVNWCUtil;
43
44
45
46 public class SvnRepositoryUtil {
47
48
49
50
51
52
53 public static SVNURL createSVNURL( String url,
54 String sourceName ) {
55
56 SVNURL theUrl;
57 try {
58 theUrl = SVNURL.parseURIDecoded(url);
59 } catch (SVNException e) {
60
61 throw new RepositorySourceException(sourceName,
62 "Protocol is not supported by this connector or there is problem in the svn url");
63 }
64 return theUrl;
65 }
66
67 public static void setNewSVNRepositoryLocation( SVNRepository oldRespository,
68 String url,
69 boolean forceReconnect,
70 String sourceName ) {
71 try {
72 oldRespository.setLocation(createSVNURL(url, sourceName), forceReconnect);
73 } catch (SVNException e) {
74 throw new RepositorySourceException(sourceName, "the old url and a new one has got different protocols");
75 }
76 }
77
78
79
80
81
82
83
84
85 public static SVNNodeKind checkThePath( SVNRepository repository,
86 String path,
87 long revisionNumber,
88 String sourceName ) {
89 SVNNodeKind kind;
90 try {
91 kind = repository.checkPath(path, revisionNumber);
92
93 } catch (SVNException e) {
94 return null;
95 }
96 return kind;
97 }
98
99
100
101
102
103
104
105
106
107 public static SVNRepository createRepository( String url,
108 String username,
109 String password ) {
110
111 DAVRepositoryFactory.setup();
112
113 FSRepositoryFactory.setup();
114
115 SVNRepositoryFactoryImpl.setup();
116
117
118 SVNRepository repository;
119 try {
120 repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
121
122 ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
123 repository.setAuthenticationManager(authManager);
124 } catch (SVNException e) {
125 throw new InvalidWorkspaceException(SvnRepositoryConnectorI18n.workspaceDoesNotExist.text(e.getMessage()));
126 }
127 return repository;
128 }
129
130
131
132
133
134
135
136 public static String getRepositoryWorspaceName( SVNRepository repository ) {
137 String[] segments = repository.getLocation().getPath().split("/");
138 return segments[segments.length - 1];
139 }
140
141 private SvnRepositoryUtil() {
142
143 }
144
145
146
147
148
149
150
151 public static boolean exist( SVNRepository repos ) {
152 try {
153 SVNNodeKind kind = repos.checkPath("", -1);
154 if (kind == SVNNodeKind.NONE) {
155 return false;
156 }
157 return true;
158
159 } catch (SVNException e) {
160 return false;
161 }
162 }
163
164
165
166
167
168
169
170
171 public static boolean isDirectory( SVNRepository repos,
172 String path ) {
173 try {
174 SVNNodeKind kind = repos.checkPath(path, -1);
175 if (kind == SVNNodeKind.DIR) {
176 return true;
177 }
178 } catch (SVNException e) {
179 return false;
180 }
181 return false;
182 }
183
184
185
186
187
188
189 @SuppressWarnings( "unchecked" )
190 public static Collection<SVNDirEntry> getDir( SVNRepository repos,
191 String path ) {
192 try {
193 return repos.getDir(path, -1, null, (Collection<SVNDirEntry>)null);
194 } catch (SVNException e) {
195 return Collections.emptyList();
196 }
197 }
198
199
200
201
202
203
204
205
206 public static boolean isFile( SVNRepository repos,
207 String path ) {
208 try {
209 SVNNodeKind kind = repos.checkPath(path, -1);
210 if (kind == SVNNodeKind.FILE) {
211 return true;
212 }
213 } catch (SVNException e) {
214 return false;
215 }
216 return false;
217 }
218
219 public static boolean exists( SVNRepository repository,
220 String path ) throws SVNException{
221 try {
222 if (repository.checkPath(path, -1) == SVNNodeKind.NONE) {
223 return false;
224 } else if (repository.checkPath(path, -1) == SVNNodeKind.UNKNOWN) {
225 return false;
226 }
227 } catch (SVNException e) {
228 SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.UNKNOWN,
229 "unknow error during delete action: {0)",
230 e.getMessage());
231 throw new SVNException(err);
232 }
233 return true;
234 }
235 }