KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.omg.CORBA.*;
32 import org.omg.CfMAF.*;
33 import java.lang.reflect.*;
34 import java.lang.Object JavaDoc;
35 import java.io.ByteArrayInputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38
39 import org.objectweb.mobilitools.smi.api.*;
40 import org.objectweb.mobilitools.smi.idl.*;
41 import org.objectweb.mobilitools.smi.lib.ClassCache;
42
43
44 /**
45  * MobiliTools $Name: $, $Id: MAFAgentSystem_impl.java,v 1.1.1.1 2003/03/28 14:48:05 dillense Exp $
46  * <P>
47  * Implementation of MAF's MAFAgentSystem interface (always bound to an Agency instance).
48  * @see Agency
49 */

50 public class MAFAgentSystem_impl extends _MAFAgentSystemComplementImplBase
51     implements MAFAgentSystemOperations, MAFAgentSystemComplementOperations
52 {
53     Agency my_agency;
54     boolean trace;
55
56
57     /**
58         Creates a new implementation of the MAFAgentSystem MAF interface.
59         Every invocation is (more or less directly) delegated to a SMI agency.
60         @param agency the bound Agency instance.
61     */

62     public MAFAgentSystem_impl(Agency agency)
63     {
64         this(agency, false);
65     }
66
67
68     /**
69         Creates Creates a new implementation of the MAFAgentSystem MAF interface.
70         Every invocation is (more or less directly) delegated to a SMI agency.
71         @param agency the bound Agency instance.
72         @param traceOn must be true to switch call trace on
73     */

74     public MAFAgentSystem_impl(Agency agency, boolean traceOn)
75     {
76         my_agency = agency;
77         trace = traceOn;
78     }
79
80
81     /////////////////////////////////////////////////////////
82
// implementation of MAF's MAFAgentSystem operations //
83
/////////////////////////////////////////////////////////
84

85
86     /**
87         Creates an agent.
88     */

89     public org.omg.CfMAF.Name create_agent(
90         org.omg.CfMAF.Name agent_name,
91         AgentProfile agent_profile,
92         byte[] agent,
93         String JavaDoc place_name,
94         byte[] arguments,
95         ClassName[] class_names,
96         String JavaDoc code_base,
97         MAFAgentSystem class_provider)
98         throws ClassUnknown, ArgumentInvalid, DeserializationFailed, MAFExtendedException
99     {
100         if (trace)
101         {
102             System.out.println("MAFAgentSystem::create_agent()");
103         }
104         if (agent_profile.agent_system_type != my_agency.getAgentSystemInfo().agent_system_type)
105         {
106             throw new MAFExtendedException();
107         }
108         try
109         {
110             ObjectInputStream JavaDoc deserializer = new ObjectInputStream JavaDoc(
111                 new ByteArrayInputStream JavaDoc(arguments));
112             java.lang.Object JavaDoc argObj = deserializer.readObject();
113             return my_agency.createLocalAgent(
114                 class_names[0].name,
115                 new org.objectweb.mobilitools.smi.api.Name(agent_name),
116                 agent_profile,
117                 place_name,
118                 code_base,
119                 agent,
120                 argObj,
121                 class_provider).getName().getmafname();
122         }
123         catch (IOException JavaDoc e)
124         {
125             throw new DeserializationFailed();
126         }
127         catch (ClassNotFoundException JavaDoc e)
128         {
129             throw new ClassUnknown();
130         }
131         catch (BadOperation ex)
132         {
133             System.err.println(ex.getMessage());
134             switch (ex.getReason())
135             {
136             case BadOperation.CLASSFAULT:
137                 throw new ClassUnknown();
138             case BadOperation.SERIALIZATION:
139                 throw new DeserializationFailed();
140             case BadOperation.REJECTED:
141                 throw new ArgumentInvalid();
142             default:
143                 throw new MAFExtendedException();
144             }
145         }
146     }
147
148
149     /**
150         Returns code for requested classes.
151     */

152     public byte[][] fetch_class(
153         ClassName[] class_name_list,
154         String JavaDoc code_base,
155         AgentProfile agent_profile)
156         throws ClassUnknown, MAFExtendedException
157     {
158         if (trace)
159         {
160             System.out.println("MAFAgentSystem::fetch_class()");
161         }
162         byte[][] code = new byte[class_name_list.length][];
163         ClassCache cache = ClassCache.getCache(code_base);
164         synchronized(cache)
165         {
166             for (int i=0 ; i<code.length ; ++i)
167             {
168                 code[i] = (byte[])cache.get(class_name_list[i].name);
169                 if (code[i] == null)
170                 {
171                     System.err.println("unknown class " + class_name_list[i].name + " for codebase " + code_base);
172                     System.out.println(cache);
173                     throw new ClassUnknown();
174                 }
175             }
176         }
177         return code;
178     }
179
180
181     /**
182         Not implemented.
183     */

184     public String JavaDoc find_nearby_agent_system_of_profile(
185         AgentProfile agent_profile)
186         throws EntryNotFound, NO_IMPLEMENT
187     {
188         if (trace)
189         {
190             System.out.println("MAFAgentSystem::find_nearby_agent_system_of_profile() - NOT IMPLEMENTED !");
191         }
192         throw new NO_IMPLEMENT("Not implemented");
193     }
194
195
196     /**
197         @return the activity status of an agent, i.e. running or suspended.
198         Note that the CfMAFTerminated status is never returned (the exception
199         AgentNotFound would be thrown is case of a terminated agent).
200         @exception AgentNotFound if the agent name does not exist.
201     */

202     public AgentStatus get_agent_status(
203         org.omg.CfMAF.Name agent_name)
204         throws AgentNotFound
205     {
206         if (trace)
207         {
208             System.out.println("MAFAgentSystem::get_agent_status()");
209         }
210         try
211         {
212             if (my_agency.localAgentIsRunning(new org.objectweb.mobilitools.smi.api.Name(agent_name)))
213             {
214                 return AgentStatus.CfMAFRunning;
215             }
216             else
217             {
218                 return AgentStatus.CfMAFSuspended;
219             }
220         }
221         catch (BadOperation ex)
222         {
223             throw new AgentNotFound();
224         }
225     }
226
227
228     /**
229         @return a information record about the agency.
230     */

231     public AgentSystemInfo get_agent_system_info()
232     {
233         if (trace)
234         {
235             System.out.println("MAFAgentSystem::get_agent_system_info()");
236         }
237         return my_agency.getAgentSystemInfo();
238     }
239
240
241     /**
242         Not implemented.
243     */

244     public AuthInfo get_authinfo(
245         org.omg.CfMAF.Name agent_name)
246         throws AgentNotFound, NO_IMPLEMENT
247     {
248         if (trace)
249         {
250             System.out.println("MAFAgentSystem::get_authinfo() - NOT IMPLEMENTED !");
251         }
252         throw new NO_IMPLEMENT("Not implemented");
253     }
254
255
256     /**
257         @return the CORBA object reference to the MAFFinder.
258         @exception FinderNotFound current agency is running without MAFFinder support.
259     */

260     public MAFFinder get_MAFFinder()
261         throws FinderNotFound
262     {
263         if (trace)
264         {
265             System.out.println("MAFAgentSystem::get_MAFFinder()");
266         }
267         MAFFinder result = my_agency.getMAFFinder();
268         if (result != null)
269         {
270             return result;
271         }
272         else
273         {
274             throw new FinderNotFound();
275         }
276     }
277
278
279     /**
280         @return the list of all agents residing in the associated agency.
281     */

282     public org.omg.CfMAF.Name[] list_all_agents()
283     {
284         if (trace)
285         {
286             System.out.println("MAFAgentSystem::list_all_agents()");
287         }
288         return my_agency.listAgents((AgentProfile)null);
289     }
290
291
292     /**
293         @return an array of agent names with given authority.
294     */

295     public org.omg.CfMAF.Name[] list_all_agents_of_authority(
296         byte[] authority)
297     {
298         if (trace)
299         {
300             System.out.println("MAFAgentSystem::list_all_agents_of_authority()");
301         }
302         return my_agency.listAgents(authority);
303     }
304
305
306     /**
307         Not implemented.
308     */

309     public String JavaDoc[] list_all_places()
310         throws NO_IMPLEMENT
311     {
312         if (trace)
313         {
314             System.out.println("MAFAgentSystem::list_all_places() - NOT IMPLEMENTED !");
315         }
316         throw new NO_IMPLEMENT("Not implemented");
317     }
318
319
320     /**
321         Receives a migrating agent.
322     */

323     public void receive_agent(
324         org.omg.CfMAF.Name agent_name,
325         AgentProfile agent_profile,
326         byte[] agent,
327         String JavaDoc place_name,
328         ClassName[] class_names,
329         String JavaDoc code_base,
330         MAFAgentSystem sender)
331         throws ClassUnknown, DeserializationFailed, MAFExtendedException
332     {
333         if (trace)
334         {
335             System.out.println("MAFAgentSystem::receive_agent()");
336         }
337         try
338         {
339             my_agency.receiveAgent(
340                 class_names[0].name,
341                 agent,
342                 agent_name,
343                 agent_profile,
344                 place_name,
345                 code_base,
346                 sender);
347         }
348         catch (ClassUnknown e)
349         {
350             throw e;
351         }
352         catch (DeserializationFailed e)
353         {
354             throw e;
355         }
356         catch (MAFExtendedException e)
357         {
358             throw e;
359         }
360         catch (Throwable JavaDoc e)
361         {
362             throw new BAD_OPERATION(e.toString());
363         }
364     }
365
366
367     /**
368         Resumes the activity of an agent.
369     */

370     public void resume_agent(
371         org.omg.CfMAF.Name agent_name)
372         throws AgentNotFound, ResumeFailed, AgentIsRunning
373     {
374         if (trace)
375         {
376             System.out.println("MAFAgentSystem::resume_agent()");
377         }
378         try
379         {
380             my_agency.resumeLocalAgent(new org.objectweb.mobilitools.smi.api.Name(agent_name));
381         }
382         catch (BadOperation ex)
383         {
384             switch (ex.getReason())
385             {
386             case BadOperation.RUNNING:
387                 throw new AgentIsRunning();
388             case BadOperation.UNKNOWNAGENT:
389                 throw new AgentNotFound();
390             default:
391                 throw new ResumeFailed();
392             }
393         }
394     }
395
396
397     /**
398         Suspends the activity of an agent.
399     */

400     public void suspend_agent(
401         org.omg.CfMAF.Name agent_name)
402         throws AgentNotFound, SuspendFailed, AgentIsSuspended
403     {
404         if (trace)
405         {
406             System.out.println("MAFAgentSystem::suspend_agent()");
407         }
408         try
409         {
410             my_agency.suspendLocalAgent(new org.objectweb.mobilitools.smi.api.Name(agent_name));
411         }
412         catch (BadOperation ex)
413         {
414             switch (ex.getReason())
415             {
416             case BadOperation.SUSPENDED:
417                 throw new AgentIsSuspended();
418             case BadOperation.UNKNOWNAGENT:
419                 throw new AgentNotFound();
420             default:
421                 throw new SuspendFailed();
422             }
423         }
424     }
425
426
427     /**
428         Terminates an agent.
429     */

430     public void terminate_agent(
431         org.omg.CfMAF.Name agent_name)
432         throws AgentNotFound, TerminateFailed
433     {
434         if (trace)
435         {
436             System.out.println("MAFAgentSystem::terminate_agent()");
437         }
438         try
439         {
440             my_agency.terminateLocalAgent(new org.objectweb.mobilitools.smi.api.Name(agent_name));
441         }
442         catch (BadOperation ex)
443         {
444             System.err.println(ex.getMessage());
445             if (ex.getReason() == BadOperation.UNKNOWNAGENT)
446             {
447                 throw new AgentNotFound();
448             }
449         }
450     }
451
452
453     /**
454         Terminates the agency.
455     */

456     public void terminate_agent_system()
457         throws TerminateFailed
458     {
459         if (trace)
460         {
461             System.out.println("MAFAgentSystem::terminate_agent_system()");
462         }
463         my_agency.terminate();
464     }
465
466
467     /////////////////////////////////////////////////////////////////
468
// implementation of SMI's MAFAgentSystemComplement operations //
469
/////////////////////////////////////////////////////////////////
470

471
472     public org.omg.CfMAF.Name[] list_agents(AgentProfile profile)
473     {
474         if (trace)
475         {
476             System.out.println("MAFAgentSystemComplement::list_agents()");
477         }
478         return my_agency.listAgents(profile);
479     }
480
481
482     public void move_agent(org.omg.CfMAF.Name name, String JavaDoc destination, String JavaDoc place)
483         throws
484         AgentNotFound,
485         ClassUnknown,
486         DeserializationFailed,
487         AgentSystemNotFound,
488         ArgumentInvalid,
489         AgentRefusedToMove,
490         MAFExtendedException
491     {
492         if (trace)
493         {
494             System.out.println("MAFAgentSystemComplement::move_agent()");
495         }
496         try
497         {
498             my_agency.moveLocalAgent(
499                 new org.objectweb.mobilitools.smi.api.Name(name),
500                 new Location(destination),
501                 place);
502         }
503         catch (BadOperation e)
504         {
505             switch (e.getReason())
506             {
507             case BadOperation.REJECTED:
508                 throw new AgentRefusedToMove();
509             case BadOperation.DESTINATION:
510                 throw new AgentSystemNotFound();
511             case BadOperation.CLASSFAULT:
512                 throw new ClassUnknown();
513             case BadOperation.UNKNOWNAGENT:
514                 throw new AgentNotFound();
515             case BadOperation.SERIALIZATION:
516                 throw new DeserializationFailed();
517             case BadOperation.INFRASTRUCTURE:
518                 throw new MAFExtendedException();
519             }
520         }
521     }
522 }
523
Popular Tags