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