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;
}
}