View Javadoc

1   /*
2    * $Id: ObjectNameHelper.java 21307 2011-02-17 15:35:44Z dfeist $
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      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                  // for some endpoints in TCK like test://xxx
43                  address = endpointUri.toString();
44              }
45              // Make sure we include the endpoint scheme in the name
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          // Check that the generated name does not conflict with an existing global
58          // endpoint.
59          // We can't check local edpoints right now but the chances of conflict are
60          // very small and will be
61          // reported during JMX object registration
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          // Check that the generated name does not conflict with an existing global
75          // endpoint.
76          // We can't check local edpoints right now but the chances of conflict are
77          // very small and will be
78          // reported during JMX object registration
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              //ignore
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 }