View Javadoc

1   /*
2    * $Id: ObjectNameHelper.java 11311 2008-03-10 20:15:57Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.RegistryContext;
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      /** Do not instanciate. */
29      protected ObjectNameHelper ()
30      {
31          // no-op
32      }
33  
34      public static String getEndpointName(final EndpointURI endpointUri)
35      {
36              String address = endpointUri.getAddress();
37              if (StringUtils.isBlank(address))
38              {
39                  // for some endpoints in TCK like test://xxx
40                  address = endpointUri.toString();
41              }
42              // Make sure we include the endpoint scheme in the name
43              address = (address.indexOf(":/") > -1 ? address : endpointUri.getScheme()
44                              + SEPARATOR + address);
45              String name = ENDPOINT_PREFIX + SEPARATOR + replaceObjectNameChars(address);
46  
47              return ensureUniqueEndpoint(name);
48      }
49  
50      protected static String ensureUniqueEndpoint(String name)
51      {
52          int i = 0;
53          String tempName = name;
54          // Check that the generated name does not conflict with an existing global
55          // endpoint.
56          // We can't check local edpoints right now but the chances of conflict are
57          // very small and will be
58          // reported during JMX object registration
59          while (RegistryContext.getRegistry().lookupObject(tempName) != null)
60          {
61              i++;
62              tempName = name + SEPARATOR + i;
63          }
64          return tempName;
65      }
66  
67      protected static String ensureUniqueConnector(String name)
68      {
69          int i = 0;
70          String tempName = name;
71          // Check that the generated name does not conflict with an existing global
72          // endpoint.
73          // We can't check local edpoints right now but the chances of conflict are
74          // very small and will be
75          // reported during JMX object registration
76          try
77          {
78              while (RegistryContext.getRegistry().lookupConnector(tempName) != null)
79              {
80                  i++;
81                  tempName = name + SEPARATOR + i;
82              }
83          }
84          catch (Exception e)
85          {
86              //ignore
87          }
88          return tempName;
89      }
90  
91      public static String getConnectorName(Connector connector)
92      {
93          if (connector.getName() != null && connector.getName().indexOf('#') == -1)
94          {
95              String name = replaceObjectNameChars(connector.getName());
96              return ensureUniqueConnector(name);
97          }
98          else
99          {
100             int i = 0;
101             String name = CONNECTOR_PREFIX + SEPARATOR + connector.getProtocol() + SEPARATOR + i;
102             return ensureUniqueConnector(name);
103         }
104     }
105 
106     public static String replaceObjectNameChars(String name)
107     {
108         String value = name.replaceAll("//", SEPARATOR);
109         value = value.replaceAll("\\p{Punct}", SEPARATOR);
110         value = value.replaceAll("\\" + SEPARATOR + "{2,}", SEPARATOR);
111         if (value.endsWith(SEPARATOR))
112         {
113             value = value.substring(0, value.length() - 1);
114         }
115         return value;
116     }
117 
118 }