读书人

java rmi兑现远程控制cvs客户端实例

发布时间: 2012-08-22 09:50:35 作者: rapoo

java rmi实现远程控制cvs客户端实例

自己总结的用java rmi实现远程控制cvs 客户端 控制的例子:

?

第一步:写CVS客户端,代码如下

cvsclient.executeCommand(command, globalOptions);
}

/**
* <p>
* Called when need add files
* </p>
*
* @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 < files.length; i++) {
command.setFiles(new File[] { new File(files[i]) });
}
command.setRevision1(revision1);
command.setRevision2(revision2);
return command;
}

/**
* Show difference between of the file that with different date
*
* @param files
* an array of files path
* @param date1
* one date
* @param date2
* another date
* @return command command of show difference between files
*/
public Command diffbydate(String[] files, String date1, String date2) {
// Init command
DiffCommand command = new DiffCommand();
// Set parameters
for (int i = 0; i < files.length; i++) {
command.setFiles(new File[] { new File(files[i]) });
}
// Format is yyyymmdd e.g 20070205
command.setBeforeDate1(date1);
command.setBeforeDate2(date2);
return command;
}

/**
* Check out the module
*
* @param modulename
* name of the module that to be checked out
* @return command command of check out the module
*/
public Command checkout(String modulename) {
// Init new command
CheckoutCommand command = new CheckoutCommand();
// Set paramaters
command.setModule(modulename);
command.setRecursive(true);
return command;
}

/**
* Check out the module
*
* @param modulename
* name of the module that to be checked out
* @return command command of check out the module
*/
public Command checkouttoOutput(String modulename) {
// Init new command
CheckoutCommand command = new CheckoutCommand();
// Set paramaters
command.setModule(modulename);
command.setPipeToOutput(true);
command.setRecursive(true);
return command;
}
}

?

?

?

第二步:写一个继承java.rmi.Remote的接口TestInterfaceRemote,代码如下:

接口TestInterfaceRemoteimport java.io.IOException;
import java.rmi.Remote;

import org.netbeans.lib.cvsclient.command.CommandAbortedException;
import org.netbeans.lib.cvsclient.command.CommandException;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;


public interface TestInterfaceRemote extends Remote{
void chechout(String cvsAdress, String filename, String checkoutDir) throws CommandAbortedException, AuthenticationException, IOException, CommandException;
}

?

?

第三步:写一个继承java.rmi.server.UnicastRemoteObject并实现TestInterfaceRemote的类TestInterfaceRemoteImpl,代码如下:

接口实现类TestInterfaceRemoteImplimport java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import org.netbeans.lib.cvsclient.command.CommandException;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;


public class TestInterfaceRemoteImpl extends UnicastRemoteObject implements
TestInterfaceRemote {

/**
*
*/
private static final long serialVersionUID = 1L;

protected TestInterfaceRemoteImpl() throws RemoteException {
super();
}

public void chechout(String cvsAdress, String filename, String checkoutDir) throws AuthenticationException, IOException, CommandException {
// CVSClient cvsclient = new CVSClient(
// ":pserver:user1:111@132.35.76.152:2401/opt/cvs/cvsroot");
CVSClient cvsclient = new CVSClient(cvsAdress);
cvsclient.openConnection();
cvsclient.setLocalPath(checkoutDir);
cvsclient.excute(cvsclient.checkout(filename));
cvsclient.closeConnection();
}

}

?

?

第四步:写rmi服务端,代码如下:

服务器端 import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;


public class ServerCheckout {

public ServerCheckout() {
TestInterfaceRemote testInterfaceRemote;
try {
testInterfaceRemote = new TestInterfaceRemoteImpl();
LocateRegistry.createRegistry(9988);
Naming.rebind("rmi://192.168.4.123:9988/TestInterfaceRemote", testInterfaceRemote);
System.out.println("服务器启动成功!");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static void main(String[] args) {
new ServerCheckout();
}

}

?

第五步:写rmi客户端,代码如下:

客户端 import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import org.netbeans.lib.cvsclient.command.CommandAbortedException;
import org.netbeans.lib.cvsclient.command.CommandException;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;


public class ClientCheckout {

/**
* @param args
* @throws RemoteException
* @throws NotBoundException
* @throws MalformedURLException
*/
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
TestInterfaceRemote testInterfaceRemote = (TestInterfaceRemote) Naming.lookup("rmi://192.168.4.123:9988/TestInterfaceRemote");
try {
testInterfaceRemote.chechout(":pserver:user1:111@132.35.76.152:2401/opt/cvs/cvsroot", "cvsext", "d:/leking/client/");
System.out.println("执行完毕!");
} catch (CommandAbortedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CommandException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

?

?

读书人网 >VSTS

热点推荐