dataprovider.java

来自「web版的SVN客户端」· Java 代码 · 共 845 行 · 第 1/3 页

JAVA
845
字号
/*
 * Copyright (c) 2004, 2005 Polarion Software, All rights reserved.
 * Email: community@polarion.org
 *
 * This program and the accompanying materials are made available under the
 * terms of the Apache License, Version 2.0 (the "License"). You may not use
 * this file except in compliance with the License. Copy of the License is
 * located in the file LICENSE.txt in the project distribution. You may also
 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 *
 * POLARION SOFTWARE MAKES NO REPRESENTATIONS OR WARRANTIES
 * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. POLARION SOFTWARE
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
 * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package org.polarion.svnwebclient.data.javasvn;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.polarion.svnwebclient.authorization.UserCredentials;
import org.polarion.svnwebclient.configuration.ConfigurationProvider;
import org.polarion.svnwebclient.data.AuthenticationException;
import org.polarion.svnwebclient.data.DataProviderException;
import org.polarion.svnwebclient.data.IDataProvider;
import org.polarion.svncommons.commentscache.CommentsCache;
import org.polarion.svncommons.commentscache.CommentsCacheConfig;
import org.polarion.svncommons.commentscache.CommentsCacheException;
import org.polarion.svncommons.commentscache.SVNAuthenticationManager;
import org.polarion.svnwebclient.data.model.DataAnnotationElement;
import org.polarion.svnwebclient.data.model.DataChangedElement;
import org.polarion.svnwebclient.data.model.DataDirectoryCompareItem;
import org.polarion.svnwebclient.data.model.DataDirectoryElement;
import org.polarion.svnwebclient.data.model.DataFile;
import org.polarion.svnwebclient.data.model.DataFileElement;
import org.polarion.svnwebclient.data.model.DataRepositoryElement;
import org.polarion.svnwebclient.data.model.DataRevision;
import org.polarion.svnwebclient.util.FileUtil;
import org.polarion.svnwebclient.util.UrlUtil;
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNAnnotationGenerator;
import org.tmatesoft.svn.core.SVNAuthenticationException;
import org.tmatesoft.svn.core.SVNCancelException;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNErrorMessage;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNProperty;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.internal.wc.SVNFileUtil;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNLocationEntry;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.DefaultSVNDiffGenerator;
import org.tmatesoft.svn.core.wc.ISVNAnnotateHandler;
import org.tmatesoft.svn.core.wc.ISVNDiffGenerator;
import org.tmatesoft.svn.core.wc.ISVNEventHandler;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.wc.SVNEvent;
import org.w3c.util.UUID;

/**
 * Implements data provider interface using JavaSVN library
 * 
 * @author <A HREF="mailto:svnbrowser@polarion.org">Polarion Software </A>
 */
public class DataProvider implements IDataProvider {
    protected static final String MIME_TYPE_BINARY = "application/octet-stream";
    protected String id;
    protected SVNRepository repository;
    protected SVNURL repositoryRoot;
    protected UserCredentials userCredentials;
    protected String reposLocation;
    
    public static void startup(String userName, String password, String id, String url) throws DataProviderException {
        try {
            DAVRepositoryFactory.setup();
    
            CommentsCacheConfig cacheConfig = new CommentsCacheConfig(
                    url, userName, password,
                    ConfigurationProvider.getInstance().getCacheDirectory(),
                    ConfigurationProvider.getInstance().getCachePageSize()); 
            
            CommentsCache.init(cacheConfig, id, url); 
            
            org.polarion.svnwebclient.data.javasvn.SVNRepositoryPool.init(id, 
                    ConfigurationProvider.getInstance().getSvnConnectionsCount(), 
                    url, userName, password);            
            CommentsCache.getInstance(id, userName, password).prefetch(ConfigurationProvider.getInstance().getCachePrefetchMessagesCount());            
        } catch (Exception e) {
        	Logger.getLogger(DataProvider.class).error(e, e);
            throw new DataProviderException(e);
        }
    }
                
    public static String getID(String url, String name, String password) {
        DAVRepositoryFactory.setup();
        SVNRepository repository = null;
        String res = null;
        try {        
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
            ISVNAuthenticationManager authManager = new SVNAuthenticationManager(name, password);
            repository.setAuthenticationManager(authManager);                        
            SVNURL root = repository.getRepositoryRoot(true);
            res = root.toString();
            repository.closeSession();
        } catch (SVNException e) {
        	e.printStackTrace();    
         }
        return res;
    }
    
    public static void  verify(String url, String name, String password) throws SVNException {
        DAVRepositoryFactory.setup();
        SVNRepository repository = null;
        try {        
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
            ISVNAuthenticationManager authManager = new SVNAuthenticationManager(name, password);
            repository.setAuthenticationManager(authManager);                        
            repository.testConnection();                                      
        } finally {
        	try {
        		if (repository != null) {
        			repository.closeSession();
        		}        		
        	} catch (SVNException e) {}        	        	
        }       
    }
    
