Coverage Report - org.mule.util.SpiUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SpiUtils
68%
23/34
62%
10/16
8.5
 
 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  0
 public class SpiUtils
 27  
 {
 28  2
     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  780
         if (type.equals(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE))
 37  
         {
 38  390
             return findServiceDescriptor(PROVIDER_SERVICE_PATH, name, TransportFactory.class);
 39  
         }
 40  390
         else if (type.equals(ServiceDescriptorFactory.EXCEPTION_SERVICE_TYPE))
 41  
         {
 42  390
             return findServiceDescriptor(EXCEPTION_SERVICE_PATH, name, ExceptionHelper.class);
 43  
         }
 44  
         else
 45  
         {
 46  0
             logger.warn("Attempt to lookup unrecognized service type: " + type);
 47  0
             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  780
             String preferredName = null;
 58  780
             String preferredPath = null;
 59  
             
 60  780
         if (!name.endsWith(".properties"))
 61  
         {
 62  780
             name += ".properties";
 63  
             //convention is preferred-<protocol>.properties
 64  780
             preferredName = "preferred-" + name;
 65  
         }
 66  
 
 67  780
         if (path.startsWith("/"))
 68  
         {
 69  0
             path = path.substring(1);
 70  
         }
 71  780
         if (!path.endsWith("/"))
 72  
         {
 73  0
             path += "/";
 74  
         }
 75  780
         if (path.startsWith(SERVICE_ROOT))
 76  
         {
 77  0
             path += name;
 78  
         }
 79  
         else
 80  
         {
 81  780
                 preferredPath = SERVICE_ROOT + path + preferredName;
 82  780
             path = SERVICE_ROOT + path + name;
 83  
         }
 84  
         try
 85  
         {
 86  
                 //get preferred path first
 87  780
             InputStream is = IOUtils.getResourceAsStream(preferredPath, currentClass, false, false);
 88  
             
 89  
             //if no resource found, then go with default path
 90  780
             if(is == null)
 91  
             {
 92  780
                     is = IOUtils.getResourceAsStream(path, currentClass, false, false);
 93  
             }
 94  
                               
 95  780
             if (is != null)
 96  
             {
 97  390
                 Properties props = new Properties();
 98  
                 try
 99  
                 {
 100  390
                     props.load(is);
 101  390
                     return props;
 102  
                 }
 103  0
                 catch (IOException e)
 104  
                 {
 105  0
                     logger.warn("Descriptor found but unable to load properties for service " + name);
 106  0
                     return null;
 107  
                 }
 108  
             }
 109  
             else
 110  
             {
 111  390
                 return null;
 112  
             }
 113  
         }
 114  0
         catch (IOException e)
 115  
         {
 116  0
             return null;
 117  
         }
 118  
     }
 119  
 }