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