初始化JavaSVN
SVNManager类是通向Subversion的路由,用于在不使用工作拷贝的情况下,通过底层JavaSVN接口直接访问Subversion仓库,通过初始化JavaSVN类库来可以使用HTTP(S)或SVN(S)与Subversion仓库进行交互。在这里,我们选择使用HTTP (WebDAV),因为可以减少在处理防火墙方面的工作。
库的初始化工作要首先调用的是方法DAVRepositoryFactory.setup()。SVNRepository类包含了所有直接访问Subversion仓库的方法,将Subversion仓库树状结构的根路径提供给SVNRepositoryFactory类后,就完成了这个类的初始化,而ISVNAuthenticationManager类的作用是向SVNRepository提供访问Subversion仓库的授权信息。
public void initRepository() {
//initialize the system to work over http
DAVRepositoryFactory.setup();
............
//point to the root folder of repository
SVNURL svnUrl = SVNURL.parseURIEncoded
("http://localhost/repos/");
//initialize the SVNRepository
theRepository = SVNRepositoryFactory.
create(svnUrl);
//Creates the Auth manager with our user
//and password credentials
ISVNAuthenticationManager authManager =
new BasicAuthenticationManager
(name, password);
//provides the credentials to the
//SVNRepository
theManager.setAuthenticationManager
(authManager);
........
}
在Subversion中存储数据
Subversion需要使用层次结构存储数据,这样我们先要设定一下领域实体的层次结构,这里使用一个命名为“DomainObjects”的文件夹来存放领域数据。领域对象类将会检测这个目录下存放领域对象的所有子目录,而每个独立的域对象被以XML格式进行存储,并以其主键进行命名。
为存储LoanData域对象,我们先要执行SVNManager对象的checkInObject方法,通过SVNRepository 执行的ISVNEditor对象来在Subversion仓库中的建立和更新域对象的版本,但只有在closeEdit被调用后,才会提交所有的操作。SVNDeltaGenerator类用于获取当前版本与被更新版本之间的差异,Subversion通过存储版本间差异部分的形式存放新的版本,这样会使提高网络效率。
public SVNResult checkInObject(
BaseTrackingObject obj){
.....
//Obtain the editor handle from the
//repository
ISVNEditor editor = theRepository.
getCommitEditor(obj.
getModificationReason(), null);
....
//create the file at the specified path
editor.addFile(path, null, -1);
}
else {
//file is already present, so open
//the file in the repository
editor.openFile(path, -1);
}
....
String checksum = deltaGenerator.
sendDelta(path,
new ByteArrayInputStream(
obj.getXmlRepresentation().
getBytes()),
editor, true);
.....
editor.closeEdit();
...
}
检索变化历史
为检索指定领域对象的历史版本,需要调用SVNManager类的getRevisionLog方法; SVNRepository类的getLatestRevision方法可以得到当前版本号;SVNManager.log方法可以检索每个版本的日志,日志可以包含版本修订的日期、修改人、修改的内容等信息;SVNManager.getFile方法可以从Subversion仓库中取得领域对象指定版本的所有内容。
public List getRevisionLog(BaseTrackingObject
obj, long startRevision,
long endRevision) {
.....
if(endRevision==-1) {
//obtain the latest revision from
//the repository
endRevision =
theRepository.getLatestRevision();
}
//retrieve the various log entries for
//the particular object path
Collection revisionLogs = theRepository.
log(new String[] {path}, null,
startRevision, endRevision,
false, true);
....
//Obtain the contents of the object at
//the specified revision
theRepository.getFile(path, revisionInfo.
getRevisionNumber(),
new Properties(),
xmlFileContentsStream);
....
}