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