1 23 package com.sun.enterprise.management.j2ee; 24 25 import java.util.Set ; 26 import java.util.Map ; 27 28 import javax.management.ObjectName ; 29 import javax.management.Notification ; 30 import javax.management.NotificationListener ; 31 import javax.management.MBeanServer ; 32 import javax.management.MBeanInfo ; 33 import javax.management.MBeanAttributeInfo ; 34 import javax.management.MBeanOperationInfo ; 35 import javax.management.MBeanServerConnection ; 36 import javax.management.MBeanException ; 37 import javax.management.AttributeNotFoundException ; 38 import javax.management.InstanceNotFoundException ; 39 import javax.management.ReflectionException ; 40 import javax.management.MBeanServerInvocationHandler ; 41 42 import com.sun.appserv.management.base.Util; 43 import com.sun.appserv.management.util.misc.GSetUtil; 44 import com.sun.appserv.management.util.jmx.JMXUtil; 45 import com.sun.appserv.management.util.misc.ExceptionUtil; 46 import com.sun.appserv.management.client.ProxyFactory; 47 import com.sun.appserv.management.j2ee.J2EEServer; 48 import com.sun.appserv.management.j2ee.J2EETypes; 49 import com.sun.appserv.management.j2ee.StateManageable; 50 import com.sun.appserv.management.j2ee.J2EEServer; 51 52 import com.sun.enterprise.management.support.Delegate; 53 import com.sun.enterprise.management.support.DummyDelegate; 54 import com.sun.enterprise.management.support.LoaderMBean; 55 import com.sun.enterprise.management.support.QueryMgrImpl; 56 import com.sun.enterprise.management.support.DelegateToMBeanDelegate; 57 58 import com.sun.enterprise.admin.mbeans.DomainStatusMBean; 59 import com.sun.enterprise.admin.mbeans.DomainStatus; 60 import com.sun.enterprise.admin.mbeans.DomainStatusHelper; 61 import com.sun.enterprise.ManagementObjectManager; 62 63 import com.sun.appserv.management.j2ee.J2EETypes; 64 65 71 public class DASJ2EEServerImpl 72 extends J2EEServerImpl 73 implements NotificationListener 74 { 75 private Delegate delegate; 77 78 public 79 DASJ2EEServerImpl() 80 { 81 super( J2EETypes.J2EE_SERVER, DummyDelegate.INSTANCE ); 82 delegate = DummyDelegate.INSTANCE; 83 } 84 85 static private final Class [] DOMAIN_STATUS_INTERFACES = 86 new Class [] { DomainStatusMBean.class }; 87 88 protected DomainStatusMBean 89 getDomainStatus() 90 { 91 DomainStatusMBean domainStatus = null; 92 try { 93 final MBeanServer mbeanServer = getMBeanServer(); 94 final Set <ObjectName > candidates = QueryMgrImpl.queryPatternObjectNameSet( 95 mbeanServer, JMXUtil.newObjectNamePattern( 96 "*", DomainStatusMBean.DOMAIN_STATUS_PROPS ) ); 97 final ObjectName on = GSetUtil.getSingleton( candidates ); 98 domainStatus = (DomainStatusMBean)MBeanServerInvocationHandler. 99 newProxyInstance( mbeanServer, on, DomainStatusMBean.class, false ); 100 } catch (Exception e) { 101 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 102 getMBeanLogger().warning( rootCause.toString() + "\n" + 103 ExceptionUtil.getStackTrace( rootCause ) ); 104 } 105 return( domainStatus ); 106 } 107 108 private boolean 109 remoteServerIsRunning() 110 { 111 return (StateManageable.STATE_RUNNING == getstate()); 112 } 113 114 private boolean 115 remoteServerIsStartable() 116 { 117 int cState = getstate(); 118 119 if ((StateManageable.STATE_STOPPED == cState) || 120 (StateManageable.STATE_FAILED == cState)) 121 { 122 return true; 123 } 124 else 125 { 126 return false; 127 } 128 } 129 130 private boolean 131 remoteServerIsStoppable() 132 { 133 int cState = getstate(); 134 135 if ((StateManageable.STATE_STARTING == cState) || 136 (StateManageable.STATE_RUNNING == cState) || 137 (StateManageable.STATE_FAILED == cState)) 138 { 139 return true; 140 } 141 else 142 { 143 return false; 144 } 145 } 146 147 public void 148 handleNotification( Notification notif , Object obj) 149 { 150 final String notifType = notif.getType(); 151 152 if ( notifType.equals( DomainStatusMBean.SERVER_STATUS_NOTIFICATION_TYPE ) ) 153 { 154 final String serverName = (String ) 155 Util.getAMXNotificationValue( notif, DomainStatusMBean.SERVER_NAME_KEY ); 156 157 if ( serverName.equals( getServerName() ) ) 158 { 159 setDelegate(); 160 } 161 } 162 } 163 164 private void 165 setDelegate() 166 { 167 if ( remoteServerIsRunning() ) 168 { 169 try { 170 com.sun.enterprise.ManagementObjectManager mgmtObjManager = 172 com.sun.enterprise.Switch.getSwitch().getManagementObjectManager(); 173 174 String strON = mgmtObjManager.getServerBaseON(false, getServerName()); 175 176 177 final MBeanServerConnection remoteConn = 178 getDomainStatus().getServerMBeanServerConnection( getServerName() ); 179 180 ObjectName onPattern = new ObjectName (strON + ",*"); 181 182 final Set <ObjectName > names = JMXUtil.queryNames( remoteConn, onPattern, null); 183 184 assert( names.size() == 1 ); 185 186 final ObjectName serverON = GSetUtil.getSingleton( names ); 187 188 final Delegate delegate = 189 new DelegateToMBeanDelegate( remoteConn, serverON ); 190 191 setDelegate( delegate ); 192 setstartTime(System.currentTimeMillis()); 193 } catch (Exception e) { 194 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 195 getMBeanLogger().warning( rootCause.toString() + "\n" + 196 ExceptionUtil.getStackTrace( rootCause ) ); 197 } 198 } 199 else 200 { 201 setDelegate( DummyDelegate.INSTANCE ); 202 setstartTime(0); 203 } 204 } 205 206 public void 207 preRegisterDone() 208 throws Exception 209 { 210 super.preRegisterDone( ); 211 212 setstartTime( 0 ); 213 setDelegate(); 214 } 215 216 protected String 217 getServerName() 218 { 219 return( getSelfName() ); 220 } 221 222 public boolean 223 isstateManageable() 224 { 225 return true; 226 } 227 228 public int 229 getstate() 230 { 231 try { 232 return (getDomainStatus().getstate(getServerName())); 233 } catch (Exception e) { 234 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 235 getMBeanLogger().warning( rootCause.toString() + "\n" + 236 ExceptionUtil.getStackTrace( rootCause ) ); 237 } 238 return StateManageable.STATE_FAILED; 239 } 240 241 public void 242 start() 243 { 244 if ( remoteServerIsStartable() ) 245 { 246 startRemoteServer(); 247 } 248 else 249 { 250 throw new RuntimeException ("server is not in a startable state"); 251 } 252 } 253 254 public void 255 startRecursive() 256 { 257 start(); 258 } 259 260 public void 261 stop() 262 { 263 if ( remoteServerIsStoppable() ) 264 { 265 stopRemoteServer(); 266 } 267 else 268 { 269 throw new RuntimeException ("server is not in a stoppable state"); 270 } 271 } 272 273 274 public MBeanInfo 275 getMBeanInfo() 276 { 277 278 MBeanInfo superMBeanInfo = super.getMBeanInfo(); 279 280 if (delegate != null) 281 { 282 try { 283 DummyDelegate dd = (DummyDelegate) delegate; 284 final MBeanInfo mbeanInfo = new MBeanInfo ( 285 superMBeanInfo.getClassName(), 286 superMBeanInfo.getDescription(), 287 mergeAttributeInfos(superMBeanInfo.getAttributes(), 288 getMBeanAttributeInfo()), 289 superMBeanInfo.getConstructors(), 290 mergeOperationInfos(superMBeanInfo.getOperations(), 291 getMBeanOperationInfo()), 292 superMBeanInfo.getNotifications()); 293 return( mbeanInfo ); 294 } catch (ClassCastException cce) { 295 return superMBeanInfo; 296 } 297 } 298 else 299 { 300 return superMBeanInfo; 301 302 } 303 304 } 305 306 307 309 private MBeanAttributeInfo [] 310 mergeAttributeInfos( 311 MBeanAttributeInfo [] infos1, 312 MBeanAttributeInfo [] infos2 ) 313 { 314 final MBeanAttributeInfo [] infos = 315 new MBeanAttributeInfo [ infos1.length + infos2.length ]; 316 317 System.arraycopy( infos1, 0, infos, 0, infos1.length ); 318 System.arraycopy( infos2, 0, infos, infos1.length, infos2.length ); 319 320 return( infos ); 321 } 322 323 324 private MBeanAttributeInfo [] 325 getMBeanAttributeInfo() 326 { 327 MBeanAttributeInfo [] dAttributes = new MBeanAttributeInfo [1]; 328 dAttributes[0] = new MBeanAttributeInfo ("state", 329 "java.lang.Integer", 330 "server state", 331 true, 332 false, 333 false); 334 return dAttributes; 335 } 336 337 338 340 private MBeanOperationInfo [] 341 mergeOperationInfos( 342 MBeanOperationInfo [] infos1, 343 MBeanOperationInfo [] infos2 ) 344 { 345 final MBeanOperationInfo [] infos = 346 new MBeanOperationInfo [ infos1.length + infos2.length ]; 347 348 System.arraycopy( infos1, 0, infos, 0, infos1.length ); 349 System.arraycopy( infos2, 0, infos, infos1.length, infos2.length ); 350 351 return( infos ); 352 } 353 354 355 private MBeanOperationInfo [] 356 getMBeanOperationInfo() 357 { 358 MBeanOperationInfo [] dOperations = new MBeanOperationInfo [3]; 359 dOperations[0] = new MBeanOperationInfo ("start", 360 "start server instance", 361 null, 362 "void", 363 MBeanOperationInfo.ACTION); 364 dOperations[1] = new MBeanOperationInfo ("stop", 365 "stop server instance", 366 null, 367 "void", 368 MBeanOperationInfo.ACTION); 369 dOperations[2] = new MBeanOperationInfo ("startRecursive", 370 "start server instance", 371 null, 372 "void", 373 MBeanOperationInfo.ACTION); 374 return dOperations; 375 } 376 377 378 private void 379 startRemoteServer() 380 { 381 382 try { 383 ObjectName on = DomainStatusHelper.getServersConfigObjectName(); 385 386 388 final MBeanServer server = getMBeanServer(); 390 391 Object [] params = new Object [1]; 393 params[0] = getServerName(); 394 String [] signature = {"java.lang.String"}; 395 396 server.invoke(on, "startServerInstance", params, signature); 398 } catch (javax.management.MalformedObjectNameException mfone) { 399 final Throwable rootCause = ExceptionUtil.getRootCause( mfone ); 400 getMBeanLogger().warning( rootCause.toString() + "\n" + 401 ExceptionUtil.getStackTrace( rootCause ) ); 402 throw new RuntimeException (mfone); 403 } catch (javax.management.InstanceNotFoundException infe) { 404 final Throwable rootCause = ExceptionUtil.getRootCause( infe ); 405 getMBeanLogger().warning( rootCause.toString() + "\n" + 406 ExceptionUtil.getStackTrace( rootCause ) ); 407 throw new RuntimeException (infe); 408 } catch (javax.management.MBeanException mbe) { 409 final Throwable rootCause = ExceptionUtil.getRootCause( mbe ); 410 getMBeanLogger().warning( rootCause.toString() + "\n" + 411 ExceptionUtil.getStackTrace( rootCause ) ); 412 throw new RuntimeException (mbe); 413 } catch (javax.management.ReflectionException rfe) { 414 final Throwable rootCause = ExceptionUtil.getRootCause( rfe ); 415 getMBeanLogger().warning( rootCause.toString() + "\n" + 416 ExceptionUtil.getStackTrace( rootCause ) ); 417 throw new RuntimeException (rfe); 418 } 419 } 420 421 422 private void 423 stopRemoteServer() 424 { 425 426 try { 427 ObjectName on = DomainStatusHelper.getServersConfigObjectName(); 429 430 432 final MBeanServer server = getMBeanServer(); 434 435 Object [] params = new Object [1]; 437 params[0] = getServerName(); 438 String [] signature = {"java.lang.String"}; 439 440 server.invoke(on, "stopServerInstance", params, signature); 442 } catch (javax.management.MalformedObjectNameException mfone) { 443 final Throwable rootCause = ExceptionUtil.getRootCause( mfone ); 444 getMBeanLogger().warning( rootCause.toString() + "\n" + 445 ExceptionUtil.getStackTrace( rootCause ) ); 446 throw new RuntimeException (mfone); 447 } catch (javax.management.InstanceNotFoundException infe) { 448 final Throwable rootCause = ExceptionUtil.getRootCause( infe ); 452 getMBeanLogger().fine( rootCause.toString() + "\n" + 453 ExceptionUtil.getStackTrace( rootCause ) ); 454 throw new RuntimeException (infe); 455 } catch (javax.management.MBeanException mbe) { 456 final Throwable rootCause = ExceptionUtil.getRootCause( mbe ); 457 getMBeanLogger().warning( rootCause.toString() + "\n" + 458 ExceptionUtil.getStackTrace( rootCause ) ); 459 throw new RuntimeException (mbe); 460 } catch (javax.management.ReflectionException rfe) { 461 final Throwable rootCause = ExceptionUtil.getRootCause( rfe ); 462 getMBeanLogger().warning( rootCause.toString() + "\n" + 463 ExceptionUtil.getStackTrace( rootCause ) ); 464 throw new RuntimeException (rfe); 465 } 466 } 467 468 } 469 | Popular Tags |