1 18 package org.apache.activemq.transport.udp; 19 20 import java.io.IOException ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 import java.net.UnknownHostException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.activemq.openwire.OpenWireFormat; 28 import org.apache.activemq.transport.CommandJoiner; 29 import org.apache.activemq.transport.InactivityMonitor; 30 import org.apache.activemq.transport.Transport; 31 import org.apache.activemq.transport.TransportFactory; 32 import org.apache.activemq.transport.TransportLogger; 33 import org.apache.activemq.transport.TransportServer; 34 import org.apache.activemq.transport.reliable.DefaultReplayStrategy; 35 import org.apache.activemq.transport.reliable.ExceptionIfDroppedReplayStrategy; 36 import org.apache.activemq.transport.reliable.ReliableTransport; 37 import org.apache.activemq.transport.reliable.ReplayStrategy; 38 import org.apache.activemq.transport.reliable.Replayer; 39 import org.apache.activemq.util.IOExceptionSupport; 40 import org.apache.activemq.util.IntrospectionSupport; 41 import org.apache.activemq.util.URISupport; 42 import org.apache.activemq.wireformat.WireFormat; 43 44 public class UdpTransportFactory extends TransportFactory { 45 46 public TransportServer doBind(String brokerId, final URI location) throws IOException { 47 try { 48 Map options = new HashMap (URISupport.parseParamters(location)); 49 if (options.containsKey("port")) { 50 throw new IllegalArgumentException ("The port property cannot be specified on a UDP server transport - please use the port in the URI syntax"); 51 } 52 WireFormat wf = createWireFormat(options); 53 int port = location.getPort(); 54 OpenWireFormat openWireFormat = asOpenWireFormat(wf); 55 UdpTransport transport = new UdpTransport(openWireFormat, port); 56 57 Transport configuredTransport = configure(transport, wf, options, true); 58 UdpTransportServer server = new UdpTransportServer(location, transport, configuredTransport, createReplayStrategy()); 59 return server; 60 } 61 catch (URISyntaxException e) { 62 throw IOExceptionSupport.create(e); 63 } 64 catch (Exception e) { 65 throw IOExceptionSupport.create(e); 66 } 67 } 68 69 public Transport configure(Transport transport, WireFormat format, Map options) throws Exception { 70 return configure(transport, format, options, false); 71 } 72 73 public Transport compositeConfigure(Transport transport, WireFormat format, Map options) { 74 IntrospectionSupport.setProperties(transport, options); 75 final UdpTransport udpTransport = (UdpTransport) transport; 76 77 transport = new CommandJoiner(transport, asOpenWireFormat(format)); 79 80 if (udpTransport.isTrace()) { 81 transport = new TransportLogger(transport); 82 } 83 84 transport = new InactivityMonitor(transport); 85 86 if (format instanceof OpenWireFormat) { 87 transport = configureClientSideNegotiator(transport, format, udpTransport); 88 } 89 90 return transport; 91 } 92 93 protected Transport createTransport(URI location, WireFormat wf) throws UnknownHostException , IOException { 94 OpenWireFormat wireFormat = asOpenWireFormat(wf); 95 return new UdpTransport(wireFormat, location); 96 } 97 98 106 protected Transport configure(Transport transport, WireFormat format, Map options, boolean acceptServer) throws Exception { 107 IntrospectionSupport.setProperties(transport, options); 108 UdpTransport udpTransport = (UdpTransport) transport; 109 110 OpenWireFormat openWireFormat = asOpenWireFormat(format); 111 112 if (udpTransport.isTrace()) { 113 transport = new TransportLogger(transport); 114 } 115 116 transport = new InactivityMonitor(transport); 117 118 if (!acceptServer && format instanceof OpenWireFormat) { 119 transport = configureClientSideNegotiator(transport, format, udpTransport); 120 } 121 122 124 if (acceptServer) { 125 udpTransport.setReplayEnabled(false); 128 129 transport = new CommandJoiner(transport, openWireFormat); 132 return transport; 133 } 134 else { 135 ReliableTransport reliableTransport = new ReliableTransport(transport, udpTransport); 136 Replayer replayer = reliableTransport.getReplayer(); 137 reliableTransport.setReplayStrategy(createReplayStrategy(replayer)); 138 139 return new CommandJoiner(reliableTransport, openWireFormat); 142 } 143 } 144 145 protected ReplayStrategy createReplayStrategy(Replayer replayer) { 146 if (replayer != null) { 147 return new DefaultReplayStrategy(5); 148 } 149 return new ExceptionIfDroppedReplayStrategy(1); 150 } 151 152 protected ReplayStrategy createReplayStrategy() { 153 return new DefaultReplayStrategy(5); 154 } 155 156 protected Transport configureClientSideNegotiator(Transport transport, WireFormat format, final UdpTransport udpTransport) { 157 return new ResponseRedirectInterceptor(transport, udpTransport); 158 } 159 160 protected OpenWireFormat asOpenWireFormat(WireFormat wf) { 161 OpenWireFormat answer = (OpenWireFormat) wf; 162 return answer; 163 } 164 } 165 | Popular Tags |