KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > smi > Finder


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.smi;
29
30
31 import java.util.Observable JavaDoc;
32 import java.util.TreeMap JavaDoc;
33 import java.util.SortedMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import org.objectweb.mobilitools.smi.api.Constants;
39 import org.objectweb.mobilitools.smi.idl.*;
40 import org.objectweb.mobilitools.util.corba.NameService;
41 import org.objectweb.mobilitools.util.corba.NameServiceException;
42 import org.omg.CfMAF.*;
43 import org.omg.CORBA.ORB JavaDoc;
44 import org.omg.CORBA.Any JavaDoc;
45
46
47 /**
48  * MobiliTools $Name: $, $Id: Finder.java,v 1.1.1.1 2003/03/28 14:48:05 dillense Exp $
49  * <P>
50  * Each instance launches a new implementation of the MAFFinder interface for a given region.
51  * The class may also be directly run to launch a single MAFFinder.
52  * In SMI, MAFFinder implementations rely on the CORBA naming service to publish their object
53  * reference.
54  * Only one MAFFinder can be launched for a given region.
55  * @see Constants
56  * @see MAFFinder_impl
57  * @see org.objectweb.mobilitools.smi.goodies.FinderGUI
58 */

59 public class Finder extends Observable JavaDoc
60 {
61     static boolean standalone = false;
62
63
64     /**
65         Launches a MAFFinder for the given region, and registers its object
66         reference in the default CORBA naming service.
67         @param args the last argument must specify the region name
68         @see #Finder(String, ORB)
69     */

70     static public void main(String JavaDoc args[])
71     {
72         ORB JavaDoc orb = ORB.init(args, null);
73         if (args.length == 0)
74         {
75             System.err.println("argument required: region_name");
76             System.exit(-1);
77         }
78         else
79         {
80             standalone = true;
81             new Finder(args[args.length-1], orb);
82             System.out.println("MAFFinder for region " + args[0] + " is ready.");
83             orb.run();
84         }
85     }
86
87
88     // string'fied name in the CORBA naming service
89
String JavaDoc my_ns_name;
90
91     // name of the region managed by current Finder
92
String JavaDoc my_region;
93
94     // object implementing the MAFFinder interface
95
_MAFFinderComplementImplBase my_finderTie;
96
97     // frontend to CORBA naming service
98
NameService my_ns;
99
100     // reference to associated ORB object
101
ORB JavaDoc my_orb;
102
103     // ordered list of Finder entries (agents and agent systems)
104
SortedMap JavaDoc entries;
105
106
107     /**
108         Launches a MAFFinder for the given region, and registers its object
109         reference in the default CORBA naming service.
110         @param region the name of the region to manage.
111         @see Constants
112     */

113     public Finder(String JavaDoc region)
114     {
115         this(region, ORB.init(new String JavaDoc[0], System.getProperties()));
116     }
117
118
119     /**
120         Launches a MAFFinder for the given region, and registers its object
121         reference in the default CORBA naming service.
122         @param region the name of the region to manage.
123         @param orb the ORB object to use.
124         @see Constants
125         @see org.omg.CORBA.ORB#init()
126     */

127     public Finder(String JavaDoc region, ORB JavaDoc orb)
128     {
129         entries = new TreeMap JavaDoc();
130         my_region = region;
131         my_orb = orb;
132         my_finderTie = new MAFFinder_impl(this);
133         orb.connect(my_finderTie);
134         my_ns_name =
135             System.getProperty(Constants.regionPrefixProp, Constants.regionPrefixDefault) +
136             "/" +
137             region +
138             "/" +
139             System.getProperty(Constants.finderNameProp, Constants.finderNameDefault);
140         try
141         {
142             my_ns = new NameService(orb);
143             my_ns.rebind(my_ns_name, my_finderTie);
144         }
145         catch (NameServiceException e)
146         {
147             e.printStackTrace();
148             System.err.println("Exception with naming service: unable to bind name " + my_ns_name);
149             System.err.println(e.toString());
150         }
151     }
152
153
154     /**
155         @return the name of the region managed by current Finder.
156     */

157     public String JavaDoc getRegion()
158     {
159         return my_region;
160     }
161
162
163     /**
164         Shuts the finder.
165         @return 0 if the MAFFinder object reference could be canceled from the CORBA naming service,
166         -1 otherwise.
167     */

168     public int exit()
169     {
170         int exitcode = -1;
171         try
172         {
173             my_ns.unbind(my_ns_name);
174             my_orb.disconnect(my_finderTie);
175             exitcode = 0;
176         }
177         catch (NameServiceException exception)
178         {
179             System.err.println(exception.toString());
180         }
181         if (standalone)
182         {
183             System.exit(exitcode);
184         }
185         return(exitcode);
186     }
187
188
189     ///////////////////////////////////////////////////////////
190
// actual implementation of MAFFinder-related operations //
191
///////////////////////////////////////////////////////////
192

193
194     public void register(FinderEntry entry)
195         throws NameInvalid
196     {
197         org.objectweb.mobilitools.smi.api.Name key = entry.getName();
198         if (key.getmafname().identity.length == 0)
199         {
200             throw new NameInvalid();
201         }
202         synchronized(entries)
203         {
204             if (entries.containsKey(key))
205             {
206                 if (entry.isAgent())
207                 {
208                     entries.remove(key);
209                 }
210                 else
211                 {
212                     throw new NameInvalid();
213                 }
214             }
215             entries.put(key, entry);
216             if (countObservers() > 0)
217             {
218                 setChanged();
219                 try
220                 {
221                     notifyObservers(entries.values().toArray(new FinderEntry[0]));
222                 }
223                 catch (Throwable JavaDoc e)
224                 {
225                     e.printStackTrace();
226                 }
227             }
228         }
229     }
230
231
232     public Name[] lookup_names(Object JavaDoc profile)
233     {
234         Name[] result;
235         Object JavaDoc names = lookup(null, null, profile, true);
236         if (names instanceof Name[])
237         {
238             result = (Name[])names;
239         }
240         else
241         {
242             result = new Name[0];
243         }
244         return result;
245     }
246
247
248     public String JavaDoc[] lookup_locations(org.objectweb.mobilitools.smi.api.Name name, String JavaDoc location, Object JavaDoc profile)
249         throws EntryNotFound
250     {
251         String JavaDoc[] result;
252         Object JavaDoc locations = lookup(name, location, profile, false);
253         if (locations instanceof String JavaDoc[])
254         {
255             result = (String JavaDoc[])locations;
256             if (result.length == 0)
257             {
258                 throw new EntryNotFound();
259             }
260         }
261         else
262         {
263             throw new EntryNotFound();
264         }
265         return result;
266     }
267
268
269     private Object JavaDoc lookup(org.objectweb.mobilitools.smi.api.Name name, String JavaDoc location, Object JavaDoc profile, boolean returnNames)
270     {
271         Object JavaDoc result = null;
272         if (name == null || name.getmafname().identity.length == 0)
273         {
274             synchronized (entries)
275             {
276                 Iterator JavaDoc all = entries.values().iterator();
277                 Vector JavaDoc select = new Vector JavaDoc();
278                 while (all.hasNext())
279                 {
280                     FinderEntry element = (FinderEntry)all.next();
281                     if (Misc.matchProfile(profile, element.getProfile()) &&
282                         (location == null || location.length() == 0 || location.equals(element.getLocation())))
283                     {
284                         if (returnNames)
285                         {
286                             select.add(element.getName().getmafname());
287                         }
288                         else
289                         {
290                             select.add(element.getLocation());
291                         }
292                     }
293                 }
294                 if (returnNames)
295                 {
296                     result = new Name[select.size()];
297                     result = (Name[])(select.toArray((Name[])result));
298                 }
299                 else
300                 {
301                     result = new String JavaDoc[select.size()];
302                     result = (String JavaDoc[])select.toArray((String JavaDoc[])result);
303                 }
304             }
305         }
306         else
307         {
308             FinderEntry element = (FinderEntry)entries.get(name);
309             if (element != null)
310             {
311                 result = new String JavaDoc[] { element.getLocation() };
312             }
313         }
314         return result;
315     }
316
317
318     public void unregister(org.objectweb.mobilitools.smi.api.Name name)
319         throws EntryNotFound
320     {
321         if (entries.remove(name) == null)
322         {
323             throw new EntryNotFound();
324         }
325         else if (countObservers() > 0)
326         {
327             setChanged();
328             notifyObservers(entries.values().toArray(new FinderEntry[0]));
329         }
330     }
331
332
333     public AgentProfile getAgentProfile(org.objectweb.mobilitools.smi.api.Name name)
334         throws AgentNotFound
335     {
336         AgentProfile result = null;
337         FinderEntry entry = (FinderEntry)entries.get(name);
338         if (entry != null && entry.getProfile() instanceof AgentProfile)
339         {
340             result = (AgentProfile)entry.getProfile();
341         }
342         if (result == null)
343         {
344             throw new AgentNotFound();
345         }
346         else
347         {
348             return result;
349         }
350     }
351
352
353     public void printEntries()
354     {
355         Iterator JavaDoc values = entries.values().iterator();
356         while (values.hasNext())
357         {
358             FinderEntry entry = (FinderEntry)values.next();
359             System.out.println(entry.toString() + ", location " + entry.getLocation());
360         }
361     }
362
363
364     public ORB JavaDoc getORB()
365     {
366         return my_orb;
367     }
368 }
369
Popular Tags