PageRenderTime 20ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/googlecode/jmxtrans/util/DatagramSocketFactory.java

http://jmxtrans.googlecode.com/
Java | 45 lines | 24 code | 8 blank | 13 comment | 0 complexity | 2b868e254e352d4865afec3dfdf97fd5 MD5 | raw file
 1package com.googlecode.jmxtrans.util;
 2
 3import java.net.DatagramSocket;
 4
 5import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
 6import org.slf4j.Logger;
 7import org.slf4j.LoggerFactory;
 8
 9/**
10 * Allows us to pool socket connections.
11 */
12public class DatagramSocketFactory extends BaseKeyedPoolableObjectFactory {
13
14    @SuppressWarnings("unused")
15    private static final Logger log = LoggerFactory.getLogger(DatagramSocketFactory.class);
16
17    /** constructor */
18    public DatagramSocketFactory() {}
19
20    /**
21     * Creates the socket and the writer to go with it.
22     */
23    @Override
24    public Object makeObject(Object key) throws Exception {
25        return new DatagramSocket();
26    }
27
28    /**
29     * Closes the socket.
30     */
31    @Override
32    public void destroyObject(Object key, Object obj) throws Exception {
33        DatagramSocket socket = (DatagramSocket) obj;
34        socket.close();
35    }
36
37    /**
38     * Validates that the socket is good.
39     */
40    @Override
41    public boolean validateObject(Object key, Object obj) {
42        DatagramSocket socket = (DatagramSocket) obj;
43        return !socket.isClosed();
44    }
45}