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