KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > ObjectNameHelper


1 /*
2  * $Id: ObjectNameHelper.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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.UMOImmutableEndpoint;
15 import org.mule.umo.provider.UMOConnector;
16
17 /**
18  * Generates consistent objects names for Mule components
19  */

20 // @ThreadSafe
21
public class ObjectNameHelper
22 {
23     public static final String JavaDoc SEPARATOR = ".";
24     public static final char HASH = '#';
25     public static final String JavaDoc CONNECTOR_PREFIX = "connector";
26     public static final String JavaDoc ENDPOINT_PREFIX = "endpoint";
27
28     public static String JavaDoc getEndpointName(UMOImmutableEndpoint endpoint)
29     {
30         String JavaDoc name = endpoint.getName();
31         if (name != null)
32         {
33             // If the name is the same as the address, we need to add the scheme
34
if (name.equals(endpoint.getEndpointURI().getAddress()))
35             {
36                 name = endpoint.getEndpointURI().getScheme() + SEPARATOR + name;
37             }
38             name = replaceObjectNameChars(name);
39             // This causes a stack overflow because we call lookup endpoint
40
// Which causes a clone of the endpoint which in turn valudates the
41
// endpoint name with this method
42
return name;
43             // return ensureUniqueEndpoint(name);
44

45         }
46         else
47         {
48             String JavaDoc address = endpoint.getEndpointURI().getAddress();
49             // Make sure we include the endpoint scheme in the name
50
address = (address.indexOf(":/") > -1 ? address : endpoint.getEndpointURI().getScheme()
51                             + SEPARATOR + address);
52             name = ENDPOINT_PREFIX + SEPARATOR + replaceObjectNameChars(address);
53
54             return ensureUniqueEndpoint(name);
55         }
56     }
57
58     protected static String JavaDoc ensureUniqueEndpoint(String JavaDoc name)
59     {
60         int i = 0;
61         String JavaDoc tempName = name;
62         // Check that the generated name does not conflict with an existing global
63
// endpoint.
64
// We can't check local edpoints right now but the chances of conflict are
65
// very small and will be
66
// reported during JMX object registration
67
while (MuleManager.getInstance().lookupEndpoint(tempName) != null)
68         {
69             i++;
70             tempName = name + SEPARATOR + i;
71         }
72         return tempName;
73     }
74
75     protected static String JavaDoc ensureUniqueConnector(String JavaDoc name)
76     {
77         int i = 0;
78         String JavaDoc tempName = name;
79         // Check that the generated name does not conflict with an existing global
80
// endpoint.
81
// We can't check local edpoints right now but the chances of conflict are
82
// very small and will be
83
// reported during JMX object registration
84
while (MuleManager.getInstance().lookupConnector(tempName) != null)
85         {
86             i++;
87             tempName = name + SEPARATOR + i;
88         }
89         return tempName;
90     }
91
92     public static String JavaDoc getConnectorName(UMOConnector connector)
93     {
94         if (connector.getName() != null && connector.getName().indexOf('#') == -1)
95         {
96             String JavaDoc name = replaceObjectNameChars(connector.getName());
97             return ensureUniqueConnector(name);
98         }
99         else
100         {
101             int i = 0;
102             String JavaDoc name = CONNECTOR_PREFIX + SEPARATOR + connector.getProtocol() + SEPARATOR + i;
103             return ensureUniqueConnector(name);
104         }
105     }
106
107     public static String JavaDoc replaceObjectNameChars(String JavaDoc name)
108     {
109         String JavaDoc value = name.replaceAll("//", SEPARATOR);
110         value = value.replaceAll("\\p{Punct}", SEPARATOR);
111         value = value.replaceAll("\\" + SEPARATOR + "{2,}", SEPARATOR);
112         if (value.endsWith(SEPARATOR))
113         {
114             value = value.substring(0, value.length() - 1);
115         }
116         return value;
117     }
118
119 }
120
Popular Tags