View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape 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. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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       * @param url
50       * @param sourceName
51       * @return SVNURL
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              // protocol not supported by this connector
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       * @param repository
80       * @param path
81       * @param revisionNumber
82       * @param sourceName
83       * @return SVNNodeKind
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      * Create a {@link SVNRepository} from a http protocol.
101      * 
102      * @param url - the url of the repository.
103      * @param username - username credential.
104      * @param password - password credential
105      * @return {@link SVNRepository}.
106      */
107     public static SVNRepository createRepository( String url,
108                                                   String username,
109                                                   String password ) {
110         // for DAV (over http and https)
111         DAVRepositoryFactory.setup();
112         // For File
113         FSRepositoryFactory.setup();
114         // for SVN (over svn and svn+ssh)
115         SVNRepositoryFactoryImpl.setup();
116 
117         // The factory knows how to create a DAVRepository
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      * Util to get the last segment from a path.
132      * 
133      * @param repository
134      * @return last segment.
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         // prvent construction
143     }
144 
145     /**
146      * Check if the repository path exist.
147      * 
148      * @param repos
149      * @return true if repository exist and false otherwise.
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      * Check if repository path is a directory.
166      * 
167      * @param repos
168      * @param path
169      * @return true if repository path is a directory and false otherwise.
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      * @param repos
186      * @param path
187      * @return a collect of entry from directory path; never null
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      * Check if the path is a file.
201      * 
202      * @param repos
203      * @param path
204      * @return true if the path is a file and false otherwise.
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 }