| 1 |
package samples.bidbuy; |
| 2 |
|
| 3 |
import java.io.FileInputStream; |
| 4 |
import java.io.FileOutputStream; |
| 5 |
import java.io.ObjectInputStream; |
| 6 |
import java.io.ObjectOutputStream; |
| 7 |
import java.util.Enumeration; |
| 8 |
import java.util.Hashtable; |
| 9 |
import java.util.Vector; |
| 10 |
|
| 11 |
public class RegistryService { |
| 12 |
|
| 13 |
private static Hashtable registry = new Hashtable(); |
| 14 |
|
| 15 |
/** |
| 16 |
* Find a named service in a list |
| 17 |
* @param list of services |
| 18 |
* @param name to search for |
| 19 |
* @return service found (or null) |
| 20 |
*/ |
| 21 |
|
| 22 |
public RegistryService() { |
| 23 |
load(); |
| 24 |
} |
| 25 |
|
| 26 |
public void load() { |
| 27 |
try { |
| 28 |
FileInputStream fis = new FileInputStream("bid.reg"); |
| 29 |
ObjectInputStream ois = new ObjectInputStream( fis ); |
| 30 |
registry = (Hashtable) ois.readObject(); |
| 31 |
ois.close(); |
| 32 |
fis.close(); |
| 33 |
} catch(java.io.FileNotFoundException fnfe){ |
| 34 |
// nop |
| 35 |
} catch(Exception e){ |
| 36 |
e.printStackTrace(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public void save() { |
| 41 |
try { |
| 42 |
FileOutputStream fos = new FileOutputStream("bid.reg"); |
| 43 |
ObjectOutputStream oos = new ObjectOutputStream( fos ); |
| 44 |
oos.writeObject( registry ); |
| 45 |
oos.close(); |
| 46 |
fos.close(); |
| 47 |
} catch(Exception e){ |
| 48 |
e.printStackTrace(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
private Service find(Vector list, String name) { |
| 53 |
Enumeration e = list.elements(); |
| 54 |
while (e.hasMoreElements()) { |
| 55 |
Service s = (Service) e.nextElement(); |
| 56 |
if (s.getServiceName().equals(name)) return s; |
| 57 |
} |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Unregister a serivce |
| 63 |
* @param server name |
| 64 |
*/ |
| 65 |
public void Unregister(String name) { |
| 66 |
Enumeration e1 = registry.keys(); |
| 67 |
while (e1.hasMoreElements()) { |
| 68 |
Vector list = (Vector) registry.get(e1.nextElement()); |
| 69 |
Enumeration e2 = list.elements(); |
| 70 |
while (e2.hasMoreElements()) { |
| 71 |
Service s = (Service) e2.nextElement(); |
| 72 |
if (s.getServiceName().equals(name)) { |
| 73 |
list.remove(s); |
| 74 |
save(); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Register a new serivce |
| 82 |
* @param server name |
| 83 |
* @param url of endpoint |
| 84 |
* @param stype |
| 85 |
* @param wsdl |
| 86 |
*/ |
| 87 |
public void Register(String name, String url, String stype, String wsdl) { |
| 88 |
Vector list = (Vector)registry.get(stype); |
| 89 |
if (list == null) registry.put(stype, list=new Vector()); |
| 90 |
Service service = find(list, name); |
| 91 |
if (service==null) |
| 92 |
list.add(service=new Service()); |
| 93 |
service.setServiceName(name); |
| 94 |
service.setServiceUrl(url); |
| 95 |
service.setServiceType(stype); |
| 96 |
service.setServiceWsdl(wsdl); |
| 97 |
save(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Return the current list of services as an array |
| 102 |
* @param Service Name |
| 103 |
* @return List of servers that implement that service |
| 104 |
*/ |
| 105 |
public Service[] Lookup(String stype) { |
| 106 |
if (!registry.containsKey(stype)) return new Service[] {}; |
| 107 |
Vector list = (Vector)registry.get(stype); |
| 108 |
Service[] result = new Service[list.size()]; |
| 109 |
list.copyInto(result); |
| 110 |
return result; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* Return the current list of services as a string |
| 115 |
*/ |
| 116 |
public String LookupAsString(String stype) { |
| 117 |
Service[] services = Lookup(stype); |
| 118 |
String result = ""; |
| 119 |
for (int i=0; i<services.length; i++) { |
| 120 |
Service service = services[i]; |
| 121 |
result += service.getServiceName() + "\t" + |
| 122 |
service.getServiceUrl() + "\t" + |
| 123 |
service.getServiceType() + "\t" + |
| 124 |
service.getServiceWsdl() + "\n"; |
| 125 |
} |
| 126 |
return result; |
| 127 |
} |
| 128 |
|
| 129 |
} |