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