1 17 package org.apache.servicemix.components.net; 18 19 import org.apache.commons.net.SocketClient; 20 import org.apache.commons.pool.ObjectPool; 21 import org.apache.commons.pool.PoolableObjectFactory; 22 import org.apache.commons.pool.impl.GenericObjectPool; 23 import org.springframework.beans.factory.DisposableBean; 24 import org.springframework.beans.factory.InitializingBean; 25 26 import javax.jbi.JBIException; 27 import java.io.IOException ; 28 import java.net.InetAddress ; 29 30 37 public abstract class SocketClientPoolSupport implements InitializingBean, DisposableBean, PoolableObjectFactory { 38 private ObjectPool pool; 39 private InetAddress address; 40 private String host; 41 private int port = -1; 42 private InetAddress localAddress; 43 private int localPort; 44 45 public void afterPropertiesSet() throws Exception { 46 if (pool == null) { 47 pool = new GenericObjectPool(); 48 } 49 pool.setFactory(this); 50 } 51 52 public void destroy() throws Exception { 53 if (pool != null) { 54 pool.close(); 55 } 56 } 57 58 public SocketClient borrowClient() throws Exception { 59 return (SocketClient) getPool().borrowObject(); 60 } 61 62 public void returnClient(SocketClient client) throws Exception { 63 getPool().returnObject(client); 64 } 65 66 public Object makeObject() throws Exception { 69 SocketClient client = createSocketClient(); 70 connect(client); 71 return client; 72 } 73 74 public void destroyObject(Object object) throws Exception { 75 SocketClient client = (SocketClient) object; 76 disconnect(client); 77 } 78 79 public boolean validateObject(Object object) { 80 return true; 81 } 82 83 public void activateObject(Object object) throws Exception { 84 } 85 86 public void passivateObject(Object object) throws Exception { 87 } 88 89 90 public InetAddress getAddress() { 93 return address; 94 } 95 96 public void setAddress(InetAddress address) { 97 this.address = address; 98 } 99 100 public String getHost() { 101 return host; 102 } 103 104 public void setHost(String host) { 105 this.host = host; 106 } 107 108 public InetAddress getLocalAddress() { 109 return localAddress; 110 } 111 112 public void setLocalAddress(InetAddress localAddress) { 113 this.localAddress = localAddress; 114 } 115 116 public int getLocalPort() { 117 return localPort; 118 } 119 120 public void setLocalPort(int localPort) { 121 this.localPort = localPort; 122 } 123 124 public int getPort() { 125 return port; 126 } 127 128 public void setPort(int port) { 129 this.port = port; 130 } 131 132 public ObjectPool getPool() { 133 return pool; 134 } 135 136 public void setPool(ObjectPool pool) { 137 if (pool != null) { 138 pool.setFactory(this); 139 } 140 this.pool = pool; 141 } 142 143 protected void connect(SocketClient client) throws IOException , JBIException, Exception { 146 if (host != null) { 147 if (localAddress != null) { 148 client.connect(host, port, localAddress, localPort); 149 } 150 else if (port >= 0) { 151 client.connect(host, port); 152 } 153 else { 154 client.connect(host); 155 } 156 } 157 else if (address != null) { 158 if (localAddress != null) { 159 client.connect(address, port, localAddress, localPort); 160 } 161 else if (port >= 0) { 162 client.connect(address, port); 163 } 164 else { 165 client.connect(address); 166 } 167 } 168 else { 169 throw new JBIException("You must configure the address or host property"); 170 } 171 } 172 173 174 protected void disconnect(SocketClient client) throws Exception { 175 if (client.isConnected()) { 176 client.disconnect(); 177 } 178 } 179 180 protected abstract SocketClient createSocketClient(); 181 182 } 183 | Popular Tags |