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