svnkit获取svn相关信息

本文介绍了一个具体的SVN操作服务类实现,包括初始化SVN操作服务、登录验证、获取文件信息、日志记录等功能。该服务能够进行版本库连接测试、获取最新版本号及根路径等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class SVNOperationServiceBean implements SVNOperationService
{
    
    private final DataVoiceLogger logger = DataVoiceLogFactory.getLogger(getClass());
    
    private SVNRepository repository = null;
    
    @PostConstruct
    public void initSVNOperationService()
    {
        
        DAVRepositoryFactory.setup();
        
        SVNRepositoryFactoryImpl.setup();
        
        FSRepositoryFactory.setup();
        
    }
    
    public void login(String username, String password, String url)
            throws Exception
    {
        password = new String(Base64.decode(password.getBytes()));
        this.repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
        
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username,
                password);
        
        this.repository.setAuthenticationManager(authManager);
        
        testConnection();
    }
    
    private void testConnection() throws SVNException
    {
        this.repository.testConnection();
    }
    
    @Override
    public boolean testRepositoryConnected(SvnConfigInfo conf)
    {
        boolean isSucceed = false;
        try
        {
            login(conf.getUserAccount(), conf.getUserPassword(), conf.getConfigCodePath());
            String[] codepath = getCodePaths(conf.getConfigCodePath());
            SVNDirEntry dir = getTargetInfo(codepath[0], -1);
            if (dir != null)
            {
                isSucceed = true;
                logger.info(conf.getConfigCodePath() + "  testlogin success ");
            }
            
        }
        catch (SVNAuthenticationException e)
        {
            
            logger.error("login error" + e.getMessage());
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
        }
        return isSucceed;
    }
    
    public SVNDirEntry getTargetInfo(String path, long revision)
    {
        SVNDirEntry collect = null;
        try
        {
            collect = this.repository.info(path, revision);
        }
        catch (SVNException e)
        {
            collect = null;
            logger.error(e.getMessage());
        }
        return collect;
    }
    
    public List<SVNLogEntry> getSVNLog(long startRevision, long endRevision,
            String[] codePaths)
    {
        List<SVNLogEntry> entries = new ArrayList<SVNLogEntry>();
        try
        {
            this.repository.log(codePaths,
                    entries,
                    startRevision,
                    endRevision,
                    true,
                    true);
        }
        catch (SVNException e)
        {
            logger.error(e.getMessage());
        }
        return entries;
    }
    
    public SvnLogFileInfo getLastFileRevision(SvnLogFileInfo fInfo)
    {
    	SvnLogFileInfo fileInfo = null;
    	List<SVNFileRevision> s = new ArrayList<SVNFileRevision>();
        int index = -2;
        try
        {
            this.repository.getFileRevisions(fInfo.getFilename(), s, 0, fInfo.getRevision());
        }
        catch (SVNException e)
        {
            logger.error(e.getMessage());
        }
        
        index = s.size() + index;
        
        if (index >= 0)
        {
            SVNFileRevision revision = s.get(index);
            fileInfo = new SvnLogFileInfo();
            fileInfo.setFilename(revision.getPath(),this.getRepositoryType());
            fileInfo.setRevision(revision.getRevision());
        }
        
        return fileInfo;
    }
    
    public boolean copyFile(String workspace, String type, SvnLogFileInfo fileInfo)
    {
        
        FileOutputStream fo = null;
        boolean isSuccess = false;
        try
        {
            SVNProperties pro = new SVNProperties();
            File file = new File(workspace + "\\" + type + "\\" + fileInfo.getShortFileName());
            fo = new FileOutputStream(file);
            try
            {
                this.repository.getFile(fileInfo.getFilename(), fileInfo.getRevision(), pro, fo);
            }
            catch (SVNException e)
            {
            	logger.error("query the file "+fileInfo.getShortFileName()+"'info from repository, but file copy failed!");
            }
            fo.close();
            isSuccess = true;
        }
        catch (Exception e)
        {
        	logger.error(e.getMessage());
        }
        finally
        {
            try
            {
                if (fo != null)
                {
                    fo.close();
                }
            }
            catch (IOException e)
            {
            	logger.error(e.getMessage());
            }
        }
        return isSuccess;
    }
    
    public long getLatestRevision()
    {
        long lastRevision = 0;
        try
        {
            lastRevision = this.repository.getLatestRevision();
        }
        catch (SVNException e)
        {
            logger.error("Lastest revision of this repository is not exists!");
        }
        return lastRevision;
    }
    
    public String getSvnRoot()
    {
        SVNURL url = null;
        try
        {
            url = this.repository.getRepositoryRoot(false);
            return url.getPath();
        }
        catch (SVNException e)
        {
            e.printStackTrace();
            logger.error("You have no right get this repository root url !!!");
        }
        return null;
    }
    
    public String[] getCodePaths(String url)
    {
        String codepath = getSvnRoot();
        int homeIndex = url.indexOf(codepath);
        return new String[] { url.substring(homeIndex + codepath.length()) };
    }

	@Override
	public String getRepositoryType() {
		
		return RepositoryOperation.IREP_TYPE_SVN;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值