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