Coverage Report - org.mule.util.ObjectNameHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectNameHelper
77%
30/39
61%
11/18
2.5
 
 1  
 /*
 2  
  * $Id: ObjectNameHelper.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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.MuleManager;
 14  
 import org.mule.umo.endpoint.UMOEndpointURI;
 15  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 16  
 import org.mule.umo.provider.UMOConnector;
 17  
 
 18  
 /**
 19  
  * Generates consistent objects names for Mule components
 20  
  */
 21  
 // @ThreadSafe
 22  
 public final class ObjectNameHelper
 23  
 {
 24  
     public static final String SEPARATOR = ".";
 25  
     public static final char HASH = '#';
 26  
     public static final String CONNECTOR_PREFIX = "connector";
 27  
     public static final String ENDPOINT_PREFIX = "endpoint";
 28  
 
 29  
     /** Do not instanciate. */
 30  
     protected ObjectNameHelper ()
 31  0
     {
 32  
         // no-op
 33  0
     }
 34  
 
 35  
     public static String getEndpointName(UMOImmutableEndpoint endpoint)
 36  
     {
 37  476
         String name = endpoint.getName();
 38  476
         final UMOEndpointURI endpointUri = endpoint.getEndpointURI();
 39  476
         if (name != null)
 40  
         {
 41  
             // If the name is the same as the address, we need to add the scheme
 42  376
             if (name.equals(endpointUri.getAddress()))
 43  
             {
 44  0
                 name = endpointUri.getScheme() + SEPARATOR + name;
 45  
             }
 46  376
             name = replaceObjectNameChars(name);
 47  
             // This causes a stack overflow because we call lookup endpoint
 48  
             // Which causes a clone of the endpoint which in turn valudates the
 49  
             // endpoint name with this method
 50  376
             return name;
 51  
             // return ensureUniqueEndpoint(name);
 52  
 
 53  
         }
 54  
         else
 55  
         {
 56  100
             String address = endpointUri.getAddress();
 57  100
             if (StringUtils.isBlank(address))
 58  
             {
 59  
                 // for some endpoints in TCK like test://xxx
 60  0
                 address = endpointUri.toString();
 61  
             }
 62  
             // Make sure we include the endpoint scheme in the name
 63  100
             address = (address.indexOf(":/") > -1 ? address : endpointUri.getScheme()
 64  
                             + SEPARATOR + address);
 65  100
             name = ENDPOINT_PREFIX + SEPARATOR + replaceObjectNameChars(address);
 66  
 
 67  100
             return ensureUniqueEndpoint(name);
 68  
         }
 69  
     }
 70  
 
 71  
     protected static String ensureUniqueEndpoint(String name)
 72  
     {
 73  100
         int i = 0;
 74  100
         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  100
         while (MuleManager.getInstance().lookupEndpoint(tempName) != null)
 81  
         {
 82  0
             i++;
 83  0
             tempName = name + SEPARATOR + i;
 84  
         }
 85  100
         return tempName;
 86  
     }
 87  
 
 88  
     protected static String ensureUniqueConnector(String name)
 89  
     {
 90  84
         int i = 0;
 91  84
         String tempName = name;
 92  
         // Check that the generated name does not conflict with an existing global
 93  
         // endpoint.
 94  
         // We can't check local edpoints right now but the chances of conflict are
 95  
         // very small and will be
 96  
         // reported during JMX object registration
 97  84
         while (MuleManager.getInstance().lookupConnector(tempName) != null)
 98  
         {
 99  0
             i++;
 100  0
             tempName = name + SEPARATOR + i;
 101  
         }
 102  84
         return tempName;
 103  
     }
 104  
 
 105  
     public static String getConnectorName(UMOConnector connector)
 106  
     {
 107  84
         if (connector.getName() != null && connector.getName().indexOf('#') == -1)
 108  
         {
 109  42
             String name = replaceObjectNameChars(connector.getName());
 110  42
             return ensureUniqueConnector(name);
 111  
         }
 112  
         else
 113  
         {
 114  42
             int i = 0;
 115  42
             String name = CONNECTOR_PREFIX + SEPARATOR + connector.getProtocol() + SEPARATOR + i;
 116  42
             return ensureUniqueConnector(name);
 117  
         }
 118  
     }
 119  
 
 120  
     public static String replaceObjectNameChars(String name)
 121  
     {
 122  518
         String value = name.replaceAll("//", SEPARATOR);
 123  518
         value = value.replaceAll("\\p{Punct}", SEPARATOR);
 124  518
         value = value.replaceAll("\\" + SEPARATOR + "{2,}", SEPARATOR);
 125  518
         if (value.endsWith(SEPARATOR))
 126  
         {
 127  0
             value = value.substring(0, value.length() - 1);
 128  
         }
 129  518
         return value;
 130  
     }
 131  
 
 132  
 }