001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.management; 019 020 import java.net.InetAddress; 021 import java.net.UnknownHostException; 022 import java.util.Hashtable; 023 024 import javax.management.MalformedObjectNameException; 025 import javax.management.ObjectName; 026 027 import org.apache.camel.CamelContext; 028 import org.apache.camel.Endpoint; 029 030 public class CamelNamingStrategy { 031 032 public static final String VALUE_UNKNOWN = "unknown"; 033 public static final String KEY_CONTEXT = "context"; 034 public static final String KEY_ENDPOINT = "endpoint"; 035 public static final String KEY_ROUTE = "route"; 036 public static final String KEY_TYPE = "type"; 037 public static final String KEY_NAME = "name"; 038 public static final String TYPE_ENDPOINTS = "Endpoints"; 039 public static final String TYPE_SERVICES = "Services"; 040 public static final String TYPE_ROUTES = "Routes"; 041 042 protected String domainName = "org.apache.camel"; 043 protected String hostName = "locahost"; 044 045 public CamelNamingStrategy(String domainName) { 046 if (domainName != null) { 047 this.domainName = domainName; 048 } 049 try { 050 hostName = InetAddress.getLocalHost().getHostName(); 051 } 052 catch (UnknownHostException ex) { 053 // ignore 054 } 055 } 056 057 /** 058 * Implements the naming strategy for a {@see CamelContext}. 059 * The convention used for a {@see CamelContext} ObjectName is 060 * "<domain>:context=<context>,name=camel". 061 * 062 * @param mbean 063 * @return generated ObjectName 064 * @throws MalformedObjectNameException 065 */ 066 public ObjectName getObjectName(CamelContext context) throws MalformedObjectNameException { 067 Hashtable<String, String> keys = new Hashtable<String, String>(); 068 keys.put(KEY_CONTEXT, getContextId(context)); 069 keys.put(KEY_NAME, "camel"); 070 return new ObjectName(domainName, keys); 071 } 072 073 /** 074 * Implements the naming strategy for a {@see ManagedEndpoint}. 075 * The convention used for a {@see ManagedEndpoint} ObjectName is 076 * "<domain>:context=<context>,type=Endpoints,endpoint=[urlPrefix]localPart". 077 * 078 * @param mbean 079 * @return generated ObjectName 080 * @throws MalformedObjectNameException 081 */ 082 public ObjectName getObjectName(ManagedEndpoint mbean) throws MalformedObjectNameException { 083 Endpoint ep = mbean.getEndpoint(); 084 Hashtable<String, String> keys = new Hashtable<String, String>(); 085 keys.put(KEY_CONTEXT, getContextId(ep.getContext())); 086 keys.put(KEY_TYPE, TYPE_ENDPOINTS); 087 keys.put(KEY_ENDPOINT, getEndpointId(ep)); 088 return new ObjectName(domainName, keys); 089 } 090 091 /** 092 * Implements the naming strategy for a {@see ServiceSpport Service}. 093 * The convention used for a {@see Service} ObjectName is 094 * "<domain>:context=<context>,type=Services,endpoint=[urlPrefix]localPart". 095 * 096 * @param mbean 097 * @return generated ObjectName 098 * @throws MalformedObjectNameException 099 */ 100 public ObjectName getObjectName(CamelContext context, ManagedService mbean) throws MalformedObjectNameException { 101 Hashtable<String, String> keys = new Hashtable<String, String>(); 102 keys.put(KEY_CONTEXT, getContextId(context)); 103 keys.put(KEY_TYPE, TYPE_SERVICES); 104 keys.put(KEY_ENDPOINT, Integer.toHexString(mbean.getService().hashCode())); 105 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 Hashtable<String, String> keys = new Hashtable<String, String>(); 119 Endpoint ep = mbean.getRoute().getEndpoint(); 120 String ctxid = ep != null ? getContextId(ep.getContext()) : VALUE_UNKNOWN; 121 keys.put(KEY_CONTEXT, ctxid); 122 keys.put(KEY_TYPE, TYPE_ROUTES); 123 keys.put(KEY_ENDPOINT, getEndpointId(ep)); 124 return new ObjectName(domainName, keys); 125 } 126 127 protected String getContextId(CamelContext context) { 128 String id = context != null ? Integer.toString(context.hashCode()) : VALUE_UNKNOWN; 129 return hostName + "/" + id; 130 } 131 132 protected String getEndpointId(Endpoint ep) { 133 String uri = ep.getEndpointUri(); 134 int pos = uri.indexOf(':'); 135 String id = (pos == -1) ? uri : 136 "[" + uri.substring(0, pos) + "]" + uri.substring(pos + 1); 137 if (!ep.isSingleton()) { 138 id += "." + Integer.toString(ep.hashCode()); 139 } 140 return id; 141 } 142 }