Coverage Report - org.mule.endpoint.DefaultEndpointFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultEndpointFactory
0%
0/54
0%
0/14
1.714
DefaultEndpointFactory$1
0%
0/2
N/A
1.714
DefaultEndpointFactory$2
0%
0/2
N/A
1.714
DefaultEndpointFactory$EndpointSource
N/A
N/A
1.714
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.endpoint;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleException;
 11  
 import org.mule.api.endpoint.EndpointBuilder;
 12  
 import org.mule.api.endpoint.EndpointException;
 13  
 import org.mule.api.endpoint.EndpointFactory;
 14  
 import org.mule.api.endpoint.EndpointURI;
 15  
 import org.mule.api.endpoint.ImmutableEndpoint;
 16  
 import org.mule.api.endpoint.InboundEndpoint;
 17  
 import org.mule.api.endpoint.OutboundEndpoint;
 18  
 import org.mule.api.registry.RegistrationException;
 19  
 import org.mule.api.registry.ServiceType;
 20  
 import org.mule.config.i18n.CoreMessages;
 21  
 import org.mule.transport.service.TransportServiceDescriptor;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  0
 public class DefaultEndpointFactory implements EndpointFactory
 27  
 {
 28  
     /**
 29  
      * logger used by this class
 30  
      */
 31  0
     protected static final Log logger = LogFactory.getLog(DefaultEndpointFactory.class);
 32  
 
 33  
     public static final String ENDPOINT_REGISTRY_PREFIX = "endpoint:";
 34  
 
 35  
     protected MuleContext muleContext;
 36  
 
 37  
     public InboundEndpoint getInboundEndpoint(String uri)
 38  
             throws MuleException
 39  
     {
 40  0
         logger.debug("DefaultEndpointFactory request for inbound endpoint for uri: " + uri);
 41  0
         EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
 42  0
         if (endpointBuilder == null)
 43  
         {
 44  0
             logger.debug("Named EndpointBuilder not found, creating endpoint from uri");
 45  0
             endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
 46  
         }
 47  0
         return getInboundEndpoint(endpointBuilder);
 48  
     }
 49  
 
 50  
     public OutboundEndpoint getOutboundEndpoint(String uri)
 51  
             throws MuleException
 52  
     {
 53  0
         logger.debug("DefaultEndpointFactory request for outbound endpoint for uri: " + uri);
 54  0
         EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
 55  0
         if (endpointBuilder == null)
 56  
         {
 57  0
             logger.debug("Named EndpointBuilder not found, creating endpoint from uri");
 58  0
             endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
 59  
 
 60  
         }
 61  0
         return getOutboundEndpoint(endpointBuilder);
 62  
     }
 63  
 
 64  
     protected EndpointBuilder lookupEndpointBuilder(String endpointName)
 65  
     {
 66  0
         logger.debug("Looking up EndpointBuilder with name:" + endpointName + " in registry");
 67  
         // TODO DF: Do some simple parsing of endpointName to not lookup endpoint builder if endpointName is
 68  
         // obviously a uri and not a substituted name ??
 69  0
         EndpointBuilder endpointBuilder = muleContext.getRegistry().lookupEndpointBuilder(endpointName);
 70  0
         if (endpointBuilder != null)
 71  
         {
 72  0
             logger.debug("EndpointBuilder with name:" + endpointName + " FOUND");
 73  
         }
 74  0
         return endpointBuilder;
 75  
     }
 76  
 
 77  
     public InboundEndpoint getInboundEndpoint(EndpointBuilder builder) throws MuleException
 78  
     {
 79  0
         InboundEndpoint endpoint = builder.buildInboundEndpoint();
 80  0
         return (InboundEndpoint) registerEndpoint(endpoint);
 81  
     }
 82  
 
 83  
     public OutboundEndpoint getOutboundEndpoint(EndpointBuilder builder) throws MuleException
 84  
     {
 85  0
         OutboundEndpoint endpoint = builder.buildOutboundEndpoint();
 86  0
         return (OutboundEndpoint) registerEndpoint(endpoint);
 87  
     }
 88  
 
 89  
     /**
 90  
      * @param endpoint
 91  
      * @throws RegistrationException
 92  
      */
 93  
     protected ImmutableEndpoint registerEndpoint(ImmutableEndpoint endpoint) throws RegistrationException
 94  
     {
 95  0
         ImmutableEndpoint registryEndpoint = (ImmutableEndpoint) muleContext.getRegistry().lookupObject(
 96  
                 ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode());
 97  0
         if (registryEndpoint == null)
 98  
         {
 99  0
             muleContext.getRegistry().registerObject(ENDPOINT_REGISTRY_PREFIX + endpoint.hashCode(), endpoint);
 100  0
             registryEndpoint = endpoint;
 101  
         }
 102  0
         return registryEndpoint;
 103  
     }
 104  
 
 105  
     public EndpointBuilder getEndpointBuilder(String uri)
 106  
             throws MuleException
 107  
     {
 108  0
         logger.debug("DefaultEndpointFactory request for endpoint builder for uri: " + uri);
 109  0
         EndpointBuilder endpointBuilder = lookupEndpointBuilder(uri);
 110  0
         if (endpointBuilder != null)
 111  
         {
 112  
             try
 113  
             {
 114  0
                 endpointBuilder = (EndpointBuilder) endpointBuilder.clone();
 115  
             }
 116  0
             catch (Exception e)
 117  
             {
 118  0
                 throw new EndpointException(CoreMessages.failedToClone("global endpoint EndpointBuilder"), e);
 119  0
             }
 120  
         }
 121  
         else
 122  
         {
 123  0
             logger.debug("Named EndpointBuilder not found, creating endpoint builder for uri");
 124  0
             EndpointURI epURI = new MuleEndpointURI(uri, muleContext);
 125  0
             TransportServiceDescriptor tsd = (TransportServiceDescriptor) muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, epURI.getFullScheme(), null);
 126  0
             endpointBuilder = tsd.createEndpointBuilder(uri);
 127  
         }
 128  0
         return endpointBuilder;
 129  
     }
 130  
 
 131  
     public void setMuleContext(MuleContext context)
 132  
     {
 133  0
         this.muleContext = context;
 134  0
     }
 135  
 
 136  
     public org.mule.api.endpoint.InboundEndpoint getInboundEndpoint(EndpointURI uri) throws MuleException
 137  
     {
 138  0
         return (InboundEndpoint) getEndpoint(uri, new EndpointSource()
 139  0
         {
 140  
             public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
 141  
             {
 142  0
                 return getInboundEndpoint(builder);
 143  
             }
 144  
         });
 145  
     }
 146  
 
 147  
     public OutboundEndpoint getOutboundEndpoint(EndpointURI uri) throws MuleException
 148  
     {
 149  0
         return (OutboundEndpoint) getEndpoint(uri, new EndpointSource()
 150  0
         {
 151  
             public ImmutableEndpoint getEndpoint(EndpointBuilder builder) throws MuleException
 152  
             {
 153  0
                 return getOutboundEndpoint(builder);
 154  
             }
 155  
         });
 156  
     }
 157  
 
 158  
     protected ImmutableEndpoint getEndpoint(EndpointURI uri, EndpointSource source) throws MuleException
 159  
     {
 160  0
         logger.debug("DefaultEndpointFactory request for endpoint for: " + uri);
 161  0
         EndpointBuilder endpointBuilder = null;
 162  0
         if (uri.getEndpointName() != null)
 163  
         {
 164  0
             endpointBuilder = lookupEndpointBuilder(uri.getEndpointName());
 165  0
             if (endpointBuilder == null)
 166  
             {
 167  0
                 throw new IllegalArgumentException("The endpoint with name: " + uri.getEndpointName()
 168  
                         + "was not found.");
 169  
             }
 170  
         }
 171  
         else
 172  
         {
 173  0
             logger.debug("Named EndpointBuilder not found, creating endpoint from uri");
 174  0
             endpointBuilder = new EndpointURIEndpointBuilder(uri);
 175  
         }
 176  0
         return source.getEndpoint(endpointBuilder);
 177  
     }
 178  
 
 179  0
     private interface EndpointSource
 180  
     {
 181  
         ImmutableEndpoint getEndpoint(EndpointBuilder endpointBuilder) throws MuleException;
 182  
     }
 183  
 
 184  
 }