001    package org.jboss.dna.connector.svn;
002    
003    import java.util.Collection;
004    import org.jboss.dna.graph.connector.RepositorySourceException;
005    import org.jboss.dna.graph.request.InvalidWorkspaceException;
006    import org.tmatesoft.svn.core.SVNDirEntry;
007    import org.tmatesoft.svn.core.SVNException;
008    import org.tmatesoft.svn.core.SVNNodeKind;
009    import org.tmatesoft.svn.core.SVNURL;
010    import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
011    import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
012    import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
013    import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
014    import org.tmatesoft.svn.core.io.SVNRepository;
015    import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
016    import org.tmatesoft.svn.core.wc.SVNWCUtil;
017    
018    /**
019     * @author Serge Pagop
020     */
021    public class SVNRepositoryUtil {
022    
023        /**
024         * @param url
025         * @param sourceName
026         * @return SVNURL
027         */
028        public static SVNURL createSVNURL( String url,
029                                           String sourceName ) {
030    
031            SVNURL theUrl;
032            try {
033                theUrl = SVNURL.parseURIDecoded(url);
034            } catch (SVNException e) {
035                // protocol not supported by this connector
036                throw new RepositorySourceException(sourceName,
037                                                    "Protocol is not supported by this connector or there is problem in the svn url");
038            }
039            return theUrl;
040        }
041    
042        public static void setNewSVNRepositoryLocation( SVNRepository oldRespository,
043                                                        String url,
044                                                        boolean forceReconnect,
045                                                        String sourceName ) {
046            try {
047                oldRespository.setLocation(createSVNURL(url, sourceName), forceReconnect);
048            } catch (SVNException e) {
049                throw new RepositorySourceException(sourceName, "the old url and a new one has got different protocols");
050            }
051        }
052    
053        /**
054         * @param repository
055         * @param path
056         * @param revisionNumber
057         * @param sourceName
058         * @return SVNNodeKind
059         */
060        public static SVNNodeKind checkThePath( SVNRepository repository,
061                                                String path,
062                                                long revisionNumber,
063                                                String sourceName ) {
064            SVNNodeKind kind;
065            try {
066                kind = repository.checkPath(path, revisionNumber);
067    
068            } catch (SVNException e) {
069                throw new RepositorySourceException(sourceName, e.getMessage());
070            }
071            return kind;
072        }
073    
074        /**
075         * Create a {@link SVNRepository} from a http protocol.
076         * 
077         * @param url - the url of the repository.
078         * @param username - username credential.
079         * @param password - password credential
080         * @return {@link SVNRepository}.
081         */
082        public static SVNRepository createRepository( String url,
083                                                      String username,
084                                                      String password ) {
085            // for DAV (over http and https)
086            DAVRepositoryFactory.setup();
087            // For File
088            FSRepositoryFactory.setup();
089            // for SVN (over svn and svn+ssh)
090            SVNRepositoryFactoryImpl.setup();
091    
092            // The factory knows how to create a DAVRepository
093            SVNRepository repository;
094            try {
095                repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
096    
097                ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
098                repository.setAuthenticationManager(authManager);
099            } catch (SVNException e) {
100                throw new InvalidWorkspaceException(SVNRepositoryConnectorI18n.workspaceDoesNotExist.text(e.getMessage()));
101            }
102            return repository;
103        }
104    
105        /**
106         * Util to get the last segment from a path.
107         * 
108         * @param repository
109         * @return last segment.
110         */
111        public static String getRepositoryWorspaceName( SVNRepository repository ) {
112            String[] segments = repository.getLocation().getPath().split("/");
113            return segments[segments.length - 1];
114        }
115    
116        private SVNRepositoryUtil() {
117            // prvent construction
118        }
119    
120        /**
121         * Check if the repository path exist.
122         * 
123         * @param repos
124         * @return true if repository exist and false otherwise.
125         */
126        public static boolean exist( SVNRepository repos ) {
127            try {
128                SVNNodeKind kind = repos.checkPath("", -1);
129                if (kind == SVNNodeKind.NONE) {
130                    return false;
131                }
132                return true;
133    
134            } catch (SVNException e) {
135                return false;
136            }
137        }
138    
139        /**
140         * Check if repository path is a directory.
141         * 
142         * @param repos
143         * @param path
144         * @return true if repository path is a directory and false otherwise.
145         */
146        public static boolean isDirectory( SVNRepository repos,
147                                           String path ) {
148            try {
149                SVNNodeKind kind = repos.checkPath(path, -1);
150                if (kind == SVNNodeKind.DIR) {
151                    return true;
152                }
153            } catch (SVNException e) {
154                return false;
155            }
156            return false;
157        }
158    
159        /**
160         * @param repos
161         * @param path
162         * @return a collect of entry from directory path.
163         */
164        @SuppressWarnings( "unchecked" )
165        public static Collection<SVNDirEntry> getDir( SVNRepository repos,
166                                                      String path ) {
167            Collection<SVNDirEntry> entries = null;
168            try {
169                return repos.getDir(path, -1, null, (Collection<SVNDirEntry>)null);
170            } catch (SVNException e) {
171                return entries;
172            }
173        }
174    
175        /**
176         * Check if the path is a file.
177         * 
178         * @param repos
179         * @param path
180         * @return true if the path is a file and false otherwise.
181         */
182        public static boolean isFile( SVNRepository repos,
183                                      String path ) {
184            try {
185                SVNNodeKind kind = repos.checkPath(path, -1);
186                if (kind == SVNNodeKind.FILE) {
187                    return true;
188                }
189            } catch (SVNException e) {
190                return false;
191            }
192            return false;
193        }
194    
195        public static boolean exists( SVNRepository repository,
196                                      String path ) {
197            try {
198                if (repository.checkPath(path, -1) == SVNNodeKind.NONE) {
199                    return false;
200                } else if (repository.checkPath(path, -1) == SVNNodeKind.UNKNOWN) {
201                    return false;
202                }
203            } catch (SVNException e) {
204                return false;
205            }
206            return true;
207        }
208    }