001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.ftp;
018
019 import java.io.IOException;
020 import java.net.InetAddress;
021 import javax.jbi.JBIException;
022
023 import org.apache.commons.net.SocketClient;
024 import org.apache.commons.pool.ObjectPool;
025 import org.apache.commons.pool.PoolableObjectFactory;
026 import org.apache.commons.pool.impl.GenericObjectPool;
027 import org.springframework.beans.factory.DisposableBean;
028 import org.springframework.beans.factory.InitializingBean;
029
030 /**
031 * A pool of {@link org.apache.commons.net.SocketClient} instances from the
032 * <a href="http://jakarta.apache.org/commons/net.html">Jakarta Commons Net</a> library to handle
033 * internet networking connections.
034 *
035 * @version $Revision: 426415 $
036 */
037 public abstract class SocketClientPoolSupport implements InitializingBean, DisposableBean, PoolableObjectFactory {
038 private ObjectPool pool;
039 private InetAddress address;
040 private String host;
041 private int port = -1;
042 private InetAddress localAddress;
043 private int localPort;
044
045 public void afterPropertiesSet() throws Exception {
046 if (pool == null) {
047 GenericObjectPool goPool = new GenericObjectPool();
048 goPool.setTestOnBorrow(true);
049 goPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
050 pool = goPool;
051 }
052 pool.setFactory(this);
053 }
054
055 public void destroy() throws Exception {
056 if (pool != null) {
057 pool.close();
058 }
059 }
060
061 public SocketClient borrowClient() throws Exception {
062 return (SocketClient) getPool().borrowObject();
063 }
064
065 public void returnClient(SocketClient client) throws Exception {
066 getPool().returnObject(client);
067 }
068
069 // PoolableObjectFactory interface
070 //-------------------------------------------------------------------------
071 public Object makeObject() throws Exception {
072 SocketClient client = createSocketClient();
073 connect(client);
074 return client;
075 }
076
077 public void destroyObject(Object object) throws Exception {
078 SocketClient client = (SocketClient) object;
079 disconnect(client);
080 }
081
082 public boolean validateObject(Object object) {
083 return true;
084 }
085
086 public void activateObject(Object object) throws Exception {
087 }
088
089 public void passivateObject(Object object) throws Exception {
090 }
091
092
093 // Properties
094 //-------------------------------------------------------------------------
095 public InetAddress getAddress() {
096 return address;
097 }
098
099 public void setAddress(InetAddress address) {
100 this.address = address;
101 }
102
103 public String getHost() {
104 return host;
105 }
106
107 public void setHost(String host) {
108 this.host = host;
109 }
110
111 public InetAddress getLocalAddress() {
112 return localAddress;
113 }
114
115 public void setLocalAddress(InetAddress localAddress) {
116 this.localAddress = localAddress;
117 }
118
119 public int getLocalPort() {
120 return localPort;
121 }
122
123 public void setLocalPort(int localPort) {
124 this.localPort = localPort;
125 }
126
127 public int getPort() {
128 return port;
129 }
130
131 public void setPort(int port) {
132 this.port = port;
133 }
134
135 public ObjectPool getPool() {
136 return pool;
137 }
138
139 public void setPool(ObjectPool pool) {
140 if (pool != null) {
141 pool.setFactory(this);
142 }
143 this.pool = pool;
144 }
145
146 // Implementation methods
147 //-------------------------------------------------------------------------
148 protected void connect(SocketClient client) throws IOException, JBIException, Exception {
149 if (host != null) {
150 if (localAddress != null) {
151 client.connect(host, port, localAddress, localPort);
152 } else if (port >= 0) {
153 client.connect(host, port);
154 } else {
155 client.connect(host);
156 }
157 } else if (address != null) {
158 if (localAddress != null) {
159 client.connect(address, port, localAddress, localPort);
160 } else if (port >= 0) {
161 client.connect(address, port);
162 } else {
163 client.connect(address);
164 }
165 } else {
166 throw new JBIException("You must configure the address or host property");
167 }
168 }
169
170
171 protected void disconnect(SocketClient client) throws Exception {
172 if (client.isConnected()) {
173 client.disconnect();
174 }
175 }
176
177 protected abstract SocketClient createSocketClient();
178
179 }