1 24 package org.objectweb.joram.mom.dest; 25 26 import java.io.IOException ; 27 import java.util.Calendar ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Properties ; 31 import java.util.Vector ; 32 33 import org.objectweb.joram.mom.notifications.AdminReply; 34 import org.objectweb.joram.mom.notifications.ClientMessages; 35 import org.objectweb.joram.mom.notifications.Monit_FreeAccess; 36 import org.objectweb.joram.mom.notifications.Monit_FreeAccessRep; 37 import org.objectweb.joram.mom.notifications.Monit_GetDMQSettings; 38 import org.objectweb.joram.mom.notifications.Monit_GetDMQSettingsRep; 39 import org.objectweb.joram.mom.notifications.Monit_GetReaders; 40 import org.objectweb.joram.mom.notifications.Monit_GetStat; 41 import org.objectweb.joram.mom.notifications.Monit_GetStatRep; 42 import org.objectweb.joram.mom.notifications.Monit_GetUsersRep; 43 import org.objectweb.joram.mom.notifications.Monit_GetWriters; 44 import org.objectweb.joram.mom.notifications.RequestGroupNot; 45 import org.objectweb.joram.mom.notifications.SetDMQRequest; 46 import org.objectweb.joram.mom.notifications.SetRightRequest; 47 import org.objectweb.joram.mom.notifications.SpecialAdminRequest; 48 import org.objectweb.joram.mom.proxies.SendRepliesNot; 49 import org.objectweb.joram.mom.proxies.SendReplyNot; 50 import org.objectweb.joram.shared.excepts.AccessException; 51 import org.objectweb.joram.shared.excepts.RequestException; 52 import org.objectweb.joram.shared.messages.Message; 53 import org.objectweb.util.monolog.api.BasicLevel; 54 import org.objectweb.util.monolog.api.Logger; 55 56 import fr.dyade.aaa.agent.AgentId; 57 import fr.dyade.aaa.agent.Channel; 58 import fr.dyade.aaa.agent.DeleteNot; 59 import fr.dyade.aaa.agent.Notification; 60 import fr.dyade.aaa.agent.UnknownAgent; 61 import fr.dyade.aaa.util.Debug; 62 63 67 public abstract class DestinationImpl implements java.io.Serializable , DestinationImplMBean { 68 public static Logger logger = Debug.getLogger(DestinationImpl.class.getName()); 69 70 74 private boolean deletable = false; 75 76 77 private AgentId adminId; 78 79 protected AgentId destId; 80 81 82 protected boolean freeReading = false; 83 84 protected boolean freeWriting = false; 85 86 protected Hashtable clients; 87 88 89 public static int READ = 1; 90 91 public static int WRITE = 2; 92 93 public static int READWRITE = 3; 94 95 99 protected AgentId dmqId = null; 100 101 106 transient StringBuffer strbuf; 107 108 private Destination agent; 109 110 113 public long creationDate = -1; 114 115 protected long nbMsgsReceiveSinceCreation = 0; 116 protected long nbMsgsDeliverSinceCreation = 0; 117 protected long nbMsgsSendToDMQSinceCreation = 0; 118 119 126 public DestinationImpl(AgentId destId, AgentId adminId, Properties prop) { 127 this.destId = destId; 128 this.adminId = adminId; 129 clients = new Hashtable (); 130 strbuf = new StringBuffer (); 131 132 if (creationDate == -1) 133 creationDate = System.currentTimeMillis(); 134 135 if (logger.isLoggable(BasicLevel.DEBUG)) 136 logger.log(BasicLevel.DEBUG, this + ", created."); 137 } 138 139 void setNoSave() { 140 if (agent != null) { 141 if (logger.isLoggable(BasicLevel.DEBUG)) 142 logger.log(BasicLevel.DEBUG, this + ".setNoSave()."); 143 agent.setNoSave(); 144 } 145 } 146 147 void setSave() { 148 if (agent != null) { 149 if (logger.isLoggable(BasicLevel.DEBUG)) 150 logger.log(BasicLevel.DEBUG, this + ".setSave()."); 151 agent.setSave(); 152 } 153 } 154 155 public void setAgent(Destination agent) { 156 setSave(); 158 this.agent = agent; 159 } 160 161 protected boolean isLocal(AgentId id) { 162 return (destId.getTo() == id.getTo()); 163 } 164 165 166 public boolean canBeDeleted() { 167 return deletable; 168 } 169 170 176 public void setRightRequest(AgentId from, SetRightRequest not) throws AccessException { 177 if (! isAdministrator(from)) 178 throw new AccessException("ADMIN right not granted"); 179 180 AgentId user = not.getClient(); 181 int right = not.getRight(); 182 String info; 183 184 try { 185 processSetRight(user,right); 186 doRightRequest(not); 187 info = strbuf.append("Request [") 188 .append(not.getClass().getName()) 189 .append("], sent to Destination [") 190 .append(destId) 191 .append("], successful [true]: user [") 192 .append(user) 193 .append("] set with right [" + right +"]").toString(); 194 strbuf.setLength(0); 195 forward(from, new AdminReply(not, true, info)); 196 } catch (RequestException exc) { 197 info = strbuf.append("Request [") 198 .append(not.getClass().getName()) 199 .append("], sent to Destination [") 200 .append(destId) 201 .append("], successful [false]: ") 202 .append(exc.getMessage()).toString(); 203 strbuf.setLength(0); 204 forward(from, new AdminReply(not, false, info)); 205 } 206 207 if (logger.isLoggable(BasicLevel.DEBUG)) 208 logger.log(BasicLevel.DEBUG, info); 209 } 210 211 212 protected void processSetRight(AgentId user, int right) 213 throws RequestException { 214 setSave(); 216 217 if (user == null) { 219 if (right == READ) 220 freeReading = true; 221 else if (right == WRITE) 222 freeWriting = true; 223 else if (right == -READ) 224 freeReading = false; 225 else if (right == -WRITE) 226 freeWriting = false; 227 else 228 throw new RequestException("Invalid right value: " + right); 229 } else { 230 Integer currentRight = (Integer ) clients.get(user); 232 if (right == READ) { 233 if (currentRight != null && currentRight.intValue() == WRITE) 234 clients.put(user, new Integer (READWRITE)); 235 else 236 clients.put(user, new Integer (READ)); 237 } else if (right == WRITE) { 238 if (currentRight != null && currentRight.intValue() == READ) 239 clients.put(user, new Integer (READWRITE)); 240 else 241 clients.put(user, new Integer (WRITE)); 242 } else if (right == -READ) { 243 if (currentRight != null && currentRight.intValue() == READWRITE) 244 clients.put(user, new Integer (WRITE)); 245 else if (currentRight != null && currentRight.intValue() == READ) 246 clients.remove(user); 247 } else if (right == -WRITE) { 248 if (currentRight != null && currentRight.intValue() == READWRITE) 249 clients.put(user, new Integer (READ)); 250 else if (currentRight != null && currentRight.intValue() == WRITE) 251 clients.remove(user); 252 } else 253 throw new RequestException("Invalid right value: " + right); 254 } 255 } 256 257 264 public void setDMQRequest(AgentId from, SetDMQRequest not) throws AccessException { 265 if (! isAdministrator(from)) 266 throw new AccessException("ADMIN right not granted"); 267 268 setSave(); 270 271 dmqId = not.getDmqId(); 272 273 String info = strbuf.append("Request [") 274 .append(not.getClass().getName()) 275 .append("], sent to Destination [") 276 .append(destId) 277 .append("], successful [true]: dmq [") 278 .append(dmqId) 279 .append("] set").toString(); 280 strbuf.setLength(0); 281 forward(from, new AdminReply(not, true, info)); 282 283 if (logger.isLoggable(BasicLevel.DEBUG)) 284 logger.log(BasicLevel.DEBUG, info); 285 } 286 287 293 public void monitGetReaders(AgentId from, Monit_GetReaders not) throws AccessException { 294 if (! isAdministrator(from)) 295 throw new AccessException("ADMIN right not granted"); 296 297 AgentId key; 298 int right; 299 Vector readers = new Vector (); 300 for (Enumeration keys = clients.keys(); keys.hasMoreElements();) { 301 key = (AgentId) keys.nextElement(); 302 right = ((Integer ) clients.get(key)).intValue(); 303 304 if (right == READ || right == READWRITE) 305 readers.add(key); 306 } 307 forward(from, new Monit_GetUsersRep(not, readers)); 308 } 309 310 316 public void monitGetWriters(AgentId from, Monit_GetWriters not) throws AccessException { 317 if (! isAdministrator(from)) 318 throw new AccessException("ADMIN right not granted"); 319 320 AgentId key; 321 int right; 322 Vector writers = new Vector (); 323 for (Enumeration keys = clients.keys(); keys.hasMoreElements();) { 324 key = (AgentId) keys.nextElement(); 325 right = ((Integer ) clients.get(key)).intValue(); 326 327 if (right == WRITE || right == READWRITE) 328 writers.add(key); 329 } 330 forward(from, new Monit_GetUsersRep(not, writers)); 331 } 332 333 public static String [] _rights = {":R;", ";W;", ":RW;"}; 334 335 340 public String [] getRights() { 341 String rigths[] = new String [clients.size()]; 342 343 AgentId key; 344 int right; 345 346 int i=0; 347 for (Enumeration keys = clients.keys(); keys.hasMoreElements();) { 348 key = (AgentId) keys.nextElement(); 349 right = ((Integer ) clients.get(key)).intValue(); 350 rigths[i] = key.toString() + _rights[right -1]; 351 } 352 353 return rigths; 354 } 355 356 363 public String getRight(String userid) { 364 AgentId key = AgentId.fromString(userid); 365 if (key == null) return userid + ":bad user;"; 366 Integer right = (Integer ) clients.get(key); 367 if (right == null) return userid + ":unknown;"; 368 369 return userid + _rights[right.intValue() -1]; 370 } 371 372 375 378 384 public void monitFreeAccess(AgentId from, Monit_FreeAccess not) throws AccessException { 385 if (! isAdministrator(from)) 386 throw new AccessException("ADMIN right not granted"); 387 388 forward(from, new Monit_FreeAccessRep(not, freeReading, freeWriting)); 389 } 390 391 397 public void monitGetDMQSettings(AgentId from, Monit_GetDMQSettings not) throws AccessException { 398 if (! isAdministrator(from)) 399 throw new AccessException("ADMIN right not granted"); 400 401 String id = null; 402 if (dmqId != null) 403 id = dmqId.toString(); 404 405 forward(from, new Monit_GetDMQSettingsRep(not, id, null)); 406 } 407 408 414 public void monitGetStat(AgentId from, Monit_GetStat not) throws AccessException { 415 if (! isAdministrator(from)) 416 throw new AccessException("ADMIN right not granted"); 417 418 Hashtable stats = new Hashtable (); 419 stats.put("nbMsgsReceiveSinceCreation", new Long (nbMsgsReceiveSinceCreation)); 420 stats.put("nbMsgsDeliverSinceCreation", new Long (nbMsgsDeliverSinceCreation)); 421 stats.put("nbMsgsSendToDMQSinceCreation", new Long (nbMsgsSendToDMQSinceCreation)); 422 stats.put("creationDate", new Long (creationDate)); 423 424 forward(from, new Monit_GetStatRep(not, stats)); 425 } 426 427 438 protected void clientMessages(AgentId from, ClientMessages not) throws AccessException { 439 if (logger.isLoggable(BasicLevel.DEBUG)) 440 logger.log(BasicLevel.DEBUG, 441 "DestinationImpl.clientMessages(" + from + ',' + not + ')'); 442 443 if (! isWriter(from)) { 446 ClientMessages deadM; 447 deadM = new ClientMessages(not.getClientContext(), not.getRequestId()); 448 449 Message msg; 450 for (Enumeration msgs = not.getMessages().elements(); 451 msgs.hasMoreElements();) { 452 msg = (Message) msgs.nextElement(); 453 msg.notWriteable = true; 454 deadM.addMessage(msg); 455 } 456 sendToDMQ(deadM, not.getDMQId()); 457 throw new AccessException("WRITE right not granted"); 458 } 459 460 doClientMessages(from, not); 461 462 466 if (! not.getPersistent() && !not.getAsyncSend()) { 467 forward(from, new SendReplyNot(not.getClientContext(), not.getRequestId())); 468 } 469 } 470 471 479 public void unknownAgent(AgentId from, UnknownAgent not) { 480 if (isAdministrator(not.agent)) { 481 if (logger.isLoggable(BasicLevel.ERROR)) 482 logger.log(BasicLevel.ERROR, 483 "Admin of dest " + destId + " does not exist anymore."); 484 } else if (not.agent.equals(dmqId)) { 485 setSave(); 487 dmqId = null; 488 } else { 489 setSave(); 491 clients.remove(from); 492 doUnknownAgent(not); 493 } 494 } 495 496 502 public void deleteNot(AgentId from, DeleteNot not) { 503 if (! isAdministrator(from)) { 504 if (logger.isLoggable(BasicLevel.WARN)) 505 logger.log(BasicLevel.WARN, 506 "Unauthorized deletion request from " + from); 507 } else { 508 doDeleteNot(not); 509 setSave(); 511 deletable = true; 512 } 513 } 514 515 520 public void specialAdminRequest(AgentId from, SpecialAdminRequest not) { 521 String info; 522 Object obj = null; 523 524 setSave(); 526 527 try { 528 if (!isAdministrator(from)) { 529 if (logger.isLoggable(BasicLevel.WARN)) 530 logger.log(BasicLevel.WARN, 531 "Unauthorized SpecialAdminRequest request from " + from); 532 throw new RequestException("ADMIN right not granted"); 533 } 534 obj = specialAdminProcess(not); 535 info = strbuf.append("Request [") 536 .append(not.getClass().getName()) 537 .append("], sent to Destination [") 538 .append(destId) 539 .append("], successful [true] ").toString(); 540 strbuf.setLength(0); 541 forward(from, new AdminReply(not, true, info, obj)); 542 } catch (RequestException exc) { 543 info = strbuf.append("Request [") 544 .append(not.getClass().getName()) 545 .append("], sent to Destination [") 546 .append(destId) 547 .append("], successful [false]: ") 548 .append(exc.getMessage()).toString(); 549 strbuf.setLength(0); 550 forward(from, new AdminReply(not, false, info, obj)); 551 } 552 if (logger.isLoggable(BasicLevel.DEBUG)) 553 logger.log(BasicLevel.DEBUG, info); 554 } 555 556 public void requestGroupNot(AgentId from, RequestGroupNot not) { 557 Enumeration en = not.getClientMessages(); 558 ClientMessages theCM = (ClientMessages) en.nextElement(); 559 Vector replies = new Vector (); 560 replies.addElement(new SendReplyNot( 561 theCM.getClientContext(), 562 theCM.getRequestId())); 563 while (en.hasMoreElements()) { 564 ClientMessages cm = (ClientMessages) en.nextElement(); 565 Vector msgs = cm.getMessages(); 566 for (int i = 0; i < msgs.size(); i++) { 567 theCM.addMessage((Message) msgs.elementAt(i)); 568 } 569 if (! cm.getAsyncSend()) { 570 replies.addElement(new SendReplyNot( 571 cm.getClientContext(), 572 cm.getRequestId())); 573 } 574 } 575 576 doClientMessages(from, theCM); 577 578 if (! not.getPersistent() && replies.size() > 0) { 582 forward(from, new SendRepliesNot(replies)); 583 } 584 } 585 586 protected Object specialAdminProcess(SpecialAdminRequest not) 587 throws RequestException { 588 return null; 589 } 590 591 596 protected boolean isReader(AgentId client) { 597 if (isAdministrator(client) || freeReading) 598 return true; 599 600 Integer clientRight = (Integer ) clients.get(client); 601 if (clientRight == null) 602 return false; 603 else 604 return ((clientRight.intValue() == READ) || 605 (clientRight.intValue() == READWRITE)); 606 } 607 608 613 protected boolean isWriter(AgentId client) { 614 if (isAdministrator(client) || freeWriting) 615 return true; 616 617 Integer clientRight = (Integer ) clients.get(client); 618 if (clientRight == null) 619 return false; 620 else 621 return ((clientRight.intValue() == WRITE) || 622 (clientRight.intValue() == READWRITE)); 623 } 624 625 630 protected boolean isAdministrator(AgentId client) { 631 return client.equals(adminId) || 632 client.equals(AdminTopic.getDefault()); 633 } 634 635 642 protected void sendToDMQ(ClientMessages deadMessages, AgentId dmqId) { 643 Vector messages = deadMessages.getMessages(); 644 nbMsgsSendToDMQSinceCreation = nbMsgsSendToDMQSinceCreation + messages.size(); 645 646 AgentId destDmqId = null; 647 if (dmqId != null) { 648 destDmqId = dmqId; 650 } else if (this.dmqId != null) { 651 destDmqId = this.dmqId; 653 } else if (DeadMQueueImpl.id != null) { 654 destDmqId = DeadMQueueImpl.id; 656 } 657 658 if (destDmqId != null && 659 ! destDmqId.equals(destId)) { 660 forward(destDmqId, deadMessages); 661 } 662 } 665 666 abstract protected void doRightRequest(SetRightRequest not); 667 abstract protected void doClientMessages(AgentId from, ClientMessages not); 668 abstract protected void doUnknownAgent(UnknownAgent not); 669 abstract protected void doDeleteNot(DeleteNot not); 670 671 public SetRightRequest preProcess(SetRightRequest req) { 672 return req; 674 } 675 676 public void postProcess(SetRightRequest req) { 677 } 679 680 public ClientMessages preProcess(AgentId from, ClientMessages msgs) { 681 return msgs; 683 } 684 685 public void postProcess(ClientMessages msgs) { 686 } 688 689 private void writeObject(java.io.ObjectOutputStream out) 690 throws IOException { 691 out.writeBoolean(deletable); 692 out.writeObject(adminId); 693 out.writeObject(destId); 694 out.writeBoolean(freeReading); 695 out.writeBoolean(freeWriting); 696 out.writeObject(clients); 697 out.writeObject(dmqId); 698 out.writeLong(creationDate); 699 out.writeLong(nbMsgsReceiveSinceCreation); 700 out.writeLong(nbMsgsDeliverSinceCreation); 701 out.writeLong(nbMsgsSendToDMQSinceCreation); 702 out.writeObject(agent); 703 } 704 705 private void readObject(java.io.ObjectInputStream in) 706 throws IOException , ClassNotFoundException { 707 deletable = in.readBoolean(); 708 adminId = (AgentId)in.readObject(); 709 destId = (AgentId)in.readObject(); 710 freeReading = in.readBoolean(); 711 freeWriting = in.readBoolean(); 712 clients = (Hashtable )in.readObject(); 713 dmqId = (AgentId)in.readObject(); 714 strbuf = new StringBuffer (); 715 creationDate = in.readLong(); 716 nbMsgsReceiveSinceCreation = in.readLong(); 717 nbMsgsDeliverSinceCreation = in.readLong(); 718 nbMsgsSendToDMQSinceCreation = in.readLong(); 719 agent = (Destination)in.readObject(); 720 } 721 722 724 729 public final String getDestinationId() { 730 return destId.toString(); 731 } 732 733 739 public boolean isFreeReading() { 740 return freeReading; 741 } 742 743 748 public void setFreeReading(boolean on) { 749 setSave(); 751 freeReading = on; 752 } 753 754 760 public boolean isFreeWriting() { 761 return freeWriting; 762 } 763 764 769 public void setFreeWriting(boolean on) { 770 setSave(); 772 freeWriting = on; 773 } 774 775 781 public String getDMQId() { 782 if (dmqId != null) 783 return dmqId.toString(); 784 return null; 785 } 786 787 792 public long getCreationTimeInMillis() { 793 return creationDate; 794 } 795 796 802 public String getCreationDate() { 803 Calendar cal = Calendar.getInstance(); 804 cal.setTimeInMillis(creationDate); 805 return cal.getTime().toString(); 806 } 807 808 814 public long getNbMsgsReceiveSinceCreation() { 815 return nbMsgsReceiveSinceCreation; 816 } 817 824 public long getNbMsgsDeliverSinceCreation() { 825 return nbMsgsDeliverSinceCreation; 826 } 827 828 834 public long getNbMsgsSendToDMQSinceCreation() { 835 return nbMsgsSendToDMQSinceCreation; 836 } 837 838 protected void replyToTopic( 839 org.objectweb.joram.shared.admin.AdminReply reply, 840 AgentId replyTo, 841 String requestMsgId, 842 String replyMsgId) { 843 Message message = new Message(); 844 message.correlationId = requestMsgId; 845 message.timestamp = System.currentTimeMillis(); 846 message.setDestination(replyTo.toString(), Topic.TOPIC_TYPE); 847 message.id = replyMsgId;; 848 try { 849 message.setObject(reply); 850 ClientMessages clientMessages = new ClientMessages(-1, -1, message); 851 forward(replyTo, clientMessages); 852 } catch (Exception exc) { 853 if (logger.isLoggable(BasicLevel.ERROR)) 854 logger.log(BasicLevel.ERROR, "", exc); 855 throw new Error (exc.getMessage()); 856 } 857 } 858 859 public final void forward(AgentId to, Notification not) { 860 Channel.sendTo(to, not); 861 } 862 } 863 | Popular Tags |