View Javadoc
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  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          if (overrides != null)
44          {
45              props.putAll(overrides);
46          }
47  
48          String scheme = name;
49          String metaScheme = null;
50          int i = name.indexOf(":");
51          if (i > -1)
52          {
53              scheme = name.substring(i + 1);
54              metaScheme = name.substring(0, i);
55          }
56          //TODO we currently need to filter out transports that implement the meta scheme the old way
57          if (isFilteredMetaScheme(metaScheme))
58          {
59              //handle things the old way for now
60              metaScheme = null;
61          }
62          else if (name.startsWith("jetty:http"))
63          {
64              scheme = "jetty";
65          }
66  
67          String serviceFinderClass = (String) props.remove(MuleProperties.SERVICE_FINDER);
68  
69          ServiceDescriptor sd;
70          if (type.equals(ServiceType.TRANSPORT))
71          {
72              try
73              {
74                  if (metaScheme != null)
75                  {
76                      sd = new MetaTransportServiceDescriptor(metaScheme, scheme, props, classLoader);
77                  }
78                  else
79                  {
80                      sd = new DefaultTransportServiceDescriptor(scheme, props, classLoader);
81                  }
82              }
83              catch (ServiceException e)
84              {
85                  throw e;
86              }
87              catch (Exception e)
88              {
89                  throw new ServiceException(CoreMessages.failedToCreate("Transport: " + name));
90              }
91              Properties exceptionMappingProps = SpiUtils.findServiceDescriptor(ServiceType.EXCEPTION, name + "-exception-mappings");
92              ((TransportServiceDescriptor) sd).setExceptionMappings(exceptionMappingProps);
93          }
94          else if (type.equals(ServiceType.MODEL))
95          {
96              sd = new DefaultModelServiceDescriptor(name, props);
97          }
98          else
99          {
100             throw new ServiceException(CoreMessages.unrecognisedServiceType(type));
101         }
102 
103         // If there is a finder service, use it to find the "real" service.
104         if (StringUtils.isNotBlank(serviceFinderClass))
105         {
106             ServiceFinder finder;
107             try
108             {
109                 finder = (ServiceFinder) ClassUtils.instanciateClass(serviceFinderClass);
110             }
111             catch (Exception e)
112             {
113                 throw new ServiceException(CoreMessages.cannotInstanciateFinder(serviceFinderClass), e);
114             }
115             String realService = finder.findService(name, sd, props);
116             if (realService != null)
117             {
118                 // Recursively look up the service descriptor for the real service.
119                 return muleContext.getRegistry().lookupServiceDescriptor(
120                         ServiceType.TRANSPORT, realService, overrides);
121             }
122             else
123             {
124                 throw new ServiceException(CoreMessages.serviceFinderCantFindService(name));
125             }
126         }
127         return sd;
128     }
129 
130     protected static boolean isFilteredMetaScheme(String metaScheme)
131     {
132         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             return true;
141         }
142         return false;
143     }
144 }
145 
146