View Javadoc

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