KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > net > SocketClientPoolSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
28 import java.net.InetAddress JavaDoc;
29
30 /**
31  * A pool of {@link SocketClient} instances from the
32  * <a HREF="http://jakarta.apache.org/commons/net.html">Jakarta Commons Net</a> library to handle
33  * internet networking connections.
34  *
35  * @version $Revision: 426415 $
36  */

37 public abstract class SocketClientPoolSupport implements InitializingBean, DisposableBean, PoolableObjectFactory {
38     private ObjectPool pool;
39     private InetAddress JavaDoc address;
40     private String JavaDoc host;
41     private int port = -1;
42     private InetAddress JavaDoc localAddress;
43     private int localPort;
44
45     public void afterPropertiesSet() throws Exception JavaDoc {
46         if (pool == null) {
47             pool = new GenericObjectPool();
48         }
49         pool.setFactory(this);
50     }
51
52     public void destroy() throws Exception JavaDoc {
53         if (pool != null) {
54             pool.close();
55         }
56     }
57
58     public SocketClient borrowClient() throws Exception JavaDoc {
59         return (SocketClient) getPool().borrowObject();
60     }
61
62     public void returnClient(SocketClient client) throws Exception JavaDoc {
63         getPool().returnObject(client);
64     }
65
66     // PoolableObjectFactory interface
67
//-------------------------------------------------------------------------
68
public Object JavaDoc makeObject() throws Exception JavaDoc {
69         SocketClient client = createSocketClient();
70         connect(client);
71         return client;
72     }
73
74     public void destroyObject(Object JavaDoc object) throws Exception JavaDoc {
75         SocketClient client = (SocketClient) object;
76         disconnect(client);
77     }
78
79     public boolean validateObject(Object JavaDoc object) {
80         return true;
81     }
82
83     public void activateObject(Object JavaDoc object) throws Exception JavaDoc {
84     }
85
86     public void passivateObject(Object JavaDoc object) throws Exception JavaDoc {
87     }
88
89
90     // Properties
91
//-------------------------------------------------------------------------
92
public InetAddress JavaDoc getAddress() {
93         return address;
94     }
95
96     public void setAddress(InetAddress JavaDoc address) {
97         this.address = address;
98     }
99
100     public String JavaDoc getHost() {
101         return host;
102     }
103
104     public void setHost(String JavaDoc host) {
105         this.host = host;
106     }
107
108     public InetAddress JavaDoc getLocalAddress() {
109         return localAddress;
110     }
111
112     public void setLocalAddress(InetAddress JavaDoc 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     // Implementation methods
144
//-------------------------------------------------------------------------
145
protected void connect(SocketClient client) throws IOException JavaDoc, JBIException, Exception JavaDoc {
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 JavaDoc {
175         if (client.isConnected()) {
176             client.disconnect();
177         }
178     }
179
180     protected abstract SocketClient createSocketClient();
181
182 }
183
Popular Tags