📄 rmilistclientimpl.java
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.rmi.*;import java.util.*;import java.rmi.server.*;import java.rmi.registry.*;public class RMIListClientImpl implements DistributedList { protected String host, service; protected int port; public RMIListClientImpl (String host, int port, String service) { this.host = host; this.port = port; this.service = service; } protected IDList idList; protected RMIListClient myself; protected RMIListServer listServer; public synchronized void start () throws RemoteException, NotBoundException { if (listServer == null) { myself = (RMIListClient) UnicastRemoteObject.exportObject (new Callback ()); Registry registry = LocateRegistry.getRegistry (host, port); listServer = (RMIListServer) registry.lookup (service); try { idList = listServer.register (myself); } catch (RemoteException ex) { listServer = null; throw ex; } fireChangeEvent (new ChangeEvent (this)); } } public synchronized void stop () throws RemoteException { try { if (listServer != null) listServer.deregister (myself); } finally { listServer = null; UnicastRemoteObject.unexportObject (myself, true); } } public synchronized void addElement (Object element) { if (listServer != null) { try { listServer.addElement (element); } catch (RemoteException ex) { listServer = null; } } } public synchronized void updateElement (Object oldElement, Object newElement) { if (listServer != null) { ID id = idList.getID (oldElement); if (id != null) { try { listServer.updateElement (id, newElement); } catch (RemoteException ex) { listServer = null; } } } } public synchronized void replaceElement (Object oldElement, Object newElement) { if (listServer != null) { ID id = idList.getID (oldElement); if (id != null) { try { listServer.replaceElement (id, newElement); } catch (RemoteException ex) { listServer = null; } } } } public synchronized void removeElement (Object element) { if (listServer != null) { ID id = idList.getID (element); if (id != null) { try { listServer.removeElement (id); } catch (RemoteException ex) { listServer = null; } } } } public Enumeration getElements () { return idList.getElements (); } protected Vector listeners = new Vector (); public void addChangeListener (ChangeListener listener) { listeners.addElement (listener); } public void removeChangeListener (ChangeListener listener) { listeners.removeElement (listener); } protected void fireChangeEvent (ChangeEvent changeEvent) { synchronized (listeners) { for (int i = 0; i < listeners.size (); ++ i) ((ChangeListener) listeners.elementAt (i)).changeOccurred (changeEvent); } } class Callback implements RMIListClient { public void elementAdded (ID id, Object element) { idList.addElement (id, element); fireChangeEvent (new ChangeEvent (RMIListClientImpl.this)); } public void elementUpdated (ID oldID, ID id, Object element) { idList.updateElement (oldID, id, element); fireChangeEvent (new ChangeEvent (RMIListClientImpl.this)); } public void elementReplaced (ID oldID, ID id, Object element) { idList.replaceElement (oldID, id, element); fireChangeEvent (new ChangeEvent (RMIListClientImpl.this)); } public void elementRemoved (ID id) { idList.removeElement (id); fireChangeEvent (new ChangeEvent (RMIListClientImpl.this)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -