Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
CamelNamingStrategy |
|
| 0.0;0 |
1 | /** |
|
2 | * |
|
3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
4 | * contributor license agreements. See the NOTICE file distributed with |
|
5 | * this work for additional information regarding copyright ownership. |
|
6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
7 | * (the "License"); you may not use this file except in compliance with |
|
8 | * the License. You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | // ignore |
|
54 | 0 | } |
55 | 0 | } |
56 | ||
57 | /** |
|
58 | * Implements the naming strategy for a {@see CamelContext}. |
|
59 | * The convention used for a {@see CamelContext} ObjectName is |
|
60 | * "<domain>:context=<context>,name=camel". |
|
61 | * |
|
62 | * @param mbean |
|
63 | * @return generated ObjectName |
|
64 | * @throws MalformedObjectNameException |
|
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 | * Implements the naming strategy for a {@see ManagedEndpoint}. |
|
75 | * The convention used for a {@see ManagedEndpoint} ObjectName is |
|
76 | * "<domain>:context=<context>,type=Endpoints,endpoint=[urlPrefix]localPart". |
|
77 | * |
|
78 | * @param mbean |
|
79 | * @return generated ObjectName |
|
80 | * @throws MalformedObjectNameException |
|
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 | * Implements the naming strategy for a {@see ServiceSpport Service}. |
|
93 | * The convention used for a {@see Service} ObjectName is |
|
94 | * "<domain>:context=<context>,type=Services,endpoint=[urlPrefix]localPart". |
|
95 | * |
|
96 | * @param mbean |
|
97 | * @return generated ObjectName |
|
98 | * @throws MalformedObjectNameException |
|
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 | * Implements the naming strategy for a {@see ManagedRoute}. |
|
110 | * The convention used for a {@see ManagedEndpoint} ObjectName is |
|
111 | * "<domain>:context=<context>,type=Routes,endpoint=[urlPrefix]localPart". |
|
112 | * |
|
113 | * @param mbean |
|
114 | * @return generated ObjectName |
|
115 | * @throws MalformedObjectNameException |
|
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 | } |