View Javadoc

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  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          if (overrides != null)
44          {
45              props.putAll(overrides);
46          }
47  
48          String serviceFinderClass = (String) props.remove(MuleProperties.SERVICE_FINDER);
49          
50          ServiceDescriptor sd = null;
51          if (type.equals(PROVIDER_SERVICE_TYPE)) 
52          {
53              try
54              {
55                  sd = new DefaultTransportServiceDescriptor(name, props, registry);
56              }
57              catch (Exception e)
58              {
59                  throw (IllegalStateException) new IllegalStateException("Cannot create transport " + name).initCause(e);
60              }
61              Properties exceptionMappingProps = SpiUtils.findServiceDescriptor(EXCEPTION_SERVICE_TYPE, name + "-exception-mappings");
62              ((TransportServiceDescriptor) sd).setExceptionMappings(exceptionMappingProps);
63          }
64          else if (type.equals(MODEL_SERVICE_TYPE))
65          {
66              sd = new DefaultModelServiceDescriptor(name, props);
67          }
68          else
69          {
70              throw new ServiceException(CoreMessages.unrecognisedServiceType(type));
71          }
72  
73          // If there is a finder service, use it to find the "real" service.
74          if (StringUtils.isNotBlank(serviceFinderClass))
75          {
76              ServiceFinder finder;
77              try
78              {
79                  finder = (ServiceFinder) ClassUtils.instanciateClass(serviceFinderClass, ClassUtils.NO_ARGS);
80              }
81              catch (Exception e)
82              {
83                  throw new ServiceException(CoreMessages.cannotInstanciateFinder(serviceFinderClass), e);
84              }
85              String realService = finder.findService(name, sd, props);
86              if (realService != null)
87              {
88                  // Recursively look up the service descriptor for the real service.
89                  return MuleServer.getMuleContext().getRegistry().lookupServiceDescriptor(
90                      ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, realService, overrides);
91              }
92              else
93              {
94                  throw new ServiceException(CoreMessages.serviceFinderCantFindService(name));
95              }
96          }
97          return sd;
98      }
99  }
100 
101