KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > io > impl > JmxIoProvider


1 // $Id$
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.io.impl;
23
24 import java.lang.management.ManagementFactory JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.concurrent.ThreadPoolExecutor JavaDoc;
27 import java.util.concurrent.TimeUnit JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 import javax.management.JMException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 import org.xsocket.Dispatcher;
34 import org.xsocket.IDispatcher;
35 import org.xsocket.IWorkerPool;
36 import org.xsocket.IntrospectionBasedDynamicBean;
37 import org.xsocket.stream.MultithreadedServer;
38 import org.xsocket.stream.io.spi.IAcceptor;
39 import org.xsocket.stream.io.spi.IServerIoJmxProvider;
40
41
42
43 /**
44  * A Mbean proxy factory, which creates and registers an appropriated mbean
45  * for a given {@link MultithreadedServer} instance.
46  *
47  * <br><br><b>This class is for test purpose only, and will be modified or discarded in future versions</b>
48  *
49  * @author grro@xsocket.org
50  */

51 public final class JmxIoProvider implements IServerIoJmxProvider {
52
53     public Object JavaDoc createMBean(MultithreadedServer server, IAcceptor acceptor, String JavaDoc domain) throws JMException JavaDoc {
54         
55         if (acceptor instanceof Acceptor) {
56             
57             Acceptor acptr = (Acceptor) acceptor;
58             Listener listener = new Listener(server, acptr, domain);
59             acptr.getDispatcherPool().addListener(listener);
60             
61             for (IDispatcher dispatcher : acptr.getDispatchers()) {
62                 try {
63                     listener.onDispatcherAdded((Dispatcher) dispatcher);
64                 } catch(Exception JavaDoc ignore) { }
65             }
66             
67             
68             return new IntrospectionBasedDynamicBean(new ServerManagementBean(server, acptr));
69             
70         } else {
71             return new IntrospectionBasedDynamicBean(server);
72         }
73     }
74     
75     
76     private static final class Listener implements IIoSocketDispatcherPoolListener {
77     
78         
79         private MultithreadedServer server = null;
80         private String JavaDoc domain = null;
81         
82         Listener(MultithreadedServer server, Acceptor acceptor, String JavaDoc domain) {
83             this.server = server;
84             this.domain = domain;
85         }
86         
87         
88         public void onWorkerPoolUpdated(IWorkerPool oldWorkerPool, IWorkerPool newWorkerPool) {
89             
90         }
91         
92         
93         public void onDispatcherAdded(IDispatcher dispatcher) {
94             try {
95                 ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain +":type=IoSocketDispatcher,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort() + "." + dispatcher.hashCode());
96                 ManagementFactory.getPlatformMBeanServer().registerMBean(new IntrospectionBasedDynamicBean(dispatcher), objectName);
97             } catch (Exception JavaDoc ignore) { }
98         }
99
100         
101         public void onDispatcherRemoved(IDispatcher dispatcher) {
102             try {
103                 ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain +":type=IoSocketDispatcher,name=" + server.getLocalAddress().getHostName() + "." + server.getLocalPort() + "." + dispatcher.hashCode());
104                 ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName);
105             } catch (Exception JavaDoc ignore) { }
106         }
107     }
108     
109     
110
111     
112     private static final class ServerManagementBean {
113         
114         private MultithreadedServer server = null;
115         private Acceptor acceptor = null;
116
117         /**
118          * constructore
119          *
120          * @param obj the object to create a mbean for
121          */

122         ServerManagementBean(MultithreadedServer server, Acceptor acceptor) {
123             this.server = server;
124             this.acceptor = acceptor;
125         }
126         
127         
128         /**
129          * set the size of the preallocation buffer,
130          * for reading incomming data
131          *
132          * @param size preallocation buffer size
133          */

134         public void setReceiveBufferPreallocationSize(int size) {
135             acceptor.setReceiveBufferPreallocationSize(size);
136         }
137
138         
139         /**
140          * get the size of the preallocation buffer,
141          * for reading incomming data
142          *
143          * @return preallocation buffer size
144          */

145         public int getReceiveBufferPreallocationSize() {
146             return acceptor.getReceiveBufferPreallocationSize();
147         }
148         
149         
150
151         
152         /**
153          * returns the idle timeout in sec.
154          *
155          * @return idle timeout in sec
156          */

157         public int getIdleTimeoutSec() {
158             return server.getIdleTimeoutSec();
159         }
160         
161         
162         
163         /**
164          * sets the idle timeout in sec
165          *
166          * @param timeoutInSec idle timeout in sec
167          */

168         public void setIdleTimeoutSec(int timeoutInSec) {
169             server.setIdleTimeoutSec(timeoutInSec);
170         }
171
172
173         /**
174          * gets the connection timeout
175          *
176          * @return connection timeout
177          */

178         public int getConnectionTimeoutSec() {
179             return server.getConnectionTimeoutSec();
180         }
181         
182         
183         /**
184          * sets the max time for a connections. By
185          * exceeding this time the connection will be
186          * terminated
187          *
188          * @param timeoutSec the connection timeout in sec
189          */

190         public void setConnectionTimeoutSec(int timeoutSec) {
191             server.setConnectionTimeoutSec(timeoutSec);
192         }
193         
194         
195         public long getCountHandledConnections() {
196             return acceptor.getNumberOfHandledConnections();
197         }
198
199         
200         public int getDispatcherPoolSize() {
201             return acceptor.getDispatcherPoolSize();
202         }
203         
204         public void setDispatcherPoolSize(int size) {
205             acceptor.setDispatcherPoolSize(size);
206         }
207         
208         
209         public List JavaDoc<String JavaDoc> getOpenConnections() {
210             return acceptor.getOpenConnections();
211         }
212   
213         
214         public int getNumberOpenConnections() {
215             return acceptor.getNumberOfOpenConnections();
216         }
217         
218         
219         public long getCountConnectionTimeouts() {
220             return acceptor.getNumberOfConnectionTimeouts();
221         }
222
223         public long getCountIdleTimeouts() {
224             return acceptor.getNumberOfIdleTimeouts();
225         }
226         
227         /**
228          * get the server port
229          *
230          * @return the server port
231          */

232         public int getLocalPort() {
233             return server.getLocalPort();
234         }
235         
236
237         /**
238          * get the local address
239          * @return the local address
240          */

241         public String JavaDoc getLocalAddressString() {
242             return server.getLocalAddress().toString();
243         }
244         
245         
246         
247         public Integer JavaDoc getWorkerpoolActiveCount() {
248             if (server.getWorkerpool() instanceof ThreadPoolExecutor JavaDoc) {
249                 ThreadPoolExecutor JavaDoc tpe = (ThreadPoolExecutor JavaDoc) server.getWorkerpool();
250                 return tpe.getActiveCount();
251                 
252             } else {
253                 return null;
254             }
255         }
256         
257         public Integer JavaDoc getWorkerpoolMaximumPoolSize() {
258             if (server.getWorkerpool() instanceof ThreadPoolExecutor JavaDoc) {
259                 ThreadPoolExecutor JavaDoc tpe = (ThreadPoolExecutor JavaDoc) server.getWorkerpool();
260                 return tpe.getMaximumPoolSize();
261                 
262             } else {
263                 return null;
264             }
265         }
266
267         
268         public Integer JavaDoc getWorkerpoolSize() {
269             if (server.getWorkerpool() instanceof ThreadPoolExecutor JavaDoc) {
270                 ThreadPoolExecutor JavaDoc tpe = (ThreadPoolExecutor JavaDoc) server.getWorkerpool();
271                 return tpe.getPoolSize();
272                 
273             } else {
274                 return null;
275             }
276         }
277         
278         public Integer JavaDoc getWorkerpoolLargestPoolSize() {
279             if (server.getWorkerpool() instanceof ThreadPoolExecutor JavaDoc) {
280                 ThreadPoolExecutor JavaDoc tpe = (ThreadPoolExecutor JavaDoc) server.getWorkerpool();
281                 return tpe.getLargestPoolSize();
282                 
283             } else {
284                 return null;
285             }
286         }
287         
288         public Integer JavaDoc getWorkerpoolKeepAliveTimeSec() {
289             if (server.getWorkerpool() instanceof ThreadPoolExecutor JavaDoc) {
290                 ThreadPoolExecutor JavaDoc tpe = (ThreadPoolExecutor JavaDoc) server.getWorkerpool();
291                 return (int) tpe.getKeepAliveTime(TimeUnit.SECONDS);
292                 
293             } else {
294                 return null;
295             }
296         }
297     }
298
299 }
Popular Tags