| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.management; |
| 19 |
|
|
| 20 |
|
import java.net.InetAddress; |
| 21 |
|
import java.net.UnknownHostException; |
| 22 |
|
import java.util.Hashtable; |
| 23 |
|
|
| 24 |
|
import javax.management.MalformedObjectNameException; |
| 25 |
|
import javax.management.ObjectName; |
| 26 |
|
|
| 27 |
|
import org.apache.camel.CamelContext; |
| 28 |
|
import org.apache.camel.Endpoint; |
| 29 |
|
|
| 30 |
|
public class CamelNamingStrategy { |
| 31 |
|
|
| 32 |
|
public static final String VALUE_UNKNOWN = "unknown"; |
| 33 |
|
public static final String KEY_CONTEXT = "context"; |
| 34 |
|
public static final String KEY_ENDPOINT = "endpoint"; |
| 35 |
|
public static final String KEY_ROUTE = "route"; |
| 36 |
|
public static final String KEY_TYPE = "type"; |
| 37 |
|
public static final String KEY_NAME = "name"; |
| 38 |
|
public static final String TYPE_ENDPOINTS = "Endpoints"; |
| 39 |
|
public static final String TYPE_SERVICES = "Services"; |
| 40 |
|
public static final String TYPE_ROUTES = "Routes"; |
| 41 |
|
|
| 42 |
0 |
protected String domainName = "org.apache.camel"; |
| 43 |
0 |
protected String hostName = "locahost"; |
| 44 |
|
|
| 45 |
0 |
public CamelNamingStrategy(String domainName) { |
| 46 |
0 |
if (domainName != null) { |
| 47 |
0 |
this.domainName = domainName; |
| 48 |
|
} |
| 49 |
|
try { |
| 50 |
0 |
hostName = InetAddress.getLocalHost().getHostName(); |
| 51 |
|
} |
| 52 |
0 |
catch (UnknownHostException ex) { |
| 53 |
|
|
| 54 |
0 |
} |
| 55 |
0 |
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
public ObjectName getObjectName(CamelContext context) throws MalformedObjectNameException { |
| 67 |
0 |
Hashtable<String, String> keys = new Hashtable<String, String>(); |
| 68 |
0 |
keys.put(KEY_CONTEXT, getContextId(context)); |
| 69 |
0 |
keys.put(KEY_NAME, "camel"); |
| 70 |
0 |
return new ObjectName(domainName, keys); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
public ObjectName getObjectName(ManagedEndpoint mbean) throws MalformedObjectNameException { |
| 83 |
0 |
Endpoint ep = mbean.getEndpoint(); |
| 84 |
0 |
Hashtable<String, String> keys = new Hashtable<String, String>(); |
| 85 |
0 |
keys.put(KEY_CONTEXT, getContextId(ep.getContext())); |
| 86 |
0 |
keys.put(KEY_TYPE, TYPE_ENDPOINTS); |
| 87 |
0 |
keys.put(KEY_ENDPOINT, getEndpointId(ep)); |
| 88 |
0 |
return new ObjectName(domainName, keys); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
public ObjectName getObjectName(CamelContext context, ManagedService mbean) throws MalformedObjectNameException { |
| 101 |
0 |
Hashtable<String, String> keys = new Hashtable<String, String>(); |
| 102 |
0 |
keys.put(KEY_CONTEXT, getContextId(context)); |
| 103 |
0 |
keys.put(KEY_TYPE, TYPE_SERVICES); |
| 104 |
0 |
keys.put(KEY_ENDPOINT, Integer.toHexString(mbean.getService().hashCode())); |
| 105 |
0 |
return new ObjectName(domainName, keys); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
public ObjectName getObjectName(ManagedRoute mbean) throws MalformedObjectNameException { |
| 118 |
0 |
Hashtable<String, String> keys = new Hashtable<String, String>(); |
| 119 |
0 |
Endpoint ep = mbean.getRoute().getEndpoint(); |
| 120 |
0 |
String ctxid = ep != null ? getContextId(ep.getContext()) : VALUE_UNKNOWN; |
| 121 |
0 |
keys.put(KEY_CONTEXT, ctxid); |
| 122 |
0 |
keys.put(KEY_TYPE, TYPE_ROUTES); |
| 123 |
0 |
keys.put(KEY_ENDPOINT, getEndpointId(ep)); |
| 124 |
0 |
return new ObjectName(domainName, keys); |
| 125 |
|
} |
| 126 |
|
|
| 127 |
|
protected String getContextId(CamelContext context) { |
| 128 |
0 |
String id = context != null ? Integer.toString(context.hashCode()) : VALUE_UNKNOWN; |
| 129 |
0 |
return hostName + "/" + id; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
protected String getEndpointId(Endpoint ep) { |
| 133 |
0 |
String uri = ep.getEndpointUri(); |
| 134 |
0 |
int pos = uri.indexOf(':'); |
| 135 |
0 |
String id = (pos == -1) ? uri : |
| 136 |
|
"[" + uri.substring(0, pos) + "]" + uri.substring(pos + 1); |
| 137 |
0 |
if (!ep.isSingleton()) { |
| 138 |
0 |
id += "." + Integer.toString(ep.hashCode()); |
| 139 |
|
} |
| 140 |
0 |
return id; |
| 141 |
|
} |
| 142 |
|
} |