001: // $Id: MultithreadedServerMBeanProxyFactory.java 1386 2007-06-28 11:47:15Z grro $
002: /*
003: * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
020: * The latest copy of this software may be found on http://www.xsocket.org/
021: */
022: package org.xcache;
023:
024: import java.lang.management.ManagementFactory;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import javax.management.JMException;
029: import javax.management.MBeanServer;
030: import javax.management.ObjectName;
031:
032: import org.xsocket.IntrospectionBasedDynamicBean;
033: import org.xsocket.stream.IServerListener;
034: import org.xsocket.stream.ServerMBeanProxyFactory;
035:
036: /**
037: * A Mbean proxy factory, which creates and registers an appropriated mbean
038: * for a given {@link Server} instance.
039: *
040: * <br><br><b>This class is for test purpose only, and will be modified or discarded in future versions</b>
041: *
042: * @author grro@xsocket.org
043: */
044: public final class CacheServerMBeanProxyFactory {
045:
046: private static final Logger LOG = Logger
047: .getLogger(CacheServerMBeanProxyFactory.class.getName());
048:
049: /**
050: * creates and registers a mbean for the given server on the platform MBeanServer
051: *
052: * @param server the server to register
053: * @throws JMException if an jmx exception occurs
054: */
055: public static void createAndRegister(CacheServer server)
056: throws JMException {
057: createAndRegister(server, "org.xcache");
058: }
059:
060: /**
061: * creates and registers a mbean for the given server on the platform MBeanServer
062: * under the given domain name
063: *
064: * @param server the server to register
065: * @param domain the domain name to use
066: * @throws JMException if an jmx exception occurs
067: */
068: public static void createAndRegister(CacheServer server,
069: String domain) throws JMException {
070: createAndRegister(ManagementFactory.getPlatformMBeanServer(),
071: server, domain);
072: }
073:
074: /**
075: * creates and registers a mbean for the given server on the given MBeanServer
076: * under the given domain name
077: *
078: * @param mbeanServer the mbean server to use
079: * @param server the server to register
080: * @param domain the domain name to use
081: * @throws JMException if an jmx exception occurs
082: */
083: public static void createAndRegister(MBeanServer mbeanServer,
084: final CacheServer server, final String domain)
085: throws JMException {
086:
087: ServerMBeanProxyFactory.createAndRegister(server
088: .getUnderlyingServer(), domain);
089:
090: register(server, domain);
091:
092: server.addListener(new IServerListener() {
093: public void onInit() {
094: }
095:
096: public void onDestroy() {
097: try {
098: unregister(server, domain);
099: } catch (Exception e) {
100: if (LOG.isLoggable(Level.FINE)) {
101: LOG.fine("couldn't unregister mbean. reason: "
102: + e.toString());
103: }
104: }
105: }
106: });
107: }
108:
109: private static void register(CacheServer server, String domain)
110: throws JMException {
111:
112: ObjectName objectName = new ObjectName(domain
113: + ":type=StoreServer,name="
114: + server.getLocalAddress().getHostName() + "."
115: + server.getLocalPort());
116:
117: IntrospectionBasedDynamicBean mbean = new IntrospectionBasedDynamicBean(
118: server);
119: ManagementFactory.getPlatformMBeanServer().registerMBean(mbean,
120: objectName);
121:
122: server.addListener(new Listener(server, domain));
123: }
124:
125: private static void unregister(CacheServer server, String domain)
126: throws JMException {
127: ObjectName objectName = new ObjectName(domain
128: + ":type=StoreServer,name="
129: + server.getLocalAddress().getHostName() + "."
130: + server.getLocalPort());
131: ManagementFactory.getPlatformMBeanServer().unregisterMBean(
132: objectName);
133: }
134:
135: private static final class Listener implements IServerListener {
136:
137: private static final Logger LOG = Logger
138: .getLogger(Listener.class.getName());
139:
140: private CacheServer server = null;
141: private String domain = null;
142:
143: Listener(CacheServer server, String domain) {
144: this .server = server;
145: this .domain = domain;
146:
147: server.addListener(this );
148: }
149:
150: public void onInit() {
151: }
152:
153: public void onDestroy() {
154: try {
155: unregister(server, domain);
156: } catch (Exception ex) {
157: if (LOG.isLoggable(Level.FINE)) {
158: LOG
159: .fine("error occured by deregistering the server (domain="
160: + domain
161: + "). reason: "
162: + ex.toString());
163: }
164: }
165: }
166: }
167: }
|