Coverage Report - org.mule.module.ibeans.spi.MuleIBeansPlugin
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleIBeansPlugin
0%
0/39
0%
0/10
0
 
 1  
 /*
 2  
  * $Id: MuleIBeansPlugin.java 19979 2010-10-21 11:18:53Z rossmason $
 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.module.ibeans.spi;
 11  
 
 12  
 import org.mule.DefaultMuleMessage;
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.registry.RegistryMap;
 16  
 
 17  
 import java.util.ArrayList;
 18  
 import java.util.HashMap;
 19  
 import java.util.LinkedList;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.activation.DataHandler;
 24  
 import javax.activation.DataSource;
 25  
 import javax.activation.MimeTypeParseException;
 26  
 
 27  
 import org.ibeans.api.CallInterceptor;
 28  
 import org.ibeans.api.IBeanInvocationData;
 29  
 import org.ibeans.api.IBeanInvoker;
 30  
 import org.ibeans.api.IBeansException;
 31  
 import org.ibeans.api.channel.CHANNEL;
 32  
 import org.ibeans.impl.DefaultIBeanInvoker;
 33  
 import org.ibeans.impl.InvokeAnnotationHandler;
 34  
 import org.ibeans.impl.TemplateAnnotationHandler;
 35  
 import org.ibeans.impl.support.util.Utils;
 36  
 import org.ibeans.spi.ErrorFilterFactory;
 37  
 import org.ibeans.spi.ExpressionParser;
 38  
 import org.ibeans.spi.IBeansPlugin;
 39  
 
 40  
 /**
 41  
  * The entry-point for Mule to integrate with IBeans
 42  
  */
 43  0
 public class MuleIBeansPlugin implements IBeansPlugin<MuleRequestMessage, MuleResponseMessage>
 44  
 {
 45  
     private MuleContext muleContext;
 46  
 
 47  
     private Map<String, Object> properties;
 48  
     private MuleExpressionParser expressionParser;
 49  
     private MuleCallAnnotationHandler callAnnotationHandler;
 50  
     private TemplateAnnotationHandler templateAnnotationHandler;
 51  
     private InvokeAnnotationHandler invokeAnnotationHandler;
 52  
     private MuleResponseTransformInterceptor responseTransformInterceptor;
 53  
     private List<ErrorFilterFactory> errorFilterFactories;
 54  
 
 55  
     public MuleIBeansPlugin(MuleContext muleContext)
 56  0
     {
 57  0
         this.muleContext = muleContext;
 58  0
         callAnnotationHandler = new MuleCallAnnotationHandler(muleContext);
 59  0
         expressionParser = new MuleExpressionParser(muleContext);
 60  0
         properties = new RegistryMap(muleContext.getRegistry());
 61  0
         templateAnnotationHandler = new TemplateAnnotationHandler(this);
 62  0
         invokeAnnotationHandler = new InvokeAnnotationHandler(this);
 63  0
         responseTransformInterceptor = new MuleResponseTransformInterceptor(muleContext, expressionParser);
 64  
 
 65  0
         errorFilterFactories = new ArrayList<ErrorFilterFactory>();
 66  0
         errorFilterFactories.add(new ExpressionErrorFilterFactory(muleContext));
 67  0
     }
 68  
 
 69  
     public CallInterceptor getResponseTransformInterceptor() throws IBeansException
 70  
     {
 71  0
         return responseTransformInterceptor;
 72  
     }
 73  
 
 74  
     public IBeanInvoker<MuleCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler> getIBeanInvoker() throws IBeansException
 75  
     {
 76  0
         return new DefaultIBeanInvoker<MuleCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler>(callAnnotationHandler, templateAnnotationHandler, invokeAnnotationHandler);
 77  
     }
 78  
 
 79  
     public IBeanInvoker<MuleMockCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler> getMockIBeanInvoker(Object mock) throws IBeansException
 80  
     {
 81  0
         return new DefaultIBeanInvoker<MuleMockCallAnnotationHandler, TemplateAnnotationHandler, InvokeAnnotationHandler>(new MuleMockCallAnnotationHandler(muleContext, mock, this), templateAnnotationHandler, invokeAnnotationHandler);
 82  
     }
 83  
 
 84  
     public List<ErrorFilterFactory> getErrorFilterFactories()
 85  
     {
 86  0
         return errorFilterFactories;
 87  
     }
 88  
 
 89  
     public Map getProperties()
 90  
     {
 91  0
         return properties;
 92  
     }
 93  
 
 94  
     public ExpressionParser getExpressionParser()
 95  
     {
 96  0
         return expressionParser;
 97  
     }
 98  
 
 99  
     public void addInterceptors(LinkedList<CallInterceptor> interceptors)
 100  
     {
 101  
         //nothing to do
 102  0
     }
 103  
 
 104  
     public MuleRequestMessage createRequest(IBeanInvocationData data) throws IBeansException
 105  
     {
 106  
         MuleRequestMessage request;
 107  0
         Object payload = (data.getPayloads().size() == 1 ? data.getPayloads().get(0) : data.getPayloads());
 108  
 
 109  0
         MuleMessage message = new DefaultMuleMessage(payload, muleContext);
 110  
 
 111  
         //We need to scrub any null header values since Mule does not allow Null headers
 112  0
         for (Map.Entry<String, Object> entry : data.getHeaderParams().entrySet())
 113  
         {
 114  0
             if (entry.getValue() != null)
 115  
             {
 116  0
                 message.setOutboundProperty(entry.getKey(), entry.getValue());
 117  
             }
 118  
         }
 119  
 
 120  
         //TODO (requires API change)
 121  
         // message.setInvocationProperty(MuleProperties.MULE_METHOD_PROPERTY, XXX);
 122  
 
 123  
         //Set the URI params so the correct URI can be constructed for this invocation
 124  0
         message.setOutboundProperty(CHANNEL.URI_PARAM_PROPERTIES, data.getUriParams());
 125  
 
 126  
         //Add any attachments
 127  0
         for (DataSource dataSource : data.getAttachments())
 128  
         {
 129  
             try
 130  
             {
 131  0
                 message.addOutboundAttachment(dataSource.getName(), new DataHandler(dataSource));
 132  
             }
 133  0
             catch (Exception e)
 134  
             {
 135  0
                 throw new IBeansException(e);
 136  0
             }
 137  
         }
 138  
 
 139  
         //Add the properties to the invocation scope
 140  0
         for (String key : data.getPropertyParams().keySet())
 141  
         {
 142  0
             message.setInvocationProperty(key, data.getPropertyParams().get(key));
 143  
         }
 144  
 
 145  0
         request = new MuleRequestMessage(data, message);
 146  
 
 147  
         //TODO It may be useful to set the Method invoked on the request, In Mule,
 148  
         //Some transports such as Axis, RMI and EJB can use the method information
 149  
         //Not doing this for now since the scope is HTTP only
 150  
 
 151  
         //Set the request timeout, the default -1 means it will not timeout
 152  0
         request.setTimeout(Utils.getInt(data.getPropertyParams().get(CHANNEL.TIMEOUT), -1));
 153  0
         return request;
 154  
     }
 155  
 
 156  
     public MuleResponseMessage createResponse(Object payload, Map<String, Object> headers, Map<String, DataHandler> attachments) throws IBeansException
 157  
     {
 158  0
         MuleMessage message = new DefaultMuleMessage(payload, headers, new HashMap<String, Object>(), attachments, muleContext);
 159  
         try
 160  
         {
 161  0
             return new MuleResponseMessage(message);
 162  
         }
 163  0
         catch (MimeTypeParseException e)
 164  
         {
 165  0
             throw new IBeansException(e);
 166  
         }
 167  
     }
 168  
 }