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