001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.examples.repository;
013:
014: import java.util.Collection;
015: import java.util.Iterator;
016:
017: import org.tmatesoft.svn.core.SVNDirEntry;
018: import org.tmatesoft.svn.core.SVNException;
019: import org.tmatesoft.svn.core.SVNNodeKind;
020: import org.tmatesoft.svn.core.SVNURL;
021: import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
022: import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
023: import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
024: import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
025: import org.tmatesoft.svn.core.io.SVNRepository;
026: import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
027: import org.tmatesoft.svn.core.wc.SVNWCUtil;
028:
029: /*
030: * This example shows how to get the repository tree at the latest (HEAD)
031: * revision starting with the directory that is the path/to/repository part of
032: * the repository location URL. The main point is SVNRepository.getDir() method
033: * that is called recursively for each directory (till the end of the tree).
034: * getDir collects all entries located inside a directory and returns them as a
035: * java.util.Collection. As an example here's one of the program layouts (for
036: * the default url used in the program ):
037: *
038: * Repository Root: http://svn.svnkit.com/repos/svnkit
039: * Repository UUID: 0a862816-5deb-0310-9199-c792c6ae6c6e
040: *
041: * /examples (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006)
042: * /examples/svnkit-examples.iml (author: 'alex'; revision: 2775; date: Fri Nov 10 02:08:45 NOVT 2006)
043: * /examples/src (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006)
044: * /examples/src/org (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006)
045: * /examples/src/org/tmatesoft (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006)
046: * /examples/src/org/tmatesoft/svn (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006)
047: * /examples/src/org/tmatesoft/svn/examples (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006)
048: * /examples/src/org/tmatesoft/svn/examples/wc (author: 'alex'; revision: 2776; date: Fri Nov 10 02:25:08 NOVT 2006)
049: * ......................................................
050: * ---------------------------------------------
051: * Repository latest revision: 2802
052: */
053: public class DisplayRepositoryTree {
054: /*
055: * args parameter is used to obtain a repository location URL, user's
056: * account name & password to authenticate him to the server.
057: */
058: public static void main(String[] args) {
059: /*
060: * default values:
061: */
062: String url = "http://svn.svnkit.com/repos/svnkit/trunk/doc";
063: String name = "anonymous";
064: String password = "anonymous";
065:
066: /*
067: * initializes the library (it must be done before ever using the
068: * library itself)
069: */
070: setupLibrary();
071: if (args != null) {
072: /*
073: * obtains a repository location URL
074: */
075: url = (args.length >= 1) ? args[0] : url;
076: /*
077: * obtains an account name (will be used to authenticate the user to
078: * the server)
079: */
080: name = (args.length >= 2) ? args[1] : name;
081: /*
082: * obtains a password
083: */
084: password = (args.length >= 3) ? args[2] : password;
085: }
086: SVNRepository repository = null;
087: try {
088: /*
089: * Creates an instance of SVNRepository to work with the repository.
090: * All user's requests to the repository are relative to the
091: * repository location used to create this SVNRepository.
092: * SVNURL is a wrapper for URL strings that refer to repository locations.
093: */
094: repository = SVNRepositoryFactory.create(SVNURL
095: .parseURIEncoded(url));
096: } catch (SVNException svne) {
097: /*
098: * Perhaps a malformed URL is the cause of this exception
099: */
100: System.err
101: .println("error while creating an SVNRepository for location '"
102: + url + "': " + svne.getMessage());
103: System.exit(1);
104: }
105:
106: /*
107: * User's authentication information (name/password) is provided via an
108: * ISVNAuthenticationManager instance. SVNWCUtil creates a default
109: * authentication manager given user's name and password.
110: *
111: * Default authentication manager first attempts to use provided user name
112: * and password and then falls back to the credentials stored in the
113: * default Subversion credentials storage that is located in Subversion
114: * configuration area. If you'd like to use provided user name and password
115: * only you may use BasicAuthenticationManager class instead of default
116: * authentication manager:
117: *
118: * authManager = new BasicAuthenticationsManager(userName, userPassword);
119: *
120: * You may also skip this point - anonymous access will be used.
121: */
122: ISVNAuthenticationManager authManager = SVNWCUtil
123: .createDefaultAuthenticationManager(name, password);
124: repository.setAuthenticationManager(authManager);
125:
126: try {
127: /*
128: * Checks up if the specified path/to/repository part of the URL
129: * really corresponds to a directory. If doesn't the program exits.
130: * SVNNodeKind is that one who says what is located at a path in a
131: * revision. -1 means the latest revision.
132: */
133: SVNNodeKind nodeKind = repository.checkPath("", -1);
134: if (nodeKind == SVNNodeKind.NONE) {
135: System.err.println("There is no entry at '" + url
136: + "'.");
137: System.exit(1);
138: } else if (nodeKind == SVNNodeKind.FILE) {
139: System.err
140: .println("The entry at '"
141: + url
142: + "' is a file while a directory was expected.");
143: System.exit(1);
144: }
145: /*
146: * getRepositoryRoot() returns the actual root directory where the
147: * repository was created. 'true' forces to connect to the repository
148: * if the root url is not cached yet.
149: */
150: System.out.println("Repository Root: "
151: + repository.getRepositoryRoot(true));
152: /*
153: * getRepositoryUUID() returns Universal Unique IDentifier (UUID) of the
154: * repository. 'true' forces to connect to the repository
155: * if the UUID is not cached yet.
156: */
157: System.out.println("Repository UUID: "
158: + repository.getRepositoryUUID(true));
159: System.out.println("");
160:
161: /*
162: * Displays the repository tree at the current path - "" (what means
163: * the path/to/repository directory)
164: */
165: listEntries(repository, "");
166: } catch (SVNException svne) {
167: System.err.println("error while listing entries: "
168: + svne.getMessage());
169: System.exit(1);
170: }
171: /*
172: * Gets the latest revision number of the repository
173: */
174: long latestRevision = -1;
175: try {
176: latestRevision = repository.getLatestRevision();
177: } catch (SVNException svne) {
178: System.err
179: .println("error while fetching the latest repository revision: "
180: + svne.getMessage());
181: System.exit(1);
182: }
183: System.out.println("");
184: System.out
185: .println("---------------------------------------------");
186: System.out.println("Repository latest revision: "
187: + latestRevision);
188: System.exit(0);
189: }
190:
191: /*
192: * Initializes the library to work with a repository via
193: * different protocols.
194: */
195: private static void setupLibrary() {
196: /*
197: * For using over http:// and https://
198: */
199: DAVRepositoryFactory.setup();
200: /*
201: * For using over svn:// and svn+xxx://
202: */
203: SVNRepositoryFactoryImpl.setup();
204:
205: /*
206: * For using over file:///
207: */
208: FSRepositoryFactory.setup();
209: }
210:
211: /*
212: * Called recursively to obtain all entries that make up the repository tree
213: * repository - an SVNRepository which interface is used to carry out the
214: * request, in this case it's a request to get all entries in the directory
215: * located at the path parameter;
216: *
217: * path is a directory path relative to the repository location path (that
218: * is a part of the URL used to create an SVNRepository instance);
219: *
220: */
221: public static void listEntries(SVNRepository repository, String path)
222: throws SVNException {
223: /*
224: * Gets the contents of the directory specified by path at the latest
225: * revision (for this purpose -1 is used here as the revision number to
226: * mean HEAD-revision) getDir returns a Collection of SVNDirEntry
227: * elements. SVNDirEntry represents information about the directory
228: * entry. Here this information is used to get the entry name, the name
229: * of the person who last changed this entry, the number of the revision
230: * when it was last changed and the entry type to determine whether it's
231: * a directory or a file. If it's a directory listEntries steps into a
232: * next recursion to display the contents of this directory. The third
233: * parameter of getDir is null and means that a user is not interested
234: * in directory properties. The fourth one is null, too - the user
235: * doesn't provide its own Collection instance and uses the one returned
236: * by getDir.
237: */
238: Collection entries = repository.getDir(path, -1, null,
239: (Collection) null);
240: Iterator iterator = entries.iterator();
241: while (iterator.hasNext()) {
242: SVNDirEntry entry = (SVNDirEntry) iterator.next();
243: System.out.println("/"
244: + (path.equals("") ? "" : path + "/")
245: + entry.getName() + " (author: '"
246: + entry.getAuthor() + "'; revision: "
247: + entry.getRevision() + "; date: "
248: + entry.getDate() + ")");
249: /*
250: * Checking up if the entry is a directory.
251: */
252: if (entry.getKind() == SVNNodeKind.DIR) {
253: listEntries(repository, (path.equals("")) ? entry
254: .getName() : path + "/" + entry.getName());
255: }
256: }
257: }
258: }
|