?? svnmanager.java
字號:
package svnexample;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNErrorCode;
import org.tmatesoft.svn.core.SVNErrorMessage;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
/**
* This class is our gateway into Subversion. It encapsulates all the calls into
* the JavaSVN.
*/
public class SVNManager {
//Since the repository does not handle multiple threads simultaneously
//there is no requirement to make the SVNManager thread safe.
//Currently all configurable items are hard coded
private SVNRepository theRepository;
public SVNManager() {
initRepository();
}
public SVNResult checkInObject(BaseTrackingObject obj){
SVNResult returnVal = new SVNResult();
try {
ISVNEditor editor = null;
//Check if the DomainObjects, Object directory are present
//and also the object file
boolean domainDirectoryPresent = false,
objectDirectoryPresent = false,
filePresent = false;
String path = "/DomainObjects/";
SVNNodeKind nodeKind = theRepository.checkPath(path, -1);
if (nodeKind != SVNNodeKind.NONE) {
domainDirectoryPresent = true;
}
path += obj.getClass() + "/";
nodeKind = theRepository.checkPath(path, -1);
if (nodeKind != SVNNodeKind.NONE) {
objectDirectoryPresent = true;
}
path += obj.getObjectId() ;
nodeKind = theRepository.checkPath(path, -1);
if (nodeKind != SVNNodeKind.NONE) {
filePresent = true;
}
//Obtain the editor handle from the repository
editor = theRepository.getCommitEditor(
obj.getModificationReason(), null);
//reset the path and start off
path = "/DomainObjects/";
editor.openRoot(-1);
//This two step process of check and then open/add is required
//as the SVNRepository locks itself once the editor is obtained
//until the time the editor has been closed.
//Check if the directories and files are present,
//if not, add them.
if(!domainDirectoryPresent) {
editor.addDir(path, null, -1);
}
else {
editor.openDir(path, -1);
}
path += obj.getClass() + "/";
if(!objectDirectoryPresent) {
editor.addDir(path, null, -1);
}
else {
editor.openDir(path, -1);
}
path += obj.getObjectId();
if(!filePresent) {
//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);
}
editor.applyTextDelta(path, null);
//Create a new instance of SVNDeltaGenerator to generate the difference
//windows and supply them to the editor
SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
String checksum = deltaGenerator.sendDelta(path,
new ByteArrayInputStream(obj.
getXmlRepresentation().getBytes()),
editor, true);
editor.closeFile(path, checksum);
//close the object directory
editor.closeDir();
//close the DomainObject directory
editor.closeDir();
//close the head
editor.closeDir();
returnVal.setSuccessful(true);
//closing the editor is equivalent of the commit
SVNCommitInfo svnMessage = editor.closeEdit();
returnVal.setSvnMessage(svnMessage.toString());
}
catch(Exception exp) {
//generic placeholder!
System.out.println(exp);
exp.printStackTrace();
returnVal.setSuccessful(false);
returnVal.setSvnMessage(exp.toString());
}
return returnVal;
}
public List getRevisionLog(BaseTrackingObject obj,
long startRevision, long endRevision) {
List returnVal = new ArrayList();
try {
String path = "/DomainObjects/"+ obj.getClass() + "/" +
obj.getObjectId();
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);
for (Iterator log = revisionLogs.iterator(); log.hasNext();) {
SVNLogEntry logEntry = (SVNLogEntry) log.next();
SVNRevisionInfo revisionInfo = new SVNRevisionInfo();
revisionInfo.setRevisionNumber(logEntry.getRevision());
revisionInfo.setModifiedUser(logEntry.getAuthor());
revisionInfo.setRevisionDate(logEntry.getDate());
revisionInfo.setModificationReason(logEntry.getMessage());
ByteArrayOutputStream xmlFileContentsStream =
new ByteArrayOutputStream();
//Obtain the contents of the object at the specified
//revision
theRepository.getFile(path, revisionInfo.getRevisionNumber(),
new Properties(),xmlFileContentsStream);
revisionInfo.setRevisionContents(
new String(xmlFileContentsStream.toByteArray()));
returnVal.add(revisionInfo);
}
}
catch(Exception exp) {
//generic placeholder!
System.out.println(exp);
exp.printStackTrace();
}
return returnVal;
}
public String showDifferences(BaseTrackingObject obj, long revision1,
long revision2) {
String returnVal = "";
try {
SVNURL svnUrl = theRepository.getLocation().
appendPath("/DomainObjects/"+ obj.getClass() + "/" +
obj.getObjectId(), false);
//Create an instance of SVNClientManager providing authentication
//information (name, password) and an options driver
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager ourClientManager =
SVNClientManager.newInstance(options, "name", "password");
//Obtain the handle to DiffClient from the client
//manager
SVNDiffClient theDiff = ourClientManager.getDiffClient();
ByteArrayOutputStream diffStream =
new ByteArrayOutputStream();
theDiff.doDiff(svnUrl, SVNRevision.create(revision1), svnUrl,
SVNRevision.create(revision2), false, false, diffStream);
returnVal = new String(diffStream.toByteArray());
}
catch(Exception e) {
e.printStackTrace();
}
return returnVal;
}
public void initRepository() {
//initialize the system to work over http
DAVRepositoryFactory.setup();
//credentials to authenticate into SVN
String name = "user";
String password = "pwd";
try {
//point to the root folder in the 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);
//pass on the credentials to the repository handle
theRepository.setAuthenticationManager(authManager);
//obtain the type of node pointed to by the location
SVNNodeKind nodeKind = theRepository.checkPath("", -1);
//ensure that we are at a top directory.
if (nodeKind == SVNNodeKind.NONE) {
SVNErrorMessage err =
SVNErrorMessage.create(SVNErrorCode.UNKNOWN,
"No entry at URL ''{0}''", svnUrl);
throw new SVNException(err);
} else if (nodeKind == SVNNodeKind.FILE) {
SVNErrorMessage err =
SVNErrorMessage.create(SVNErrorCode.UNKNOWN,
"Entry at URL ''{0}'' is a file while " +
"directory was expected", svnUrl);
throw new SVNException(err);
}
} catch (SVNException e) {
System.err.println("Error in initializing the system !");
System.exit(1);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -