Class CompositeSubProtocolURLConnectionFactory

  • All Implemented Interfaces:
    SubProtocolURLConnectionFactory

    public class CompositeSubProtocolURLConnectionFactory
    extends java.lang.Object
    implements SubProtocolURLConnectionFactory
    A composite implementation of SubProtocolURLConnectionFactory 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