View Javadoc

1   /*
2    * $Id: ObjectNameHelper.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.MuleManager;
14  import org.mule.umo.endpoint.UMOEndpointURI;
15  import org.mule.umo.endpoint.UMOImmutableEndpoint;
16  import org.mule.umo.provider.UMOConnector;
17  
18  /**
19   * Generates consistent objects names for Mule components
20   */
21  // @ThreadSafe
22  public final class ObjectNameHelper
23  {
24      public static final String SEPARATOR = ".";
25      public static final char HASH = '#';
26      public static final String CONNECTOR_PREFIX = "connector";
27      public static final String ENDPOINT_PREFIX = "endpoint";
28  
29      /** Do not instanciate. */
30      protected ObjectNameHelper ()
31      {
32          // no-op
33      }
34  
35      public static String getEndpointName(UMOImmutableEndpoint endpoint)
36      {
37          String name = endpoint.getName();
38          final UMOEndpointURI endpointUri = endpoint.getEndpointURI();
39          if (name != null)
40          {
41              // If the name is the same as the address, we need to add the scheme
42              if (name.equals(endpointUri.getAddress()))
43              {
44                  name = endpointUri.getScheme() + SEPARATOR + name;
45              }
46              name = replaceObjectNameChars(name);
47              // This causes a stack overflow because we call lookup endpoint
48              // Which causes a clone of the endpoint which in turn valudates the
49              // endpoint name with this method
50              return name;
51              // return ensureUniqueEndpoint(name);
52  
53          }
54          else
55          {
56              String address = endpointUri.getAddress();
57              if (StringUtils.isBlank(address))
58              {
59                  // for some endpoints in TCK like test://xxx
60                  address = endpointUri.toString();
61              }
62              // Make sure we include the endpoint scheme in the name
63              address = (address.indexOf(":/") > -1 ? address : endpointUri.getScheme()
64                              + SEPARATOR + address);
65              name = ENDPOINT_PREFIX + SEPARATOR + replaceObjectNameChars(address);
66  
67              return ensureUniqueEndpoint(name);
68          }
69      }
70  
71      protected static String ensureUniqueEndpoint(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          while (MuleManager.getInstance().lookupEndpoint(tempName) != null)
81          {
82              i++;
83              tempName = name + SEPARATOR + i;
84          }
85          return tempName;
86      }
87  
88      protected static String ensureUniqueConnector(String name)
89      {
90          int i = 0;
91          String tempName = name;
92          // Check that the generated name does not conflict with an existing global
93          // endpoint.
94          // We can't check local edpoints right now but the chances of conflict are
95          // very small and will be
96          // reported during JMX object registration
97          while (MuleManager.getInstance().lookupConnector(tempName) != null)
98          {
99              i++;
100             tempName = name + SEPARATOR + i;
101         }
102         return tempName;
103     }
104 
105     public static String getConnectorName(UMOConnector connector)
106     {
107         if (connector.getName() != null && connector.getName().indexOf('#') == -1)
108         {
109             String name = replaceObjectNameChars(connector.getName());
110             return ensureUniqueConnector(name);
111         }
112         else
113         {
114             int i = 0;
115             String name = CONNECTOR_PREFIX + SEPARATOR + connector.getProtocol() + SEPARATOR + i;
116             return ensureUniqueConnector(name);
117         }
118     }
119 
120     public static String replaceObjectNameChars(String name)
121     {
122         String value = name.replaceAll("//", SEPARATOR);
123         value = value.replaceAll("\\p{Punct}", SEPARATOR);
124         value = value.replaceAll("\\" + SEPARATOR + "{2,}", SEPARATOR);
125         if (value.endsWith(SEPARATOR))
126         {
127             value = value.substring(0, value.length() - 1);
128         }
129         return value;
130     }
131 
132 }