2018-06-09

public class CVSClient { /** Cvs clinet instance used to communicate with cvs server */ private Client cvsclient; /** Cvs connect string */ private CVSRoot cvsroot; /** Connection instance to keep connect with cvs server */ private Connection connection; /** Global options to store the requied parameter for cvs server */ private GlobalOptions globalOptions; /** The local path on ur local machine */ private String localPath; /** * Inner class extend CVSAdapter

*

used to get and send message of the CVS server

* */ class BasicListener extends CVSAdapter {? /**? * Stores a tagged line? */? private final StringBuffer taggedLine = new StringBuffer();? /**? * Called when the server wants to send a message to be displayed to the? * user. The message is only for information purposes and clients can? * choose to ignore these messages if they wish.? *? ? * @param e? *? ? ? ? ? ? the event? */? public void messageSent(MessageEvent e) {? String line = e.getMessage();? PrintStream stream = e.isError() ? System.err : System.out;? if (e.isTagged()) {? ? String message = MessageEvent.parseTaggedMessage(taggedLine,? ? ? "Daniel Six");? ? // if we get back a non-null line, we have something? ? // to output. Otherwise, there is more to come and we? ? // should do nothing yet.? ? if (message != null) {? ? stream.println(message);? ? }? } else {? ? stream.println(line);? }? } }? ? /**? ? * Default constructor allows to construct CVSRoot from Properties object.? ? * The names are exactly the same as the attribute names in this class.? ? */ public CVSClient() { }? ? /**? ? * Breaks the string representation of CVSClient into it's components:? ? *? ? * The valid format (from the cederqvist) is:? ? *? ? * :method:[[user][:password]@]hostname[:[port]]/path/to/repository? ? *? ? * e.g. :pserver;username=anonymous;hostname=localhost:/path/to/repository? ? */? public CVSClient(String connectionString) {? cvsroot = CVSRoot.parse(connectionString); } /**? * Get the localPath? * @return localPath the local path to get project from the CVS server? * */ public String getLocalPath() {? return localPath; } /**? * Set the localPath? *? * */ public void setLocalPath(String localPath) {? this.localPath = localPath; }? /**? * Parse the CVSROOT string into CVSRoot object.? *? ? ? * The valid format (from the cederqvist) is:? ? *? ? * :method:[[user][:password]@]hostname[:[port]]/path/to/repository? ? *? ? * e.g. :pserver;username=anonymous;hostname=localhost:/path/to/repository? */ public void createConnection(String connectionString) {? cvsroot = CVSRoot.parse(connectionString); } /**? * Open connection to the cvs server

*? * @return connection to cvs server? * @throws AuthenticationException? * @throws CommandAbortedException? */ public Connection openConnection() throws AuthenticationException,? CommandAbortedException {? connection = ConnectionFactory.getConnection(cvsroot);? connection.open();? return connection; }? /**? * Close connection to the cvs server

* */ public void closeConnection() throws IOException{? connection.close(); }? /**? *

Excute cvs command

*? * @param command to be excute by the cliet? * @throws AuthenticationException? * @throws CommandAbortedException? * @throws IOException? * @throws CommandException? */ public void excute(Command command) throws AuthenticationException,? CommandAbortedException, IOException, CommandException {? cvsclient = new Client(connection, new StandardAdminHandler());? cvsclient.setLocalPath(localPath);? globalOptions = new GlobalOptions();? globalOptions.setCVSRoot("d:/client/java");? cvsclient.getEventManager().addCVSListener(new BasicListener());? //put the command to the console? System.out.println("***Command***"+command.getCVSCommand());? cvsclient.executeCommand(command, globalOptions); } /**? *

Called when need add files

* @param files that indicate to be added? * @return command of add files? */ public Command add(String[] files) {? AddCommand command = new AddCommand();? command.setBuilder(null);? for (int i = 0; i < files.length; i++) {? command.setFiles(new File[] { new File(files[i]) });? }? return command; } /**? * Called when need commit all files under the local path? * @return command command of commit files? */ public Command commit() {? CommitCommand command = new CommitCommand();? command.setBuilder(null);? command.setForceCommit(true);? command.setRecursive(true);? return command; } /**? * Called when need commit files? * @param files need to be commit? * @return command command of commit files? * */ public Command commit(String[] files) {? CommitCommand command = new CommitCommand();? for (int i = 0; i < files.length; i++) {? command.setFiles(new File[] { new File(files[i])});? }? command.setBuilder(null);? command.setForceCommit(true);? command.setRecursive(true);? return command; } /**? * Called when need update the certain files? * @param files need to be update? * @return command command of update files and directoris? * */ public Command update(String[] files) {? UpdateCommand command = new UpdateCommand();? //fetch files from the array? for (int i = 0; i < files.length; i++) {? command.setFiles(new File[] { new File(files[i]) });? }? command.setBuilder(null);? command.setRecursive(true);? command.setBuildDirectories(true);? command.setPruneDirectories(true);? return command; } /**? * Called to show the history list since given date? * @param date Date of the history? *@return command command show history list? * */ public Command historysincedate(String date){? HistoryCommand command=new HistoryCommand();? //Format is yyyymmdd e.g 20070205? command.setSinceDate(date);? return command; }? /**? *Called to show the history list since given version? *@param reversion reversion of the history? *@return command command show history list? **/ public Command historysincerRevision(String reversion){? //Init command? HistoryCommand command=new HistoryCommand();? //set parameters? command.setSinceRevision(reversion);? return command; }? /**? * Called to show the different between two versions? * @param files the files to compare with? * @param revision1 one revision? * @param revision2 another revision? * @return? * */ public Command diffbyreveision(String[] files,String revision1,String revision2){? //Inite command? DiffCommand command=new DiffCommand();? //Set parameters? for(int i=0;i

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容