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