1 23 package com.sun.enterprise.web.connector.extension; 24 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import javax.management.Attribute ; 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectInstance ; 30 import javax.management.j2ee.statistics.Stats ; 31 32 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistry; 33 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistrationException; 34 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel; 35 import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener; 36 import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType; 37 import com.sun.enterprise.config.serverbeans.ModuleMonitoringLevels; 38 39 import com.sun.enterprise.config.ConfigContext; 40 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 41 import com.sun.enterprise.config.serverbeans.Config; 42 import com.sun.enterprise.web.WebContainer; 43 import com.sun.enterprise.config.serverbeans.HttpService; 44 import com.sun.enterprise.config.serverbeans.HttpFileCache; 45 import com.sun.logging.LogDomains; 46 import com.sun.enterprise.config.ConfigException; 47 48 import java.util.ArrayList ; 49 import java.util.logging.Logger ; 50 import java.util.logging.Level ; 51 import java.text.MessageFormat ; 52 import javax.management.ObjectName ; 53 import javax.management.MBeanServerFactory ; 54 import javax.management.MBeanServer ; 55 56 62 public class GrizzlyConfig implements MonitoringLevelListener{ 63 private final static Logger logger 64 = LogDomains.getLogger(LogDomains.WEB_LOGGER); 65 66 69 private MBeanServer mBeanServer; 70 71 72 75 private boolean isMonitoringEnabled = false; 76 77 80 private String domain; 81 82 83 86 private int port; 87 88 89 92 private static ArrayList grizzlyConfigList = new ArrayList (); 93 94 95 97 98 101 public GrizzlyConfig(String domain, int port){ 102 this.domain = domain; 103 this.port = port; 104 105 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 107 if(!servers.isEmpty()) 108 mBeanServer = (MBeanServer )servers.get(0); 109 else 110 mBeanServer = MBeanServerFactory.createMBeanServer(); 111 112 grizzlyConfigList.add(this); 113 } 114 115 116 public void initConfig(){ 117 initMonitoringLevel(); 118 } 119 120 121 private void initMonitoringLevel() { 122 try{ 123 Config cfg = ServerBeansFactory 124 .getConfigBean( 125 WebContainer.getInstance().getServerContext().getConfigContext()); 126 127 MonitoringLevel monitoringLevel = MonitoringLevel.OFF; 129 if (cfg.getMonitoringService() != null) { 130 ModuleMonitoringLevels levels = 131 cfg.getMonitoringService().getModuleMonitoringLevels(); 132 if (levels != null) { 133 monitoringLevel = MonitoringLevel.instance( 134 levels.getHttpService()); 135 } 136 } 137 138 if(MonitoringLevel.OFF.equals(monitoringLevel)) { 139 isMonitoringEnabled = false; 140 } else { 141 isMonitoringEnabled = true; 142 } 143 144 String methodToInvoke = isMonitoringEnabled ? "enableMonitoring" : 145 "disableMonitoring"; 146 invokeGrizzly(methodToInvoke); 147 } catch (Exception ex) { 148 logger.log(Level.WARNING, 149 "selectorThread.initMonitoringException", 150 new Object []{new Integer (port),ex}); 151 } 152 } 153 154 155 public void registerMonitoringLevelEvents() { 156 MonitoringRegistry monitoringRegistry = 157 WebContainer.getInstance().getServerContext().getMonitoringRegistry(); 158 monitoringRegistry.registerMonitoringLevelListener( 159 this, MonitoredObjectType.HTTP_LISTENER); 160 } 161 162 163 public void unregisterMonitoringLevelEvents() { 164 MonitoringRegistry monitoringRegistry = 165 WebContainer.getInstance().getServerContext().getMonitoringRegistry(); 166 monitoringRegistry.unregisterMonitoringLevelListener(this); 167 } 168 169 170 public void setLevel(MonitoringLevel level) { 171 } 173 174 175 public void changeLevel(MonitoringLevel from, MonitoringLevel to, 176 MonitoredObjectType type) { 177 if (MonitoredObjectType.HTTP_LISTENER.equals(type)) { 178 if(MonitoringLevel.OFF.equals(to)) { 179 isMonitoringEnabled = false; 180 } else { 181 isMonitoringEnabled = true; 182 } 183 } 184 String methodToInvoke = isMonitoringEnabled ? "enableMonitoring" : 185 "disabledMonitoring"; 186 invokeGrizzly(methodToInvoke); 187 } 188 189 190 public void changeLevel(MonitoringLevel from, MonitoringLevel to, 191 Stats handback) { 192 } 194 195 196 private final void invokeGrizzly(String methodToInvoke) { 197 try{ 198 String onStr = domain + ":type=Selector,name=http" + port; 199 ObjectName objectName = new ObjectName (onStr); 200 mBeanServer.invoke(objectName,methodToInvoke,null,null); 201 } catch ( Exception ex ){ 202 throw new RuntimeException (ex); 203 } 204 } 205 206 207 210 public final void setEnableCallFlow(boolean enableCallFlow){ 211 String methodToInvoke = enableCallFlow ? "enableMonitoring" : 212 "disabledMonitoring"; 213 invokeGrizzly(methodToInvoke); 214 } 215 216 217 220 public static ArrayList getGrizzlyConfigInstances(){ 221 return grizzlyConfigList; 222 } 223 224 225 } 226 | Popular Tags |