1 10 11 package org.mule.util; 12 13 import org.mule.MuleManager; 14 import org.mule.umo.endpoint.UMOImmutableEndpoint; 15 import org.mule.umo.provider.UMOConnector; 16 17 20 public 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 public static String getEndpointName(UMOImmutableEndpoint endpoint) 29 { 30 String name = endpoint.getName(); 31 if (name != null) 32 { 33 if (name.equals(endpoint.getEndpointURI().getAddress())) 35 { 36 name = endpoint.getEndpointURI().getScheme() + SEPARATOR + name; 37 } 38 name = replaceObjectNameChars(name); 39 return name; 43 45 } 46 else 47 { 48 String address = endpoint.getEndpointURI().getAddress(); 49 address = (address.indexOf(":/") > -1 ? address : endpoint.getEndpointURI().getScheme() 51 + SEPARATOR + address); 52 name = ENDPOINT_PREFIX + SEPARATOR + replaceObjectNameChars(address); 53 54 return ensureUniqueEndpoint(name); 55 } 56 } 57 58 protected static String ensureUniqueEndpoint(String name) 59 { 60 int i = 0; 61 String tempName = name; 62 while (MuleManager.getInstance().lookupEndpoint(tempName) != null) 68 { 69 i++; 70 tempName = name + SEPARATOR + i; 71 } 72 return tempName; 73 } 74 75 protected static String ensureUniqueConnector(String name) 76 { 77 int i = 0; 78 String tempName = name; 79 while (MuleManager.getInstance().lookupConnector(tempName) != null) 85 { 86 i++; 87 tempName = name + SEPARATOR + i; 88 } 89 return tempName; 90 } 91 92 public static String getConnectorName(UMOConnector connector) 93 { 94 if (connector.getName() != null && connector.getName().indexOf('#') == -1) 95 { 96 String name = replaceObjectNameChars(connector.getName()); 97 return ensureUniqueConnector(name); 98 } 99 else 100 { 101 int i = 0; 102 String name = CONNECTOR_PREFIX + SEPARATOR + connector.getProtocol() + SEPARATOR + i; 103 return ensureUniqueConnector(name); 104 } 105 } 106 107 public static String replaceObjectNameChars(String name) 108 { 109 String value = name.replaceAll("//", SEPARATOR); 110 value = value.replaceAll("\\p{Punct}", SEPARATOR); 111 value = value.replaceAll("\\" + SEPARATOR + "{2,}", SEPARATOR); 112 if (value.endsWith(SEPARATOR)) 113 { 114 value = value.substring(0, value.length() - 1); 115 } 116 return value; 117 } 118 119 } 120 | Popular Tags |