3030import java .io .StringWriter ;
3131import java .nio .charset .Charset ;
3232import java .util .*;
33- import java .util .concurrent .ConcurrentHashMap ;
3433import java .util .concurrent .ExecutorService ;
3534import java .util .concurrent .Executors ;
3635import java .util .logging .Level ;
@@ -79,28 +78,9 @@ public abstract class Transport<I, O> implements MALTransport {
7978 */
8079 protected static final Random RANDOM_NAME = new Random ();
8180 /**
82- * The delimiter to use to separate the protocol part from the address part
83- * of the URL.
81+ * The shape of the URIs used by this transport, and the parsing of them.
8482 */
85- protected final String protocolDelim ;
86- /**
87- * The delimiter to use to separate the external address part from the
88- * internal object part of the URL.
89- */
90- protected final char serviceDelim ;
91- /**
92- * If the protocol delimiter is the same as the service delimiter then we
93- * need a count to find the correct service delimiter.
94- */
95- protected final int serviceDelimCounter ;
96- /**
97- * Delimiter to use when holding routing information in a URL
98- */
99- protected final char routingDelim ;
100- /**
101- * True if protocol supports the concept of routing.
102- */
103- protected final boolean supportsRouting ;
83+ protected final TransportAddressing addressing ;
10484 /**
10585 * True if calls to ourselves should be handled in-process i.e. not via the
10686 * underlying transport.
@@ -110,10 +90,6 @@ public abstract class Transport<I, O> implements MALTransport {
11090 * The timeout in seconds to wait for confirmation of delivery.
11191 */
11292 protected final int deliveryTimeout ;
113- /**
114- * The string used to represent this protocol.
115- */
116- protected final String protocol ;
11793 /**
11894 * The endpoints created by this transport, indexed by MAL local name and by
11995 * transport routing name.
@@ -147,14 +123,6 @@ public abstract class Transport<I, O> implements MALTransport {
147123 * The stream factory used for encoding and decoding messages.
148124 */
149125 private final MALElementStreamFactory streamFactory ;
150- /**
151- * The base string for URL for this protocol.
152- */
153- protected String uriBase ;
154- /**
155- * Map of cachedRoutingParts. This associates a URI to its Routing part.
156- */
157- private final ConcurrentHashMap <String , String > cachedRoutingParts = new ConcurrentHashMap <>();
158126
159127 /**
160128 * Constructor.
@@ -193,24 +161,14 @@ public Transport(final String protocol,
193161 final char routingDelim ,
194162 final boolean supportsRouting ,
195163 final java .util .Map properties ) throws MALException {
196- this .protocol = protocol ;
197- this .supportsRouting = supportsRouting ;
198- this .protocolDelim = protocolDelim ;
199- this .serviceDelim = serviceDelim ;
200- this .routingDelim = routingDelim ;
164+ this .addressing = new TransportAddressing (protocol , protocolDelim ,
165+ serviceDelim , routingDelim , supportsRouting );
201166 this .qosProperties = properties ;
202167
203168 streamFactory = MALElementStreamFactory .newFactory (protocol , properties );
204169 LOGGER .log (Level .FINE , "Created element stream: {0}" ,
205170 streamFactory .getClass ().getName ());
206171
207- if (protocolDelim .contains ("" + serviceDelim )) {
208- String replaced = protocolDelim .replace ("" + serviceDelim , "" );
209- serviceDelimCounter = protocolDelim .length () - replaced .length ();
210- } else {
211- serviceDelimCounter = 0 ;
212- }
213-
214172 // default values
215173 boolean lInProcessSupport = true ;
216174 int lDeliveryTime = 10 ;
@@ -241,12 +199,7 @@ public Transport(final String protocol,
241199 * @throws MALException On error
242200 */
243201 public void init () throws MALException {
244- String protocolString = protocol ;
245- if (protocol .contains (":" )) {
246- protocolString = protocol .substring (0 , protocol .indexOf (':' ));
247- }
248-
249- uriBase = protocolString + protocolDelim + createTransportAddress () + serviceDelim ;
202+ addressing .initUriBase (createTransportAddress ());
250203 }
251204
252205 @ Override
@@ -328,13 +281,13 @@ public void sendMessage(final Object multiSendHandle, final boolean lastForHandl
328281
329282 // get the root URI, (e.g. maltcp://10.0.0.1:61616 )
330283 String destinationURI = header .getTo ().getValue ();
331- String remoteRootURI = header .getToURI (). getRootURI ( serviceDelim , serviceDelimCounter );
284+ String remoteRootURI = addressing . getRootURI ( header .getToURI ());
332285
333286 // first check if its actually a message to ourselves
334287 String endpointUriPart = getRoutingPart (destinationURI );
335288
336289 if (inProcessSupport
337- && ( uriBase . startsWith (remoteRootURI ) || remoteRootURI . startsWith ( uriBase ) )
290+ && addressing . matchesLocalBase (remoteRootURI )
338291 && endpoints .containsRoutingName (endpointUriPart )) {
339292 LOGGER .log (Level .FINE , "Routing msg internally to: {0}" ,
340293 new Object []{endpointUriPart });
@@ -622,20 +575,7 @@ protected String getLocalName(String localName, final java.util.Map properties)
622575 * @return the routing part of the URI
623576 */
624577 public String getRoutingPart (String uriValue ) {
625- String routingPart = cachedRoutingParts .get (uriValue );
626-
627- if (routingPart == null ) {
628- final int iFirst = URI .nthIndexOf (uriValue , serviceDelim , serviceDelimCounter );
629- int iSecond = supportsRouting ? uriValue .indexOf (routingDelim ) : uriValue .length ();
630- if (iSecond < 0 ) {
631- iSecond = uriValue .length ();
632- }
633-
634- routingPart = uriValue .substring (iFirst + 1 , iSecond );
635- cachedRoutingParts .put (uriValue , routingPart );
636- }
637-
638- return routingPart ;
578+ return addressing .getRoutingPart (uriValue );
639579 }
640580
641581 /**
@@ -651,7 +591,8 @@ public String getRoutingPart(String uriValue) {
651591 protected Endpoint internalCreateEndpoint (final String localName ,
652592 final String routingName , final Map qosProperties ,
653593 final NamedValueList supplements ) throws MALException {
654- return new Endpoint (this , localName , routingName , uriBase + routingName , supplements );
594+ return new Endpoint (this , localName , routingName ,
595+ addressing .getUriBase () + routingName , supplements );
655596 }
656597
657598 /**
@@ -679,7 +620,7 @@ public synchronized ConcurrentMessageSender manageCommunicationChannel(GENMessag
679620 // this is the first message received form this reception handler
680621 // add the remote base URI it is receiving messages from
681622 URI sourceURI = msg .getHeader ().getFromURI ();
682- String sourceRootURI = sourceURI .getRootURI (serviceDelim , serviceDelimCounter );
623+ String sourceRootURI = addressing .getRootURI (sourceURI );
683624 receptionHandler .setRemoteURI (sourceRootURI );
684625
685626 //register the communication channel with this URI if needed
@@ -689,7 +630,7 @@ public synchronized ConcurrentMessageSender manageCommunicationChannel(GENMessag
689630 // outgoing message
690631 // get target URI
691632 URI reroutedMsg = this .rerouteMessage (msg );
692- String remoteRootURI = reroutedMsg .getRootURI (serviceDelim , serviceDelimCounter );
633+ String remoteRootURI = addressing .getRootURI (reroutedMsg );
693634 sender = outgoingDataChannelsManager .manageCommunicationChannelOutgoing (msg .getHeader (), remoteRootURI );
694635 }
695636
0 commit comments