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 ofSubProtocolURLConnectionFactory
that 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
Prioritized
interface. 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 CompositeSubProtocolURLConnectionFactory
add(SubProtocolURLConnectionFactory factory)
CompositeSubProtocolURLConnectionFactory
add(SubProtocolURLConnectionFactory... factories)
protected boolean
addInternal(SubProtocolURLConnectionFactory factory)
java.net.URLConnection
create(java.net.URL url, java.util.List<java.lang.String> subProtocols, java.net.Proxy proxy)
Create the sub-protocols'URLConnection
boolean
remove(SubProtocolURLConnectionFactory factory)
boolean
supports(java.net.URL url, java.util.List<java.lang.String> subProtocols)
Supports the current factory to create theURLConnection
or 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:SubProtocolURLConnectionFactory
Supports the current factory to create theURLConnection
or not- Specified by:
supports
in interfaceSubProtocolURLConnectionFactory
- Parameters:
url
- the URL that this connects tosubProtocols
- the list of sub-protocols- Returns:
true
if 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.IOException
Description copied from interface:SubProtocolURLConnectionFactory
Create the sub-protocols'URLConnection
- Specified by:
create
in interfaceSubProtocolURLConnectionFactory
- Parameters:
url
- the URL that this connects tosubProtocols
- the list of sub-protocolsproxy
-Proxy
the 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
-
-