Coverage Report - org.mule.util.SpiUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SpiUtils
0%
0/38
0%
0/20
6.333
 
 1  
 /*
 2  
  * $Id: SpiUtils.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.util;
 12  
 
 13  
 import org.mule.api.registry.ServiceType;
 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  
 
 26  
 //@ThreadSafe
 27  0
 public class SpiUtils
 28  
 {
 29  0
     private static final Log logger = LogFactory.getLog(SpiUtils.class);
 30  
 
 31  
     public static final String SERVICE_ROOT = "META-INF/services/";
 32  
 
 33  
 
 34  
     public static Properties findServiceDescriptor(ServiceType type, String name)
 35  
     {
 36  0
         if (type.equals(ServiceType.TRANSPORT))
 37  
         {
 38  
             // for better EE transports support from earlier versions, try the preferred-xxx lookup first without fallback
 39  0
             Properties tsd = findServiceDescriptor(type.getPath(), name, TransportFactory.class, false);
 40  
 
 41  0
             if (tsd == null)
 42  
             {
 43  
                 // regular flow
 44  0
                 tsd = findServiceDescriptor(type.getPath(), name, TransportFactory.class);
 45  
             }
 46  
 
 47  0
             return tsd;
 48  
         }
 49  0
         else if (type.equals(ServiceType.EXCEPTION))
 50  
         {
 51  0
             return findServiceDescriptor(type.getPath(), name, ExceptionHelper.class);
 52  
         }
 53  
         else
 54  
         {
 55  0
             logger.warn("Attempt to lookup unrecognized service type: " + type);
 56  0
             return null;
 57  
         }
 58  
 
 59  
     }
 60  
 
 61  
     public static Properties findServiceDescriptor(String path, String name, Class currentClass)
 62  
     {
 63  0
         return findServiceDescriptor(path, name, currentClass, true);
 64  
     }
 65  
 
 66  
     /**
 67  
      * @param fallbackToNonPreferred whether the search should attempt the preferred-xxx.properties lookup
 68  
      */
 69  
     public static Properties findServiceDescriptor(String path, String name, Class currentClass, boolean fallbackToNonPreferred)
 70  
     {
 71  
         //Preferred name and preferred path - used to construct a URI for alternative or preferred
 72  
         //property set.  This enables alternative implementations of a transport to exist side by side
 73  
         //in a single Mule VM.  Most transports will not have a preferred property set.
 74  0
         String preferredName = null;
 75  0
         String preferredPath = null;
 76  
 
 77  0
         if (!name.endsWith(".properties"))
 78  
         {
 79  0
             name += ".properties";
 80  
             //convention is preferred-<protocol>.properties
 81  0
             preferredName = "preferred-" + name;
 82  
         }
 83  
 
 84  0
         if (path.startsWith("/"))
 85  
         {
 86  0
             path = path.substring(1);
 87  
         }
 88  0
         if (!path.endsWith("/"))
 89  
         {
 90  0
             path += "/";
 91  
         }
 92  0
         if (path.startsWith(SERVICE_ROOT))
 93  
         {
 94  0
             path += name;
 95  
         }
 96  
         else
 97  
         {
 98  0
             preferredPath = SERVICE_ROOT + path + preferredName;
 99  0
             path = SERVICE_ROOT + path + name;
 100  
         }
 101  
         try
 102  
         {
 103  
             //get preferred path first
 104  0
             InputStream is = IOUtils.getResourceAsStream(preferredPath, currentClass, false, false);
 105  
 
 106  
             //if no resource found, then go with default path
 107  0
             if (is == null && fallbackToNonPreferred)
 108  
             {
 109  0
                 is = IOUtils.getResourceAsStream(path, currentClass, false, false);
 110  
             }
 111  
 
 112  0
             if (is != null)
 113  
             {
 114  0
                 Properties props = new Properties();
 115  
                 try
 116  
                 {
 117  0
                     props.load(is);
 118  0
                     return props;
 119  
                 }
 120  0
                 catch (IOException e)
 121  
                 {
 122  0
                     logger.warn("Descriptor found but unable to load properties for service " + name);
 123  0
                     return null;
 124  
                 }
 125  
             }
 126  
             else
 127  
             {
 128  0
                 return null;
 129  
             }
 130  
         }
 131  0
         catch (IOException e)
 132  
         {
 133  0
             return null;
 134  
         }
 135  
     }
 136  
 }