Coverage Report - org.mule.util.ObjectNameHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectNameHelper
74%
25/34
57%
8/14
2.167
 
 1  
 /*
 2  
  * $Id: ObjectNameHelper.java 11311 2008-03-10 20:15:57Z dfeist $
 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.RegistryContext;
 14  
 import org.mule.api.endpoint.EndpointURI;
 15  
 import org.mule.api.transport.Connector;
 16  
 
 17  
 /**
 18  
  * Generates consistent objects names for Mule components
 19  
  */
 20  
 // @ThreadSafe
 21  
 public final class ObjectNameHelper
 22  
 {
 23  
     public static final String SEPARATOR = ".";
 24  
     //public static final char HASH = '#';
 25  
     public static final String CONNECTOR_PREFIX = "connector";
 26  
     public static final String ENDPOINT_PREFIX = "endpoint";
 27  
 
 28  
     /** Do not instanciate. */
 29  
     protected ObjectNameHelper ()
 30  0
     {
 31  
         // no-op
 32  0
     }
 33  
 
 34  
     public static String getEndpointName(final EndpointURI endpointUri)
 35  
     {
 36  62
             String address = endpointUri.getAddress();
 37  62
             if (StringUtils.isBlank(address))
 38  
             {
 39  
                 // for some endpoints in TCK like test://xxx
 40  0
                 address = endpointUri.toString();
 41  
             }
 42  
             // Make sure we include the endpoint scheme in the name
 43  62
             address = (address.indexOf(":/") > -1 ? address : endpointUri.getScheme()
 44  
                             + SEPARATOR + address);
 45  62
             String name = ENDPOINT_PREFIX + SEPARATOR + replaceObjectNameChars(address);
 46  
 
 47  62
             return ensureUniqueEndpoint(name);
 48  
     }
 49  
 
 50  
     protected static String ensureUniqueEndpoint(String name)
 51  
     {
 52  62
         int i = 0;
 53  62
         String tempName = name;
 54  
         // Check that the generated name does not conflict with an existing global
 55  
         // endpoint.
 56  
         // We can't check local edpoints right now but the chances of conflict are
 57  
         // very small and will be
 58  
         // reported during JMX object registration
 59  62
         while (RegistryContext.getRegistry().lookupObject(tempName) != null)
 60  
         {
 61  0
             i++;
 62  0
             tempName = name + SEPARATOR + i;
 63  
         }
 64  62
         return tempName;
 65  
     }
 66  
 
 67  
     protected static String ensureUniqueConnector(String name)
 68  
     {
 69  68
         int i = 0;
 70  68
         String tempName = name;
 71  
         // Check that the generated name does not conflict with an existing global
 72  
         // endpoint.
 73  
         // We can't check local edpoints right now but the chances of conflict are
 74  
         // very small and will be
 75  
         // reported during JMX object registration
 76  
         try
 77  
         {
 78  68
             while (RegistryContext.getRegistry().lookupConnector(tempName) != null)
 79  
             {
 80  0
                 i++;
 81  0
                 tempName = name + SEPARATOR + i;
 82  
             }
 83  
         }
 84  0
         catch (Exception e)
 85  
         {
 86  
             //ignore
 87  68
         }
 88  68
         return tempName;
 89  
     }
 90  
 
 91  
     public static String getConnectorName(Connector connector)
 92  
     {
 93  68
         if (connector.getName() != null && connector.getName().indexOf('#') == -1)
 94  
         {
 95  34
             String name = replaceObjectNameChars(connector.getName());
 96  34
             return ensureUniqueConnector(name);
 97  
         }
 98  
         else
 99  
         {
 100  34
             int i = 0;
 101  34
             String name = CONNECTOR_PREFIX + SEPARATOR + connector.getProtocol() + SEPARATOR + i;
 102  34
             return ensureUniqueConnector(name);
 103  
         }
 104  
     }
 105  
 
 106  
     public static String replaceObjectNameChars(String name)
 107  
     {
 108  96
         String value = name.replaceAll("//", SEPARATOR);
 109  96
         value = value.replaceAll("\\p{Punct}", SEPARATOR);
 110  96
         value = value.replaceAll("\\" + SEPARATOR + "{2,}", SEPARATOR);
 111  96
         if (value.endsWith(SEPARATOR))
 112  
         {
 113  0
             value = value.substring(0, value.length() - 1);
 114  
         }
 115  96
         return value;
 116  
     }
 117  
 
 118  
 }