View Javadoc

1   /*
2    * $Id: ObjectNameHelper.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * Generates consistent objects names for Mule components
19   */
20  // @ThreadSafe
21  public final class ObjectNameHelper
22  {
23      public static final String SEPARATOR = ".";
24      //public static final char HASH = '#';
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                  // for some endpoints in TCK like test://xxx
42                  address = endpointUri.toString();
43              }
44              // Make sure we include the endpoint scheme in the name
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          // Check that the generated name does not conflict with an existing global
57          // endpoint.
58          // We can't check local edpoints right now but the chances of conflict are
59          // very small and will be
60          // reported during JMX object registration
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          // Check that the generated name does not conflict with an existing global
74          // endpoint.
75          // We can't check local edpoints right now but the chances of conflict are
76          // very small and will be
77          // reported during JMX object registration
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              //ignore
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 }