001: package samples.bidbuy;
002:
003: import java.io.FileInputStream;
004: import java.io.FileOutputStream;
005: import java.io.ObjectInputStream;
006: import java.io.ObjectOutputStream;
007: import java.util.Enumeration;
008: import java.util.Hashtable;
009: import java.util.Vector;
010:
011: public class RegistryService {
012:
013: private static Hashtable registry = new Hashtable();
014:
015: /**
016: * Find a named service in a list
017: * @param list of services
018: * @param name to search for
019: * @return service found (or null)
020: */
021:
022: public RegistryService() {
023: load();
024: }
025:
026: public void load() {
027: try {
028: FileInputStream fis = new FileInputStream("bid.reg");
029: ObjectInputStream ois = new ObjectInputStream(fis);
030: registry = (Hashtable) ois.readObject();
031: ois.close();
032: fis.close();
033: } catch (java.io.FileNotFoundException fnfe) {
034: // nop
035: } catch (Exception e) {
036: e.printStackTrace();
037: }
038: }
039:
040: public void save() {
041: try {
042: FileOutputStream fos = new FileOutputStream("bid.reg");
043: ObjectOutputStream oos = new ObjectOutputStream(fos);
044: oos.writeObject(registry);
045: oos.close();
046: fos.close();
047: } catch (Exception e) {
048: e.printStackTrace();
049: }
050: }
051:
052: private Service find(Vector list, String name) {
053: Enumeration e = list.elements();
054: while (e.hasMoreElements()) {
055: Service s = (Service) e.nextElement();
056: if (s.getServiceName().equals(name))
057: return s;
058: }
059: return null;
060: }
061:
062: /**
063: * Unregister a serivce
064: * @param server name
065: */
066: public void Unregister(String name) {
067: Enumeration e1 = registry.keys();
068: while (e1.hasMoreElements()) {
069: Vector list = (Vector) registry.get(e1.nextElement());
070: Enumeration e2 = list.elements();
071: while (e2.hasMoreElements()) {
072: Service s = (Service) e2.nextElement();
073: if (s.getServiceName().equals(name)) {
074: list.remove(s);
075: save();
076: }
077: }
078: }
079: }
080:
081: /**
082: * Register a new serivce
083: * @param server name
084: * @param url of endpoint
085: * @param stype
086: * @param wsdl
087: */
088: public void Register(String name, String url, String stype,
089: String wsdl) {
090: Vector list = (Vector) registry.get(stype);
091: if (list == null)
092: registry.put(stype, list = new Vector());
093: Service service = find(list, name);
094: if (service == null)
095: list.add(service = new Service());
096: service.setServiceName(name);
097: service.setServiceUrl(url);
098: service.setServiceType(stype);
099: service.setServiceWsdl(wsdl);
100: save();
101: }
102:
103: /**
104: * Return the current list of services as an array
105: * @param Service Name
106: * @return List of servers that implement that service
107: */
108: public Service[] Lookup(String stype) {
109: if (!registry.containsKey(stype))
110: return new Service[] {};
111: Vector list = (Vector) registry.get(stype);
112: Service[] result = new Service[list.size()];
113: list.copyInto(result);
114: return result;
115: }
116:
117: /*
118: * Return the current list of services as a string
119: */
120: public String LookupAsString(String stype) {
121: Service[] services = Lookup(stype);
122: String result = "";
123: for (int i = 0; i < services.length; i++) {
124: Service service = services[i];
125: result += service.getServiceName() + "\t"
126: + service.getServiceUrl() + "\t"
127: + service.getServiceType() + "\t"
128: + service.getServiceWsdl() + "\n";
129: }
130: return result;
131: }
132:
133: }
|