Coverage Report - org.mule.api.registry.ServiceDescriptorFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceDescriptorFactory
42%
11/26
30%
3/10
13
 
 1  
 /*
 2  
  * $Id: ServiceDescriptorFactory.java 11518 2008-03-31 22:07:18Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MuleServer;
 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.TransportServiceDescriptor;
 19  
 import org.mule.util.ClassUtils;
 20  
 import org.mule.util.SpiUtils;
 21  
 
 22  
 import java.util.Properties;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 
 26  
 /**
 27  
  * Factory used to create a new service descriptor.
 28  
  */
 29  
 // TODO MULE-2102
 30  0
 public class ServiceDescriptorFactory 
 31  
 {
 32  
     // Service types (used for looking up the service descriptors)
 33  
     public static final String PROVIDER_SERVICE_TYPE = "transport";
 34  
     public static final String MODEL_SERVICE_TYPE = "model";
 35  
     public static final String EXCEPTION_SERVICE_TYPE = "exception";
 36  
     
 37  
     /**
 38  
      * Factory method to create a new service descriptor.
 39  
      */
 40  
     public static ServiceDescriptor create(String type, String name, Properties props, Properties overrides, Registry registry) throws ServiceException
 41  
     {       
 42  
 
 43  390
         if (overrides != null)
 44  
         {
 45  0
             props.putAll(overrides);
 46  
         }
 47  
 
 48  390
         String serviceFinderClass = (String) props.remove(MuleProperties.SERVICE_FINDER);
 49  
         
 50  390
         ServiceDescriptor sd = null;
 51  390
         if (type.equals(PROVIDER_SERVICE_TYPE)) 
 52  
         {
 53  
             try
 54  
             {
 55  390
                 sd = new DefaultTransportServiceDescriptor(name, props, registry);
 56  
             }
 57  0
             catch (Exception e)
 58  
             {
 59  0
                 throw (IllegalStateException) new IllegalStateException("Cannot create transport " + name).initCause(e);
 60  390
             }
 61  390
             Properties exceptionMappingProps = SpiUtils.findServiceDescriptor(EXCEPTION_SERVICE_TYPE, name + "-exception-mappings");
 62  390
             ((TransportServiceDescriptor) sd).setExceptionMappings(exceptionMappingProps);
 63  390
         }
 64  0
         else if (type.equals(MODEL_SERVICE_TYPE))
 65  
         {
 66  0
             sd = new DefaultModelServiceDescriptor(name, props);
 67  
         }
 68  
         else
 69  
         {
 70  0
             throw new ServiceException(CoreMessages.unrecognisedServiceType(type));
 71  
         }
 72  
 
 73  
         // If there is a finder service, use it to find the "real" service.
 74  390
         if (StringUtils.isNotBlank(serviceFinderClass))
 75  
         {
 76  
             ServiceFinder finder;
 77  
             try
 78  
             {
 79  0
                 finder = (ServiceFinder) ClassUtils.instanciateClass(serviceFinderClass, ClassUtils.NO_ARGS);
 80  
             }
 81  0
             catch (Exception e)
 82  
             {
 83  0
                 throw new ServiceException(CoreMessages.cannotInstanciateFinder(serviceFinderClass), e);
 84  0
             }
 85  0
             String realService = finder.findService(name, sd, props);
 86  0
             if (realService != null)
 87  
             {
 88  
                 // Recursively look up the service descriptor for the real service.
 89  0
                 return MuleServer.getMuleContext().getRegistry().lookupServiceDescriptor(
 90  
                     ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, realService, overrides);
 91  
             }
 92  
             else
 93  
             {
 94  0
                 throw new ServiceException(CoreMessages.serviceFinderCantFindService(name));
 95  
             }
 96  
         }
 97  390
         return sd;
 98  
     }
 99  
 }
 100  
 
 101