1 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 47 final class AgentFactory extends Agent { 48 53 AgentFactory(AgentId factoryId) { 54 super("AgentFactory#" + AgentServer.getServerId(), 55 true, 56 factoryId); 57 } 58 59 62 protected String getLogTopic() { 63 return Debug.A3Agent + ".AgentFactory.#" + AgentServer.getServerId(); 64 } 65 66 76 protected void agentInitialize(boolean firstTime) throws Exception { 77 super.agentInitialize(firstTime); 78 } 79 88 public void react(AgentId from, Notification not) throws Exception { 89 if (not instanceof AgentCreateRequest) { 90 AgentCreateRequest cnot = (AgentCreateRequest) not; 91 try { 92 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 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 error) { 116 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 AgentServer.engine.deleteAgent(from); 137 if (((AgentDeleteRequest) not).reply != null) 138 sendTo(((AgentDeleteRequest) not).reply, new DeleteAck(from)); 139 } catch (Exception 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 exc) { 147 sendTo(from, 148 new ExceptionNotification(getId(), not, exc)); 149 } 150 } 151 } 152 } 153 154 | Popular Tags |