KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > util > JiniNodeListener


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.ic2d.util;
32
33 import net.jini.core.discovery.LookupLocator;
34 import net.jini.core.lookup.ServiceMatches;
35 import net.jini.core.lookup.ServiceRegistrar;
36 import net.jini.core.lookup.ServiceTemplate;
37
38 import net.jini.discovery.DiscoveryEvent;
39 import net.jini.discovery.DiscoveryListener;
40 import net.jini.discovery.LookupDiscovery;
41
42 import org.apache.log4j.Logger;
43
44 import org.objectweb.proactive.core.node.NodeImpl;
45 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
46 import org.objectweb.proactive.core.runtime.VMInformation;
47 import org.objectweb.proactive.core.runtime.jini.JiniRuntime;
48 import org.objectweb.proactive.core.runtime.jini.JiniRuntimeAdapter;
49
50 import java.rmi.RMISecurityManager JavaDoc;
51
52
53 public class JiniNodeListener implements DiscoveryListener {
54     static Logger log4jlogger = Logger.getLogger(JiniNodeListener.class.getName());
55     protected java.util.ArrayList JavaDoc nodes = new java.util.ArrayList JavaDoc();
56     private String JavaDoc host;
57     private IC2DMessageLogger logger;
58
59     public JiniNodeListener() {
60         this(null);
61     }
62
63     public JiniNodeListener(String JavaDoc _host) {
64         host = _host;
65         if ((System.getSecurityManager() == null) &&
66                 !("false".equals(System.getProperty("proactive.securitymanager")))) {
67             System.setSecurityManager(new RMISecurityManager JavaDoc());
68         }
69
70         // this.logger = logger;
71
LookupDiscovery discover = null;
72         LookupLocator lookup = null;
73         if (host != null) {
74             // recherche unicast
75
try {
76                 lookup = new LookupLocator("jini://" + host);
77                 // System.out.println("Lookup.getRegistrar() on " + host);
78
ServiceRegistrar registrar = lookup.getRegistrar();
79                 Class JavaDoc[] classes = new Class JavaDoc[] { JiniRuntime.class };
80                 JiniRuntime runtime = null;
81                 ServiceMatches matches = null;
82                 VMInformation info = null;
83                 ServiceTemplate template = new ServiceTemplate(null, classes,
84                         null);
85
86                 //System.out.println("JiniNodeListener: lookup registrar");
87
matches = registrar.lookup(template, Integer.MAX_VALUE);
88                 if (matches.totalMatches > 0) {
89                     //System.out.println("matches "+matches.items.length);
90
for (int i = 0; i < matches.items.length; i++) {
91                         //check if it is really a node and not a ProactiveRuntime
92
String JavaDoc jiniName = matches.items[i].attributeSets[0].toString();
93
94                         //System.out.println("name of the node "+jiniName);
95
if ((jiniName.indexOf("PA_RT") == -1) &&
96                                 (jiniName.indexOf("_VN") == -1)) {
97                             // it is a node
98
int k = jiniName.indexOf("=");
99                             String JavaDoc name = jiniName.substring(k + 1,
100                                     jiniName.length() - 1);
101
102                             //System.out.println("-----------------name "+name);
103
try {
104                                 if (matches.items[i].service == null) {
105                                     log4jlogger.warn("Service : NULL !!!");
106                                 } else {
107                                     runtime = (JiniRuntime) matches.items[i].service;
108                                     //System.out.println("JiniNodeListener: node "+node);
109
info = runtime.getVMInformation();
110                                     if (info != null) {
111                                         //System.out.println("JiniNodeListener: Node name "+info.getName());
112
//System.out.println("JiniNodeListener: Inet Address "+info.getInetAddress());
113
try {
114                                             //System.out.println("JiniNodeListener: on gere le host");
115
//System.out.println("host non null: "+host+" "+info.getInetAddress().getHostName());
116
//if (info.getInetAddress().getHostName().equals(host)){
117
if ((info.getInetAddress()).equals(
118                                                         java.net.InetAddress.getByName(
119                                                             host))) {
120                                                 ProActiveRuntime part = new JiniRuntimeAdapter(runtime);
121
122                                                 //System.out.println("JiniNodeListener: ajout du noeud pour le host "+host);
123
nodes.add(new NodeImpl(part,
124                                                         name, "jini",
125                                                         part.getJobID(name)));
126                                             }
127                                         } catch (java.net.UnknownHostException JavaDoc e) {
128                                             log4jlogger.error("Unknown host " +
129                                                 host);
130                                         } catch (org.objectweb.proactive.core.ProActiveException e) {
131                                             e.printStackTrace();
132                                         }
133                                     }
134                                 }
135                             } catch (java.rmi.ConnectException JavaDoc e) {
136                                 log4jlogger.error(
137                                     "JiniNodeListener ConnectException ");
138                                 //e.printStackTrace();
139
continue;
140                                 //e.printStackTrace();
141
}
142                         }
143                     }
144                 } else {
145                     log4jlogger.error("JiniNodeListener: No JiniNode");
146                 }
147             } catch (java.net.MalformedURLException JavaDoc e) {
148                 log4jlogger.error("Lookup failed: " + e.getMessage());
149             } catch (java.io.IOException JavaDoc e) {
150                 log4jlogger.error("Registrar search failed: " + e.getMessage());
151             } catch (java.lang.ClassNotFoundException JavaDoc e) {
152                 log4jlogger.error("Class Not Found: " + e.getMessage());
153             }
154         } else {
155             //recherche multicast
156
try {
157                 discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS);
158             } catch (Exception JavaDoc e) {
159                 log4jlogger.fatal(" JiniNodeFinder exception");
160                 e.printStackTrace();
161             }
162
163             discover.addDiscoveryListener(this);
164         }
165     }
166
167     public void discovered(DiscoveryEvent evt) {
168         ServiceRegistrar[] registrars = evt.getRegistrars();
169
170         //System.out.println("registar lenght :"+registrars.length);
171
Class JavaDoc[] classes = new Class JavaDoc[] { JiniRuntime.class };
172         JiniRuntime runtime = null;
173         ServiceMatches matches = null;
174         ServiceTemplate template = new ServiceTemplate(null, classes, null);
175         VMInformation info = null;
176
177         for (int n = 0; n < registrars.length; n++) {
178             //System.out.println("JiniNodeListener: Service found");
179
ServiceRegistrar registrar = registrars[n];
180             try {
181                 //System.out.println("JiniNodeListener: lookup registrar");
182
matches = registrar.lookup(template, Integer.MAX_VALUE);
183                 if (matches.totalMatches > 0) {
184                     //System.out.println("matches "+matches.items.length);
185
for (int i = 0; i < matches.items.length; i++) {
186                         //check if it is really a node and not a ProactiveRuntime
187
String JavaDoc jiniName = matches.items[i].attributeSets[0].toString();
188
189                         //System.out.println("name of the node "+jiniName);
190
if ((jiniName.indexOf("PA_RT") == -1) &&
191                                 (jiniName.indexOf("_VN") == -1)) {
192                             // it is a node
193
int k = jiniName.indexOf("=");
194                             String JavaDoc name = jiniName.substring(k + 1,
195                                     jiniName.length() - 1);
196
197                             //System.out.println("-----------------name "+name);
198
try {
199                                 if (matches.items[i].service == null) {
200                                     log4jlogger.warn("Service : NULL !!!");
201                                 } else {
202                                     runtime = (JiniRuntime) matches.items[i].service;
203                                     //System.out.println("JiniNodeListener: node "+node);
204
info = runtime.getVMInformation();
205                                     if (info != null) {
206                                         //System.out.println("JiniNodeListener: Node name "+info.getName());
207
//System.out.println("JiniNodeListener: Inet Address "+info.getInetAddress());
208
try {
209                                             //System.out.println("JiniNodeListener: on gere le host");
210
if (host != null) {
211                                                 //System.out.println("host non null: "+host+" "+info.getInetAddress().getHostName());
212
//if (info.getInetAddress().getHostName().equals(host)){
213
try {
214                                                     if ((info.getInetAddress()).equals(
215                                                                 java.net.InetAddress.getByName(
216                                                                     host))) {
217                                                                         ProActiveRuntime part = new JiniRuntimeAdapter(runtime);
218                                                         //System.out.println("JiniNodeListener: ajout du noeud pour le host "+host);
219
nodes.add(new NodeImpl(
220                                                                 part,
221                                                                 name, "jini",part.getJobID(name)));
222                                                     }
223                                                 } catch (java.net.UnknownHostException JavaDoc e) {
224                                                     log4jlogger.error(
225                                                         "Unknown host " + host);
226                                                 }
227                                             } else {
228                                                 //System.out.println("host null: ");
229
//System.out.println("JiniNodeListener: ajout du noeud");
230
ProActiveRuntime part = new JiniRuntimeAdapter(runtime);
231                                                 nodes.add(new NodeImpl(
232                                                         part, name,
233                                                         "jini",part.getJobID(name)));
234                                             }
235                                         } catch (org.objectweb.proactive.core.ProActiveException e) {
236                                             e.printStackTrace();
237                                         }
238                                     }
239                                 }
240                             } catch (java.rmi.ConnectException JavaDoc e) {
241                                 log4jlogger.error(
242                                     "JiniNodeListener ConnectException ");
243                                 //e.printStackTrace();
244
continue;
245                                 //e.printStackTrace();
246
}
247                         }
248                     }
249                 } else {
250                     log4jlogger.error("JiniNodeListener: No JiniNode");
251                 }
252             } catch (java.rmi.RemoteException JavaDoc e) {
253                 //System.err.println("JiniNodeListener RemoteException ");
254
continue;
255                 //e.printStackTrace();
256
}
257         }
258
259         //System.out.println("JiniNodeListener: Fin recherche multicast");
260
}
261
262     public void discarded(DiscoveryEvent evt) {
263     }
264
265     public java.util.ArrayList JavaDoc getNodes() {
266         return nodes;
267     }
268
269     public static void main(String JavaDoc[] args) {
270         JiniNodeListener jnf = new JiniNodeListener(null);
271
272         // stay around long enough to receive replies
273
try {
274             Thread.sleep(100000L);
275         } catch (java.lang.InterruptedException JavaDoc e) {
276             // do nothing
277
}
278     }
279 }
280
Popular Tags