📄 servletlistclient.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.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletListClient implements DistributedList, Runnable {
public static final int UPDATE_PERIOD = 10 * 1000;
protected URL servlet;
protected IDList idList;
public ServletListClient (URL servlet) {
try {
this.servlet = new URL (servlet, servlet.getFile () + '/');
} catch (MalformedURLException ignored) {
}
idList = new IDList ();
}
protected Thread updater;
public synchronized void start ()
throws IOException, ClassNotFoundException {
if (updater == null) {
getUpdate ();
updater = new Thread (this);
updater.start ();
}
}
public synchronized void stop () {
Thread updater = this.updater;
this.updater = null;
if (updater != null)
updater.interrupt ();
}
public void run () {
Thread myself = Thread.currentThread ();
try {
while (updater == myself) {
Thread.sleep (UPDATE_PERIOD);
getUpdate ();
}
} catch (InterruptedException ignored) {
} catch (IOException ex) {
ex.printStackTrace ();
} catch (ClassNotFoundException ex) {
ex.printStackTrace ();
} finally {
updater = null;
}
}
protected synchronized void getUpdate () throws IOException, ClassNotFoundException {
int count = idList.getUpdateCount ();
URL url = new URL (servlet, "getUpdate?count=" + count);
URLConnection conn = url.openConnection ();
conn.setUseCaches (false);
ObjectInputStream in = new ObjectInputStream (conn.getInputStream ());
IDList newList = (IDList) in.readObject ();
in.close ();
if (newList != null) {
idList = newList;
fireChangeEvent (new ChangeEvent (this));
}
}
public synchronized void addElement (Object element) {
try {
URL url = new URL (servlet, "addElement");
URLConnection conn = url.openConnection ();
conn.setRequestProperty ("Content-type", "application/octet-stream");
conn.setDoOutput (true);
ObjectOutputStream out = new ObjectOutputStream (
conn.getOutputStream ());
out.writeObject (element);
out.close ();
ObjectInputStream in = new ObjectInputStream (
conn.getInputStream ());
ID id = (ID) in.readObject ();
in.close ();
if (id != null) {
idList.addElement (id, element);
fireChangeEvent (new ChangeEvent (this));
}
} catch (IOException ex) {
ex.printStackTrace ();
} catch (ClassNotFoundException ex) {
ex.printStackTrace ();
}
}
public synchronized void updateElement (Object oldElement, Object newElement) {
ID id = idList.getID (oldElement);
if (id != null) {
try {
URL url = new URL (servlet, "updateElement");
URLConnection conn = url.openConnection ();
conn.setDoOutput (true);
ObjectOutputStream out = new ObjectOutputStream (
conn.getOutputStream ());
out.writeObject (id);
out.writeObject (newElement);
out.close ();
ObjectInputStream in = new ObjectInputStream (
conn.getInputStream ());
ID newID = (ID) in.readObject ();
in.close ();
if (newID != null) {
idList.updateElement (id, newID, newElement);
fireChangeEvent (new ChangeEvent (this));
}
} catch (IOException ex) {
ex.printStackTrace ();
} catch (ClassNotFoundException ex) {
ex.printStackTrace ();
}
}
}
public synchronized void replaceElement (Object oldElement, Object newElement) {
ID id = idList.getID (oldElement);
if (id != null) {
try {
URL url = new URL (servlet, "replaceElement");
URLConnection conn = url.openConnection ();
conn.setDoOutput (true);
ObjectOutputStream out = new ObjectOutputStream (
conn.getOutputStream ());
out.writeObject (id);
out.writeObject (newElement);
out.close ();
ObjectInputStream in = new ObjectInputStream (
conn.getInputStream ());
ID newID = (ID) in.readObject ();
in.close ();
if (newID != null) {
idList.replaceElement (id, newID, newElement);
fireChangeEvent (new ChangeEvent (this));
}
} catch (IOException ex) {
ex.printStackTrace ();
} catch (ClassNotFoundException ex) {
ex.printStackTrace ();
}
}
}
public synchronized void removeElement (Object element) {
ID id = idList.getID (element);
if (id != null) {
try {
URL url = new URL (servlet, "removeElement");
URLConnection conn = url.openConnection ();
conn.setDoOutput (true);
ObjectOutputStream out = new ObjectOutputStream (
conn.getOutputStream ());
out.writeObject (id);
out.close ();
ObjectInputStream in = new ObjectInputStream (
conn.getInputStream ());
boolean okay = in.readBoolean ();
in.close ();
if (okay) {
idList.removeElement (id);
fireChangeEvent (new ChangeEvent (this));
}
} catch (IOException ex) {
ex.printStackTrace ();
}
}
}
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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -