00001
00024 package org.objectweb.jonas.server;
00025
00026
00027 import java.util.ArrayList;
00028 import java.util.List;
00029
00030 import javax.management.JMException;
00031 import javax.management.MBeanServer;
00032 import javax.management.MBeanServerNotification;
00033 import javax.management.Notification;
00034 import javax.management.NotificationFilter;
00035 import javax.management.NotificationListener;
00036 import javax.management.ObjectName;
00037
00038 import org.objectweb.jonas.common.Log;
00039 import org.objectweb.jonas.container.EJBService;
00040 import org.objectweb.jonas.ear.EarService;
00041 import org.objectweb.jonas.jmx.JmxService;
00042 import org.objectweb.jonas.management.AttributeAddNotification;
00043 import org.objectweb.jonas.management.AttributeRemoveNotification;
00044 import org.objectweb.jonas.management.ListenerJavaBean;
00045 import org.objectweb.jonas.management.ReconfigException;
00046 import org.objectweb.jonas.management.j2eemanagement.J2EEManagedObject;
00047 import org.objectweb.jonas.resource.ResourceService;
00048 import org.objectweb.jonas.service.ServiceManager;
00049 import org.objectweb.jonas.web.JWebContainerService;
00050 import org.objectweb.jonas_timer.TimerEvent;
00051 import org.objectweb.jonas_timer.TimerEventListener;
00052 import org.objectweb.jonas_timer.TimerManager;
00053 import org.objectweb.util.monolog.api.BasicLevel;
00054 import org.objectweb.util.monolog.api.Logger;
00055
00065 public class J2EEServer extends J2EEManagedObject implements TimerEventListener, NotificationListener {
00069 private Server server = null;
00070 private String serverName = null;
00071 private String serverVersion = null;
00072 private String serverVendor = null;
00073 private String protocols = null;
00074 private ArrayList deployedObjects = null;
00075 private ArrayList resources = null;
00076 private ArrayList javaVMs = null;
00080 private boolean activated = true;
00081
00082 private int sizeTableMeasures = 120;
00083 private int range = 10;
00084 private Long[] tableMeasures;
00085 private long maxtotalmemory;
00086 private TimerEvent mytimer = null;
00087
00091 private static Logger mgtLogger = Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX);
00095 private static Logger evtLogger = Log.getLogger(Log.JONAS_MANAGEMENT_EVENT_PREFIX);
00096
00100
00101 private long sequenceNumber = 0;
00102
00106 private ListenerJavaBean myListener = null;
00107
00108
00112 private EJBService ejbService = null;
00116 private EarService earService = null;
00120 private ResourceService rarService = null;
00124 private JWebContainerService warService = null;
00125
00126 private MBeanServer jmxServer = null;
00127
00137 public J2EEServer(String objectName, Server server, String serverName, String serverVersion
00138 , String serverVendor, String protocols) {
00139 super(objectName);
00140 this.server = server;
00141 this.serverName = serverName;
00142 this.serverVersion = serverVersion;
00143 this.serverVendor = serverVendor;
00144 this.protocols = protocols;
00145 initTable();
00146 deployedObjects = new ArrayList();
00147 resources = new ArrayList();
00148 javaVMs = new ArrayList();
00149
00150 mytimer = TimerManager.getInstance().addTimerMs(this, range * 1000, new Integer(1), true);
00151
00152
00153
00154 try {
00155 ejbService = (EJBService) ServiceManager.getInstance().getEjbService();
00156 } catch (Exception e) {
00157 }
00158 try {
00159 earService = (EarService) ServiceManager.getInstance().getEarService();
00160 } catch (Exception e) {
00161 }
00162 try {
00163 rarService = (ResourceService) ServiceManager.getInstance().getRarService();
00164 } catch (Exception e) {
00165 }
00166 try {
00167 warService = (JWebContainerService) ServiceManager.getInstance().getWebContainerService();
00168 } catch (Exception e) {
00169 }
00170 try {
00171 JmxService jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
00172 jmxServer = jmxService.getJmxServer();
00173 } catch (Exception e) {
00174 }
00175
00176
00177
00178 try {
00179 ObjectName delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
00180 jmxServer.addNotificationListener(delegate, this, null, null);
00181 } catch (JMException me) {
00182
00183
00184 throw new ReconfigException("ReconfigManager can't listen to MBeanServerNotifications because of exception: " + me.toString());
00185 }
00186 }
00187
00192 public String getServerName() {
00193 return serverName;
00194 }
00195
00200 public String getServerVersion() {
00201 return serverVersion;
00202 }
00203
00208 public String getServerVendor() {
00209 return serverVendor;
00210 }
00211
00216 public List getDeployedObjects() {
00217 return deployedObjects;
00218 }
00219
00224 public void addDeployedObject(String objectName) {
00225 deployedObjects.add(objectName);
00226 }
00227
00232 public void removeDeployedObject(String objectName) {
00233 deployedObjects.remove(objectName);
00234 }
00235
00240 public List getResources() {
00241 return resources;
00242 }
00243
00248 public List getJavaVMs() {
00249 return javaVMs;
00250 }
00251
00256 public void addJavaVM(String objectName) {
00257 javaVMs.add(objectName);
00258 }
00259
00264 public String getProtocols() {
00265 return protocols;
00266 }
00267
00271 public void stop()
00272 throws Exception {
00273 server.stop();
00274 }
00275
00279 public void runGC() {
00280 Runtime.getRuntime().gc();
00281 }
00282
00287 public long getCurrentUsedMemory() {
00288 return usedMemory();
00289 }
00290
00295 public long getCurrentTotalMemory() {
00296 return totalMemory();
00297 }
00298
00303 public void setRange(int range) {
00304 if (this.range != range) {
00305 synchronized (this) {
00306 if (range < 10) {
00307 throw new IllegalArgumentException("range could not be < 10");
00308 }
00309 this.range = range;
00310
00311 initTable();
00313 mytimer.change(this.range * 1000, new Integer(1));
00314 }
00315 }
00316 }
00317
00322 public int getRange() {
00323 return range;
00324 }
00325
00330 public void setSizeTableMeasures(int sizeMeasuresTable) {
00331 if (sizeMeasuresTable != this.sizeTableMeasures) {
00332 synchronized (this) {
00333 if (sizeMeasuresTable <= 1) {
00334 throw new IllegalArgumentException("number of measures could not be <= 1");
00335 }
00336 this.sizeTableMeasures = sizeMeasuresTable;
00337 initTable();
00338 }
00339 }
00340 }
00341
00346 public int getSizeTableMeasures() {
00347 return sizeTableMeasures;
00348 }
00349
00354 public Long[] getTableMeasures() {
00355 return tableMeasures;
00356 }
00357
00358
00359
00360
00366 public void timeoutExpired(Object arg) {
00367 int argvalue = ((Integer) arg).intValue();
00368 switch (argvalue) {
00369 case 1:
00370
00371
00372 long uvalue = usedMemory();
00373 long tmvalue = totalMemory();
00374
00375
00376 if (tmvalue > maxtotalmemory) {
00377 maxtotalmemory = tmvalue;
00378 }
00379 for (int i = 0; i < tableMeasures.length; i++) {
00380 if (i != tableMeasures.length - 1) {
00381 tableMeasures[i] = tableMeasures[i + 1];
00382 } else {
00383 tableMeasures[i] = new Long(uvalue);
00384 }
00385 }
00386 break;
00387 default:
00388
00389
00390 break;
00391 }
00392 }
00393
00398 public void setActivated(boolean pActivated) {
00399 if (pActivated) {
00400 if (!activated) {
00401 synchronized (this) {
00402 activated = true;
00403 initTable();
00404 }
00405
00406 mytimer = TimerManager.getInstance().addTimerMs(this, range * 1000, (Object) new Integer(1), true);
00407 }
00408 } else {
00409 if (activated) {
00410 synchronized (this) {
00411 activated = false;
00412 initTable();
00413
00414 mytimer.stop();
00415 mytimer.unset();
00416 }
00417 }
00418 }
00419 }
00420
00426 public boolean isActivated() {
00427 return activated;
00428 }
00429
00435 private long usedMemory() {
00436 long result = -1;
00437 while (result < 0) {
00438 result = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
00439 }
00440
00441 return result / 1000;
00442 }
00443
00449 private long totalMemory() {
00450 return Runtime.getRuntime().totalMemory() / 1000;
00451 }
00452
00457 private void initTable() {
00458 tableMeasures = new Long[sizeTableMeasures];
00459
00460 maxtotalmemory = totalMemory();
00461 for (int i = 0; i < tableMeasures.length; i++) {
00462 tableMeasures[i] = new Long(0);
00463 }
00464
00465 tableMeasures[tableMeasures.length - 1] = new Long(usedMemory());
00466 }
00467
00474 public String deployJar(String fileName) throws Exception {
00475 if (ejbService != null) {
00476 return ejbService.createContainer(fileName);
00477 } else {
00478 throw new Exception("EJB Container service not started");
00479 }
00480 }
00481
00488 public Boolean isJarDeployed(String fileName) throws Exception {
00489 if (ejbService != null) {
00490 return ejbService.isJarDeployed(fileName);
00491 } else {
00492 throw new Exception("EJB Container service not started");
00493 }
00494 }
00495
00501 public void unDeployJar(String fileName) throws Exception {
00502 if (ejbService != null) {
00503 ejbService.removeContainer(fileName);
00504 } else {
00505 throw new Exception("EJB Container service not started");
00506 }
00507 }
00508
00515 public Boolean isWarDeployed(String fileName) throws Exception {
00516 if (warService != null) {
00517 return new Boolean(warService.isWarLoaded(fileName));
00518 } else {
00519 throw new Exception("WEB Container service not started");
00520 }
00521 }
00522
00529 public void deployWar(String fileName) throws Exception {
00530 if (warService != null) {
00531 warService.registerWarMBean(fileName);
00532 } else {
00533 throw new Exception("Web Container service not started");
00534 }
00535 }
00536
00537
00543 public void unDeployWar(String fileName) throws Exception {
00544 if (warService != null) {
00545 warService.unRegisterWarMBean(fileName);
00546 } else {
00547 throw new Exception("Web Container service not started");
00548 }
00549 }
00556 public String deployEar(String fileName) throws Exception {
00557 if (earService != null) {
00558 return earService.deployEar(fileName);
00559 } else {
00560 throw new Exception("EAR Container service not started");
00561 }
00562 }
00563
00571 public Boolean isEarDeployed(String fileName) throws Exception {
00572 if (earService != null) {
00573 return earService.isEarDeployed(fileName);
00574 } else {
00575 throw new Exception("EAR Container service not started");
00576 }
00577 }
00578
00584 public void unDeployEar(String fileName) throws Exception {
00585 if (earService != null) {
00586 earService.unDeployEar(fileName);
00587 } else {
00588 throw new Exception("EAR Container service not started");
00589 }
00590 }
00591
00598 public String deployRar(String fileName) throws Exception {
00599 if (rarService != null) {
00600 return rarService.deployRar(fileName);
00601 } else {
00602 throw new Exception("Resource service not started");
00603 }
00604 }
00605
00612 public Boolean isRarDeployed(String fileName) throws Exception {
00613 if (rarService != null) {
00614 return rarService.isRarDeployed(fileName);
00615 } else {
00616 throw new Exception("Resource service not started");
00617 }
00618 }
00619
00625 public void unDeployRar(String fileName) throws Exception {
00626 if (rarService != null) {
00627 rarService.unDeployRar(fileName);
00628 } else {
00629 throw new Exception("Resource service not started");
00630 }
00631 }
00632
00637 public void addResource(String pObjectName) {
00638 synchronized (resources) {
00639 if (resources.contains(pObjectName)) {
00640 if (mgtLogger.isLoggable(BasicLevel.DEBUG)) {
00641 mgtLogger.log(BasicLevel.DEBUG
00642 , "The object name: " + pObjectName + " is already in the resources list");
00643 }
00644 } else {
00645
00646 resources.add(pObjectName);
00647
00648 AttributeAddNotification notification = new AttributeAddNotification(getObjectName()
00649 , sequenceNumber++, System.currentTimeMillis(), "", "resource"
00650 , pObjectName.toString());
00651 sendNotification(notification);
00652 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00653 evtLogger.log(BasicLevel.DEBUG
00654 , "AttributeAddNotification emitted: resource added to the J2EEServer: "
00655 + pObjectName.toString());
00656 }
00657 }
00658 }
00659 }
00660
00666 public String removeResource(String pObjectName) {
00667 String sRet = null;
00668 synchronized (resources) {
00669 int index = resources.indexOf(pObjectName);
00670 if (index > -1) {
00671
00672 sRet = (String) resources.remove(index);
00673
00674 AttributeRemoveNotification notification = new AttributeRemoveNotification(
00675 getObjectName(), sequenceNumber++, System.currentTimeMillis(), "", "resource"
00676 , pObjectName.toString());
00677 sendNotification(notification);
00678 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00679 evtLogger.log(BasicLevel.DEBUG
00680 , "AttributeRemoveNotification emitted: resource removed from the J2EEServer: "
00681 + pObjectName.toString());
00682 }
00683 }
00684 }
00685 return sRet;
00686 }
00687
00692 public void sendNotification(Notification notification) {
00693
00694 if (myListener != null) {
00695 NotificationListener listener = myListener.getListener();
00696 Object handback = myListener.getHandback();
00697 listener.handleNotification(notification, handback);
00698 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00699 evtLogger.log(BasicLevel.DEBUG, "Notification sent");
00700 }
00701 }
00702 }
00703
00711 public void addNotificationListener(NotificationListener listner, NotificationFilter filter
00712 , java.lang.Object handback)
00713 throws java.lang.IllegalArgumentException {
00714
00715 ListenerJavaBean lmb = new ListenerJavaBean(listner, filter, handback);
00716 myListener = lmb;
00717 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00718 evtLogger.log(BasicLevel.DEBUG
00719 , "Listener added for J2EEServer MBean: " + getObjectName().toString());
00720 if (handback != null) {
00721 evtLogger.log(BasicLevel.DEBUG, " - with handback: " + handback.toString());
00722 }
00723 }
00724 }
00731 public void handleNotification(Notification notification, java.lang.Object handback) {
00732 if (notification instanceof MBeanServerNotification) {
00733
00734
00735
00736 ObjectName causeObjectName = ((MBeanServerNotification) notification).getMBeanName();
00737 String causeJ2eeType = causeObjectName.getKeyProperty("j2eeType");
00738 if (causeJ2eeType != null) {
00739
00740
00741 String serverName = causeObjectName.getKeyProperty("J2EEServer");
00742 if (serverName != null) {
00743 if (!serverName.equals(getServerName())) {
00744 return;
00745 }
00746 } else {
00747 return;
00748 }
00749
00750 boolean j2eeResource = false;
00751 if (causeJ2eeType.equals("JavaMailResource")
00752 || causeJ2eeType.equals("JNDIResource")
00753 || causeJ2eeType.equals("JDBCResource")
00754 || causeJ2eeType.equals("JMSResource")
00755 || causeJ2eeType.equals("JTAResource")
00756 || causeJ2eeType.equals("JCAResource")
00757 || causeJ2eeType.equals("RMIIIOPResource")
00758 || causeJ2eeType.equals("URLResource")) {
00759 j2eeResource = true;
00760 }
00761 if (j2eeResource) {
00762 String notificationType = notification.getType();
00763 handleResourceNotification(causeObjectName, notificationType);
00764 }
00765
00766 boolean j2eeDeployed = false;
00767 if (causeJ2eeType.equals("J2EEApplication")
00768 || causeJ2eeType.equals("AppClientModule")
00769 || causeJ2eeType.equals("EJBModule")
00770 || causeJ2eeType.equals("WebModule")
00771 || causeJ2eeType.equals("ResourceAdapterModule")) {
00772 j2eeDeployed = true;
00773 }
00774 if (j2eeDeployed) {
00775 String notificationType = notification.getType();
00776 handleDeployedNotification(causeObjectName, notificationType);
00777 }
00778 }
00779 }
00780 }
00786 private void handleResourceNotification(ObjectName resourceObjectName, String notificationType) {
00787 if (notificationType.equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
00788 addResource(resourceObjectName.toString());
00789 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00790 evtLogger.log(BasicLevel.DEBUG, "Resource " + resourceObjectName.toString() + " added to J2EEServer " + getServerName());
00791 }
00792 } else if (notificationType.equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
00793 removeResource(resourceObjectName.toString());
00794 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00795 evtLogger.log(BasicLevel.DEBUG, "Resource " + resourceObjectName.toString() + " removed from J2EEServer " + getServerName());
00796 }
00797 }
00798 }
00804 private void handleDeployedNotification(ObjectName deployedObjectName, String notificationType) {
00805 if (notificationType.equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
00806 addDeployedObject(deployedObjectName.toString());
00807 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00808 evtLogger.log(BasicLevel.DEBUG, "J2EEDeployedObject " + deployedObjectName.toString() + " added to J2EEServer " + getServerName());
00809 }
00810 } else if (notificationType.equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
00811 removeDeployedObject(deployedObjectName.toString());
00812 if (evtLogger.isLoggable(BasicLevel.DEBUG)) {
00813 evtLogger.log(BasicLevel.DEBUG, "J2EEDeployedObject " + deployedObjectName.toString() + " removed from J2EEServer " + getServerName());
00814 }
00815 }
00816 }
00817 }