1 24 package org.objectweb.jonas.server; 25 26 import java.util.ArrayList ; 28 import java.util.List ; 29 30 import javax.management.JMException ; 31 import javax.management.MBeanServer ; 32 import javax.management.MBeanServerNotification ; 33 import javax.management.Notification ; 34 import javax.management.NotificationFilter ; 35 import javax.management.NotificationListener ; 36 import javax.management.ObjectName ; 37 38 import org.objectweb.jonas.common.Log; 39 import org.objectweb.jonas.container.EJBService; 40 import org.objectweb.jonas.ear.EarService; 41 import org.objectweb.jonas.jmx.JmxService; 42 import org.objectweb.jonas.management.AttributeAddNotification; 43 import org.objectweb.jonas.management.AttributeRemoveNotification; 44 import org.objectweb.jonas.management.ListenerJavaBean; 45 import org.objectweb.jonas.management.ReconfigException; 46 import org.objectweb.jonas.management.j2eemanagement.J2EEManagedObject; 47 import org.objectweb.jonas.resource.ResourceService; 48 import org.objectweb.jonas.service.ServiceManager; 49 import org.objectweb.jonas.web.JWebContainerService; 50 import org.objectweb.jonas_timer.TimerEvent; 51 import org.objectweb.jonas_timer.TimerEventListener; 52 import org.objectweb.jonas_timer.TimerManager; 53 import org.objectweb.util.monolog.api.BasicLevel; 54 import org.objectweb.util.monolog.api.Logger; 55 56 65 public class J2EEServer extends J2EEManagedObject implements TimerEventListener, NotificationListener { 66 69 private Server server = null; 70 private String serverName = null; 71 private String serverVersion = null; 72 private String serverVendor = null; 73 private String protocols = null; 74 private ArrayList deployedObjects = null; 75 private ArrayList resources = null; 76 private ArrayList javaVMs = null; 77 80 private boolean activated = true; 81 82 private int sizeTableMeasures = 120; 83 private int range = 10; 84 private Long [] tableMeasures; 85 private long maxtotalmemory; 86 private TimerEvent mytimer = null; 87 88 91 private static Logger mgtLogger = Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX); 92 95 private static Logger evtLogger = Log.getLogger(Log.JONAS_MANAGEMENT_EVENT_PREFIX); 96 97 100 private long sequenceNumber = 0; 102 103 106 private ListenerJavaBean myListener = null; 107 108 112 private EJBService ejbService = null; 113 116 private EarService earService = null; 117 120 private ResourceService rarService = null; 121 124 private JWebContainerService warService = null; 125 126 private MBeanServer jmxServer = null; 127 128 137 public J2EEServer(String objectName, Server server, String serverName, String serverVersion 138 , String serverVendor, String protocols) { 139 super(objectName); 140 this.server = server; 141 this.serverName = serverName; 142 this.serverVersion = serverVersion; 143 this.serverVendor = serverVendor; 144 this.protocols = protocols; 145 initTable(); 146 deployedObjects = new ArrayList (); 147 resources = new ArrayList (); 148 javaVMs = new ArrayList (); 149 mytimer = TimerManager.getInstance().addTimerMs(this, range * 1000, new Integer (1), true); 151 try { 155 ejbService = (EJBService) ServiceManager.getInstance().getEjbService(); 156 } catch (Exception e) { 157 } 158 try { 159 earService = (EarService) ServiceManager.getInstance().getEarService(); 160 } catch (Exception e) { 161 } 162 try { 163 rarService = (ResourceService) ServiceManager.getInstance().getRarService(); 164 } catch (Exception e) { 165 } 166 try { 167 warService = (JWebContainerService) ServiceManager.getInstance().getWebContainerService(); 168 } catch (Exception e) { 169 } 170 try { 171 JmxService jmxService = (JmxService) ServiceManager.getInstance().getJmxService(); 172 jmxServer = jmxService.getJmxServer(); 173 } catch (Exception e) { 174 } 175 try { 179 ObjectName delegate = new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 180 jmxServer.addNotificationListener(delegate, this, null, null); 181 } catch (JMException me) { 182 throw new ReconfigException("ReconfigManager can't listen to MBeanServerNotifications because of exception: " + me.toString()); 185 } 186 } 187 188 192 public String getServerName() { 193 return serverName; 194 } 195 196 200 public String getServerVersion() { 201 return serverVersion; 202 } 203 204 208 public String getServerVendor() { 209 return serverVendor; 210 } 211 212 216 public List getDeployedObjects() { 217 return deployedObjects; 218 } 219 220 224 public void addDeployedObject(String objectName) { 225 deployedObjects.add(objectName); 226 } 227 228 232 public void removeDeployedObject(String objectName) { 233 deployedObjects.remove(objectName); 234 } 235 236 240 public List getResources() { 241 return resources; 242 } 243 244 248 public List getJavaVMs() { 249 return javaVMs; 250 } 251 252 256 public void addJavaVM(String objectName) { 257 javaVMs.add(objectName); 258 } 259 260 264 public String getProtocols() { 265 return protocols; 266 } 267 268 271 public void stop() 272 throws Exception { 273 server.stop(); 274 } 275 276 279 public void runGC() { 280 Runtime.getRuntime().gc(); 281 } 282 283 287 public long getCurrentUsedMemory() { 288 return usedMemory(); 289 } 290 291 295 public long getCurrentTotalMemory() { 296 return totalMemory(); 297 } 298 299 303 public void setRange(int range) { 304 if (this.range != range) { 305 synchronized (this) { 306 if (range < 10) { 307 throw new IllegalArgumentException ("range could not be < 10"); 308 } 309 this.range = range; 310 initTable(); 312 mytimer.change(this.range * 1000, new Integer (1)); 314 } 315 } 316 } 317 318 322 public int getRange() { 323 return range; 324 } 325 326 330 public void setSizeTableMeasures(int sizeMeasuresTable) { 331 if (sizeMeasuresTable != this.sizeTableMeasures) { 332 synchronized (this) { 333 if (sizeMeasuresTable <= 1) { 334 throw new IllegalArgumentException ("number of measures could not be <= 1"); 335 } 336 this.sizeTableMeasures = sizeMeasuresTable; 337 initTable(); 338 } 339 } 340 } 341 342 346 public int getSizeTableMeasures() { 347 return sizeTableMeasures; 348 } 349 350 354 public Long [] getTableMeasures() { 355 return tableMeasures; 356 } 357 358 366 public void timeoutExpired(Object arg) { 367 int argvalue = ((Integer ) arg).intValue(); 368 switch (argvalue) { 369 case 1: 370 371 long uvalue = usedMemory(); 373 long tmvalue = totalMemory(); 374 375 if (tmvalue > maxtotalmemory) { 377 maxtotalmemory = tmvalue; 378 } 379 for (int i = 0; i < tableMeasures.length; i++) { 380 if (i != tableMeasures.length - 1) { 381 tableMeasures[i] = tableMeasures[i + 1]; 382 } else { 383 tableMeasures[i] = new Long (uvalue); 384 } 385 } 386 break; 387 default: 388 389 break; 391 } 392 } 393 394 398 public void setActivated(boolean pActivated) { 399 if (pActivated) { 400 if (!activated) { 401 synchronized (this) { 402 activated = true; 403 initTable(); 404 } 405 mytimer = TimerManager.getInstance().addTimerMs(this, range * 1000, (Object ) new Integer (1), true); 407 } 408 } else { 409 if (activated) { 410 synchronized (this) { 411 activated = false; 412 initTable(); 413 mytimer.stop(); 415 mytimer.unset(); 416 } 417 } 418 } 419 } 420 421 425 426 public boolean isActivated() { 427 return activated; 428 } 429 430 434 435 private long usedMemory() { 436 long result = -1; 437 while (result < 0) { 438 result = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 439 } 440 441 return result / 1000; 442 } 443 444 448 449 private long totalMemory() { 450 return Runtime.getRuntime().totalMemory() / 1000; 451 } 452 453 456 457 private void initTable() { 458 tableMeasures = new Long [sizeTableMeasures]; 459 maxtotalmemory = totalMemory(); 461 for (int i = 0; i < tableMeasures.length; i++) { 462 tableMeasures[i] = new Long (0); 463 } 464 tableMeasures[tableMeasures.length - 1] = new Long (usedMemory()); 466 } 467 468 474 public String deployJar(String fileName) throws Exception { 475 if (ejbService != null) { 476 return ejbService.createContainer(fileName); 477 } else { 478 throw new Exception ("EJB Container service not started"); 479 } 480 } 481 482 488 public Boolean isJarDeployed(String fileName) throws Exception { 489 if (ejbService != null) { 490 return ejbService.isJarDeployed(fileName); 491 } else { 492 throw new Exception ("EJB Container service not started"); 493 } 494 } 495 496 501 public void unDeployJar(String fileName) throws Exception { 502 if (ejbService != null) { 503 ejbService.removeContainer(fileName); 504 } else { 505 throw new Exception ("EJB Container service not started"); 506 } 507 } 508 509 515 public Boolean isWarDeployed(String fileName) throws Exception { 516 if (warService != null) { 517 return new Boolean (warService.isWarLoaded(fileName)); 518 } else { 519 throw new Exception ("WEB Container service not started"); 520 } 521 } 522 523 529 public void deployWar(String fileName) throws Exception { 530 if (warService != null) { 531 warService.registerWarMBean(fileName); 532 } else { 533 throw new Exception ("Web Container service not started"); 534 } 535 } 536 537 538 543 public void unDeployWar(String fileName) throws Exception { 544 if (warService != null) { 545 warService.unRegisterWarMBean(fileName); 546 } else { 547 throw new Exception ("Web Container service not started"); 548 } 549 } 550 556 public String deployEar(String fileName) throws Exception { 557 if (earService != null) { 558 return earService.deployEar(fileName); 559 } else { 560 throw new Exception ("EAR Container service not started"); 561 } 562 } 563 564 571 public Boolean isEarDeployed(String fileName) throws Exception { 572 if (earService != null) { 573 return earService.isEarDeployed(fileName); 574 } else { 575 throw new Exception ("EAR Container service not started"); 576 } 577 } 578 579 584 public void unDeployEar(String fileName) throws Exception { 585 if (earService != null) { 586 earService.unDeployEar(fileName); 587 } else { 588 throw new Exception ("EAR Container service not started"); 589 } 590 } 591 592 598 public String deployRar(String fileName) throws Exception { 599 if (rarService != null) { 600 return rarService.deployRar(fileName); 601 } else { 602 throw new Exception ("Resource service not started"); 603 } 604 } 605 606 612 public Boolean isRarDeployed(String fileName) throws Exception { 613 if (rarService != null) { 614 return rarService.isRarDeployed(fileName); 615 } else { 616 throw new Exception ("Resource service not started"); 617 } 618 } 619 620 625 public void unDeployRar(String fileName) throws Exception { 626 if (rarService != null) { 627 rarService.unDeployRar(fileName); 628 } else { 629 throw new Exception ("Resource service not started"); 630 } 631 } 632 633 637 public void addResource(String pObjectName) { 638 synchronized (resources) { 639 if (resources.contains(pObjectName)) { 640 if (mgtLogger.isLoggable(BasicLevel.DEBUG)) { 641 mgtLogger.log(BasicLevel.DEBUG 642 , "The object name: " + pObjectName + " is already in the resources list"); 643 } 644 } else { 645 resources.add(pObjectName); 647 AttributeAddNotification notification = new AttributeAddNotification(getObjectName() 649 , sequenceNumber++, System.currentTimeMillis(), "", "resource" 650 , pObjectName.toString()); 651 sendNotification(notification); 652 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 653 evtLogger.log(BasicLevel.DEBUG 654 , "AttributeAddNotification emitted: resource added to the J2EEServer: " 655 + pObjectName.toString()); 656 } 657 } 658 } 659 } 660 661 666 public String removeResource(String pObjectName) { 667 String sRet = null; 668 synchronized (resources) { 669 int index = resources.indexOf(pObjectName); 670 if (index > -1) { 671 sRet = (String ) resources.remove(index); 673 AttributeRemoveNotification notification = new AttributeRemoveNotification( 675 getObjectName(), sequenceNumber++, System.currentTimeMillis(), "", "resource" 676 , pObjectName.toString()); 677 sendNotification(notification); 678 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 679 evtLogger.log(BasicLevel.DEBUG 680 , "AttributeRemoveNotification emitted: resource removed from the J2EEServer: " 681 + pObjectName.toString()); 682 } 683 } 684 } 685 return sRet; 686 } 687 688 692 public void sendNotification(Notification notification) { 693 if (myListener != null) { 695 NotificationListener listener = myListener.getListener(); 696 Object handback = myListener.getHandback(); 697 listener.handleNotification(notification, handback); 698 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 699 evtLogger.log(BasicLevel.DEBUG, "Notification sent"); 700 } 701 } 702 } 703 704 711 public void addNotificationListener(NotificationListener listner, NotificationFilter filter 712 , java.lang.Object handback) 713 throws java.lang.IllegalArgumentException { 714 ListenerJavaBean lmb = new ListenerJavaBean(listner, filter, handback); 716 myListener = lmb; 717 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 718 evtLogger.log(BasicLevel.DEBUG 719 , "Listener added for J2EEServer MBean: " + getObjectName().toString()); 720 if (handback != null) { 721 evtLogger.log(BasicLevel.DEBUG, " - with handback: " + handback.toString()); 722 } 723 } 724 } 725 731 public void handleNotification(Notification notification, java.lang.Object handback) { 732 if (notification instanceof MBeanServerNotification ) { 733 ObjectName causeObjectName = ((MBeanServerNotification ) notification).getMBeanName(); 737 String causeJ2eeType = causeObjectName.getKeyProperty("j2eeType"); 738 if (causeJ2eeType != null) { 739 String serverName = causeObjectName.getKeyProperty("J2EEServer"); 742 if (serverName != null) { 743 if (!serverName.equals(getServerName())) { 744 return; 745 } 746 } else { 747 return; 748 } 749 boolean j2eeResource = false; 751 if (causeJ2eeType.equals("JavaMailResource") 752 || causeJ2eeType.equals("JNDIResource") 753 || causeJ2eeType.equals("JDBCResource") 754 || causeJ2eeType.equals("JMSResource") 755 || causeJ2eeType.equals("JTAResource") 756 || causeJ2eeType.equals("JCAResource") 757 || causeJ2eeType.equals("RMIIIOPResource") 758 || causeJ2eeType.equals("URLResource")) { 759 j2eeResource = true; 760 } 761 if (j2eeResource) { 762 String notificationType = notification.getType(); 763 handleResourceNotification(causeObjectName, notificationType); 764 } 765 boolean j2eeDeployed = false; 767 if (causeJ2eeType.equals("J2EEApplication") 768 || causeJ2eeType.equals("AppClientModule") 769 || causeJ2eeType.equals("EJBModule") 770 || causeJ2eeType.equals("WebModule") 771 || causeJ2eeType.equals("ResourceAdapterModule")) { 772 j2eeDeployed = true; 773 } 774 if (j2eeDeployed) { 775 String notificationType = notification.getType(); 776 handleDeployedNotification(causeObjectName, notificationType); 777 } 778 } 779 } 780 } 781 786 private void handleResourceNotification(ObjectName resourceObjectName, String notificationType) { 787 if (notificationType.equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) { 788 addResource(resourceObjectName.toString()); 789 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 790 evtLogger.log(BasicLevel.DEBUG, "Resource " + resourceObjectName.toString() + " added to J2EEServer " + getServerName()); 791 } 792 } else if (notificationType.equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) { 793 removeResource(resourceObjectName.toString()); 794 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 795 evtLogger.log(BasicLevel.DEBUG, "Resource " + resourceObjectName.toString() + " removed from J2EEServer " + getServerName()); 796 } 797 } 798 } 799 804 private void handleDeployedNotification(ObjectName deployedObjectName, String notificationType) { 805 if (notificationType.equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) { 806 addDeployedObject(deployedObjectName.toString()); 807 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 808 evtLogger.log(BasicLevel.DEBUG, "J2EEDeployedObject " + deployedObjectName.toString() + " added to J2EEServer " + getServerName()); 809 } 810 } else if (notificationType.equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) { 811 removeDeployedObject(deployedObjectName.toString()); 812 if (evtLogger.isLoggable(BasicLevel.DEBUG)) { 813 evtLogger.log(BasicLevel.DEBUG, "J2EEDeployedObject " + deployedObjectName.toString() + " removed from J2EEServer " + getServerName()); 814 } 815 } 816 } 817 } 818 | Popular Tags |