KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > bidbuy > RegistryService


1 package samples.bidbuy;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.ObjectInputStream JavaDoc;
6 import java.io.ObjectOutputStream JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.Hashtable JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 public class RegistryService {
12
13     private static Hashtable JavaDoc registry = new Hashtable JavaDoc();
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 JavaDoc fis = new FileInputStream JavaDoc("bid.reg");
29         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc( fis );
30         registry = (Hashtable JavaDoc) ois.readObject();
31         ois.close();
32         fis.close();
33       } catch(java.io.FileNotFoundException JavaDoc fnfe){
34         // nop
35
} catch(Exception JavaDoc e){
36         e.printStackTrace();
37       }
38     }
39
40     public void save() {
41       try {
42         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("bid.reg");
43         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc( fos );
44         oos.writeObject( registry );
45         oos.close();
46         fos.close();
47       } catch(Exception JavaDoc e){
48         e.printStackTrace();
49       }
50     }
51
52     private Service find(Vector JavaDoc list, String JavaDoc name) {
53         Enumeration JavaDoc 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 JavaDoc name) {
66         Enumeration JavaDoc e1 = registry.keys();
67         while (e1.hasMoreElements()) {
68             Vector JavaDoc list = (Vector JavaDoc) registry.get(e1.nextElement());
69             Enumeration JavaDoc 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 JavaDoc name, String JavaDoc url, String JavaDoc stype, String JavaDoc wsdl) {
88         Vector JavaDoc list = (Vector JavaDoc)registry.get(stype);
89         if (list == null) registry.put(stype, list=new Vector JavaDoc());
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 JavaDoc stype) {
106         if (!registry.containsKey(stype)) return new Service[] {};
107         Vector JavaDoc list = (Vector JavaDoc)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 JavaDoc LookupAsString(String JavaDoc stype) {
117         Service[] services = Lookup(stype);
118         String JavaDoc 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 }
130
Popular Tags