Вы находитесь на странице: 1из 38

BootstrapExample

ClassFileServer.java

// Copyright MageLang Institute; Version $Id:


//depot/main/src/edu/modules/RMI2/magercises/BootstrapHTTPServer/working/ClassFileServe
r.java#2 $

/*
* Copyright (c) 1996, 1996, 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* SUN НЕ ДАЕТ НИКАКИХ ГАРАНТИЙ И НЕ ДЕЛАЕТ ЗАЯВЛЕНИЙ О ГОДНОСТИ ПРОГРАММНОГО
ОБЕСПЕЧЕНИЯ,
* НИ ВЫРАЖЕННЫХ, НИ ПОДРАЗУМЕВАЕМЫХ, ВКЛЮЧАЯ, НО НЕ ОГРАНИЧИВАЯСЬ, ПОДРАЗУМЕВАЕМЫЕ
* ГАРАНТИИ ПРИГОДНОСТИ К ПРОДАЖЕ, СООТВЕТСТВИЯ СПЕЦИАЛЬНЫМ ТРЕБОВАНИЯМ ИЛИ
* НЕНАРУШЕНИЯ ПАТЕНТОВ. SUN НЕ БУДЕТ ОТВЕТСТВЕННА ЗА ЛЮБЫЕ ПРИЧИНЕННЫЕ ПОВРЕЖДЕНИЯ
* ИЗ-ЗА ИСПОЛЬЗОВАНИЯ, МОДИФИКАЦИИ ИЛИ РАСПРОСТРАНЕНИЯ ЭТОГО
* ПРОГРАММНОГО ОБЕСПЕЧЕНИЯ ИЛИ ЕГО ПРОИЗВОДНЫХ.
*
*/
//package examples.classServer;

import java.io.*;
import java.net.*;

/**
* ClassFileServer реализует ClassServer, который
* читает файлы классов из файловой системы. Смотрите комментарий
* метода "Main" чтобы узнать, как запустить этот сервер.
*
*/
public class ClassFileServer

extends ClassServer
{
private String classpath;

private static int DefaultServerPort = 2001;

/**
* Создает ClassFileServer.
*
* @param classpath - classpath, где сервер ищет классы
*/
public ClassFileServer(int port, String classpath) throws IOException
{
super(port);
this.classpath = classpath;
}

/**
* Возвращает массив байт, содержащий байт-коды для
* класса, представленного аргументом <b>path</b>.
* <b>path</b> является разделенным точкой именем класса с
* удаленным расширением ".class".
*
* @return - байт-коды для класса
* @exception ClassNotFoundException, если класс соответствующий
* <b>path</b> не может быть создан.
*/
public byte[] getBytes(String path) throws IOException, ClassNotFoundException
{
System.out.println("reading: " + path);
File f = new File(classpath + File.separator + path.replace('.',
File.separatorChar) + ".class");
int length = (int)(f.length());

if (length == 0)
{
System.out.println( "Zero length file" );
throw new IOException("File length is zero: " + path);
}
else
{
FileInputStream fin = new FileInputStream(f);
DataInputStream in = new DataInputStream(fin);

byte[] bytecodes = new byte[length];


in.readFully(bytecodes);
return bytecodes;
}
}

/**
* Метод Main для создания сервера класса, который читает
* файлы классов. Он принимает два аргумента командной строки,
* номер порта, на котором сервер принимает запросы и
* корневой classpath. Для запуска сервера: <br><br>
*
* <code> java ClassFileServer <port> <classpath>
* </code><br><br>
*
* codebase RMI-сервера, использующая этот Web-сервер, будет
* просто содержать URL с хостом и портом Web-сервера
* (если classpath Web-сервера такой же как и classpath
* RMI-сервера): <br><br>
*
* <code> java -Djava.rmi.server.codebase=http://zaphod:2001/ RMIServer
* </code> <br><br>
*
* Вы можете создать свой собственный сервер классов внутри вашего серверного RMI-
* приложения вместо запуска его отдельно. В методе main вашего сервера просто
создается
* ClassFileServer: <br><br>
*
* <code> new ClassFileServer(port, classpath);
* </code>
*/
public static void main(String args[])
{
int port = DefaultServerPort;
String classpath = "";

if (args.length >= 1)
{
port = Integer.parseInt(args[0]);
}

if (args.length >= 2)
{
classpath = args[1];
}

try
{
new ClassFileServer(port, classpath);
System.out.println("ClassFileServer started...");
}
catch (IOException e)
{
System.out.println("Unable to start ClassServer: " + e.getMessage());
e.printStackTrace();
}
}
}

ClassServer.java
// Copyright MageLang Institute; Version $Id:
//depot/main/src/edu/modules/RMI2/magercises/BootstrapHTTPServer/working/ClassServer.ja
va#2 $

/*
* Copyright (c) 1996, 1996, 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* SUN НЕ ДАЕТ НИКАКИХ ГАРАНТИЙ И НЕ ДЕЛАЕТ ЗАЯВЛЕНИЙ О ГОДНОСТИ ПРОГРАММНОГО
ОБЕСПЕЧЕНИЯ,
* НИ ВЫРАЖЕННЫХ, НИ ПОДРАЗУМЕВАЕМЫХ, ВКЛЮЧАЯ, НО НЕ ОГРАНИЧИВАЯСЬ, ПОДРАЗУМЕВАЕМЫЕ
* ГАРАНТИИ ПРИГОДНОСТИ К ПРОДАЖЕ, СООТВЕТСТВИЯ СПЕЦИАЛЬНЫМ ТРЕБОВАНИЯМ ИЛИ
* НЕНАРУШЕНИЯ ПАТЕНТОВ. SUN НЕ БУДЕТ ОТВЕТСТВЕННА ЗА ЛЮБЫЕ ПРИЧИНЕННЫЕ ПОВРЕЖДЕНИЯ
* ИЗ-ЗА ИСПОЛЬЗОВАНИЯ, МОДИФИКАЦИИ ИЛИ РАСПРОСТРАНЕНИЯ ЭТОГО
* ПРОГРАММНОГО ОБЕСПЕЧЕНИЯ ИЛИ ЕГО ПРОИЗВОДНЫХ.
*
* CopyrightVersion 1.1_beta
*/

//package examples.classServer;

import java.io.*;
import java.net.*;

