KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > p2p > registry > ServiceFinder


1 /*
2  * Created on Jan 29, 2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.objectweb.proactive.p2p.registry;
8
9 import java.io.*;
10 import java.rmi.*;
11 import java.util.*;
12 import net.jini.discovery.*;
13 import net.jini.core.lookup.*;
14 import net.jini.core.entry.*;
15
16 public class ServiceFinder implements DiscoveryListener {
17         private static String JavaDoc[] publicGroup = new String JavaDoc[] { "" };
18         private Vector returnObject = new Vector();
19
20         private LookupDiscovery reg;
21         private ServiceTemplate template;
22
23         public ServiceFinder(Class JavaDoc serviceInterface) throws IOException {
24             this(publicGroup, serviceInterface, (Entry[])null);
25         }
26
27         public ServiceFinder(Class JavaDoc serviceInterface, Entry attribute)
28                              throws IOException {
29             this(publicGroup, serviceInterface, new Entry[] { attribute });
30         }
31
32         public ServiceFinder(Class JavaDoc serviceInterface, Entry[] attributes)
33                              throws IOException {
34             this(publicGroup, serviceInterface, attributes);
35         }
36
37         public ServiceFinder(String JavaDoc[] groups, Class JavaDoc serviceInterface,
38                              Entry[] attributes) throws IOException {
39             // Construct the template here for matching in the lookup service
40
// We don't use the template until we actually discover a service
41
Class JavaDoc[] name = new Class JavaDoc[] { serviceInterface };
42             template = new ServiceTemplate(null, name, attributes);
43
44             // Create the facility to perform multicast discovery for all
45
// lookup services
46
reg = new LookupDiscovery(groups);
47             reg.addDiscoveryListener(this);
48         }
49
50         // Automatically called when a lookup service is discovered
51
// (the listener callback of the addDiscoveryListener method)
52
public synchronized void discovered(DiscoveryEvent dev) {
53             ServiceRegistrar[] lookup = dev.getRegistrars();
54             // We may have discovered one or more lookup services
55
for (int i = 0; i < lookup.length; i++) {
56                 try {
57                     ServiceMatches items =
58                         lookup[i].lookup(template, Integer.MAX_VALUE);
59                     // Each lookup service may have zero or more registered
60
// servers that implement our desired template
61
for (int j = 0; j < items.items.length; j++) {
62                         if (items.items[j].service != null)
63                             // Put each matching service into our vector
64
returnObject.addElement(items.items[j]);
65                 // else the service item couldn't be deserialized
66
// so the lookup() method skipped it
67
}
68                         notifyAll();
69                 } catch (RemoteException ex) {
70                     System.err.println("[Service Finder] ServiceFinder Error: " + ex);
71                 }
72             }
73         }
74
75 /************* DISCARDED ***************/
76         public synchronized void discarded(DiscoveryEvent dev) {
77         }
78
79
80 /************** GET OBJECT ****************/
81
82         // This class is to be used by the client. It will return only
83
// the first service object that satisfies the template request.
84
public synchronized Object JavaDoc getObject() {
85             while (returnObject.size() == 0) {
86                 try {
87                     wait();
88                 } catch (InterruptedException JavaDoc ex) {
89                 };
90             }
91 // if (returnObject.size() == 0) return null;
92
return ((ServiceItem)returnObject.elementAt(0)).service;
93         }
94
95 /*************** ERROR HANDLE ********************/
96
97         // If an error is encountered when using a service object, the client
98
// shoud call this method.
99
// A new object can then be gotten from the getObject() method.
100
public synchronized void errored(Object JavaDoc obj) {
101             if ((obj != null) && (returnObject.size() != 0)) {
102                 if (obj.equals(((ServiceItem)returnObject.elementAt(0)).service)) {
103                     returnObject.removeElementAt(0);
104                 }
105             }
106         }
107     }
108
109
Popular Tags