| 1 | // jademx - JADE management using JMX |
| 2 | // Copyright 2005 Caboodle Networks, Inc. |
| 3 | // |
| 4 | // This library is free software; you can redistribute it and/or |
| 5 | // modify it under the terms of the GNU Lesser General Public |
| 6 | // License as published by the Free Software Foundation; either |
| 7 | // version 2.1 of the License, or (at your option) any later version. |
| 8 | // |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | // Lesser General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Lesser General Public |
| 15 | // License along with this library; if not, write to the Free Software |
| 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 17 | |
| 18 | package jade.jademx.mbean; |
| 19 | |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.Properties; |
| 23 | |
| 24 | import javax.management.InstanceAlreadyExistsException; |
| 25 | import javax.management.InstanceNotFoundException; |
| 26 | import javax.management.MBeanNotificationInfo; |
| 27 | import javax.management.MBeanRegistrationException; |
| 28 | import javax.management.MalformedObjectNameException; |
| 29 | import javax.management.NotCompliantMBeanException; |
| 30 | import javax.management.Notification; |
| 31 | import javax.management.ObjectName; |
| 32 | |
| 33 | import jade.util.Logger; |
| 34 | import jade.wrapper.AgentController; |
| 35 | import jade.wrapper.ControllerException; |
| 36 | import jade.wrapper.PlatformController; |
| 37 | import jade.wrapper.PlatformEvent; |
| 38 | |
| 39 | /** |
| 40 | * Implementation class for JadePlatformMBean. |
| 41 | * @author David Bernstein, <a href="http://www.caboodlenetworks.com" |
| 42 | * >Caboodle Networks, Inc.</a> |
| 43 | */ |
| 44 | public class JadePlatform extends JadeBase |
| 45 | implements JadePlatformMBean, PlatformController.Listener { |
| 46 | |
| 47 | /** JadeRuntime MBean superior to this JadePlatform MBean */ |
| 48 | private JadeRuntime jadeRuntime; |
| 49 | |
| 50 | /** platform controller this MBean represents */ |
| 51 | private PlatformController platformController = null; |
| 52 | |
| 53 | /** a map on agent nicknames to their MBeans. */ |
| 54 | private HashMap agentMap = new HashMap(); |
| 55 | |
| 56 | /** my logger */ |
| 57 | private final Logger logger = |
| 58 | Logger.getMyLogger(JadePlatform.class.getName()); |
| 59 | |
| 60 | /** my thread */ |
| 61 | private Thread thread; |
| 62 | |
| 63 | /** my thread group name */ |
| 64 | private String threadGroupName; |
| 65 | |
| 66 | /** my thread name */ |
| 67 | private String threadName; |
| 68 | |
| 69 | /** my thread id: default to -1 if not available */ |
| 70 | private long threadId = -1; |
| 71 | |
| 72 | // /** |
| 73 | // * Construct a JADE platform MBean implementation. |
| 74 | // * If using this constructor, then platform controller must be set |
| 75 | // * before doing anything else. |
| 76 | // * @param jadeRuntime runtime mbean superior to this platform mbean |
| 77 | // * @param name MBean name property |
| 78 | // * @throws ControllerException |
| 79 | // * @throws NotCompliantMBeanException |
| 80 | // * @throws MBeanRegistrationException |
| 81 | // * @throws InstanceAlreadyExistsException |
| 82 | // * @throws MalformedObjectNameException |
| 83 | // */ |
| 84 | // public JadePlatform( JadeRuntime jadeRuntime, String name ) |
| 85 | // throws MalformedObjectNameException, InstanceAlreadyExistsException, |
| 86 | // MBeanRegistrationException, NotCompliantMBeanException, |
| 87 | // ControllerException{ |
| 88 | // this( jadeRuntime, name, null, null ); |
| 89 | // } |
| 90 | |
| 91 | /** |
| 92 | * Construct a JADE platform MBean implementation and register with server. |
| 93 | * @param jadeRuntime runtime mbean superior to this platform mbean |
| 94 | * @param name MBean name property |
| 95 | * @param platformController platform controller to set for this MBean |
| 96 | * @param platformThread platform's thread |
| 97 | * @throws ControllerException on error setting platform controller |
| 98 | * @throws NotCompliantMBeanException |
| 99 | * @throws MBeanRegistrationException |
| 100 | * @throws InstanceAlreadyExistsException |
| 101 | * @throws MalformedObjectNameException |
| 102 | */ |
| 103 | public JadePlatform( |
| 104 | JadeRuntime jadeRuntime, String name, |
| 105 | PlatformController platformController, |
| 106 | Thread platformThread ) |
| 107 | throws ControllerException, MalformedObjectNameException, |
| 108 | InstanceAlreadyExistsException, MBeanRegistrationException, |
| 109 | NotCompliantMBeanException { |
| 110 | super( jadeRuntime.getJadeFactory(), TYPE, name ); |
| 111 | this.jadeRuntime = jadeRuntime; |
| 112 | addNotificationInfo( mBeanNotificationInfo ); |
| 113 | setPlatformControllerAndThread( platformController, platformThread ); |
| 114 | Properties p = new Properties(); |
| 115 | p.setProperty( PROP_RUNTIME , jadeRuntime.getName() ); |
| 116 | jadeRuntime.addPlatform( this ); |
| 117 | register( name, p ); |
| 118 | logger.log( Logger.CONFIG, "instantiated JadePlatform "+this); |
| 119 | } |
| 120 | |
| 121 | /** The notification set. */ |
| 122 | private final static String notifications[] = { |
| 123 | NOTIFICATION_BORN_AGENT, |
| 124 | NOTIFICATION_DEAD_AGENT //, |
| 125 | //NOTIFICATION_STARTED_PLATFORM, |
| 126 | //NOTIFICATION_SUSPENDED_PLATFORM, |
| 127 | //NOTIFICATION_RESUMED_PLATFORM, |
| 128 | //NOTIFICATION_KILLED_PLATFORM |
| 129 | }; |
| 130 | |
| 131 | /** description for MBean notification info */ |
| 132 | private final static String NOTIFICATION_INFO_DESCRIPTION = |
| 133 | "notification set for " + JadePlatformMBean.class.getName(); |
| 134 | |
| 135 | /** mBeanNotificationInfo for this class */ |
| 136 | private final static MBeanNotificationInfo mBeanNotificationInfo = |
| 137 | new MBeanNotificationInfo(notifications, |
| 138 | Notification.class.getName(), |
| 139 | NOTIFICATION_INFO_DESCRIPTION ); |
| 140 | |
| 141 | /** |
| 142 | * set platform controller for this MBean. |
| 143 | * must be set via this method or constructor before can do anything. |
| 144 | * @param controller platform's controller |
| 145 | * @param thread platform's thread |
| 146 | */ |
| 147 | private void setPlatformControllerAndThread( |
| 148 | PlatformController controller, Thread thread ) |
| 149 | throws ControllerException { |
| 150 | |
| 151 | // if ( logger.isLoggable( Logger.FINE ) ) { |
| 152 | logger.log( Logger.FINE, "platform name="+ |
| 153 | ((null==controller)?null:controller.getName()) ); |
| 154 | // } |
| 155 | |
| 156 | if ( null != platformController ) { |
| 157 | platformController.removePlatformListener( this ); |
| 158 | } |
| 159 | platformController = controller; |
| 160 | if ( null != platformController ) { |
| 161 | platformController.addPlatformListener( this ); |
| 162 | } |
| 163 | this.thread = thread; |
| 164 | if ( null != thread ) { |
| 165 | try { |
| 166 | threadId = thread.getId(); |
| 167 | } |
| 168 | catch ( Throwable t ) { |
| 169 | // intentionally empty |
| 170 | // Thread.getId() showed up with Java 1.5, use default -1 |
| 171 | } |
| 172 | threadName = thread.getName(); |
| 173 | ThreadGroup threadGroup = thread.getThreadGroup(); |
| 174 | threadGroupName = threadGroup.getName(); |
| 175 | } |
| 176 | jadeName = ( ( null == platformController ) |
| 177 | ? null |
| 178 | : platformController.getName() ); |
| 179 | } |
| 180 | |
| 181 | /** store this so can be used after platform gone while MBean going away */ |
| 182 | private String jadeName = null; |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * get the JADE platform controller for this platform. |
| 187 | * access only for this package |
| 188 | * @return the JADE platform controller for this platform. |
| 189 | */ |
| 190 | PlatformController getPlatformController() { |
| 191 | return platformController; |
| 192 | } |
| 193 | |
| 194 | ///** |
| 195 | // * service method to make sure platform controller is set. |
| 196 | // * give an obvious message if not. |
| 197 | // */ |
| 198 | //private void checkPlatformController() { |
| 199 | // if ( null == platformController ) { |
| 200 | // throw new IllegalStateException("platform controller not set"); |
| 201 | // } |
| 202 | //} |
| 203 | |
| 204 | // BEGIN PlatformController.Listener IMPLEMENTATION |
| 205 | |
| 206 | /* (non-Javadoc) |
| 207 | * @see jade.wrapper.PlatformController.Listener#bornAgent( |
| 208 | * jade.wrapper.PlatformEvent) |
| 209 | */ |
| 210 | public void bornAgent(PlatformEvent anEvent) { |
| 211 | String agentGUID = anEvent.getAgentGUID(); |
| 212 | // if ( logger.isLoggable( Logger.FINE ) ) { |
| 213 | logger.log( Logger.FINE, agentGUID + " agent born"); |
| 214 | // } |
| 215 | try { |
| 216 | String agentNick = agentLocalName( agentGUID ); |
| 217 | AgentController agentController = |
| 218 | platformController.getAgent( agentNick ); |
| 219 | JadeAgent jadeAgent = |
| 220 | makeOrFindAgentMBean( agentController, agentNick, null, null ); |
| 221 | // register this agent's mbean with the mbean server |
| 222 | Properties p = new Properties(); |
| 223 | p.setProperty( PROP_RUNTIME, jadeRuntime.getName() ); |
| 224 | p.setProperty( PROP_PLATFORM, this.getName() ); |
| 225 | jadeAgent.register( agentNick, p ); |
| 226 | } |
| 227 | catch ( Exception e ) { |
| 228 | logger.log( Logger.SEVERE, |
| 229 | "problem making and recording MBean "+ |
| 230 | "upon agent birth of agent "+agentGUID, e ); |
| 231 | } |
| 232 | notifyListeners( NOTIFICATION_BORN_AGENT, null, anEvent ); |
| 233 | } |
| 234 | /* (non-Javadoc) |
| 235 | * @see jade.wrapper.PlatformController.Listener#deadAgent( |
| 236 | * jade.wrapper.PlatformEvent) |
| 237 | */ |
| 238 | public void deadAgent(PlatformEvent anEvent) { |
| 239 | String agentGUID = anEvent.getAgentGUID(); |
| 240 | String agentNick = agentLocalName( agentGUID ); |
| 241 | synchronized ( agentMap ) { |
| 242 | JadeAgent jadeAgent = (JadeAgent)agentMap.get( agentNick ); |
| 243 | if ( null != jadeAgent ) { |
| 244 | try { |
| 245 | jadeAgent.unregister(); |
| 246 | } |
| 247 | catch ( InstanceNotFoundException infe ) { |
| 248 | // there was no instance to unregister. ignore. |
| 249 | } |
| 250 | catch ( MBeanRegistrationException mbre ) { |
| 251 | logger.log( Logger.SEVERE, |
| 252 | "problem unregistering MBean "+jadeAgent+ |
| 253 | " upon agent death", mbre ); |
| 254 | } |
| 255 | } |
| 256 | agentMap.remove( agentNick ); |
| 257 | } |
| 258 | notifyListeners( NOTIFICATION_DEAD_AGENT, null, anEvent ); |
| 259 | } |
| 260 | |
| 261 | // need following to implement PlatformController.Listener |
| 262 | |
| 263 | /* (non-Javadoc) |
| 264 | * @see jade.wrapper.PlatformController.Listener#killedPlatform( |
| 265 | * jade.wrapper.PlatformEvent) |
| 266 | */ |
| 267 | public void killedPlatform(PlatformEvent anEvent) { |
| 268 | //notifyListeners( NOTIFICATION_KILLED_PLATFORM, null, anEvent ); |
| 269 | } |
| 270 | /* (non-Javadoc) |
| 271 | * @see jade.wrapper.PlatformController.Listener#resumedPlatform( |
| 272 | * jade.wrapper.PlatformEvent) |
| 273 | */ |
| 274 | public void resumedPlatform(PlatformEvent anEvent) { |
| 275 | //notifyListeners( NOTIFICATION_RESUMED_PLATFORM, null, anEvent ); |
| 276 | } |
| 277 | /* (non-Javadoc) |
| 278 | * @see jade.wrapper.PlatformController.Listener#startedPlatform( |
| 279 | * jade.wrapper.PlatformEvent) |
| 280 | */ |
| 281 | public void startedPlatform(PlatformEvent anEvent) { |
| 282 | //notifyListeners( NOTIFICATION_STARTED_PLATFORM, null, anEvent ); |
| 283 | } |
| 284 | /* (non-Javadoc) |
| 285 | * @see jade.wrapper.PlatformController.Listener#suspendedPlatform( |
| 286 | * jade.wrapper.PlatformEvent) |
| 287 | */ |
| 288 | public void suspendedPlatform(PlatformEvent anEvent) { |
| 289 | //notifyListeners( NOTIFICATION_SUSPENDED_PLATFORM, null, anEvent ); |
| 290 | } |
| 291 | |
| 292 | // END PlatformController.Listener IMPLEMENTATION |
| 293 | |
| 294 | /** |
| 295 | * return local name for given agent name |
| 296 | * @param agentName agent name to get nickname for |
| 297 | * @return local name for given agent name |
| 298 | */ |
| 299 | private String agentLocalName( String agentName ) { |
| 300 | // there should be a JADE-supported way of extracting the local name |
| 301 | int atPos = agentName.lastIndexOf('@'); |
| 302 | String aName = agentName; |
| 303 | if( -1 != atPos ) { |
| 304 | aName = agentName.substring(0, atPos); |
| 305 | } |
| 306 | return aName; |
| 307 | } |
| 308 | |
| 309 | // ATTRIBUTES DESCRIBING CHILD MBEANS |
| 310 | |
| 311 | /* (non-Javadoc) |
| 312 | * @see jade.jademx.mbean.JadePlatformMBean#getAgents() |
| 313 | */ |
| 314 | public JadeAgent[] getAgents() { |
| 315 | JadeAgent agents[]; |
| 316 | synchronized ( agentMap ) { |
| 317 | int agentCount = agentMap.size(); |
| 318 | agents = new JadeAgent[agentCount]; |
| 319 | Iterator agentIter = agentMap.values().iterator(); |
| 320 | int agentIndex = 0; |
| 321 | while ( agentIter.hasNext() ) { |
| 322 | agents[agentIndex++] = (JadeAgent)agentIter.next(); |
| 323 | } |
| 324 | } |
| 325 | return agents; |
| 326 | } |
| 327 | |
| 328 | /* (non-Javadoc) |
| 329 | * @see jade.jademx.mbean.JadePlatformMBean#getAgents() |
| 330 | */ |
| 331 | public String[] getAgentNames() { |
| 332 | String agents[]; |
| 333 | synchronized ( agentMap ) { |
| 334 | int agentCount = agentMap.size(); |
| 335 | agents = new String[agentCount]; |
| 336 | Iterator agentIter = agentMap.values().iterator(); |
| 337 | int agentIndex = 0; |
| 338 | while ( agentIter.hasNext() ) { |
| 339 | agents[agentIndex++] = agentIter.next().toString(); |
| 340 | } |
| 341 | } |
| 342 | return agents; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /* (non-Javadoc) |
| 347 | * @see jade.jademx.mbean.JadePlatformMBean#getAgentObjectNames() |
| 348 | */ |
| 349 | public ObjectName[] getAgentObjectNames() { |
| 350 | ObjectName agents[]; |
| 351 | synchronized ( agentMap ) { |
| 352 | int agentCount = agentMap.size(); |
| 353 | agents = new ObjectName[agentCount]; |
| 354 | Iterator agentIter = agentMap.values().iterator(); |
| 355 | int agentIndex = 0; |
| 356 | while ( agentIter.hasNext() ) { |
| 357 | agents[agentIndex++] = |
| 358 | ((JadeAgent)agentIter.next()).getObjectName(); |
| 359 | } |
| 360 | } |
| 361 | return agents; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | // BEGIN PlatformController MEDIATION |
| 366 | |
| 367 | |
| 368 | /* (non-Javadoc) |
| 369 | * @see jade.management.mbean.JadePlatformMBean#getAgent(java.lang.String) |
| 370 | */ |
| 371 | public JadeAgent getAgent(String localAgentName) throws JademxException { |
| 372 | //checkPlatformController(); |
| 373 | JadeAgent jadeAgent; |
| 374 | try { |
| 375 | AgentController agentController = |
| 376 | platformController.getAgent( localAgentName ); |
| 377 | //String agentName = agentController.getName(); |
| 378 | jadeAgent = |
| 379 | makeOrFindAgentMBean( agentController, localAgentName, |
| 380 | null, null ); |
| 381 | } |
| 382 | catch ( Exception e ) { |
| 383 | throw new JademxException("exception getting agent \""+ |
| 384 | localAgentName+"\"", e ); |
| 385 | } |
| 386 | return jadeAgent; |
| 387 | } |
| 388 | |
| 389 | /* (non-Javadoc) |
| 390 | * @see jade.jademx.mbean.JadePlatformMBean#getAgentName(java.lang.String) |
| 391 | */ |
| 392 | public String getAgentName( String localAgentName ) throws JademxException { |
| 393 | JadeAgent jadeAgent = getAgent( localAgentName ); |
| 394 | String agentName = jadeAgent.toString(); |
| 395 | return agentName; |
| 396 | } |
| 397 | |
| 398 | /* (non-Javadoc) |
| 399 | * @see jade.jademx.mbean.JadePlatformMBean#getAgentObjectName( |
| 400 | * java.lang.String) |
| 401 | */ |
| 402 | public ObjectName getAgentObjectName( String localAgentName ) |
| 403 | throws JademxException { |
| 404 | JadeAgent jadeAgent = getAgent( localAgentName ); |
| 405 | ObjectName agentObjectName = jadeAgent.getObjectName(); |
| 406 | return agentObjectName; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * get agent's MBean (name), creating if doesn't previously exist |
| 411 | * @param agentController agent's controller |
| 412 | * @param nickname local name for agent |
| 413 | * @param className agent's class name |
| 414 | * @param args agent's startup arguments |
| 415 | * @return agent's MBean name |
| 416 | * @throws JademxException problem making agent's mbean |
| 417 | */ |
| 418 | private JadeAgent makeOrFindAgentMBean( |
| 419 | AgentController agentController, |
| 420 | String nickname, |
| 421 | String className, |
| 422 | Object[] args ) |
| 423 | throws JademxException { |
| 424 | synchronized ( agentMap ) { |
| 425 | JadeAgent jadeAgent = (JadeAgent)agentMap.get( nickname ); |
| 426 | if ( null == jadeAgent ) { |
| 427 | // if ( logger.isLoggable( Logger.FINE ) ) { |
| 428 | logger.log( Logger.FINE, "in platform "+this+ |
| 429 | " didn't find agent with nickname \""+nickname+ |
| 430 | "\" so constructing its MBean"); |
| 431 | // } |
| 432 | jadeAgent = constructAgentMBean( |
| 433 | agentController, nickname, className, args ); |
| 434 | } |
| 435 | return jadeAgent; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * get agent's MBean, creating if doesn't previously exist. |
| 441 | * also, if the agent is a jademx agent, then the agent and jademx |
| 442 | * get connected. |
| 443 | * @param agentController agent's controller |
| 444 | * @param nickname local name for agent |
| 445 | * @param className agent's class name |
| 446 | * @param args agent's startup arguments |
| 447 | * @return constructed agent mbean |
| 448 | * @throws JademxException problem making agent's mbean |
| 449 | */ |
| 450 | JadeAgent constructAgentMBean( |
| 451 | AgentController agentController, |
| 452 | String nickname, |
| 453 | String className, |
| 454 | Object[] args ) throws JademxException{ |
| 455 | |
| 456 | // if ( logger.isLoggable( Logger.FINE ) ) { |
| 457 | logger.log( Logger.FINE, |
| 458 | "entering JadePlatform.constructAndRegisterAgentMBean()..." ); |
| 459 | // } |
| 460 | //if ( logger.isLoggable( Logger.INFO ) ) { |
| 461 | // java.io.StringWriter sw = new java.io.StringWriter(); |
| 462 | // java.io.PrintWriter pw = new java.io.PrintWriter( sw ); |
| 463 | // Throwable t = new Throwable(); |
| 464 | // t.printStackTrace( pw ); |
| 465 | // logger.log( Logger.INFO, "constructAgentMBean call stack: "+sw ); |
| 466 | // pw.flush(); |
| 467 | //} |
| 468 | |
| 469 | JadeAgent jadeAgent = null; |
| 470 | |
| 471 | synchronized ( agentMap ) { |
| 472 | |
| 473 | jadeAgent = (JadeAgent)agentMap.get( nickname ); |
| 474 | if ( null != jadeAgent ) { |
| 475 | throw new JademxException("platform "+this+ |
| 476 | " already has agent named \""+nickname+"\""); |
| 477 | } |
| 478 | |
| 479 | jadeAgent = |
| 480 | new JadeAgent( this, |
| 481 | agentController, |
| 482 | getMBeanName(), |
| 483 | nickname, |
| 484 | className, |
| 485 | args); |
| 486 | jadeAgent.setParentObjectName( getObjectName() ); |
| 487 | // JadePlatform.bornAgent() does MBean registration |
| 488 | |
| 489 | agentMap.put( nickname, jadeAgent ); |
| 490 | } |
| 491 | return jadeAgent; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | // WHY DID I WANT THIS? |
| 496 | // |
| 497 | // /** |
| 498 | // * return extra properties for MBean relative to platform |
| 499 | // * @return extra properties for MBean relative to platform |
| 500 | // */ |
| 501 | // public Properties platformProps() { |
| 502 | // Properties extraProps = new Properties(); |
| 503 | // extraProps.setProperty( |
| 504 | // JadeRuntime.OBJECT_NAME_PLATFORM_PROPERTY_NAME, |
| 505 | // ObjectName.quote( getMBeanName() ) ); |
| 506 | // return extraProps; |
| 507 | // } |
| 508 | |
| 509 | /* (non-Javadoc) |
| 510 | * @see jade.management.mbean.JadePlatformMBean#createNewAgent( |
| 511 | * java.lang.String, java.lang.String, java.lang.Object[]) |
| 512 | */ |
| 513 | public JadeAgent createNewAgent( |
| 514 | String nickname, String className, Object[] args) |
| 515 | throws JademxException { |
| 516 | //checkPlatformController(); |
| 517 | |
| 518 | // use the following ordering: |
| 519 | // |
| 520 | // 1. construct the agent mbean, remembering it in platform, but not |
| 521 | // registering it with the mbeanserver |
| 522 | // 2. create the agent |
| 523 | // 3. start the agent (starts new thread, calling Agent.setup(), which |
| 524 | // will look for the agent mbean to exchange references) |
| 525 | // 4. the agent mbean will be registered with the mbeanserver |
| 526 | // upon notification that it has been born |
| 527 | // |
| 528 | // this way there exists an mbean for the agent to exchange references |
| 529 | // with during its setup() method, but it's not visible from the |
| 530 | // mbean server until after it has started. |
| 531 | |
| 532 | // STATE: |
| 533 | // agent: doesn't exist yet |
| 534 | // MBean: doesn't exist yet |
| 535 | |
| 536 | // construct the agent's MBean. it isn't registered until it is born. |
| 537 | JadeAgent jadeAgent = |
| 538 | constructAgentMBean( null, nickname, className, args ); |
| 539 | |
| 540 | // STATE: |
| 541 | // agent: doesn't exist yet |
| 542 | // MBean: exists, known by platform MBean, not yet registered |
| 543 | |
| 544 | // create agent |
| 545 | AgentController agentController = null; |
| 546 | try { |
| 547 | agentController = |
| 548 | getPlatformController().createNewAgent( |
| 549 | nickname, className, args ); |
| 550 | // STATE: |
| 551 | // agent: created, not yet started |
| 552 | // MBean: exists, known by platform MBean, |
| 553 | // doesn't yet have reference to agent, |
| 554 | // registered asynchronously by JadePlatform.bornAgent() |
| 555 | |
| 556 | jadeAgent.setAgentController( agentController ); |
| 557 | |
| 558 | } |
| 559 | catch ( Throwable t ) { |
| 560 | String msg = "problem creating JADE agent "+nickname + |
| 561 | " for platform "+this; |
| 562 | logger.log( Logger.SEVERE, msg, t ); |
| 563 | synchronized ( agentMap ) { |
| 564 | agentMap.remove( nickname ); |
| 565 | } |
| 566 | throw new JademxException( msg, t ); |
| 567 | } |
| 568 | |
| 569 | // STATE: |
| 570 | // agent: created, not yet started |
| 571 | // MBean: exists, known by platform MBean, |
| 572 | // has reference to agent, |
| 573 | // registered asynchronously by JadePlatform.bornAgent() |
| 574 | |
| 575 | // start up agent |
| 576 | try { |
| 577 | agentController.start(); |
| 578 | } |
| 579 | catch ( Throwable t ) { |
| 580 | String msg = "problem starting agent "+nickname+ |
| 581 | " in platform "+this; |
| 582 | logger.log( Logger.SEVERE, msg, t ); |
| 583 | synchronized ( agentMap ) { |
| 584 | agentMap.remove( nickname ); |
| 585 | } |
| 586 | throw new JademxException( msg, t ); |
| 587 | } |
| 588 | |
| 589 | // STATE: |
| 590 | // agent: started |
| 591 | // MBean: exists, known by platform MBean, |
| 592 | // has reference to agent, |
| 593 | // registered asynchronously by JadePlatform.bornAgent() |
| 594 | |
| 595 | return jadeAgent; |
| 596 | |
| 597 | } |
| 598 | |
| 599 | /* (non-Javadoc) |
| 600 | * @see jade.jademx.mbean.JadePlatformMBean#createNewAgentReturnName( |
| 601 | * java.lang.String, java.lang.String, java.lang.Object[]) |
| 602 | */ |
| 603 | public String createNewAgentReturnName( |
| 604 | String nickname, String className, Object[] args) |
| 605 | throws JademxException { |
| 606 | JadeAgent jadeAgent = createNewAgent( nickname, className, args ); |
| 607 | String agentName = jadeAgent.toString(); |
| 608 | return agentName; |
| 609 | } |
| 610 | |
| 611 | /* (non-Javadoc) |
| 612 | * @see jade.management.mbean.JadePlatformMBean#getPlatformName() |
| 613 | */ |
| 614 | public String getPlatformName() { |
| 615 | return jadeName; |
| 616 | } |
| 617 | /* (non-Javadoc) |
| 618 | * @see jade.management.mbean.JadePlatformMBean#getStateCode() |
| 619 | */ |
| 620 | public int getStateCode() { |
| 621 | //checkPlatformController(); |
| 622 | return platformController.getState().getCode(); |
| 623 | } |
| 624 | /* (non-Javadoc) |
| 625 | * @see jade.management.mbean.JadePlatformMBean#getStateName() |
| 626 | */ |
| 627 | public String getStateName() { |
| 628 | //checkPlatformController(); |
| 629 | return platformController.getState().getName(); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * unregister all the agents for this platform |
| 634 | * @throws JademxException |
| 635 | */ |
| 636 | private void unregisterAgents() throws JademxException { |
| 637 | JademxException je = null; |
| 638 | JadeAgent jadeAgents[] = getAgents(); |
| 639 | for ( int i = 0; i < jadeAgents.length; i++ ) { |
| 640 | JadeAgent jadeAgent = jadeAgents[i]; |
| 641 | // if ( logger.isLoggable( Logger.FINE ) ) { |
| 642 | logger.log( Logger.FINE, ((jadeAgent.isRegistered())?"":"un")+ |
| 643 | "registered agent "+i+": "+jadeAgent); |
| 644 | // } |
| 645 | if ( jadeAgent.isRegistered() ) { |
| 646 | try { |
| 647 | jadeAgent.unregister(); |
| 648 | } |
| 649 | catch ( InstanceNotFoundException e ) { |
| 650 | // didn't find it while trying to get rid of it: ignore |
| 651 | } |
| 652 | catch ( Exception e ) { |
| 653 | if ( null != je ) { |
| 654 | logger.log( Logger.SEVERE, |
| 655 | "subsequent exception unregistering agent \""+ |
| 656 | jadeAgent+"\"", e ); |
| 657 | } |
| 658 | else { |
| 659 | je = new JademxException( |
| 660 | "exception unregistering agent \""+ |
| 661 | jadeAgent+"\"", e ); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | |
| 669 | |
| 670 | /* (non-Javadoc) |
| 671 | * @see jade.management.mbean.JadePlatformMBean#kill() |
| 672 | */ |
| 673 | public void kill() throws JademxException { |
| 674 | logger.log( Logger.FINE, "entering for "+this+" ..."); |
| 675 | JademxException je = null; |
| 676 | |
| 677 | // in this method, try to be as thorough as possible in cleaning up, |
| 678 | // so even if an exception is thrown, catch it and keep on going. |
| 679 | // the first exception caught is re-thrown, subsequent exceptions are |
| 680 | // logged |
| 681 | |
| 682 | // unregister the platform's agents from the MBeanServer |
| 683 | logger.log( Logger.FINE, "about to unregister agents for "+this); |
| 684 | try { |
| 685 | unregisterAgents(); |
| 686 | } |
| 687 | catch ( Exception e ) { |
| 688 | // commented out while 1st opportunity for exception |
| 689 | //if ( null != je ) { |
| 690 | // logger.log( Logger.SEVERE, |
| 691 | // "subsequent exception unregistering agents ", e ); |
| 692 | //} |
| 693 | //else { |
| 694 | je = new JademxException("exception unregistering agents", e ); |
| 695 | //} |
| 696 | } |
| 697 | |
| 698 | // unregister the platform from the MBeanServer |
| 699 | |
| 700 | logger.log( Logger.FINE, "about to unregister "+this); |
| 701 | try { |
| 702 | unregister(); |
| 703 | logger.log( Logger.FINER, "did unregister "+this); |
| 704 | } |
| 705 | catch ( InstanceNotFoundException infe ) { |
| 706 | // intentionally empty, there was no instance to unregister |
| 707 | } |
| 708 | catch ( MBeanRegistrationException mbre ) { |
| 709 | if ( null != je ) { |
| 710 | logger.log( Logger.SEVERE, "subsequent exception", mbre ); |
| 711 | } |
| 712 | else { |
| 713 | je = new JademxException("exception unregistering platform "+ |
| 714 | this, mbre ); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // unregister from JadeRuntime |
| 719 | jadeRuntime.removePlatform( getPlatformName() ); |
| 720 | |
| 721 | // kill the JADE platform via its controller |
| 722 | logger.log( Logger.FINER, "entering platform controller kill try block ..."); |
| 723 | try { |
| 724 | //checkPlatformController(); |
| 725 | logger.log(Logger.FINER,"calling platformController.kill()..."); |
| 726 | platformController.kill(); |
| 727 | } |
| 728 | catch ( Exception e) { |
| 729 | |
| 730 | if ( null != je ) { |
| 731 | logger.log( Logger.SEVERE, "subsequent exception", e ); |
| 732 | } |
| 733 | else { |
| 734 | // logging because may contain clue to RMI unbind problem |
| 735 | logger.log( Logger.SEVERE, "exception killing platform", e ); |
| 736 | je = new JademxException("exception killing platform \""+this+ |
| 737 | "\"", e ); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // interrupt the container's thread and wait for it to stop |
| 742 | |
| 743 | long myThreadId = -1; |
| 744 | try { |
| 745 | myThreadId = thread.getId(); |
| 746 | } |
| 747 | catch ( Throwable t ) { |
| 748 | // Thread.getId() showed up with Java 1.5 |
| 749 | } |
| 750 | logger.log( Logger.FINEST, |
| 751 | "my thread id = "+myThreadId); |
| 752 | logger.log( Logger.FINEST, |
| 753 | "platform's thread id = "+threadId ); |
| 754 | logger.log( Logger.FINEST, |
| 755 | "platform's thread name = "+threadName); |
| 756 | logger.log( Logger.FINEST, |
| 757 | "platform's thread group name = "+threadGroupName); |
| 758 | |
| 759 | logger.log( Logger.FINE, "interrupting "+thread+"..."); |
| 760 | try { |
| 761 | thread.interrupt(); |
| 762 | } |
| 763 | catch ( Exception e ) { |
| 764 | logger.log(Logger.FINE,"from thread interrupt call", e ); |
| 765 | } |
| 766 | |
| 767 | logger.log(Logger.FINE,"waiting for "+thread+" to die..." ); |
| 768 | try { |
| 769 | thread.join(); |
| 770 | } |
| 771 | catch ( InterruptedException ie) { |
| 772 | if ( null != je ) { |
| 773 | logger.log( Logger.SEVERE, "subsequent exception", ie ); |
| 774 | } |
| 775 | else { |
| 776 | je = new JademxException( |
| 777 | "interrupted waiting for platform thread for \""+ |
| 778 | this+"\" to stop", |
| 779 | ie ); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | if ( null != je ) { |
| 784 | throw je; |
| 785 | } |
| 786 | |
| 787 | } |
| 788 | // /* (non-Javadoc) |
| 789 | // * @see jade.management.mbean.JadePlatformMBean#resume() |
| 790 | // */ |
| 791 | // public void resume() throws JademxException { |
| 792 | // checkPlatformController(); |
| 793 | // try { |
| 794 | // platformController.resume(); |
| 795 | // } |
| 796 | // catch (ControllerException ce) { |
| 797 | // throw new JademxException("exception resuming platform \""+ |
| 798 | // this+"\"", ce ); |
| 799 | // } |
| 800 | // } |
| 801 | // /* (non-Javadoc) |
| 802 | // * @see jade.management.mbean.JadePlatformMBean#start() |
| 803 | // */ |
| 804 | // public void start() throws JademxException { |
| 805 | // checkPlatformController(); |
| 806 | // try { |
| 807 | // platformController.start(); |
| 808 | // } |
| 809 | // catch (ControllerException ce) { |
| 810 | // throw new JademxException("exception starting platform \""+ |
| 811 | // this+"\"", ce ); |
| 812 | // } |
| 813 | // } |
| 814 | // /* (non-Javadoc) |
| 815 | // * @see jade.management.mbean.JadePlatformMBean#suspend() |
| 816 | // */ |
| 817 | // public void suspend() throws JademxException { |
| 818 | // checkPlatformController(); |
| 819 | // try { |
| 820 | // platformController.suspend(); |
| 821 | // } |
| 822 | // catch (ControllerException ce) { |
| 823 | // throw new JademxException("exception suspending platform \""+ |
| 824 | // this+"\"", ce ); |
| 825 | // } |
| 826 | // } |
| 827 | |
| 828 | // END PlatformController MEDIATION |
| 829 | |
| 830 | // BEGIN thread information |
| 831 | |
| 832 | /** |
| 833 | * return thread group name |
| 834 | * @return thread group name |
| 835 | */ |
| 836 | public String getThreadGroupName() { |
| 837 | return threadGroupName; |
| 838 | } |
| 839 | /** |
| 840 | * return thread id |
| 841 | * @return thread id |
| 842 | */ |
| 843 | public long getThreadId() { |
| 844 | return threadId; |
| 845 | } |
| 846 | /** |
| 847 | * return thread name |
| 848 | * @return thread name |
| 849 | */ |
| 850 | public String getThreadName() { |
| 851 | return threadName; |
| 852 | } |
| 853 | |
| 854 | // END thread information |
| 855 | |
| 856 | /** |
| 857 | * get the JadeRuntime MBean superior to this JadePlatform MBean |
| 858 | * @return get the JadeRuntime MBean superior to this JadePlatform MBean |
| 859 | */ |
| 860 | public JadeRuntime getJadeRuntime() { |
| 861 | return jadeRuntime; |
| 862 | } |
| 863 | } |