/**
* ClassServer является абстрактным классом, обеспечивающим
* базовую функциональность мини Web-сервера, предназначенного
* только для загрузки файлов классов. ClassServer должен быть расширен
* и конкретный подкласс должен определить метод <b>getBytes</b>, который
* отвечает за извлечение байт-кодов класса.<p>
*
* ClassServer создает поток, который прослушивает сокет и
* принимает HTTP GET запросы.HTTP-ответ содержит байт-коды для класса,
* запрошенного в GET-заголовке. <p>
*
* Для загрузки удаленных классов RMI-приложение может использовать
* конкретный подкласс этого сервера вместо http-сервера.
*
*
* @see ClassFileServer
*/
public abstract class ClassServer

implements Runnable
{

private ServerSocket server = null;


private int port;

/**
* Конструирует ClassServer, который прослушивает <b>port</b> и
* получает байт-коды класса, используя метод <b>getBytes</b>.
*
* @param port номер порта
* @exception IOException, если ClassServer не может прослушивать
* <b>port</b>.
*/
protected ClassServer(int port) throws IOException
{
this.port = port;
server = new ServerSocket(port);
newListener();
}

/**
*
* Возвращает массив байт, содержащий байт-коды для
* класса, представленного аргументом <b>path</b>.
* <b>path</b> является разделенным точкой именем класса с
* удаленным расширением ".class".
*
* @return - байт-коды для класса
* @exception ClassNotFoundException, если класс соответствующий
* <b>path</b> не может быть создан.
* @exception IOException если возникает ошибка чтения класса
*/
public abstract byte[] getBytes(String path) throws IOException,
ClassNotFoundException;

/**
* "Прослушивающий" поток, который принимает соединение к
* серверу, разбирает заголовок для получения имени файла класса
* и посылает назад байт-коды класса (или ошибку, если
* класс не найден, или ответ был нераспознан).
*/
public void run()
{
Socket socket;

// принять соединение
try
{
socket = server.accept();
}
catch (IOException e)
{
System.out.println("Class Server died: " + e.getMessage());
e.printStackTrace();
return;
}

// создать новый поток для приема следующего соединения


newListener();

try
{
DataOutputStream out =
new DataOutputStream(socket.getOutputStream());
try
{
// получить путь к файлам классов из заголовка
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String path = getPath(in);
// извлечь байт-коды
byte[] bytecodes = getBytes(path);
// послать байт-коды в качестве ответа (предполагает наличие HTTP/1.0
или более поздних)
try
{
out.writeBytes("HTTP/1.0 200 OK\r\n");
out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");
out.writeBytes("Content-Type: application/java\r\n\r\n");
out.write(bytecodes);
out.flush();
}
catch (IOException ie)
{
return;
}

}
catch (Exception e)
{
// отобразить ошибку ответа
out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.flush();
}

}
catch (IOException ex)
{
// перехватить исключительную ситуацию
// (может регистрироваться на сервере в log-файл,
// но в данный момент только выводит информацию на stdout).
System.out.println("error writing response: " + ex.getMessage());
ex.printStackTrace();

}
finally
{
try
{
socket.close();
}
catch (IOException e)
{
}
}
}

/**
* Создание нового потока для прослушивания.
*/
private void newListener()
{
(new Thread(this)).start();
}

/**
* Возвращает путь к файлу классов, полученного из анализатора
* HTML заголовка.
*/
private static String getPath(BufferedReader in) throws IOException
{
String line = in.readLine();
String path = "";

// изъять класс из строки GET


if (line.startsWith("GET /"))
{
line = line.substring(5, line.length()-1).trim();

int index = line.indexOf(".class ");


if (index != -1)
{
path = line.substring(0, index).replace('/', '.');
}
}

// обработать остаток заголовка


do
{
line = in.readLine();

} while ((line.length() != 0) && (line.charAt(0) != '\r') && (line.charAt(0) !=


'\n'));

if (path.length() != 0)
{
return path;
}
else
{
throw new IOException("Malformed Header");
}
}
}

HelloImpl.java
// Copyright MageLang Institute; Version $Id:
//depot/rkahle/src/edu/modules/RMI/magercises/BootstrapServer/HelloImpl.java#4 $
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public

class HelloImpl

extends UnicastRemoteObject

