001: /*
002: * $Id: UdpSocketFactory.java 10961 2008-02-22 19:01:02Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010: package org.mule.transport.udp;
011:
012: import org.mule.api.endpoint.ImmutableEndpoint;
013: import org.mule.api.endpoint.InboundEndpoint;
014: import org.mule.api.transport.Connector;
015: import org.mule.util.MapUtils;
016:
017: import java.io.IOException;
018: import java.net.DatagramSocket;
019: import java.net.InetAddress;
020: import java.net.Socket;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.commons.pool.KeyedPoolableObjectFactory;
025:
026: /**
027: * Creates a client socket using the host and port address supplied in the endpoint URI. Addtional
028: * socket parameters will also be set from the connector
029: */
030: public class UdpSocketFactory implements KeyedPoolableObjectFactory {
031: /**
032: * logger used by this class
033: */
034: protected transient final Log logger = LogFactory
035: .getLog(UdpSocketFactory.class);
036:
037: public Object makeObject(Object key) throws Exception {
038: ImmutableEndpoint ep = (ImmutableEndpoint) key;
039: DatagramSocket socket;
040:
041: if (ep instanceof InboundEndpoint) {
042: int port = ep.getEndpointURI().getPort();
043: String host = ep.getEndpointURI().getHost();
044: if (port > 0) {
045: if ("null".equalsIgnoreCase(host)) {
046: socket = createSocket(port);
047: } else {
048: socket = createSocket(port, InetAddress
049: .getByName(host));
050: }
051: } else {
052: socket = createSocket();
053: }
054: } else {
055: //If this is a client socket create a default instance
056: socket = createSocket();
057: }
058:
059: UdpConnector connector = (UdpConnector) ep.getConnector();
060: //There is some overhead in stting socket timeout and buffer size, so we're
061: //careful here only to set if needed
062: if (connector.getReceiveBufferSize() != Connector.INT_VALUE_NOT_SET
063: && socket.getReceiveBufferSize() != connector
064: .getReceiveBufferSize()) {
065: socket.setReceiveBufferSize(connector
066: .getReceiveBufferSize());
067: }
068: if (connector.getSendBufferSize() != Connector.INT_VALUE_NOT_SET
069: && socket.getSendBufferSize() != connector
070: .getSendBufferSize()) {
071: socket.setSendBufferSize(connector.getSendBufferSize());
072: }
073: if (connector.getReceiveTimeout() != Connector.INT_VALUE_NOT_SET
074: && socket.getSoTimeout() != connector
075: .getReceiveTimeout()) {
076: socket.setSoTimeout(connector.getSendTimeout());
077: }
078: socket.setBroadcast(connector.isBroadcast());
079: return socket;
080: }
081:
082: public void destroyObject(Object key, Object object)
083: throws Exception {
084: Socket socket = (Socket) object;
085: if (!socket.isClosed()) {
086: socket.close();
087: }
088: }
089:
090: public boolean validateObject(Object key, Object object) {
091: DatagramSocket socket = (DatagramSocket) object;
092: return !socket.isClosed();
093: }
094:
095: public void activateObject(Object key, Object object)
096: throws Exception {
097: // nothing to do
098: }
099:
100: public void passivateObject(Object key, Object object)
101: throws Exception {
102: ImmutableEndpoint ep = (ImmutableEndpoint) key;
103:
104: boolean keepSocketOpen = MapUtils.getBooleanValue(ep
105: .getProperties(),
106: UdpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY,
107: ((UdpConnector) ep.getConnector())
108: .isKeepSendSocketOpen());
109: DatagramSocket socket = (DatagramSocket) object;
110:
111: if (!keepSocketOpen) {
112: if (socket != null) {
113: socket.close();
114: }
115: }
116: }
117:
118: protected DatagramSocket createSocket() throws IOException {
119: return new DatagramSocket();
120: }
121:
122: protected DatagramSocket createSocket(int port) throws IOException {
123: return new DatagramSocket(port);
124: }
125:
126: protected DatagramSocket createSocket(int port,
127: InetAddress inetAddress) throws IOException {
128: return new DatagramSocket(port, inetAddress);
129: }
130: }
|