View Javadoc

1   /*
2    * $Id: SpiUtils.java 12370 2008-07-17 13:11:17Z 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.util;
12  
13  import org.mule.api.registry.ServiceDescriptorFactory;
14  import org.mule.config.ExceptionHelper;
15  import org.mule.transport.service.TransportFactory;
16  
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.util.Properties;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  // TODO This will eventually use the OSGi Service Registry for locating services
25  // @ThreadSafe
26  public class SpiUtils
27  {
28      private static final Log logger = LogFactory.getLog(SpiUtils.class);
29  
30      public static final String SERVICE_ROOT = "META-INF/services/";
31      public static final String PROVIDER_SERVICE_PATH = "org/mule/providers/";
32      public static final String EXCEPTION_SERVICE_PATH = "org/mule/config/";
33  
34      public static Properties findServiceDescriptor(String type, String name)
35      {
36          if (type.equals(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE))
37          {
38              return findServiceDescriptor(PROVIDER_SERVICE_PATH, name, TransportFactory.class);
39          }
40          else if (type.equals(ServiceDescriptorFactory.EXCEPTION_SERVICE_TYPE))
41          {
42              return findServiceDescriptor(EXCEPTION_SERVICE_PATH, name, ExceptionHelper.class);
43          }
44          else
45          {
46              logger.warn("Attempt to lookup unrecognized service type: " + type);
47              return null;
48          }
49  
50      }
51  
52      public static Properties findServiceDescriptor(String path, String name, Class currentClass)
53      {
54      	//Preferred name and preferred path - used to construct a URI for alternative or preferred 
55      	//property set.  This enables alternative implementations of a transport to exist side by side
56      	//in a single Mule VM.  Most transports will not have a preferred property set.
57      	String preferredName = null;
58      	String preferredPath = null;
59      	
60          if (!name.endsWith(".properties"))
61          {
62              name += ".properties";
63              //convention is preferred-<protocol>.properties
64              preferredName = "preferred-" + name;
65          }
66  
67          if (path.startsWith("/"))
68          {
69              path = path.substring(1);
70          }
71          if (!path.endsWith("/"))
72          {
73              path += "/";
74          }
75          if (path.startsWith(SERVICE_ROOT))
76          {
77              path += name;
78          }
79          else
80          {
81          	preferredPath = SERVICE_ROOT + path + preferredName;
82              path = SERVICE_ROOT + path + name;
83          }
84          try
85          {
86          	//get preferred path first
87              InputStream is = IOUtils.getResourceAsStream(preferredPath, currentClass, false, false);
88              
89              //if no resource found, then go with default path
90              if(is == null)
91              {
92              	is = IOUtils.getResourceAsStream(path, currentClass, false, false);
93              }
94                        	
95              if (is != null)
96              {
97                  Properties props = new Properties();
98                  try
99                  {
100                     props.load(is);
101                     return props;
102                 }
103                 catch (IOException e)
104                 {
105                     logger.warn("Descriptor found but unable to load properties for service " + name);
106                     return null;
107                 }
108             }
109             else
110             {
111                 return null;
112             }
113         }
114         catch (IOException e)
115         {
116             return null;
117         }
118     }
119 }