View Javadoc

1   /*
2    * $Id$
3    *
4    * Copyright 2006 Commsen International
5    * 
6    * Licensed under the Common Public License, Version 1.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.opensource.org/licenses/cpl1.0.txt
11   * 
12   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
14   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   */
17  package com.commsen.stopwatch.jmx;
18  
19  import org.apache.log4j.Logger;
20  
21  import java.util.ArrayList;
22  
23  import javax.management.InstanceAlreadyExistsException;
24  import javax.management.InstanceNotFoundException;
25  import javax.management.MBeanRegistrationException;
26  import javax.management.MBeanServer;
27  import javax.management.MBeanServerFactory;
28  import javax.management.MalformedObjectNameException;
29  import javax.management.NotCompliantMBeanException;
30  import javax.management.ObjectName;
31  
32  /***
33   * TODO Dokumentacja 
34   *
35   * @author Milen Dyankov
36   *
37   */
38  public class StopwatchAgent {
39  	
40  	
41  	/***
42  	 * Logger for this class
43  	 */
44  	private static final Logger log = Logger.getLogger(StopwatchAgent.class);
45  
46  	
47  	
48  	String serverName;
49  	boolean rememberToReleaseServer = false;
50  	MBeanServer server;
51  	ObjectName stopwatchManagerName;
52  	
53  	/***
54  	 * @param serverName 
55  	 */
56  	public StopwatchAgent(String serverName) {
57  		this.serverName = serverName;
58  	}
59  	
60  	
61  	public void start () {
62  		 
63  		ArrayList servers = MBeanServerFactory.findMBeanServer(serverName);
64  		if (servers == null || servers.isEmpty()) {
65  			servers = MBeanServerFactory.findMBeanServer(null);
66  		}
67  
68  		if (servers == null || servers.isEmpty()) {
69  			server = MBeanServerFactory.createMBeanServer(serverName);
70  			rememberToReleaseServer = true;
71  		} else {
72  			server = (MBeanServer)servers.get(0);
73  			rememberToReleaseServer = false;
74  		}
75  		
76  		try {
77  			stopwatchManagerName = new ObjectName ("Stopwatch:name=Monitor");
78  		} catch (MalformedObjectNameException e) {
79  			log.error(e,e);
80  		}
81  		
82  		try {
83  			server.registerMBean(new StopwatchManager(), stopwatchManagerName);
84  		} catch (InstanceAlreadyExistsException e) {
85  			log.error(e,e);
86  		} catch (MBeanRegistrationException e) {
87  			log.error(e,e);
88  		} catch (NotCompliantMBeanException e) {
89  			log.error(e,e);
90  		}
91  	}
92  
93  	public void stop() {
94  		if (rememberToReleaseServer) {
95  			MBeanServerFactory.releaseMBeanServer(server);
96  			server = null;
97  		} else {
98  			try {
99  				server.unregisterMBean(stopwatchManagerName);
100 			} catch (InstanceNotFoundException e) {
101 				log.error(e,e);
102 			} catch (MBeanRegistrationException e) {
103 				log.error(e,e);
104 			}
105 		}
106 	}
107 
108 }