View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * Generates consistent objects names for Mule components
15   */
16  // @ThreadSafe
17  public final class ObjectNameHelper
18  {
19      public static final String SEPARATOR = ".";
20      //public static final char HASH = '#';
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              // for some endpoints in TCK like test://xxx
46              address = endpointUri.toString();
47          }
48          // Make sure we include the endpoint scheme in the name
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          // Check that the generated name does not conflict with an existing global
59          // endpoint.
60          // We can't check local edpoints right now but the chances of conflict are
61          // very small and will be
62          // reported during JMX object registration
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          // Check that the generated name does not conflict with an existing global
76          // endpoint.
77          // We can't check local edpoints right now but the chances of conflict are
78          // very small and will be
79          // reported during JMX object registration
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              //ignore
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 }