Coverage Report - org.mule.api.registry.ServiceDescriptorFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceDescriptorFactory
0%
0/42
0%
0/32
11
 
 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.api.registry;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.config.MuleProperties;
 11  
 import org.mule.config.i18n.CoreMessages;
 12  
 import org.mule.model.DefaultModelServiceDescriptor;
 13  
 import org.mule.transport.service.DefaultTransportServiceDescriptor;
 14  
 import org.mule.transport.service.MetaTransportServiceDescriptor;
 15  
 import org.mule.transport.service.TransportServiceDescriptor;
 16  
 import org.mule.util.ClassUtils;
 17  
 import org.mule.util.SpiUtils;
 18  
 
 19  
 import java.util.Properties;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 
 23  
 /**
 24  
  * Factory used to create a new service descriptor.
 25  
  */
 26  0
 public class ServiceDescriptorFactory
 27  
 {
 28  
     /**
 29  
      * Factory method to create a new service descriptor.
 30  
      *
 31  
      * @param type        the service type to create
 32  
      * @param name        the name of the service.  In the case of a stransport service, the full endpoint sheme should be used here
 33  
      *                    i.e. 'cxf:http'
 34  
      * @param props       The properties defined by this service type
 35  
      * @param overrides   any overrides that should be configured on top of the standard propertiers for the service
 36  
      * @param muleContext the MuleContext for this mule instance
 37  
      * @param classLoader the ClassLoader to use when loading classes
 38  
      * @return a ServiceDescriptor instance that can be used to create the service objects associated with the service name
 39  
      * @throws ServiceException if the service cannot be located
 40  
      */
 41  
     public static ServiceDescriptor create(ServiceType type, String name, Properties props, Properties overrides, MuleContext muleContext, ClassLoader classLoader) throws ServiceException
 42  
     {
 43  0
         if (overrides != null)
 44  
         {
 45  0
             props.putAll(overrides);
 46  
         }
 47  
 
 48  0
         String scheme = name;
 49  0
         String metaScheme = null;
 50  0
         int i = name.indexOf(":");
 51  0
         if (i > -1)
 52  
         {
 53  0
             scheme = name.substring(i + 1);
 54  0
             metaScheme = name.substring(0, i);
 55  
         }
 56  
         //TODO we currently need to filter out transports that implement the meta scheme the old way
 57  0
         if (isFilteredMetaScheme(metaScheme))
 58  
         {
 59  
             //handle things the old way for now
 60  0
             metaScheme = null;
 61  
         }
 62  0
         else if (name.startsWith("jetty:http"))
 63  
         {
 64  0
             scheme = "jetty";
 65  
         }
 66  
 
 67  0
         String serviceFinderClass = (String) props.remove(MuleProperties.SERVICE_FINDER);
 68  
 
 69  
         ServiceDescriptor sd;
 70  0
         if (type.equals(ServiceType.TRANSPORT))
 71  
         {
 72  
             try
 73  
             {
 74  0
                 if (metaScheme != null)
 75  
                 {
 76  0
                     sd = new MetaTransportServiceDescriptor(metaScheme, scheme, props, classLoader);
 77  
                 }
 78  
                 else
 79  
                 {
 80  0
                     sd = new DefaultTransportServiceDescriptor(scheme, props, classLoader);
 81  
                 }
 82  
             }
 83  0
             catch (ServiceException e)
 84  
             {
 85  0
                 throw e;
 86  
             }
 87  0
             catch (Exception e)
 88  
             {
 89  0
                 throw new ServiceException(CoreMessages.failedToCreate("Transport: " + name));
 90  0
             }
 91  0
             Properties exceptionMappingProps = SpiUtils.findServiceDescriptor(ServiceType.EXCEPTION, name + "-exception-mappings");
 92  0
             ((TransportServiceDescriptor) sd).setExceptionMappings(exceptionMappingProps);
 93  0
         }
 94  0
         else if (type.equals(ServiceType.MODEL))
 95  
         {
 96  0
             sd = new DefaultModelServiceDescriptor(name, props);
 97  
         }
 98  
         else
 99  
         {
 100  0
             throw new ServiceException(CoreMessages.unrecognisedServiceType(type));
 101  
         }
 102  
 
 103  
         // If there is a finder service, use it to find the "real" service.
 104  0
         if (StringUtils.isNotBlank(serviceFinderClass))
 105  
         {
 106  
             ServiceFinder finder;
 107  
             try
 108  
             {
 109  0
                 finder = (ServiceFinder) ClassUtils.instanciateClass(serviceFinderClass);
 110  
             }
 111  0
             catch (Exception e)
 112  
             {
 113  0
                 throw new ServiceException(CoreMessages.cannotInstanciateFinder(serviceFinderClass), e);
 114  0
             }
 115  0
             String realService = finder.findService(name, sd, props);
 116  0
             if (realService != null)
 117  
             {
 118  
                 // Recursively look up the service descriptor for the real service.
 119  0
                 return muleContext.getRegistry().lookupServiceDescriptor(
 120  
                         ServiceType.TRANSPORT, realService, overrides);
 121  
             }
 122  
             else
 123  
             {
 124  0
                 throw new ServiceException(CoreMessages.serviceFinderCantFindService(name));
 125  
             }
 126  
         }
 127  0
         return sd;
 128  
     }
 129  
 
 130  
     protected static boolean isFilteredMetaScheme(String metaScheme)
 131  
     {
 132  0
         if ("axis".equals(metaScheme) ||
 133  
             "wsdl-axis".equals(metaScheme) || 
 134  
             "cxf".equals(metaScheme) || 
 135  
             "wsdl-cxf".equals(metaScheme) || 
 136  
             "jms".equals(metaScheme) || 
 137  
             "wmq".equals(metaScheme) || 
 138  
             "ajax".equals(metaScheme))
 139  
         {
 140  0
             return true;
 141  
         }
 142  0
         return false;
 143  
     }
 144  
 }
 145  
 
 146