    public void connect(UserCredentials credentials, String id, String url) throws DataProviderException {                       
    	try {
	    	this.id = id;
	    	this.userCredentials = credentials;
	        if (id != null) {	        		        
	             this.repository = SVNRepositoryPool.getInstance(id).getRepository(credentials);	             
	        }
	        if (ConfigurationProvider.getInstance().isMultiRepositoryMode()) {
	        	SVNURL s = this.repository.getRepositoryRoot(true);	        		        	
	        	if (url.equals(s.toString())) {
	        		this.repository.setLocation(SVNURL.parseURIDecoded(url), false);
	        	} else if (url.indexOf(s.toString() + "/") != -1) { 	        	
	        		this.repository.setLocation(SVNURL.parseURIDecoded(url), false);
	        	} else {
	        		throw new DataProviderException("Incorrect location");
	        	}           		             	        	
	        }
	        
    	} catch (SVNException e) {	     
	    		 throw new DataProviderException(e);
	    }
    }

    
    public void close() throws DataProviderException {
    	if (this.repository != null) {
    		ISVNAuthenticationManager authManager = new SVNAuthenticationManager(null, null);
    		repository.setAuthenticationManager(authManager);
        }    	
    	org.polarion.svnwebclient.data.javasvn.SVNRepositoryPool.getInstance(this.id).releaseRepository(this.repository);
    }
    
    public long getHeadRevision() throws DataProviderException {
        try {
            return this.repository.getLatestRevision();
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);            
        } catch (SVNException e) {
            throw new DataProviderException(e);
        }
    }
    
    public DataDirectoryElement getDirectory(String url, long revision) throws DataProviderException {
        return this.getDirectory(url, revision, false);
    }
    
    public DataDirectoryElement getDirectory(String url, long revision, boolean recusive) throws DataProviderException {
        try {
            DataDirectoryElement directory = new DataDirectoryElement();
            
            List directoryEntries = new ArrayList();
            SVNDirEntry directoryEntry = this.repository.getDir(url, revision, false, directoryEntries);
    
            directory.setName(directoryEntry.getName());
            directory.setRevision(directoryEntry.getRevision());        
            directory.setAuthor(directoryEntry.getAuthor());
            directory.setDate(directoryEntry.getDate());
            directory.setComment(CommentsCache.getInstance(this.id, 
            		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(directoryEntry.getRevision()));
    
            List childElements = new ArrayList();
            for (Iterator i = directoryEntries.iterator(); i.hasNext();) {
                SVNDirEntry childEntry = (SVNDirEntry) i.next();
                DataRepositoryElement childElement;
                if (childEntry.getKind() == SVNNodeKind.DIR) {
                    childElement = new DataDirectoryElement();
                } else {
                    childElement = new DataFileElement();
                    childElement.setSize(childEntry.getSize());
                }
                
                childElement.setName(childEntry.getName());
                childElement.setRevision(childEntry.getRevision());   
                
                
                childElement.setAuthor(childEntry.getAuthor());
                childElement.setDate(childEntry.getDate());
                childElement.setComment(CommentsCache.getInstance(this.id,
                		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(childEntry.getRevision()));
                
                childElements.add(childElement);
                
                if (recusive) {
                    String childUrl = url + "/" + childElement.getName();
                    DataDirectoryElement child = this.getDirectory(childUrl, revision, true);
                    for (Iterator j = child.getChildElements().iterator(); j.hasNext(); ) {
                        DataRepositoryElement nested = (DataRepositoryElement) j.next();
                        nested.setName(childElement.getName() + "/" + nested.getName());
                        childElements.add(nested);
                    }
                }
            }
    
            directory.setChildElements(childElements);
            return directory;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e); 
        } catch (CommentsCacheException e) {
            throw new DataProviderException(e); 
        }                
    }
    
    public static void shutdown() throws DataProviderException {      
            org.polarion.svncommons.commentscache.CommentsCache.shutdown();
            org.polarion.svnwebclient.data.javasvn.SVNRepositoryPool.terminate();
            org.polarion.svncommons.commentscache.SVNRepositoryPool.terminate();
      
    }    
    
    public List getRevisions(String url, long fromRevision, long toRevision, long count) throws DataProviderException {
        try {
            List ret = new ArrayList();
            
            final List logEntries = new ArrayList();
            if (count > 0) {
                this.repository.log(new String[] {url}, fromRevision, toRevision, false, false, count, 
                        new ISVNLogEntryHandler() {
                            public void handleLogEntry(SVNLogEntry logEntry) {
                                logEntries.add(logEntry);
                            }
                        });
            } else {
                this.repository.log(new String[] {url}, fromRevision, toRevision, false, false, 
                        new ISVNLogEntryHandler() {
                            public void handleLogEntry(SVNLogEntry logEntry) {
                                logEntries.add(logEntry);
                            }
                        });
            }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?