- 浏览: 2170 次
- 性别:
- 来自: 北京
最新评论
自己总结的用java rmi实现远程控制cvs 客户端 控制的例子:
第一步:写CVS客户端,代码如下
类CVSClient
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import org.netbeans.lib.cvsclient.CVSRoot;
import org.netbeans.lib.cvsclient.Client;
import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
import org.netbeans.lib.cvsclient.command.Command;
import org.netbeans.lib.cvsclient.command.CommandAbortedException;
import org.netbeans.lib.cvsclient.command.CommandException;
import org.netbeans.lib.cvsclient.command.GlobalOptions;
import org.netbeans.lib.cvsclient.command.add.AddCommand;
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
import org.netbeans.lib.cvsclient.command.commit.CommitCommand;
import org.netbeans.lib.cvsclient.command.diff.DiffCommand;
import org.netbeans.lib.cvsclient.command.history.HistoryCommand;
import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;
import org.netbeans.lib.cvsclient.connection.Connection;
import org.netbeans.lib.cvsclient.connection.ConnectionFactory;
import org.netbeans.lib.cvsclient.event.CVSAdapter;
import org.netbeans.lib.cvsclient.event.MessageEvent;
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 <br>
* <p>
* used to get and send message of the CVS server
* </p>
*/
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 <br>
*
* @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 <br>
*/
public void closeConnection() throws IOException {
connection.close();
}
/**
* <p>
* Excute cvs command
* </p>
*
* @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);
}
/**
* <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,代码如下:
接口TestInterfaceRemote
import 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;
}
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,代码如下:
接口实现类TestInterfaceRemoteImpl
import 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();
}
}
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();
}
}
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();
}
}
}
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();
}
}
}
相关推荐
- **安全性**:RMI支持Java安全模型,可以控制客户端对远程对象的访问权限。 - **性能优化**:RMI提供了优化选项,如缓存远程对象引用,减少网络通信次数,以及调整传输协议参数来提升性能。 - **故障恢复**:RMI...
这个简单的例子将引导我们了解如何利用Java RMI实现远程调用。 首先,我们要创建远程接口。在Java RMI中,远程接口是一个实现了`java.rmi.Remote`接口的Java接口。接口中声明的方法将在客户端调用,实际上会在...
使用Java的RMI来实现远程控制的小程序,类似qq的远程协助。 本程序是根据网上看到的源代码改写而成,主要在用户界面和服务端与客户端的交互上增加了许多功能,并对代码结构进行了重新组织。里面附有原作者的链接,...
通过这个“java RMI实现代码”项目,你可以深入理解RMI的使用方式,包括如何创建远程接口、实现远程对象,以及客户端和服务器端的交互过程。实践中,结合代码注释逐步调试,将有助于更好地掌握这一核心技术。
RMI是Java提供的核心API,它允许对象在不同的Java虚拟机(JVM)之间进行通信,从而实现远程控制。RMI包含两个主要部分:远程接口(Remote Interface)和远程对象(Remote Object)。远程接口定义了可以在远程服务器...
3. **聊天室服务器**:`chatserver`可能是这个项目的服务器端程序,它包含RMI服务器的实现,包括创建远程对象,注册到RMI注册表,并处理来自客户端的请求,如接收消息、广播消息等。 4. **聊天室客户端**:`...
标题中的“rmi 远程调用 实现客户端之间会话”指的是利用RMI技术让多个客户端与同一服务端建立会话,进行交互。这通常涉及到以下几个核心概念: 1. **远程接口**:这是定义了可以远程调用的方法的接口。远程接口...
在你提供的实例中,“RMI实现远程文件传输”是利用RMI来传输“KEY文件”的具体应用。 首先,理解RMI的基本概念: 1. 远程对象:这是在远程服务器上运行并可以通过网络访问的对象。它们需要实现特定的接口,这个接口...
在这个"JAVA RMI实现程序实例"中,我们将会探讨如何使用RMI实现一个简单的“Hello, World”示例,并已经在两台机器上进行了成功的测试。 首先,我们需要了解RMI的基本概念。RMI的核心思想是将对象的方法调用转化为...
【基于RMI的远程控制器(JAVA版)】是一款利用Java的Remote Method Invocation(RMI)技术实现的类似于QQ远程控制的程序。RMI是Java提供的一种分布式计算模型,它允许在不同的Java虚拟机之间调用对象的方法,实现了...
Java RMI(Remote Method Invocation,远程方法调用)是一种Java技术,它允许对象在不同的Java虚拟机(JVMs)之间进行通信,从而实现分布式计算。这种技术在开发跨网络的分布式应用程序时非常有用,因为它使得对象...
Java RMI(Remote Method Invocation)是Java平台提供的一种用于分布式计算的技术,它允许在不同的Java虚拟机之间调用方法,从而实现远程对象的交互。在本场景中,我们利用Java RMI来实现实时的远程文件下载功能。...
- **客户端(Client)**: 客户端通过RMI连接到服务器,获取远程对象实例,并调用其方法来发送和接收消息。客户端可能包含用户界面,如GUI,用于输入和显示聊天内容。 ### 3. 图形界面 这个聊天室项目还包含了图形...
Java远程控制技术是一种在计算机网络中,通过编程实现一台设备(客户端)对另一台设备(服务器端)进行操作的方法。这种技术广泛应用于系统管理、自动化运维、分布式系统监控等领域。在这个"java版远程控制的实例...
Java RMI,全称为Remote Method Invocation,是Java平台上的一个核心特性,用于实现分布式计算。它允许Java对象在不同的JVM之间进行通信,仿佛这些对象都在同一台机器上一样。这个技术尤其适用于构建大规模的、分散...
服务器端会实现这个接口并提供相应的服务,而客户端则可以通过RMI调用这些方法来远程控制另一台计算机。尽管界面设计简陋,但该项目的核心在于展示了RMI的强大功能,即在不同设备间实现分布式计算和控制。
在提供的压缩包文件中,"三种方式(原始方式_spring_jndi)实现java远程调用(rmi)"包含了相关的示例代码,帮助开发者理解并实践这三种RMI实现方法。在MyEclipse或其他Java开发环境中导入这些代码,可以进行调试和...
在本例中,"java RMI客户端调用远程服务器系统命令"可能指的是一个实际的应用场景,其中服务器端提供了一个可以执行操作系统命令的远程接口,如`executeCommand`。客户端通过RMI调用这个方法,传递需要执行的命令...
在RMI中,这个远程对象通常实现了特定的远程接口,该接口定义了可供客户端调用的聊天方法,如发送消息、接收消息等。服务器端启动后,会监听客户端的连接请求,并为每个连接的客户端提供服务。 2. **客户端(Client...