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