KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.core.ProActiveException;
36 import org.objectweb.proactive.core.node.Node;
37 import org.objectweb.proactive.core.node.NodeImpl;
38 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
39 import org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime;
40 import org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntimeAdapter;
41
42 import java.rmi.registry.LocateRegistry JavaDoc;
43 import java.rmi.registry.Registry JavaDoc;
44
45
46 /**
47  * This class talks to ProActive nodes
48  */

49 public class RMIHostNodeFinder implements HostNodeFinder {
50     static Logger log4jlogger = Logger.getLogger(RMIHostNodeFinder.class.getName());
51     private static final int DEFAULT_RMI_PORT = 1099;
52     private IC2DMessageLogger logger;
53
54     //
55
// -- CONSTRUCTORS -----------------------------------------------
56
//
57
public RMIHostNodeFinder(IC2DMessageLogger logger) {
58         this.logger = logger;
59     }
60
61     public RMIHostNodeFinder() {
62     }
63
64     //
65
// -- PUBLIC METHODS -----------------------------------------------
66
//
67
//
68
// -- implements HostNodeFinder -----------------------------------------------
69
//
70
public Node[] findNodes(String JavaDoc host) throws java.io.IOException JavaDoc {
71         // Try to determine the hostname
72
log("RMIHostNodeFinder findNodes for " + host);
73         String JavaDoc hostname = host;
74         int port = DEFAULT_RMI_PORT;
75         int pos = host.lastIndexOf(":");
76         if (pos != -1) {
77             // if the hostname is host:port
78
try {
79                 port = Integer.parseInt(host.substring(1 + pos));
80             } catch (NumberFormatException JavaDoc e) {
81                 port = DEFAULT_RMI_PORT;
82             }
83             hostname = host.substring(0, pos);
84         }
85         log("Trying " + hostname + ":" + port);
86
87         // Hook the registry
88
Registry JavaDoc registry = LocateRegistry.getRegistry(hostname, port);
89         return findNodes(registry, host);
90     }
91
92     //
93
// -- PRIVATE METHODS -----------------------------------------------
94
//
95
private Node[] findNodes(Registry JavaDoc registry, String JavaDoc host)
96         throws java.io.IOException JavaDoc {
97         // enumarate through the rmi binding on the registry
98
log("Listing bindings for " + registry);
99         String JavaDoc[] list = registry.list();
100         if (log4jlogger.isDebugEnabled()) {
101             log4jlogger.debug("list " + list.length);
102         }
103         if (list.length == 0) {
104             return new Node[0];
105         }
106         java.util.ArrayList JavaDoc nodes = new java.util.ArrayList JavaDoc(list.length);
107         for (int i = 0; i < list.length; i++) {
108             Object JavaDoc obj;
109
110             //-----------------added lines-------------------------------
111
if ((list[i].indexOf("PA_RT") == -1) &&
112                     (list[i].indexOf("SpyListenerNode") == -1) &&
113                     (list[i].indexOf("_VN") == -1)) {
114                 //-----------------added lines----------------------------
115
try {
116                     obj = registry.lookup(list[i]);
117                 } catch (java.rmi.NotBoundException JavaDoc e) {
118                     //ignore that item;
119
log(" registry.lookup of " + list[i] + " throwed ex=" + e);
120                     continue;
121                 }
122
123                 // If we've found a node..
124
//if (obj instanceof org.objectweb.proactive.core.node.rmi.RemoteNode) {
125
//---------------added lines ------------------------
126
if (obj instanceof org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime) {
127                     //--------------added lines --------------------------
128
log(" -> Found remote node " + list[i]);
129                     //Node realNode = NodeFactory.getNode(fullName);
130
try {
131                         ProActiveRuntime part = new RemoteProActiveRuntimeAdapter((RemoteProActiveRuntime) obj);
132                         String JavaDoc url = "//" + host + "/" + list[i];
133                         nodes.add(new NodeImpl(part, url, "rmi",
134                                 part.getJobID(url)));
135                     } catch (ProActiveException e) {
136                         log(
137                             "Error while trying to create a RuntimeAdapter for " +
138                             list[i] +
139                             ", check the version of ProActive or jdk");
140                     }
141                 }
142             }
143         }
144         Node[] nodeArray = new Node[nodes.size()];
145         if (nodes.size() > 0) {
146             nodeArray = (Node[]) nodes.toArray(nodeArray);
147         }
148         return nodeArray;
149     }
150
151     private void log(String JavaDoc s) {
152         if (logger != null) {
153             logger.log(s);
154         } else {
155             log4jlogger.info(s);
156         }
157     }
158
159     private void log(String JavaDoc s, Exception JavaDoc e) {
160         if (logger != null) {
161             logger.log(s, e);
162         } else {
163             log4jlogger.info(s);
164             e.printStackTrace();
165         }
166     }
167 }
168
Popular Tags