1 21 package fr.dyade.aaa.agent; 22 23 import java.io.*; 24 import java.util.*; 25 import java.lang.reflect.*; 26 27 import org.objectweb.util.monolog.api.BasicLevel; 28 import org.objectweb.util.monolog.api.Logger; 29 30 import fr.dyade.aaa.util.*; 31 import fr.dyade.aaa.util.management.MXWrapper; 32 33 68 public abstract class Agent implements AgentMBean, Serializable { 69 static final long serialVersionUID = 2955513886633164244L; 70 71 77 private transient boolean updated = true; 78 79 84 protected void setNoSave() { 85 updated = false; 86 } 87 88 92 protected void setSave() { 93 updated = true; 94 } 95 96 99 protected final boolean needToBeCommited() { 100 try { 101 ((EngineThread) Thread.currentThread()).engine.needToBeCommited = true; 102 return true; 103 } catch (ClassCastException exc) { 104 return false; 105 } 106 } 107 108 111 protected final void save() throws IOException { 112 if (updated) { 113 AgentServer.getTransaction().save(this, id.toString()); 114 if (logmon.isLoggable(BasicLevel.DEBUG)) 115 logmon.log(BasicLevel.DEBUG, 116 "Agent" + id + " [" + name + "] saved"); 117 } else { 118 updated = true; 119 if (logmon.isLoggable(BasicLevel.DEBUG)) 120 logmon.log(BasicLevel.DEBUG, 121 "Agent" + id + " [" + name + "] not saved"); 122 } 123 } 124 125 133 final static Agent 134 load(AgentId id) throws IOException, ClassNotFoundException { 135 Agent ag = (Agent) AgentServer.getTransaction().load(id.toString()); 136 if (ag != null) { 137 ag.id = id; 138 ag.deployed = true; 139 } 140 return ag; 141 } 142 143 146 151 transient AgentId id; 152 153 public transient String name; 154 155 160 public String getName() { 161 if ((name == null) || (name == nullName)) { 162 return getClass().getName() + id.toString(); 163 } else { 164 return name; 165 } 166 } 167 168 173 public void setName(String name) { 174 if (name == null) 175 this.name = nullName; 176 else 177 this.name = name; 178 } 179 180 184 protected transient boolean fixed; 185 186 protected transient Logger logmon = null; 187 188 193 protected String getLogTopic() { 194 return fr.dyade.aaa.agent.Debug.A3Agent; 195 } 196 197 protected static final String nullName = ""; 198 199 205 transient long last; 206 207 private void writeObject(java.io.ObjectOutputStream out) 208 throws IOException { 209 out.writeUTF(name); 210 out.writeBoolean(fixed); 211 } 212 213 private void readObject(java.io.ObjectInputStream in) 214 throws IOException, ClassNotFoundException { 215 if ((name = in.readUTF()).equals(nullName)) 216 name = nullName; 217 fixed = in.readBoolean(); 218 updated = true; 219 } 220 221 230 public Agent() { 231 this(null, false); 232 } 233 234 242 public Agent(boolean fixed) { 243 this(null, fixed); 244 } 245 246 254 public Agent(String name) { 255 this(name, false); 256 } 257 258 267 public Agent(String name, boolean fixed) { 268 this(AgentServer.getServerId(), name, fixed); 269 } 270 271 279 public Agent(short to) { 280 this(to, null, false); 281 } 282 283 292 public Agent(short to, String name) { 293 this(to, name, false); 294 } 295 296 305 public Agent(short to, boolean fixed) { 306 this(to, null, fixed); 307 } 308 309 319 public Agent(short to, String name, boolean fixed) { 320 AgentId id = null; 321 322 try { 323 id = new AgentId(to); 324 } catch (IOException exc) { 325 logmon = Debug.getLogger(fr.dyade.aaa.agent.Debug.A3Agent + 326 ".#" + AgentServer.getServerId()); 327 logmon.log(BasicLevel.ERROR, 328 AgentServer.getName() + ", can't allocate new AgentId", exc); 329 } 331 initState(name, fixed, id); 332 } 333 334 343 Agent(String name, boolean fixed, AgentId id) { 344 initState(name, fixed, id); 345 } 346 347 private void initState(String name, boolean fixed, AgentId id) { 348 if (name == null) 349 this.name = nullName; 350 else 351 this.name = name; 352 this.fixed = fixed; 353 this.id = id; 354 this.logmon = Debug.getLogger(getLogTopic()); 356 } 357 358 376 public Agent(String name, boolean fixed, int stamp) { 377 if (stamp < AgentId.MinWKSIdStamp || 378 stamp > AgentId.MaxWKSIdStamp) { 379 logmon = Debug.getLogger(fr.dyade.aaa.agent.Debug.A3Agent + 380 ".#" + AgentServer.getServerId()); 381 logmon.log(BasicLevel.ERROR, 382 AgentServer.getName() + 383 ", well known service stamp out of range: " + stamp); 384 throw new IllegalArgumentException ( 385 "Well known service stamp out of range: " + stamp); 386 } 387 AgentId id = new AgentId(AgentServer.getServerId(), 388 AgentServer.getServerId(), 389 stamp); 390 initState(name, fixed, id); 391 } 392 393 396 transient boolean deployed = false; 397 398 401 public boolean isDeployed() { 402 return deployed; 403 } 404 405 424 public final void deploy() throws IOException { 425 deploy(null); 426 } 427 428 438 public final void deploy(AgentId reply) throws IOException { 439 if ((id == null) || id.isNullId()) { 440 logmon.log(BasicLevel.ERROR, 441 AgentServer.getName() + 442 ", can't deploy " + this.toString() + ", id is null"); 443 throw new IOException("Can't deploy agent, id is null"); 444 } 445 if (deployed) { 446 logmon.log(BasicLevel.ERROR, 447 AgentServer.getName() + 448 ", can't deploy " + this.toString() + ", already deployed"); 449 throw new IOException("Can't deploy agent, already deployed"); 450 } 451 452 Channel.sendTo(AgentId.factoryId(id.getTo()), 455 new AgentCreateRequest(this, reply)); 456 deployed = true; 457 458 if (logmon.isLoggable(BasicLevel.DEBUG)) 459 logmon.log(BasicLevel.DEBUG, this.toString() + " deployed"); 460 } 461 462 468 public String toString() { 469 StringBuffer strbuf = new StringBuffer (); 470 471 strbuf.append('(').append(super.toString()); 472 strbuf.append(",name=").append(name); 473 strbuf.append(",id=").append(id.toString()); 474 strbuf.append(",fixed=").append(fixed); 475 strbuf.append(')'); 476 477 return strbuf.toString(); 478 } 479 480 485 public final String getAgentId() { 486 return id.toString(); 487 } 488 489 497 public final AgentId getId() { 498 return id; 499 } 500 501 506 public final boolean isFixed() { 507 return fixed; 508 } 509 510 530 protected void agentInitialize(boolean firstTime) throws Exception { 531 this.logmon = Debug.getLogger(getLogTopic()); 533 this.updated = true; 535 536 try { 537 MXWrapper.registerMBean(this, "AgentServer", getMBeanName()); 538 } catch (Exception exc) { 539 logmon.log(BasicLevel.ERROR, getName() + " jmx failed", exc); 540 } 541 542 if (logmon.isLoggable(BasicLevel.DEBUG)) 543 logmon.log(BasicLevel.DEBUG, 544 "Agent" + id + " [" + name + 545 (firstTime?"] , first initialized":"] , initialized")); 546 } 547 548 private String getMBeanName() { 549 return new StringBuffer () 550 .append("server=").append(AgentServer.getName()) 551 .append(",cons=Engine#").append(getId().getTo()) 552 .append(",agent=").append((name == nullName)?getId().toString():name) 553 .toString(); 554 } 555 556 570 protected final void 571 sendTo(AgentId to, Notification not) { 572 583 if (Thread.currentThread() == AgentServer.engine.thread) { 585 AgentServer.engine.push(getId(), to, not); 586 } else { 587 Channel.channel.directSendTo(getId(), to, not); 588 } 589 } 590 591 598 protected final void sendTo(Role role, Notification not) { 599 if (role == null) return; 600 sendTo(role.getListener(), not); 601 } 602 603 609 protected final void 610 sendTo(RoleMultiple role, Notification not) { 611 if (role == null) return; 612 Enumeration to = role.getListeners(); 613 if (to == null) 614 return; 615 while (to.hasMoreElements()) 616 sendTo((AgentId) to.nextElement(), not); 617 } 618 619 623 public void delete() { 624 delete(null); 625 } 626 627 633 public void delete(AgentId agent) { 634 if (deployed) 635 sendTo(AgentId.factoryId(id.getTo()), 636 new AgentDeleteRequest(agent)); 637 } 638 639 653 public void react(AgentId from, Notification not) throws Exception { 654 if (not instanceof DeleteNot) { 655 delete(((DeleteNot) not).reply); 656 } else if ((not instanceof UnknownAgent) || 657 (not instanceof UnknownNotification) || 658 (not instanceof ExceptionNotification)) { 659 logmon.log(BasicLevel.WARN, 660 this.toString() + ".react(" + from + ", " + not + ")"); 661 } else { 662 logmon.log(BasicLevel.ERROR, 663 this.toString() + ".react(" + from + ", " + not + ")"); 664 sendTo(from, new UnknownNotification(id, not)); 665 } 666 } 667 668 681 public void agentFinalize(boolean lastTime) { 682 try { 683 MXWrapper.unregisterMBean("AgentServer", getMBeanName()); 684 } catch (Exception exc) { 685 logmon.log(BasicLevel.ERROR, getName() + " jmx failed", exc); 686 } 687 } 688 } 689 | Popular Tags |