Package io.microsphere.net
Class CompositeSubProtocolURLConnectionFactory
- java.lang.Object
-
- io.microsphere.net.CompositeSubProtocolURLConnectionFactory
-
- All Implemented Interfaces:
SubProtocolURLConnectionFactory
public class CompositeSubProtocolURLConnectionFactory extends java.lang.Object implements SubProtocolURLConnectionFactory
A composite implementation ofSubProtocolURLConnectionFactorythat combines multiple factories. This class allows dynamic modification at runtime by adding or removing individual factories.The composite factory delegates the creation of URL connections to its internal list of factories, selecting the appropriate one based on support for the given URL and sub-protocols.
Key Features
- Dynamic Composition: Add or remove factories at runtime.
- Prioritized Ordering: Factories are sorted based on their priority using the
Prioritizedinterface. Higher priority factories are consulted first when determining support. - Efficient Delegation: Delegates connection creation to the first factory that supports the URL and sub-protocols.
Example Usage
CompositeSubProtocolURLConnectionFactory compositeFactory = new CompositeSubProtocolURLConnectionFactory(); // Create and add a custom factory SubProtocolURLConnectionFactory myFactory = new MySubProtocolURLConnectionFactory(); compositeFactory.add(myFactory); // Use the composite factory to create a connection URL url = new URL("http://example.com"); List<String> subProtocols = Arrays.asList("myprotocol", "anotherprotocol"); Proxy proxy = Proxy.NO_PROXY; if (compositeFactory.supports(url, subProtocols)) { URLConnection connection = compositeFactory.create(url, subProtocols, proxy); // proceed with using the connection }This class is thread-safe as long as modifications to the factory list happen through the provided methods, which re-sort the internal list in a thread-safe manner.
- Since:
- 1.0.0
- Author:
- Mercy
-
-
Constructor Summary
Constructors Constructor Description CompositeSubProtocolURLConnectionFactory()CompositeSubProtocolURLConnectionFactory(java.lang.Iterable<SubProtocolURLConnectionFactory> factories)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompositeSubProtocolURLConnectionFactoryadd(SubProtocolURLConnectionFactory factory)CompositeSubProtocolURLConnectionFactoryadd(SubProtocolURLConnectionFactory... factories)protected booleanaddInternal(SubProtocolURLConnectionFactory factory)java.net.URLConnectioncreate(java.net.URL url, java.util.List<java.lang.String> subProtocols, java.net.Proxy proxy)Create the sub-protocols'URLConnectionbooleanremove(SubProtocolURLConnectionFactory factory)booleansupports(java.net.URL url, java.util.List<java.lang.String> subProtocols)Supports the current factory to create theURLConnectionor not
-
-
-
Constructor Detail
-
CompositeSubProtocolURLConnectionFactory
public CompositeSubProtocolURLConnectionFactory()
-
CompositeSubProtocolURLConnectionFactory
public CompositeSubProtocolURLConnectionFactory(java.lang.Iterable<SubProtocolURLConnectionFactory> factories)
-
-
Method Detail
-
add
public CompositeSubProtocolURLConnectionFactory add(SubProtocolURLConnectionFactory factory)
-
add
public CompositeSubProtocolURLConnectionFactory add(SubProtocolURLConnectionFactory... factories)
-
addInternal
protected boolean addInternal(SubProtocolURLConnectionFactory factory)
-
remove
public boolean remove(SubProtocolURLConnectionFactory factory)
-
supports
public boolean supports(java.net.URL url, java.util.List<java.lang.String> subProtocols)Description copied from interface:SubProtocolURLConnectionFactorySupports the current factory to create theURLConnectionor not- Specified by:
supportsin interfaceSubProtocolURLConnectionFactory- Parameters:
url- the URL that this connects tosubProtocols- the list of sub-protocols- Returns:
trueif supports,otherwisefalse
-
create
public java.net.URLConnection create(java.net.URL url, java.util.List<java.lang.String> subProtocols, java.net.Proxy proxy) throws java.io.IOExceptionDescription copied from interface:SubProtocolURLConnectionFactoryCreate the sub-protocols'URLConnection- Specified by:
createin interfaceSubProtocolURLConnectionFactory- Parameters:
url- the URL that this connects tosubProtocols- the list of sub-protocolsproxy-Proxythe proxy through which the connection will be made. If direct connection is desired, Proxy.NO_PROXY should be specified.- Returns:
URLConnection- Throws:
java.io.IOException- If the process is failed
-
-