KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > MultithreadedServerMBeanProxyFactory


1 // $Id: MultithreadedServerMBeanProxyFactory.java 1386 2007-06-28 11:47:15Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream;
23
24 import java.lang.management.ManagementFactory JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import javax.management.JMException JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.xsocket.IDispatcher;
33 import org.xsocket.IWorkerPool;
34 import org.xsocket.stream.io.impl.JmxIoProvider;
35 import org.xsocket.stream.io.spi.IServerIoJmxProvider;
36
37
38
39 /**
40  * A Mbean proxy factory, which creates and registers an appropriated mbean
41  * for a given {@link MultithreadedServer} instance.
42  *
43  * <br><br><b>This class is for test purpose only, and will be modified or discarded in future versions</b>
44  *
45  * @author grro@xsocket.org
46  */

47 public final class MultithreadedServerMBeanProxyFactory {
48     
49     private static final Logger JavaDoc LOG = Logger.getLogger(MultithreadedServerMBeanProxyFactory.class.getName());
50
51     private static IServerIoJmxProvider jmxProvider = null;
52     
53     
54     static {
55         String JavaDoc jmxIoProviderClassname = System.getProperty(IServerIoJmxProvider.PROVIDER_CLASSNAME_KEY);
56         if (jmxIoProviderClassname != null) {
57             try {
58                 Class JavaDoc jmxProviderClass = Class.forName(jmxIoProviderClassname);
59                 jmxProvider = (IServerIoJmxProvider) jmxProviderClass.newInstance();
60             } catch (Exception JavaDoc e) {
61                 LOG.warning("error occured by creating jmxProivder " + jmxIoProviderClassname + ": " + e.toString());
62             }
63         }
64          
65         if (jmxProvider == null) {
66             jmxProvider = new JmxIoProvider();
67         }
68     }
69     
70     
71     
72     
73     /**
74      * creates and registers a mbean for the given server on the platform MBeanServer
75      *
76      * @param server the server to register
77      * @throws JMException if an jmx exception occurs
78      */

79     public static void createAndRegister(MultithreadedServer server) throws JMException JavaDoc {
80         createAndRegister(server, "org.xsocket.stream");
81     }
82     
83
84     /**
85      * creates and registers a mbean for the given server on the platform MBeanServer
86      * under the given domain name
87      *
88      * @param server the server to register
89      * @param domain the domain name to use
90      * @throws JMException if an jmx exception occurs
91      */

92     public static void createAndRegister(MultithreadedServer server, String JavaDoc domain) throws JMException JavaDoc {
93         createAndRegister(ManagementFactory.getPlatformMBeanServer(), server, domain);
94     }
95     
96     
97     /**
98      * creates and registers a mbean for the given server on the given MBeanServer
99      * under the given domain name
100      *
101      * @param mbeanServer the mbean server to use
102      * @param server the server to register
103      * @param domain the domain name to use
104      * @throws JMException if an jmx exception occurs
105      */

106     public static void createAndRegister(MBeanServer JavaDoc mbeanServer, MultithreadedServer server, String JavaDoc domain) throws JMException JavaDoc {
107         ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain + ":type=MultithreadedServer,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort());
108
109         Object JavaDoc mbean = jmxProvider.createMBean(server, server.getAcceptor(), domain);
110         ManagementFactory.getPlatformMBeanServer().registerMBean(mbean, objectName);
111         
112         server.addListener(new Listener(server, domain));
113     }
114     
115     
116     private static void unregister(IMultithreadedServer server, String JavaDoc domain) throws JMException JavaDoc {
117         ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain + ":type=MultithreadedServer,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort());
118         ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName);
119     }
120     
121     
122     private static final class Listener implements IMutlithreadedServerListener {
123         
124         private static final Logger JavaDoc LOG = Logger.getLogger(Listener.class.getName());
125         
126         private MultithreadedServer server = null;
127         private String JavaDoc domain = null;
128         
129         Listener(MultithreadedServer server, String JavaDoc domain) {
130             this.server = server;
131             this.domain = domain;
132             
133             server.addListener(this);
134         }
135         
136         public void onInit() {
137         }
138         
139         public void onDestroy() {
140             try {
141                 unregister(server, domain);
142             } catch (Exception JavaDoc ex) {
143                 if (LOG.isLoggable(Level.FINE)) {
144                     LOG.fine("error occured by deregistering the server (domain=" + domain + "). reason: " + ex.toString());
145                 }
146             }
147         }
148         
149         public void onWorkerPoolUpdated(IWorkerPool oldWorkerPool, IWorkerPool newWorkerPool) { }
150         
151         
152         public void onDispatcherAdded(IDispatcher dispatcher) { }
153         
154         public void onDispatcherRemoved(IDispatcher dispatcher) { }
155     }
156 }
157
Popular Tags