KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > runtime > jini > JiniRuntimeFactory


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.core.runtime.jini;
32
33 import net.jini.core.discovery.LookupLocator;
34 import net.jini.core.entry.Entry;
35 import net.jini.core.lookup.ServiceRegistrar;
36 import net.jini.core.lookup.ServiceTemplate;
37
38 import net.jini.lookup.entry.Name;
39
40 import org.objectweb.proactive.core.ProActiveException;
41 import org.objectweb.proactive.core.jini.ServiceLocatorHelper;
42 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
43 import org.objectweb.proactive.core.runtime.RuntimeFactory;
44 import org.objectweb.proactive.core.util.UrlBuilder;
45
46
47 public class JiniRuntimeFactory extends RuntimeFactory {
48     protected static int MAX_RETRY = 3;
49     private final static long WAITFOR = 100000L;
50     protected java.util.Random JavaDoc random;
51     protected static ServiceLocatorHelper serviceLocatorHelper = new ServiceLocatorHelper();
52     private static ProActiveRuntime defaultJiniRuntime = null;
53     private int count;
54
55     static {
56         if (JiniRuntimeFactory.class.getClassLoader() != null) {
57             if(logger.isDebugEnabled()){
58             logger.debug("JiniRuntimeFactory created with " +
59                 JiniRuntimeFactory.class.getClassLoader().getClass().getName());
60             }
61         }
62     }
63
64     //
65
// -- CONSTRUCTORS -----------------------------------------------
66
//
67
public JiniRuntimeFactory() throws java.io.IOException JavaDoc {
68         // Obligatoire d'avoir le security manager fixe
69
if ((System.getSecurityManager() == null) &&
70                 !("false".equals(System.getProperty("proactive.securitymanager")))) {
71             System.setSecurityManager(new java.rmi.RMISecurityManager JavaDoc());
72         }
73         random = new java.util.Random JavaDoc(System.currentTimeMillis());
74         //serviceLocatorHelper.initializeServiceLocator();
75
}
76
77     //
78
// -- PROTECTED METHODS -----------------------------------------------
79
//
80
protected synchronized ProActiveRuntime getProtocolSpecificRuntimeImpl()
81         throws ProActiveException {
82         //return createRuntimeAdapter(s,false);
83
if (defaultJiniRuntime == null) {
84             serviceLocatorHelper.initializeServiceLocator();
85             defaultJiniRuntime = createRuntimeAdapter();
86         }
87
88         return defaultJiniRuntime;
89     }
90
91     protected ProActiveRuntime getRemoteRuntimeImpl(String JavaDoc s)
92         throws ProActiveException {
93         logger.info("> JiniRuntimeFactory.getJiniRuntimeImpl(" + s + ")");
94         String JavaDoc host = null;
95         Entry[] entries;
96         JiniRuntime jiniRuntime = null;
97         LookupLocator lookup = null;
98         ServiceRegistrar registrar = null;
99         try {
100             host = UrlBuilder.getHostNameFromUrl(s);
101             logger.info("Try to find the service lookup on host: " + host);
102         } catch (java.net.UnknownHostException JavaDoc e) {
103             logger.fatal("Unable to locate host");
104             e.printStackTrace();
105         }
106
107         //ServiceDiscoveryManager clientMgr = null;
108
if (host != null) {
109             // recherche unicast
110
try {
111                 lookup = new LookupLocator("jini://" + host);
112                 logger.info("Service lookup found ");
113                 registrar = lookup.getRegistrar();
114             } catch (java.net.MalformedURLException JavaDoc e) {
115                 throw new ProActiveException("Lookup failed: " +
116                     e.getMessage());
117             } catch (java.io.IOException JavaDoc e) {
118                 logger.error("Registrar search failed: " + e.getMessage());
119                 if (MAX_RETRY-- > 0) {
120                     logger.info(
121                         "failed to contact the service lookup, retrying ...");
122                     getRemoteRuntimeImpl(s);
123                 } else {
124                     throw new ProActiveException(
125                         "Cannot contact the lookup service for node " + s);
126                 }
127             } catch (java.lang.ClassNotFoundException JavaDoc e) {
128                 throw new ProActiveException("Registrar search failed: " +
129                     e.toString());
130             }
131             Class JavaDoc[] classes = new Class JavaDoc[] { JiniRuntime.class };
132
133             entries = new Entry[] { new Name(s) };
134
135             ServiceTemplate template = new ServiceTemplate(null, classes,
136                     entries);
137             try {
138                 jiniRuntime = (JiniRuntime) registrar.lookup(template);
139                 if (jiniRuntime == null) {
140                     logger.info("No service found for url: " + s);
141                 }
142                 return createRuntimeAdapter(jiniRuntime);
143             } catch (java.rmi.RemoteException JavaDoc e) {
144                 throw new ProActiveException(e);
145                 //e.printStackTrace();
146
}
147         } else {
148             throw new ProActiveException("node url should not be null");
149         }
150
151         // try {
152
// // recherche multicast
153
// LookupDiscoveryManager mgr = new LookupDiscoveryManager(LookupDiscovery.ALL_GROUPS,
154
// null,null);
155
//
156
// clientMgr = new ServiceDiscoveryManager(mgr,new LeaseRenewalManager());
157
// } catch (Exception e) {
158
// throw new ProActiveException("Remote",e);
159
// }
160
// Class[] classes = new Class[] {JiniRuntime.class};
161
// construction de la template pour la recherche
162
// on peut ne pas mettre le nom de l'objet
163
// ensuite on recherche une certaine classe d'objet
164
// ServiceItem item = null;
165
// try {
166
// item = clientMgr.lookup(template, null, WAITFOR);
167
// } catch (Exception e) {
168
// throw new ProActiveException("Remote",e);
169
// }
170
//
171
// if (item == null) {
172
// System.out.println("no service found");
173
// return null;
174
// } else {
175
// jiniRuntime = (JiniRuntime) item.service;
176
// if (jiniRuntime == null) {
177
// System.out.println("item null");
178
// return null;
179
// }
180
// return createRuntimeAdapter(jiniRuntime);
181
// }
182
}
183
184     protected JiniRuntimeAdapter createRuntimeAdapter(JiniRuntime jiniRuntime)
185         throws ProActiveException {
186         return new JiniRuntimeAdapter(jiniRuntime);
187     }
188
189     protected JiniRuntimeAdapter createRuntimeAdapter()
190         throws ProActiveException {
191         return new JiniRuntimeAdapter();
192     }
193
194     public static void setMulticastLocator(boolean multicastLocator) {
195         serviceLocatorHelper.setMulticastLocator(multicastLocator);
196     }
197 }
198
Popular Tags