1
2
3
4
5
6
7
8
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
19
20
21 public final class ObjectNameHelper
22 {
23 public static final String SEPARATOR = ".";
24
25 public static final String CONNECTOR_PREFIX = "connector";
26 public static final String ENDPOINT_PREFIX = "endpoint";
27
28
29 protected ObjectNameHelper ()
30 {
31
32 }
33
34 public static String getEndpointName(final EndpointURI endpointUri)
35 {
36 String address = endpointUri.getAddress();
37 if (StringUtils.isBlank(address))
38 {
39
40 address = endpointUri.toString();
41 }
42
43 address = (address.indexOf(":/") > -1 ? address : endpointUri.getScheme()
44 + SEPARATOR + address);
45 String name = ENDPOINT_PREFIX + SEPARATOR + replaceObjectNameChars(address);
46
47 return ensureUniqueEndpoint(name);
48 }
49
50 protected static String ensureUniqueEndpoint(String name)
51 {
52 int i = 0;
53 String tempName = name;
54
55
56
57
58
59 while (RegistryContext.getRegistry().lookupObject(tempName) != null)
60 {
61 i++;
62 tempName = name + SEPARATOR + i;
63 }
64 return tempName;
65 }
66
67 protected static String ensureUniqueConnector(String name)
68 {
69 int i = 0;
70 String tempName = name;
71
72
73
74
75
76 try
77 {
78 while (RegistryContext.getRegistry().lookupConnector(tempName) != null)
79 {
80 i++;
81 tempName = name + SEPARATOR + i;
82 }
83 }
84 catch (Exception e)
85 {
86
87 }
88 return tempName;
89 }
90
91 public static String getConnectorName(Connector connector)
92 {
93 if (connector.getName() != null && connector.getName().indexOf('#') == -1)
94 {
95 String name = replaceObjectNameChars(connector.getName());
96 return ensureUniqueConnector(name);
97 }
98 else
99 {
100 int i = 0;
101 String name = CONNECTOR_PREFIX + SEPARATOR + connector.getProtocol() + SEPARATOR + i;
102 return ensureUniqueConnector(name);
103 }
104 }
105
106 public static String replaceObjectNameChars(String name)
107 {
108 String value = name.replaceAll("//", SEPARATOR);
109 value = value.replaceAll("\\p{Punct}", SEPARATOR);
110 value = value.replaceAll("\\" + SEPARATOR + "{2,}", SEPARATOR);
111 if (value.endsWith(SEPARATOR))
112 {
113 value = value.substring(0, value.length() - 1);
114 }
115 return value;
116 }
117
118 }