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