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