Coverage Report - org.mule.config.endpoint.AnnotatedEndpointHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnotatedEndpointHelper
0%
0/59
0%
0/30
0
 
 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.config.endpoint;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleException;
 11  
 import org.mule.api.annotations.meta.ChannelType;
 12  
 import org.mule.api.endpoint.EndpointBuilder;
 13  
 import org.mule.api.endpoint.ImmutableEndpoint;
 14  
 import org.mule.api.expression.PropertyConverter;
 15  
 import org.mule.api.routing.filter.Filter;
 16  
 import org.mule.api.transformer.Transformer;
 17  
 import org.mule.config.i18n.AnnotationsMessages;
 18  
 import org.mule.endpoint.MuleEndpointURI;
 19  
 import org.mule.registry.RegistryMap;
 20  
 import org.mule.routing.MessageFilter;
 21  
 import org.mule.transport.AbstractConnector;
 22  
 import org.mule.transport.service.TransportFactory;
 23  
 import org.mule.util.TemplateParser;
 24  
 
 25  
 import java.util.Collection;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * This is a wrapper helper that can process {@link AnnotatedEndpointData} objects (Annotaiton config data)
 31  
  * and turn them into {@link org.mule.api.endpoint.EndpointBuilder} or {@link org.mule.api.endpoint.ImmutableEndpoint} objects.
 32  
  * <p/>
 33  
  * THis is an internal class that should only be used by the Annotation parser code.
 34  
  */
 35  
 public class AnnotatedEndpointHelper
 36  
 {
 37  0
     protected TemplateParser parser = TemplateParser.createAntStyleParser();
 38  
 
 39  
     protected RegistryMap regMap;
 40  
     protected MuleContext muleContext;
 41  
     protected TransportFactory transportFactory;
 42  
 
 43  
     public AnnotatedEndpointHelper(MuleContext muleContext) throws MuleException
 44  0
     {
 45  0
         this.muleContext = muleContext;
 46  0
         this.transportFactory = new ConfigurableTransportFactory(muleContext);
 47  0
         regMap = new RegistryMap(muleContext.getRegistry());
 48  0
     }
 49  
 
 50  
     protected String parsePlaceholderValues(String key)
 51  
     {
 52  0
         return parser.parse(regMap, key);
 53  
     }
 54  
 
 55  
     protected EndpointBuilder getEndpointBuilder(AnnotatedEndpointData epData) throws MuleException
 56  
     {
 57  0
         String uri = parsePlaceholderValues(epData.getAddress());
 58  
 
 59  0
         EndpointBuilder endpointBuilder = muleContext.getEndpointFactory().getEndpointBuilder(uri);
 60  0
         endpointBuilder.setMuleContext(muleContext);
 61  
 
 62  0
         return endpointBuilder;
 63  
     }
 64  
 
 65  
     public ImmutableEndpoint processEndpoint(AnnotatedEndpointData epData) throws MuleException
 66  
     {
 67  0
         preprocessEndpointData(epData);
 68  
 
 69  
         ImmutableEndpoint endpoint;
 70  0
         EndpointBuilder endpointBuilder = getEndpointBuilder(epData);
 71  
 
 72  0
         if (epData.getProperties() != null && epData.getProperties().size() > 0)
 73  
         {
 74  0
             endpointBuilder.setProperties(epData.getProperties());
 75  
         }
 76  
 
 77  0
         if (epData.getTransformers() != null)
 78  
         {
 79  0
             List<Transformer> transformers = (List) convertProperty(List.class, epData.getTransformers());
 80  0
             endpointBuilder.setTransformers(transformers);
 81  
         }
 82  
 
 83  0
         if (epData.getFilter() != null)
 84  
         {
 85  0
             Filter filter = (Filter) convertProperty(Filter.class, epData.getFilter());
 86  0
             endpointBuilder.addMessageProcessor(new MessageFilter(filter));
 87  
 
 88  
         }
 89  
 
 90  0
         if (epData.getEncoding() != null)
 91  
         {
 92  0
             endpointBuilder.setEncoding(parsePlaceholderValues(epData.getEncoding()));
 93  
         }
 94  
 
 95  
         AbstractConnector connector;
 96  0
         if (epData.getConnectorName() != null)
 97  
         {
 98  0
             connector = (AbstractConnector) muleContext.getRegistry().lookupConnector(parsePlaceholderValues(epData.getConnectorName()));
 99  
         }
 100  0
         else if (epData.getConnector() != null)
 101  
         {
 102  0
             connector = (AbstractConnector) epData.getConnector();
 103  
         }
 104  
         else
 105  
         {
 106  
             //We always create a new connecotr for annotations when one has not been configured
 107  0
             MuleEndpointURI uri = new MuleEndpointURI(parsePlaceholderValues(epData.getAddress()), muleContext);
 108  
 
 109  0
             connector = (AbstractConnector) transportFactory.createConnector(uri);
 110  
             //The ibeans transport factory will not always create a new connector, check before registering
 111  0
             if (muleContext.getRegistry().lookupConnector(connector.getName()) == null)
 112  
             {
 113  0
                 muleContext.getRegistry().registerConnector(connector);
 114  
             }
 115  
         }
 116  0
         endpointBuilder.setConnector(connector);
 117  
 
 118  
         //Set threading for this connector. Note we simplify by setting all profiles with a single value 'threads'
 119  
         //that can be set by the user
 120  0
         String threadsString = (String) epData.getProperties().get("threads");
 121  0
         if (threadsString != null)
 122  
         {
 123  0
             int threads = Integer.valueOf(threadsString);
 124  0
             connector.setMaxDispatchersActive(threads);
 125  0
             connector.setMaxRequestersActive(threads);
 126  0
             connector.getReceiverThreadingProfile().setMaxThreadsActive(threads);
 127  0
             connector.getReceiverThreadingProfile().setMaxThreadsIdle(threads);
 128  
         }
 129  
 
 130  0
         if (epData.getName() != null)
 131  
         {
 132  0
             endpointBuilder.setName(parsePlaceholderValues(epData.getName()));
 133  
         }
 134  
 
 135  0
         endpointBuilder.setExchangePattern(epData.getMep());
 136  
 
 137  0
         if (epData.getType() == ChannelType.Inbound)
 138  
         {
 139  0
             endpoint = endpointBuilder.buildInboundEndpoint();
 140  
         }
 141  0
         else if (epData.getType() == ChannelType.Outbound)
 142  
         {
 143  0
             endpoint = endpointBuilder.buildOutboundEndpoint();
 144  
         }
 145  
         else
 146  
         {
 147  0
             throw new IllegalArgumentException("Channel type not recognised: " + epData.getType());
 148  
         }
 149  
         //TODO: not sure where to put this yet
 150  0
         if (epData.getName() != null)
 151  
         {
 152  0
             muleContext.getRegistry().registerEndpointBuilder(epData.getName(), endpointBuilder);
 153  
         }
 154  0
         return endpoint;
 155  
     }
 156  
 
 157  
     /**
 158  
      * This method can be overridden to process endpoints before they get built. This may be useful in environments
 159  
      * where the characteristics of the endpoint change depending on the deployed environment
 160  
      *
 161  
      * @param data the endpoint data to process
 162  
      */
 163  
     protected void preprocessEndpointData(AnnotatedEndpointData data)
 164  
     {
 165  
         //no=op
 166  0
     }
 167  
 
 168  
     public Object convertProperty(Class type, String property)
 169  
     {
 170  0
         String prop = parsePlaceholderValues(property);
 171  0
         Collection c = muleContext.getRegistry().lookupObjects(PropertyConverter.class);
 172  0
         for (Iterator iterator = c.iterator(); iterator.hasNext();)
 173  
         {
 174  0
             PropertyConverter converter = (PropertyConverter) iterator.next();
 175  0
             if (converter.getType().equals(type))
 176  
             {
 177  0
                 return converter.convert(prop, muleContext);
 178  
             }
 179  0
         }
 180  0
         throw new IllegalArgumentException(AnnotationsMessages.noPropertyConverterForType(type).getMessage());
 181  
     }
 182  
 
 183  
 }