implements Hello
{
public HelloImpl() throws RemoteException
{
super();
}

public String sayHello() throws RemoteException


{
return "Hello!";

public MessageObject getMessageObject() throws RemoteException


{
return new MessageObject();
}

MessageObject.java
// Copyright MageLang Institute; Version $Id:
//depot/rkahle/src/edu/modules/RMI/magercises/BootstrapServer/MessageObject.java#4 $
import java.io.Serializable;

public
class MessageObject

implements Serializable
{
static int number = 0;
private int objNumber;

public MessageObject()
{
objNumber = number;
System.out.println( "MessageObject: Class Number is #" + number + " Object Number
is #" + objNumber );
number = number + 1;
}

public int getNumberFromObject()


{
return objNumber;
}

public int getNumberFromClass()


{
return number;
}
}

RMIClient.java
// Copyright MageLang Institute; Version $Id:
//depot/rkahle/src/edu/modules/RMI/magercises/BootstrapServer/RMIClient.java#4 $
import java.net.*;
import java.io.*;
import java.util.Properties;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

/**
* RMIClient - это начальный загрузчик RMI-клиента.
*
* Он реализует интерфейс Runnable, так что
* имеет хорошо известный способ запуска.
*
*/

public class RMIClient

implements Runnable
{
// Экземпляр объекта Root
private Hello hello;

private String rmiName;


private Properties p;
private MessageObject mo;

public void run()


{
try
{
// Извлечь месторасположение в сети RMIServer.
// Оно записывается в системном свойстве
// программой первоначальной загрузки.
p = System.getProperties();
rmiName = (String)p.get( "java.rmi.server.rminode" );
hello = (Hello)Naming.lookup( rmiName + "Hello" );

System.out.println( "Message from Server: " + hello.sayHello() );

for ( int i = 0; i< 10; i++ )


{
mo = hello.getMessageObject();
System.out.println( "MessageObject: Class Number is #" +
mo.getNumberFromClass() + " Object Number is #" + mo.getNumberFromObject() );
}
}
catch ( Exception e )
{
System.out.println( "RMIClient encountered an error when trying to locate
Hello on the server" + e );
e.printStackTrace();
}
}

} // класс RMIClient

RMIClientBootstrapSecurityManager.java
// Copyright MageLang Institute; Version $Id:
//depot/main/src/edu/modules/RMI2/magercises/BootstrapClient/working/RMIClientBootstrap
SecurityManager.java#3 $
import java.rmi.RMISecurityManager;

/**
* Этот класс определяет политику безопасности для RMI-приложений,
* которые загружаются с сервера. Снижение безопасности,
* обеспечиваемое этим классом, является минимальным,
* требуемым для начальной загрузки и запуска
* клиентского RMI-приложения
*
* Изменения в политике по сравнению с RMISecurityManager:
*
* Проверка безопасности Данная политика RMISecurityManager
* ------------------------------ ------------ ------------------
* Доступ к группе Thread YES NO
* Доступ к Threads YES NO
* Создание загрузчика классов. YES NO
* Доступ к системным свойствам. YES NO
* Соединения YES NO
*
*/

public class RMIClientBootstrapSecurityManager


extends RMISecurityManager {

/**
* Загруженные классы разрешаются для создания загрузчика классов.
*/
public synchronized void checkCreateClassLoader() {
// Provide null override
}

/**
* Разрешены соединения с другими машинами
*/
public synchronized void checkConnect(String host, int port) {
// Provide null override
}
/**
* Загруженным классам разрешено манипулировать потоком.
*/
public synchronized void checkAccess(Thread t) {
// Provide null override
}

/**
* Загруженным классам разрешено манипулировать группами потоков.
*/
public synchronized void checkAccess(ThreadGroup g) {
// Provide null override
}

/**
* Загруженным классам разрешено получить доступ к списку системных свойств.
*/
public synchronized void checkPropertiesAccess() {
// Обеспечьте переопределение null
}
}

RMIClientLoader.java
// Copyright MageLang Institute; Version $Id:
//depot/main/src/edu/modules/RMI2/magercises/BootstrapClient/working/RMIClientLoader.ja
va#3 $
import java.net.*;
import java.io.*;
import java.util.Properties;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

/**
* RMIClientLoader используется для "начальной загрузки" системы RMI-клиента.
* Класс клиента и все его поддерживающие классы загружаются
* по ссылке RMI (через http-сервер)
*
*/

public class RMIClientLoader {


private static final int PORT = 10009;
private static final String HOST_NAME = "localhost";

// Ссылка на самих себя


private static RMIClientLoader rcl;

// Класс для загружаемого клиента


private Class clientClass;
private Runnable client;

private URL url;


private Properties p;

// Переменные команд
private String clientName;

// Конструктор без аргументов


public RMIClientLoader() throws
MalformedURLException,
ClassNotFoundException,
InstantiationException,
IllegalAccessException {

p = System.getProperties();

url = new URL(p.getProperty("java.rmi.server.codebase"));

System.out.println("" + url);

// Поместите эту информацию в свойство, так чтобы загруженный


// клиент мог извлечь ее
p.put(
"java.rmi.server.rminode",
"rmi://" +
HOST_NAME +
":" +
Integer.toString(PORT) +
"/");

// Используйте RMIClassLoader для извлечения класса клиентской


// программы "clientName" из URL, указанного в переменной
// "url", в переменную "clientClass".

clientName = "RMIClient";

System.out.println("Asking for: " + url + " and " + clientName);

clientClass = RMIClassLoader.loadClass(url, clientName);

System.out.println("After loading Client Class");

// Создайте новый экземпляр класса клиента и поместите его в


// переменную "client".
client = (Runnable)clientClass.newInstance();

// Запустите клиентскую программу, выполнив его метод "run".


client.run();
}

public static void main (String args[]) {


System.setSecurityManager(
new RMIClientBootstrapSecurityManager());
try {
rcl = new RMIClientLoader();
} catch (MalformedURLException mURLe) {
System.out.println(
"URL not specified correctly for the Client class: " + mURLe);
} catch (ClassNotFoundException cnfe) {
System.out.println("RMIClientLoader, class not found: " + cnfe);
} catch (InstantiationException ie) {
System.out.println(
"RMIClientLoader, class could not be instantiated" + ie);
} catch (IllegalAccessException iae) {
System.out.println("Internal error" + iae);
}
} // main
} // класс RMIClientLoader

RMIServer.java
// Copyright MageLang Institute; Version $Id:
//depot/main/src/edu/modules/RMI2/magercises/BootstrapServer/working/RMIServer.java#3 $
import java.net.*;
import java.io.*;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

/**
* RMIServer работает как сервер для удаленной службы "Hello".
*
* RMIServer начинает выполнение со стандартной точки входа "public static void main";
* Он создает экземпляр самого себя и продолжает работу в конструкторе.
*
*/

public class RMIServer


{
private static final int PORT = 10009;

//
// -> Измените имя на имя вашего компьютера!
//
private static final String HOST_NAME = "localhost";

// Экземпляр самого себя


private static RMIServer rmi;

public static void main ( String[] args )


{
// Мы должны установить менеджер безопасности в RMISecurityManager
System.setSecurityManager( new RMIServerSecurityManager() );

try
{
rmi = new RMIServer();
}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println
(
"The host computer name you have specified, " +
HOST_NAME +
" does not match your real computer name."
);

}
catch ( RemoteException re )
{
System.out.println( "Error starting service" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "Internal error" + mURLe );
}
catch ( NotBoundException nbe )
{
System.out.println( "Not Bound" );
System.out.println( "" + nbe );
}

} // main

// Конструктор
public RMIServer()
throws RemoteException,
MalformedURLException,
NotBoundException
{

LocateRegistry.createRegistry( PORT );

System.out.println
(
"Registry created on host computer " +
HOST_NAME +
" on port " +
Integer.toString( PORT)
);

Hello hello = new HelloImpl();

System.out.println( "Remote Hello implementation object created" );

String urlString = "//" +


HOST_NAME +
":" +
Integer.toString( PORT ) +
"/" +
"Hello";

Naming.rebind( urlString, hello );

System.out.println( "Bindings Finished, waiting for client requests." );


}

} // класс RMIServer

/*
Это новый класс для упражнения (1999/07/26), который попытается переделать
это все для работы с JDK 1.2
*/

/**
* Этот класс определяет политику безопасности для RMI-приложений,
* которые загружаются с сервера. Снижение безопасности,
* обеспечиваемое этим классом, является минимально
* необходимым для начальной загрузки и запуска
* клиентского RMI-приложения
*
* Изменения в политике по сравнению с RMISecurityManager:
*
* Проверка безопасности Данная политика RMISecurityManager
* ------------------------------ ------------ ------------------
* Доступ к группе Thread YES NO
* Доступ к Threads YES NO
* Создание загрузчика классов. YES NO
* Доступ к системным свойствам. YES NO
* Соединения YES NO
*
*/

class RMIServerSecurityManager
extends RMISecurityManager
{

/**
* Загруженным классам разрешается создание загрузчиков классов
*/
public synchronized void checkCreateClassLoader()
{
// Обеспечьте null-переопределение
System.out.println( "checkCreateClassLoader()");
}

/**
* Разрешены соединения с другой машиной.
*/
public synchronized void checkConnect( String host, int port )
{
// Обеспечьте null-переопределение
System.out.println( "checkConnect()");
}

/**
* Загруженным классам разрешается манипулировать потоками.
*/
public synchronized void checkAccess(Thread t)
{
// Обеспечьте null-переопределение
System.out.println( "checkAccess()");
}

/**
* Загруженным классам разрешается манипулировать группами потоков.
*/
public synchronized void checkAccess(ThreadGroup g)
{
// Обеспечьте null-переопределение
System.out.println( "checkAccess()");
}

/**
* Загруженным классам разрешается получать доступ к списку системных свойств..
*/
public synchronized void checkPropertiesAccess()
{
// Обеспечьте null-переопределение
System.out.println( "checkPropertiesAccess()");
}

runclient.bat
java -Djava.rmi.server.codebase=http://pickle:2002/ RMIClientLoader

runhttp.bat
rem Этот файл запустит простой HTTP-сервер, каторый функционирует только
rem как источник для файлов классов Java.
rem
rem Первый параметр - номер порта TCP/IP, который
rem этот сервер прослушивает.
rem
rem Второй параметр - локальный каталог, содержащий
rem файлы классов Java для обслуживания.

java ClassFileServer 2002 <path_to_class_files>

runserver.bat
java RMIServer

DistributedGarbageCollector
Hello.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/DistributedGarbageCollector/Solution/Hello.java#2 $
import java.rmi.*;

public interface Hello extends java.rmi.Remote


{
public String sayHello() throws RemoteException;

public MessageObject getMessageObject() throws RemoteException;

HelloImpl.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/DistributedGarbageCollector/Solution/HelloImpl.java#2 $
import java.rmi.*;
import java.rmi.server.*;

public

class HelloImpl

extends UnicastRemoteObject

implements Hello, Unreferenced


{

public HelloImpl() throws RemoteException


{
super();
}

public String sayHello() throws RemoteException


{
return "Hello!";

public MessageObject getMessageObject() throws RemoteException


{
MessageObject mo = new MessageObjectImpl();

return mo;
}

public void unreferenced()


{
System.out.println( "HelloImpl: Unreferenced" );
}

public void finalize() throws Throwable


{
super.finalize();

System.out.println( "HelloImpl: Finalize called" );


}
}

MessageObject.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/DistributedGarbageCollector/Solution/MessageObject.java#2 $
import java.io.Serializable;
import java.rmi.server.*;

public

interface MessageObject extends java.rmi.Remote

public int getNumberFromObject() throws java.rmi.RemoteException;

public int getNumberFromClass() throws java.rmi.RemoteException;

MessageObjectImpl.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/DistributedGarbageCollector/Solution/MessageObjectImpl.java#2 $
import java.io.Serializable;
import java.rmi.server.*;
import java.rmi.*;

public

class MessageObjectImpl

extends UnicastRemoteObject

implements MessageObject, Serializable, Unreferenced


{
static int number = 0;
private int objNumber;

public MessageObjectImpl() throws RemoteException


{
objNumber = number;
System.out.println( "MessageObject: Class Number is #" + number + " Object Number
is #" + objNumber );
number = number + 1;
}

public int getNumberFromObject()


{
return objNumber;
}

public int getNumberFromClass()


{
return number;
}

public void finalize() throws Throwable


{
super.finalize();

System.out.println( "MessageObject: Finalize for object #: " + objNumber );


}
public void unreferenced()
{
System.out.println( "MessageObject: Unreferenced for object #: " + objNumber );
}

RMIClient.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/DistributedGarbageCollector/Solution/RMIClient.java#2 $
import java.util.Date;
import java.net.MalformedURLException;

import java.rmi.*;

/**
* RMIClient будет запрашивать удаленную службу "HelloService".
*
* Будет сделано много запросов, заставляя работать и
* распределенный сборщик мусора и локальный.
*
*/

public class RMIClient


{
private static final int PORT = 10007;

//
// -> Измените это имя на имя вашего компьютера
//
private static final String HOST_NAME = "ROSA";

// Экземпляр самого себя


private static RMIClient rmi;

public static void main ( String[] args )


{
rmi = new RMIClient();
} // main

// Конструктор
public RMIClient()
{

try
{
Hello hello = (Hello)Naming.lookup( "//" + HOST_NAME + ":" + Integer.toString(
PORT ) + "/" + "Hello" );

System.out.println( "HelloService lookup successful" );

System.out.println( "Message from Server: " + hello.sayHello() );

MessageObject mo;

for ( int i = 0; i< 1000; i++ )


{
mo = hello.getMessageObject();
System.out.println( "MessageObject: Class Number is #" +
mo.getNumberFromClass() + " Object Number is #" + mo.getNumberFromObject() );
mo = null;
}

}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println( "The host computer name you have specified, " + HOST_NAME
+ " does not match your real computer name." );
}
catch ( RemoteException re )
{
System.out.println( "A Remote Exception was thrown when requesting the
TimeService" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "There is a problem with the rmi: URL you are using" );
System.out.println( "" + mURLe );
}
catch ( NotBoundException nbe )
{
System.out.println( "" + nbe );
}
}

} // класс RMIClient

RMIServer.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/DistributedGarbageCollector/Solution/RMIServer.java#2 $
import java.net.*;
import java.io.*;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

/**
* RMIServer работает как сервер для удаленной службы "HelloService".
*
* RMIServer начинает выполнение со стандартной точки входа "public static void main";
* Он создает экземпляр самого себя и продолжает работу в конструкторе.
*
*/

public class RMIServer


{
private static final int PORT = 10007;

//
// -> Измените имя на имя вашего компьютера!
//
private static final String HOST_NAME = "ROSA";

// Экземпляр самого себя


private static RMIServer rmi;

public static void main ( String[] args )


{
// Мы должны установить менеджер безопасности в RMISecurityManager
System.setSecurityManager( new RMISecurityManager() );

try
{
rmi = new RMIServer();
}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println( "The host computer name you have specified, " + HOST_NAME
+ " does not match your real computer name." );

}
catch ( RemoteException re )
{
System.out.println( "Error starting service" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "Internal error" + mURLe );
}
catch ( NotBoundException nbe )
{
System.out.println( "Not Bound" );
System.out.println( "" + nbe );
}

} // main

// Конструктор
public RMIServer() throws RemoteException, MalformedURLException, NotBoundException
{
LocateRegistry.createRegistry( PORT );

System.out.println( "Registry created on host computer " + HOST_NAME + " on port


" + Integer.toString( PORT) );

Hello hello = new HelloImpl();

System.out.println( "Remote HelloService implementation object created" );

String urlString = "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" +


"Hello";

Naming.rebind( urlString, hello );

System.out.println( "Bindings Finished, waiting for client requests." );


}

} // класс RMIServer

LocalRemoteClient
LocalRemoteClient.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/LocalRemoteClient/Solution/LocalRemoteClient.java#2 $
/**
* Класс: LocalRemoteClient
*
* Цель: Протестировать использование модели делегирования для
* реализации удаленных и локальных версий
* сетевых служб RMI.
*/
import java.net.*;
import java.io.*;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

public class LocalRemoteClient


{
private static final int PORT = 10009;
//
// -> Измените это имя на имя вашего компьютера!
//
private static final String HOST_NAME = "ROSA";

// Экземпляр самого себя


private static LocalRemoteClient lrc;

public static void main


(
String[] args
)
{

lrc = new LocalRemoteClient();

} // main

// Конструктор
public LocalRemoteClient()
{

RemoteModelMgr rmm;

LocalModel lm;
RemoteModelRef rmr;

String versionLocal;
String versionRemote;

try
{
rmm = (RemoteModelMgr)Naming.lookup( "rmi://" + HOST_NAME + ":" +
Integer.toString( PORT ) + "/RemoteModelManager" );

System.out.println( "RemoteModelManager lookup successful" );

rmr = rmm.getRemoteModelRef();
versionRemote = rmr.getVersionNumber();

System.out.println( "Remote version: " + versionRemote );

lm = rmm.getLocalModel();

System.out.println( "Local Version of the model loaded" );

versionLocal = lm.getVersionNumber();

System.out.println( "Local version: " + versionLocal );


}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println( "The host computer name you have specified, " + HOST_NAME
+ " does not match your real computer name." );
}
catch ( RemoteException re )
{
System.out.println( "A Remote Exception was thrown when requesting the
RemoteModelManager Server" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "There is a problem with the rmi: URL you are using" );
System.out.println( "" + mURLe );
}
catch ( NotBoundException nbe )
{
System.out.println( "" + nbe );
}
catch ( Exception e )
{
System.out.println( "" + e );
}

} // класс LocalRemoteClient

runclient.bat
java -Djava.rmi.server.codebase=http://ROSA:2002/ RMIClientLoader

LocalRemoteServer
COMPILE.BAT
del *.class
javac *.java
rmic RemoteModelMgrImpl
rmic RemoteModelImpl

LocalModel.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/LocalRemoteServer/Solution/LocalModel.java#2 $
/**
* Класс: LocalModel
*
* Цель: Обеспечить фактические службы для модели.
* Этот класс может быть сериализован и передан в
* другую JVM или он может быть делегирован
* из удаленного интерфейсного класса.
*/

public class LocalModel


implements java.io.Serializable
{
public String getVersionNumber()
{
String version = "Version 1.0";
System.out.println( "LocalModel...Returning: " + version );

return version;
}
}

LocalRemoteServer.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/LocalRemoteServer/Solution/LocalRemoteServer.java#2 $
/**
* Класс: LocalRemoteServer
*
* Цель: Создать экземпляры и зарегистрировать удаленные службы,
* необходимые для этого упражнения.
*/
import java.net.*;
import java.io.*;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

public class LocalRemoteServer


{
private static final int PORT = 10009;

//
// -> Измените это имя на имя вашего компьютера
//
private static final String HOST_NAME = "ROSA";

// Экземпляр самого себя


private static LocalRemoteServer lrs;

public static void main


(
String[] args
)
{
// Мы должны установить менеджер безопасности в RMISecurityManager
System.setSecurityManager( new RMISecurityManager() );

try
{
lrs = new LocalRemoteServer();
}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println( "The host computer name you have specified, " + HOST_NAME
+ " does not match your real computer name." );

}
catch ( RemoteException re )
{
System.out.println( "Error starting service" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "Internal error" + mURLe );
}
catch ( NotBoundException nbe )
{
System.out.println( "Not Bound" );
System.out.println( "" + nbe );
}

} // main

// Конструктор
public LocalRemoteServer()
throws RemoteException,
MalformedURLException,
NotBoundException
{

LocateRegistry.createRegistry( PORT );

System.out.println( "Registry created on host computer " + HOST_NAME + " on port


" + Integer.toString( PORT) );

RemoteModelMgrImpl rmmImpl = new RemoteModelMgrImpl();

System.out.println( "RemoteModelImpl object created" );

String urlString = "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" +


"RemoteModelManager";

Naming.rebind( urlString, rmmImpl );

System.out.println( "Bindings Finished, waiting for client requests." );


}

} // класс LocalRemoteServer

RemoteModelImpl.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/LocalRemoteServer/Solution/RemoteModelImpl.java#2 $
/**
* Класс: RemoteModelImpl
*
* Цель: Обеспечить удаленный интерфейс для делегирования LocalModel
*
*/
public class RemoteModelImpl
extends java.rmi.server.UnicastRemoteObject
implements RemoteModelRef
{
LocalModel lm;

public RemoteModelImpl( LocalModel lm )


throws java.rmi.RemoteException
{
super();

this.lm = lm;
System.out.println( "RemoteModelImpl...Constructor finished" );

// Делегировать в реализацию локальной модели


public String getVersionNumber()
throws java.rmi.RemoteException
{
String s = lm.getVersionNumber();
System.out.println( "RemoteModelImpl...Returning delegated VersionNumber: " + s
);

return s;
}

RemoteModelMgr.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/LocalRemoteServer/Solution/RemoteModelMgr.java#2 $
import java.rmi.*;

/**
* Интерфейс: RemoteModelMgr
*
* Цель: Обеспечить удаленный доступ к удаленной
* или возвращенной локальной копии "ComputationalModel"
*
* Примечания: RemoteModel является RMI "удаленным" интерфейсом.
* LocalModel является локальным интерфейсом
*/
interface RemoteModelMgr
extends java.rmi.Remote
{
RemoteModelRef getRemoteModelRef()
throws java.rmi.RemoteException;

LocalModel getLocalModel()
throws java.rmi.RemoteException;
}

RemoteModelMgrImpl.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/LocalRemoteServer/Solution/RemoteModelMgrImpl.java#2 $
/**
* Класс: RemoteModelMgrImpl
*
* Цель: Обеспечить реализацию RMI-службы для RemoteModelMgr.
*
*
* Примечания: RemoteModel является RMI "удаленным" интерфейсом.
* LocalModel является локальным интерфейсом
*/
public class RemoteModelMgrImpl
extends java.rmi.server.UnicastRemoteObject
implements RemoteModelMgr
{
LocalModel lm;
RemoteModelImpl rmImpl;

/**
* Конструктор
*/
public RemoteModelMgrImpl()
throws java.rmi.RemoteException
{
super();
}

public RemoteModelRef getRemoteModelRef()


throws java.rmi.RemoteException
{
// Отложенная инсталляция делегата
if ( null == lm )
{
lm = new LocalModel();
}

// Отложенное создание экземпляра удаленного интерфейса


if ( null == rmImpl )
{
rmImpl = new RemoteModelImpl( lm );
}

return ( (RemoteModelRef) rmImpl );


}

/**
* Возвращает ссылку на объект, выполняющий
* фактические операции. Если это RMI-вызов, будет возвращаться
* копия.
*/
public LocalModel getLocalModel()
throws java.rmi.RemoteException
{
// Отложенная инсталляция делегата
if ( null == lm )
{
lm = new LocalModel();
}

return lm;
}
}

RemoteModelRef.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/LocalRemoteServer/Solution/RemoteModelRef.java#2 $
/**
* Интерфейс: RemoteModelRef
*
* Цель: Обеспечить удаленный доступ к "ComputationalModel"
*
* Примечания:
*/
interface RemoteModelRef
extends java.rmi.Remote
{
String getVersionNumber() throws java.rmi.RemoteException;
}

RMICallback
Applet1.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMICallback/Solution/Applet1.java#2 $
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Date;
import java.net.URL;
import java.rmi.*;
import java.rmi.server.*;

public

class Applet1

extends Applet

implements TimeMonitor
{
// Измените имя на имя вашего компьютера
private final static String HOST_NAME = "ROSA";

private TimeServer ts;

public void init()


{
super.init();

uiInit();

try
{
System.out.println( "Exporting the Applet" );
UnicastRemoteObject.exportObject( this );
URL base = getDocumentBase();

String hostName = base.getHost();

if ( 0 == hostName.length() )
{
hostName = HOST_NAME;
}

String serverName = "rmi://" + hostName + ":" + getParameter( "registryPort"


) + "/TimeServer" ;

System.out.println( "Looking up TimeService at: " + serverName );

try
{
ts = (TimeServer)Naming.lookup( serverName );
}
catch ( Exception e )
{
System.out.println( "" + e );
}

ts.registerTimeMonitor( this );

System.out.println( "We have been registered!" );

}
catch ( RemoteException re )
{
System.out.println( "" + re );
}
}

public void tellMeTheTime( Date d )


{
textArea1.appendText( d.toString() + "\n" );
}

public void uiInit()


{
setLayout(null);
resize(456,266);
textArea1 = new java.awt.TextArea();
textArea1.reshape(36,24,252,170);
add(textArea1);

button1 = new java.awt.Button("Clear");


button1.reshape(324,36,72,24);
add(button1);

button1.addActionListener
(
new ActionListener()
{

public void actionPerformed(ActionEvent event)


{
textArea1.setText("");
}
}
);

java.awt.TextArea textArea1;
java.awt.Button button1;
}

RMIServer.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMICallback/Solution/RMIServer.java#2 $
import java.net.*;
import java.io.*;
import java.util.Date;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

/**
* RMIServer работает как сервер для удаленной службы "TimeServer" .
*
* RMIServer начинает выполнение со стандартной точки входа "public static void main";
* Он создает экземпляр самого себя и продолжает работу в конструкторе.
*
*/

public class RMIServer implements Remote, TimeServer


{
private static final int PORT = 10005;

//
// -> Измените имя на имя вашего компьютера!
//
private static final String HOST_NAME = "ROSA";
// Экземпляр самого себя
private static RMIServer rmi;

public static void main ( String[] args )


{
// Мы должны установить менеджер безопасности в RMISecurityManager
System.setSecurityManager( new RMISecurityManager() );

try
{
rmi = new RMIServer();

LocateRegistry.createRegistry( PORT );

System.out.println( "Registry created" );

UnicastRemoteObject.exportObject( ((TimeServer)rmi) );

Naming.rebind( "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" +


"TimeServer", rmi );

System.out.println( "Bindings Finished" );

System.out.println( "Waiting for Client requests" );

}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println( "The host computer name you have specified, " + HOST_NAME
+ " does not match your real computer name." );

}
catch ( RemoteException re )
{
System.out.println( "Error starting service" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "Internal error" + mURLe );
}

} // main

public void registerTimeMonitor( TimeMonitor tm )


{
System.out.println( "Client requesting a connection" );

TimeTicker tt;

// Вставьте две строки кода, завершающих реализацию этого метода.


// 1. Создайте новый объект TimeTicker
// 2. Запустите этот новый объект.
tt = new TimeTicker( tm );
tt.start();

System.out.println( "Timer Started" );


}

} // класс RMIServer

class TimeTicker extends Thread


{
private TimeMonitor tm;

TimeTicker( TimeMonitor tm )
{
this.tm = tm;
}

public void run()


{
while ( true )
{
try
{
sleep( 2000 );

// Вставьте одну строку кода, которая вызывает метод tellMeTheTime


// интерфейса TimeMonitor.
// Передайте ему новый экземпляр класса Date.
tm.tellMeTheTime( new Date() );
}
catch ( Exception e )
{
stop();
}
}
}
}

TimeMonitor.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMICallback/Solution/TimeMonitor.java#2 $
import java.rmi.*;
import java.util.Date;

// Создайте интерфейс TimeMonitor.


// Он содержит один метод tellMeTheTime.
// Этот метод принимает параметр с типом Date
// и возвращает void.
public interface TimeMonitor extends java.rmi.Remote
{
public void tellMeTheTime( Date d ) throws RemoteException;
}

TimeServer.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMICallback/Solution/TimeServer.java#2 $
import java.rmi.*;

// Создайте интерфейс TimeServer.


// Он содержит один метод, регистрирующий TimeMonitor.
// Этот метод принимает параметр с типом TimeMonitor
// и возвращает void.

public interface TimeServer extends java.rmi.Remote


{
public void registerTimeMonitor( TimeMonitor tm) throws RemoteException;

}
RMIParameters
Hello.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMIParameters/Solution/Hello.java#2 $
import java.rmi.*;

public interface Hello extends java.rmi.Remote


{
public String sayHello() throws RemoteException;

public MessageObject getMessageObject() throws RemoteException;

HelloImpl.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMIParameters/Solution/HelloImpl.java#2 $
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public

class HelloImpl

extends UnicastRemoteObject

implements Hello
{

public HelloImpl() throws RemoteException


{
super();
}

public String sayHello() throws RemoteException


{
return "Hello!";

public MessageObject getMessageObject() throws RemoteException


{
return new MessageObject();
}

MessageObject.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMIParameters/Solution/MessageObject.java#2 $
import java.io.Serializable;

public

class MessageObject

implements Serializable
{
static int number = 0;
private int objNumber;
public MessageObject()
{
objNumber = number;

System.out.println( "MessageObject: Class Number is #" + number + " Object Number


is #" + objNumber );

number = number + 1;
}

public int getNumberFromObject()


{
return objNumber;
}

public int getNumberFromClass()


{
return number;
}
}

RMIClient.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMIParameters/Solution/RMIClient.java#2 $
import java.net.*;
import java.io.*;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

/**
* RMIClient02 утилизирует удаленный объект "Hello".
*
* RMIClient02 обращается к RMIServer для обработки командной строки.
*/

public class RMIClient


{
private static final int PORT = 10002;
//
// -> Измените имя на имя вашего компьютера!
//
private static final String HOST_NAME = "ROSA";

// Экземпляр самого себя


private static RMIClient rmi;

// Экземпляр объекта Root


private static Hello hello;

public static void main ( String[] args )


{

rmi = new RMIClient();

} // main

// Конструктор
public RMIClient()
{
Hello h;
String helloString;
MessageObject mo;

try
{
h = (Hello)Naming.lookup( "rmi://" + HOST_NAME + ":" + Integer.toString( PORT
) + "/HelloService" );

System.out.println( "HelloService lookup successful" );

helloString = h.sayHello();

System.out.println( "The server says: " + helloString );

for ( int i = 0; i< 10; i++ )


{
mo = h.getMessageObject();
System.out.println( "MessageObject: Class Number is #" +
mo.getNumberFromClass() + " Object Number is #" + mo.getNumberFromObject() );
}

}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println( "The host computer name you have specified, " + HOST_NAME
+ " does not match your real computer name." );
}
catch ( RemoteException re )
{
System.out.println( "A Remote Exception was thrown when requesting the
HelloService" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "There is a problem with the rmi: URL you are using" );
System.out.println( "" + mURLe );
}
catch ( NotBoundException nbe )
{
System.out.println( "" + nbe );
}

} // класс RMIClient

RMIServer.java
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-
mml2/magercises/RMIParameters/Solution/RMIServer.java#2 $
import java.net.*;
import java.io.*;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;

/**
* RMIServer работает как сервер для удаленной службы "HelloService" .
*
* RMIServer начинает выполнение со стандартной точки входа "public static void main";
* Он создает экземпляр самого себя и продолжает работу в конструкторе.
*
*/

public class RMIServer


{
private static final int PORT = 10002;

//
// -> Измените имя на имя вашего компьютера!
//
private static final String HOST_NAME = "ROSA";

// Экземпляр самого себя


private static RMIServer rmi;

public static void main ( String[] args )


{
// Мы должны установить менеджер безопасности в RMISecurityManager
System.setSecurityManager( new RMISecurityManager() );

try
{
rmi = new RMIServer();
}
catch ( java.rmi.UnknownHostException uhe )
{
System.out.println( "The host computer name you have specified, " + HOST_NAME
+ " does not match your real computer name." );

}
catch ( RemoteException re )
{
System.out.println( "Error starting service" );
System.out.println( "" + re );
}
catch ( MalformedURLException mURLe )
{
System.out.println( "Internal error" + mURLe );
}
catch ( NotBoundException nbe )
{
System.out.println( "Not Bound" );
System.out.println( "" + nbe );
}

} // main

// Конструктор
public RMIServer() throws RemoteException, MalformedURLException, NotBoundException
{
LocateRegistry.createRegistry( PORT );

System.out.println( "Registry created on host computer " + HOST_NAME + " on port


" + Integer.toString( PORT) );

Hello h = new HelloImpl();

System.out.println( "Remote HelloService implementation object created" );

String urlString = "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" +


"HelloService";
Naming.rebind( urlString, h );

System.out.println( "Bindings Finished, waiting for client requests." );


}

} // класс RMIServer

SimpleBankingSystem
Account.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Account extends Remote {


// Добавьте метод для возврата BankManager
public BankManager getBankManager()
throws RemoteException;

// Добавьте метод для возврата клиента этого счета


public Client getClient()
throws RemoteException;

// Добавьте метод для возврата баланса этого счета


public long getBalance()
throws RemoteException;

// Добавьте метод для снятия наличности с этого счета


public long getCash (long amount)
throws NoCashAvailableException, RemoteException;
}

AccountImpl.java
import java.rmi.RemoteException;

public class AccountImpl implements Account {

private BankManager bankManager;


private Client client;
private long balance;
private String accountNumber;

// общедоступный конструктор
public AccountImpl (
BankManager bankManager,
Client client,
String accountNumber) {
this.bankManager = bankManager;
this.client = client;
this.balance = 0;
this.accountNumber = accountNumber;
}

public void deposit(long amount) {


balance += amount;
}

public BankManager getBankManager()


throws RemoteException {
return bankManager;
}
public Client getClient()
throws RemoteException {
return client;
}

public long getBalance()


throws RemoteException {
return balance;
}

public long getCash(long amount)


throws NoCashAvailableException, RemoteException {
if (amount > balance) {
throw new NoCashAvailableException();
}
balance = balance - amount;
return amount;
}
}

BankManager.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface BankManager extends Remote {

// Добавьте метод для возврата службы Account


public Account getAccount(String accountNumber)
throws RemoteException;

// Добавьте метод для возврата службы Client


public Client getClient(String clientName)
throws RemoteException;
}

BankManagerImpl.java
import java.util.Hashtable;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class BankManagerImpl implements BankManager {


private Hashtable accounts;
private Hashtable clients;

// общедоступный конструктор без аргументов


public BankManagerImpl()
throws java.rmi.RemoteException {
initialize();
}

public Account getAccount(String accountNumber)


throws RemoteException {
AccountImpl account = (AccountImpl)accounts.get(accountNumber);
return account;
}

public Client getClient(String clientName)


throws RemoteException {
ClientImpl client = (ClientImpl)clients.get(clientName);
return client;
}
public void initialize()
throws java.rmi.RemoteException {
// Создайте хэш-таблицы
accounts = new Hashtable(20);
clients = new Hashtable(10);
// Создайте клиентов и поместите их в хэш-таблицу
Client clientCharlie = new ClientImpl(this, "Charlie");
UnicastRemoteObject.exportObject(clientCharlie);
Client clientShannon = new ClientImpl(this, "Shannon");
UnicastRemoteObject.exportObject(clientShannon);
clients.put("Charlie", clientCharlie);
clients.put("Shannon", clientShannon);

// Создайте счета:
// * поместите их в хэш-таблицу
// * назначьте их клиентам
Account account;
account = new AccountImpl(this, clientCharlie, "4434");
((AccountImpl)account).deposit(500);
UnicastRemoteObject.exportObject(account);
accounts.put("4434", account);
account = new AccountImpl(this, clientCharlie, "4461");
((AccountImpl)account).deposit(600);
UnicastRemoteObject.exportObject(account);
accounts.put("4461", account);
account = new AccountImpl(this, clientShannon, "6678");
((AccountImpl)account).deposit(700);
UnicastRemoteObject.exportObject(account);
accounts.put("6678", account);
}
}

BankSystemServer.java
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.net.MalformedURLException;

public class BankSystemServer {

// общедоступный конструктор без аргументов


public BankSystemServer() {
}

public static void main(String args[]) {


new BankSystemServer();

BankManager bm = null;

try {
// Создайте объект BankManager
bm = new BankManagerImpl();

// Экспортируйте его в RMI


UnicastRemoteObject.exportObject( bm );
} catch (RemoteException remoteException) {
System.err.println(
"Failure during object export to RMI: " +
remoteException);
}

// Зарегистрируйте внешнее имя для службы


try {
Naming.rebind("//localhost/BankSystem", bm);
} catch (RemoteException remoteException) {
System.err.println(
"Failure during Name registration: " +
remoteException);
} catch (MalformedURLException malformedException) {
System.err.println(
"Failure during Name registration: " +
malformedException);
}

System.out.println("Server started.");
System.out.println("Enter <CR> to end.");
try {
int i = System.in.read();
} catch (IOException ioException) {
}
System.exit(0);
}
}

BankUser.java
import java.rmi.*;
import java.net.MalformedURLException;
import java.util.Locale;
import java.text.NumberFormat;

public class BankUser {

// Ссылка на интерфейс BankManager


private BankManager bm;

// Конструктор без аргументов


public BankUser() {

try {
bm = (BankManager)Naming.lookup(
"rmi://localhost:1099/BankSystem");
} catch (MalformedURLException malformedException) {
System.err.println("Bad URL: " + malformedException);
} catch (NotBoundException notBoundException) {
System.err.println("Not Bound: " + notBoundException);
} catch (RemoteException remoteException) {
System.err.println("Remote Exception: " + remoteException);
}

try {
// Найдите счет 4461
Account account = bm.getAccount("4461");

// Получите объект клиента для этого счета


Client client = account.getClient();

// Получите имя клиента


String name = client.getName();

// Получите баланс для счета


long cash = account.getBalance();

// Отформатируйте и отобразите выходную информацию


NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance(Locale.US);
String balanceString = currencyFormat.format(cash);
System.out.println(name + "'s account has " + balanceString);
} catch (RemoteException remoteException) {
System.err.println(remoteException);
}
}

public static void main(String[] args) {


new BankUser();
}
}

Client.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Client extends Remote {

// Добавьте метод для возврата BankManager


public BankManager getBankManager()
throws RemoteException;

// Добавьте метод для возврата имени клиента


public String getName()
throws RemoteException;
}

ClientImpl.java
import java.rmi.RemoteException;

public class ClientImpl implements Client {

private BankManager bankManager;


private String clientName;

// общедоступный конструктор
public ClientImpl(BankManager bm, String name) {
this.bankManager = bm;
this.clientName = name;
}

public BankManager getBankManager()


throws RemoteException {
return bankManager;
}

public String getName()


throws RemoteException {
return clientName;
}
}

NoCashAvailableException.java
public class NoCashAvailableException extends Exception {
}

UMLDefinition
Account.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Account extends Remote
{
// Добавьте метод для возврата BankManager
public BankManager getBankManager()
throws RemoteException;

// Добавьте метод для возврата клиента этого счета


public Client getClient()
throws RemoteException;

// Добавьте метод для возврата баланса этого счета


public long getBalance()
throws RemoteException;

// Добавьте метод для снятия наличности с этого счета


public long getCash( long amount )
throws RemoteException;
}

BankManager.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface BankManager extends Remote


{
// Добавьте метод для возврата службы Account
public Account getAccount( String accountNumber )
throws RemoteException;

// Добавьте метод для возврата службы Client


public Client getClient( String clientName )
throws RemoteException;
}

Client.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Client extends Remote


{
// Добавьте метод для возврата BankManager
public BankManager getBankManager()
throws RemoteException;

// Добавьте метод для возврата имени клиента


public String getName()
throws RemoteException;
}

Вам также может понравиться