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