KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > AgentFactory


1 /*
2  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
3  * Copyright (C) 1996 - 2000 BULL
4  * Copyright (C) 1996 - 2000 INRIA
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.1 of the License, or 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
19  * USA.
20  */

21 package fr.dyade.aaa.agent;
22
23 import java.io.*;
24 import java.util.*;
25
26 import org.objectweb.util.monolog.api.BasicLevel;
27
28 /**
29  * <code>Agent</code> used to allow remote agent creation. Every agent
30  * server hosts a factory agent, they all use a predefined stamp identifier
31  * <code>AgentId.factoryId</code>. The factory must be able to create all
32  * types of objects, actually it is supposed that the corresponding classes
33  * can be reached.<p>
34  * The agent creation process involves the following steps
35  * <ul>
36  * <li>locally creating the <code>Agent</code> object in memory ;
37  * <li>serializing the agent state, building an <code>AgentCreateRequest</code>
38  * notification with the resulting string, sending it to the target
39  * Factory agent ;
40  * <li>the factory agent building the object in memory from the serialized
41  * image, and saving it into operational storage ;
42  * <li>the factory agent calling the initialize method.
43  * </ul>
44  * The AgentDeleteRequest class of notification follows a similar process
45  * to remotely delete agents.
46  */

47 final class AgentFactory extends Agent {
48   /**
49    * Allocates a new <code>AgentFactory</code> agent.
50    * An <code>AgentFactory</code> agent must be created on every agent
51    * server the first time it runs.
52    */

53   AgentFactory(AgentId factoryId) {
54     super("AgentFactory#" + AgentServer.getServerId(),
55       true,
56       factoryId);
57   }
58
59   /**
60    * Returns log topic for factory agent.
61    */

62   protected String JavaDoc getLogTopic() {
63     return Debug.A3Agent + ".AgentFactory.#" + AgentServer.getServerId();
64   }
65
66   /**
67    * Gives this agent an opportunity to initialize after having been
68    * deployed, and each time it is loaded into memory. Loads the list of
69    * agents with a <code>fixed</code> field set to <code>true</code>.
70    *
71    * @param firstTime true when first called by the factory
72    *
73    * @exception Exception
74    * unspecialized exception
75    */

76   protected void agentInitialize(boolean firstTime) throws Exception JavaDoc {
77     super.agentInitialize(firstTime);
78   }
79   /**
80    * Reacts to notifications ... .
81    *
82    * @param from agent sending notification
83    * @param not notification to react to
84    *
85    * @exception Exception
86    * unspecialized exception
87    */

88   public void react(AgentId from, Notification not) throws Exception JavaDoc {
89     if (not instanceof AgentCreateRequest) {
90       AgentCreateRequest cnot = (AgentCreateRequest) not;
91       try {
92     // Restore the new agent state.
93
ObjectInputStream ois =
94       new ObjectInputStream(
95         new ByteArrayInputStream(
96           cnot.agentState, 0, cnot.agentState.length));
97     Agent ag = (Agent) ois.readObject();
98     try {
99       ois.close();
100     } catch (IOException exc) {}
101
102         // Initializes and creates the agent
103
// TODO: (ThreadEngine) Thread.currentThread() ...
104
AgentServer.engine.createAgent(cnot.deploy, ag);
105
106         if (logmon.isLoggable(BasicLevel.DEBUG))
107           logmon.log(BasicLevel.DEBUG,
108                      "AgentFactory" + id +
109                      ", create Agent" + ag.id + " [" + ag.name + "]");
110     if (cnot.reply != null) {
111           AgentCreateReply reply = new AgentCreateReply(ag.getId());
112           reply.setContext(cnot.getContext());
113       sendTo(cnot.reply, reply);
114         }
115       } catch (Throwable JavaDoc error) {
116     // If there is an explicit reply request send it the
117
// ExceptionNotification to the requester else to the
118
// sender.
119
cnot.agentState = null;
120         logmon.log(BasicLevel.ERROR,
121                    "AgentFactory" + id + ", can't create Agent" + cnot.deploy,
122                    error);
123         ExceptionNotification excNot =
124           new ExceptionNotification(
125             getId(), cnot, new AgentException(error));
126         excNot.setContext(cnot.getContext());
127     if (cnot.reply != null) {
128       sendTo(cnot.reply, excNot);
129     } else {
130       sendTo(from, excNot);
131     }
132       }
133     } else if (not instanceof AgentDeleteRequest) {
134       try {
135 // TODO: (ThreadEngine) Thread.currentThread() ...
136
AgentServer.engine.deleteAgent(from);
137     if (((AgentDeleteRequest) not).reply != null)
138           sendTo(((AgentDeleteRequest) not).reply, new DeleteAck(from));
139       } catch (Exception JavaDoc exc) {
140     if (((AgentDeleteRequest) not).reply != null)
141           sendTo(((AgentDeleteRequest) not).reply, new DeleteAck(from, exc));
142       }
143     } else {
144       try {
145     super.react(from, not);
146       } catch (Exception JavaDoc exc) {
147     sendTo(from,
148            new ExceptionNotification(getId(), not, exc));
149       }
150     }
151   }
152 }
153
154
Popular Tags