001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.management;
018    
019    import java.net.InetAddress;
020    import java.net.UnknownHostException;
021    
022    import javax.management.MalformedObjectNameException;
023    import javax.management.ObjectName;
024    
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.Endpoint;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.Route;
029    import org.apache.camel.impl.RouteContext;
030    import org.apache.camel.model.RouteType;
031    
032    public class CamelNamingStrategy {
033        public static final String VALUE_UNKNOWN = "unknown";
034        public static final String VALUE_ROUTE = "route";
035        public static final String VALUE_STATS = "Stats";
036        public static final String VALUE_DEFAULT_BUILDER = "default";
037        public static final String KEY_NAME = "name";
038        public static final String KEY_TYPE = "type";
039        public static final String KEY_CONTEXT = "context";
040        public static final String KEY_GROUP = "group";
041        public static final String KEY_COMPONENT = "component";
042        public static final String KEY_BUILDER = "builder";
043        public static final String KEY_ROUTE_TYPE = "routeType";
044        public static final String KEY_ROUTE = "route";
045        public static final String GROUP_ENDPOINTS = "endpoints";
046        public static final String GROUP_SERVICES = "services";
047        public static final String GROUP_ROUTES = "routes";
048        public static final String GROUP_ROUTE_TYPE = "routeType";
049    
050        protected String domainName;
051        protected String hostName = "locahost";
052    
053        public CamelNamingStrategy() {
054            this("org.apache.camel");
055        }
056    
057        public CamelNamingStrategy(String domainName) {
058            if (domainName != null) {
059                this.domainName = domainName;
060            }
061            try {
062                hostName = InetAddress.getLocalHost().getHostName();
063            } catch (UnknownHostException ex) {
064                // ignore, use the default "locahost"
065            }
066        }
067    
068        /**
069         * Implements the naming strategy for a {@link CamelContext}.
070         * The convention used for a {@link CamelContext} ObjectName is:
071         * <tt>&lt;domain&gt;:context=&lt;context&gt;,name=camel</tt>
072         *
073         * @param context the camel context
074         * @return generated ObjectName
075         * @throws MalformedObjectNameException
076         */
077        public ObjectName getObjectName(CamelContext context) throws MalformedObjectNameException {
078            StringBuffer buffer = new StringBuffer();
079            buffer.append(domainName + ":");
080            buffer.append(KEY_CONTEXT + "=" + getContextId(context) + ",");
081            buffer.append(KEY_NAME + "=" + "context");
082            return createObjectName(buffer);
083        }
084    
085        /**
086         * Implements the naming strategy for a {@link ManagedEndpoint}.
087         * The convention used for a {@link ManagedEndpoint} ObjectName is:
088         * <tt>&lt;domain&gt;:context=&lt;context&gt;,type=Services,endpoint=[urlPrefix]localPart</tt>
089         *
090         * @param mbean
091         * @return generated ObjectName
092         * @throws MalformedObjectNameException
093         */
094        public ObjectName getObjectName(ManagedEndpoint mbean) throws MalformedObjectNameException {
095            Endpoint<? extends Exchange> ep = mbean.getEndpoint();
096    
097            StringBuffer buffer = new StringBuffer();
098            buffer.append(domainName + ":");
099            buffer.append(KEY_CONTEXT + "=" + getContextId(ep.getCamelContext()) + ",");
100            buffer.append(KEY_GROUP + "=" + GROUP_ENDPOINTS + ",");
101            buffer.append(KEY_COMPONENT + "=" + getComponentId(ep) + ",");
102            buffer.append(KEY_NAME + "=" + getEndpointId(ep));
103            return createObjectName(buffer);
104        }
105    
106        /**
107         * Implements the naming strategy for a {@link org.apache.camel.impl.ServiceSupport Service}.
108         * The convention used for a {@link org.apache.camel.Service Service} ObjectName is
109         * <tt>&lt;domain&gt;:context=&lt;context&gt;,type=Services,endpoint=[urlPrefix]localPart</tt>
110         *
111         * @param context the camel context
112         * @param mbean
113         * @return generated ObjectName
114         * @throws MalformedObjectNameException
115         */
116        public ObjectName getObjectName(CamelContext context, ManagedService mbean) throws MalformedObjectNameException {
117            StringBuffer buffer = new StringBuffer();
118            buffer.append(domainName + ":");
119            buffer.append(KEY_CONTEXT + "=" + getContextId(context) + ",");
120            buffer.append(KEY_GROUP + "=" + GROUP_SERVICES + ",");
121            buffer.append(KEY_NAME + "=" + Integer.toHexString(mbean.getService().hashCode()));
122            return createObjectName(buffer);
123        }
124    
125    
126        /**
127         * Implements the naming strategy for a {@link ManagedRoute}.
128         * The convention used for a {@link ManagedEndpoint} ObjectName is:
129         * <tt>&lt;domain&gt;:context=&lt;context&gt;,type=Routes,endpoint=[urlPrefix]localPart</tt>
130         *
131         * @param mbean
132         * @return generated ObjectName
133         * @throws MalformedObjectNameException
134         */
135        public ObjectName getObjectName(ManagedRoute mbean) throws MalformedObjectNameException {
136            Route<? extends Exchange> route = mbean.getRoute();
137            Endpoint<? extends Exchange> ep = route.getEndpoint();
138    
139            String ctxid = ep != null ? getContextId(ep.getCamelContext()) : VALUE_UNKNOWN;
140            String cid = getComponentId(ep);
141            String id = VALUE_UNKNOWN.equals(cid) ? getEndpointId(ep)
142                : "[" + cid + "]" + getEndpointId(ep);
143            String group = (String)route.getProperties().get(Route.GROUP_PROPERTY);
144    
145            StringBuffer buffer = new StringBuffer();
146            buffer.append(domainName + ":");
147            buffer.append(KEY_CONTEXT + "=" + ctxid + ",");
148            buffer.append(KEY_GROUP + "=" + GROUP_ROUTES + ",");
149            buffer.append(KEY_BUILDER + "=" + (group != null ? group : VALUE_DEFAULT_BUILDER) + ",");
150            buffer.append(KEY_ROUTE_TYPE + "=" + route.getProperties().get(Route.PARENT_PROPERTY) + ",");
151            buffer.append(KEY_ROUTE + "=" + id + ",");
152            buffer.append(KEY_TYPE + "=" + VALUE_ROUTE);
153            return createObjectName(buffer);
154        }
155    
156        /**
157         * Implements the naming strategy for a {@link PerformanceCounter}.
158         * The convention used for a {@link ManagedEndpoint} ObjectName is:
159         * <tt>&lt;domain&gt;:context=&lt;context&gt;,type=Routes,endpoint=[urlPrefix]localPart</tt>
160         *
161         * @param context the camel context
162         * @param mbean
163         * @param routeContext
164         * @return generated ObjectName
165         * @throws MalformedObjectNameException
166         */
167        public ObjectName getObjectName(CamelContext context, PerformanceCounter mbean, RouteContext routeContext)
168            throws MalformedObjectNameException {
169    
170            RouteType route = routeContext.getRoute();
171            Endpoint<? extends Exchange> ep = routeContext.getEndpoint();
172            String ctxid = ep != null ? getContextId(ep.getCamelContext()) : VALUE_UNKNOWN;
173            String cid = getComponentId(ep);
174            String id = VALUE_UNKNOWN.equals(cid) ? getEndpointId(ep) : "[" + cid + "]" + getEndpointId(ep);
175            String group = route.getGroup();
176    
177            StringBuffer buffer = new StringBuffer();
178            buffer.append(domainName + ":");
179            buffer.append(KEY_CONTEXT + "=" + ctxid + ",");
180            buffer.append(KEY_GROUP + "=" + GROUP_ROUTES + ",");
181            buffer.append(KEY_BUILDER + "=" + (group != null ? group : VALUE_DEFAULT_BUILDER) + ",");
182            buffer.append(KEY_ROUTE_TYPE + "=" + route.hashCode() + ",");
183            buffer.append(KEY_ROUTE + "=" + id + ",");
184            buffer.append(KEY_TYPE + "=" + VALUE_STATS);
185            return createObjectName(buffer);
186        }
187    
188        public String getDomainName() {
189            return domainName;
190        }
191    
192        public void setDomainName(String domainName) {
193            this.domainName = domainName;
194        }
195    
196        public String getHostName() {
197            return hostName;
198        }
199    
200        public void setHostName(String hostName) {
201            this.hostName = hostName;
202        }
203    
204        protected String getContextId(CamelContext context) {
205            String id = context != null ? context.getName() : VALUE_UNKNOWN;
206            return hostName + "/" + id;
207        }
208    
209        protected String getComponentId(Endpoint<? extends Exchange> ep) {
210            String uri = ep.getEndpointUri();
211            int pos = uri.indexOf(':');
212            return (pos == -1) ? VALUE_UNKNOWN : uri.substring(0, pos);
213        }
214    
215        protected String getEndpointId(Endpoint<? extends Exchange> ep) {
216            String uri = ep.getEndpointUri();
217            int pos = uri.indexOf(':');
218            String id = (pos == -1) ? uri : uri.substring(pos + 1);
219            if (!ep.isSingleton()) {
220                id += "." + Integer.toString(ep.hashCode());
221            }
222            return ObjectNameEncoder.encode(id);
223        }
224    
225        /**
226         * Factory method to create an ObjectName escaping any required characters
227         */
228        protected ObjectName createObjectName(StringBuffer buffer) throws MalformedObjectNameException {
229            String text = buffer.toString();
230            try {
231                return new ObjectName(text);
232            } catch (MalformedObjectNameException e) {
233                throw new MalformedObjectNameException("Could not create ObjectName from: " + text + ". Reason: " + e);
234            }
235        }
236    }