KickJava   Java API By Example, From Geeks To Geeks.

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


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

49 public class IbisHostNodeFinder implements HostNodeFinder {
50
51 static Logger log4jlogger = Logger.getLogger(IbisHostNodeFinder.class.getName());
52
53 static {
54     IbisProperties.load();
55 }
56
57   private static final int DEFAULT_RMI_PORT = 1099;
58   private IC2DMessageLogger logger;
59   
60   //
61
// -- CONSTRUCTORS -----------------------------------------------
62
//
63

64   public IbisHostNodeFinder(IC2DMessageLogger logger) {
65     this.logger = logger;
66   }
67   
68   public IbisHostNodeFinder() {
69   }
70   
71   
72   //
73
// -- PUBLIC METHODS -----------------------------------------------
74
//
75

76   //
77
// -- implements HostNodeFinder -----------------------------------------------
78
//
79

80   public Node[] findNodes(String JavaDoc host) throws java.io.IOException JavaDoc {
81     // Try to determine the hostname
82
log("RMIHostNodeFinder findNodes for " + host);
83     String JavaDoc hostname = host;
84     int port = DEFAULT_RMI_PORT;
85     int pos = host.lastIndexOf(":");
86     if (pos != -1) {
87       // if the hostname is host:port
88
try {
89         port = Integer.parseInt(host.substring(1 + pos));
90       } catch (NumberFormatException JavaDoc e) {
91         port = DEFAULT_RMI_PORT;
92       }
93       hostname = host.substring(0, pos);
94     }
95     log("Trying " + hostname + ":" + port);
96
97     // Hook the registry
98
Registry registry = LocateRegistry.getRegistry(hostname, port);
99     return findNodes(registry,host);
100   }
101   
102   
103   //
104
// -- PRIVATE METHODS -----------------------------------------------
105
//
106

107   private Node[] findNodes(Registry registry, String JavaDoc host) throws java.io.IOException JavaDoc {
108     // enumarate through the rmi binding on the registry
109
log("Listing bindings for " + registry);
110     String JavaDoc[] list = registry.list();
111     if(log4jlogger.isDebugEnabled()){
112     log4jlogger.debug("list " + list.length);
113     }
114     if (list.length == 0) {
115       return new Node[0];
116     }
117     java.util.ArrayList JavaDoc nodes = new java.util.ArrayList JavaDoc(list.length);
118     for (int i = 0; i < list.length; i++) {
119       Object JavaDoc obj;
120       //-----------------added lines-------------------------------
121
if(list[i].indexOf("PA_RT")== -1 && list[i].indexOf("SpyListenerNode") == -1 && list[i].indexOf("_VN")== -1){
122       //-----------------added lines----------------------------
123
try {
124         obj = registry.lookup(list[i]);
125       } catch (NotBoundException e) {
126         //ignore that item;
127
log(" registry.lookup of "+list[i]+" throwed ex=" + e);
128         continue;
129       }
130       // If we've found a node..
131
//if (obj instanceof org.objectweb.proactive.core.node.rmi.RemoteNode) {
132
//---------------added lines ------------------------
133
if (obj instanceof org.objectweb.proactive.core.runtime.ibis.RemoteProActiveRuntime) {
134       //--------------added lines --------------------------
135
log(" -> Found remote node " + list[i]);
136         //Node realNode = NodeFactory.getNode(fullName);
137
try {
138             ProActiveRuntime part = new RemoteProActiveRuntimeAdapter((RemoteProActiveRuntime)obj);
139             String JavaDoc url = "//"+host+"/"+list[i];
140           nodes.add(new NodeImpl(part,url,"rmi", url));
141         } catch (ProActiveException e) {
142           log("Error while trying to create a RuntimeAdapter for "+list[i]+", check the version of ProActive or jdk");
143         }
144       }
145      }
146     }
147     Node[] nodeArray = new Node[nodes.size()];
148     if (nodes.size() > 0) {
149       nodeArray = (Node[]) nodes.toArray(nodeArray);
150     }
151     return nodeArray;
152   }
153   
154   
155   private void log(String JavaDoc s) {
156     if (logger != null) {
157       logger.log(s);
158     } else {
159       log4jlogger.info(s);
160     }
161   }
162   
163   private void log(String JavaDoc s, Exception JavaDoc e) {
164     if (logger != null) {
165       logger.log(s, e);
166     } else {
167       log4jlogger.info(s);
168       e.printStackTrace();
169     }
170   }
171 }
172
Popular Tags