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 */ 017package org.apache.camel.management.mbean; 018 019import java.util.List; 020import java.util.Map; 021import javax.management.openmbean.CompositeData; 022import javax.management.openmbean.CompositeDataSupport; 023import javax.management.openmbean.CompositeType; 024import javax.management.openmbean.TabularData; 025import javax.management.openmbean.TabularDataSupport; 026 027import org.apache.camel.CamelContext; 028import org.apache.camel.ServiceStatus; 029import org.apache.camel.StatefulService; 030import org.apache.camel.api.management.ManagedInstance; 031import org.apache.camel.api.management.ManagedResource; 032import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; 033import org.apache.camel.api.management.mbean.ManagedDataFormatMBean; 034import org.apache.camel.spi.DataFormat; 035import org.apache.camel.spi.DataFormatName; 036import org.apache.camel.spi.ManagementStrategy; 037import org.apache.camel.util.JsonSchemaHelper; 038import org.apache.camel.util.ObjectHelper; 039 040@ManagedResource(description = "Managed DataFormat") 041public class ManagedDataFormat implements ManagedInstance, ManagedDataFormatMBean { 042 private final CamelContext camelContext; 043 private final DataFormat dataFormat; 044 045 public ManagedDataFormat(CamelContext camelContext, DataFormat dataFormat) { 046 this.camelContext = camelContext; 047 this.dataFormat = dataFormat; 048 } 049 050 public void init(ManagementStrategy strategy) { 051 // noop 052 } 053 054 public DataFormat getDataFormat() { 055 return dataFormat; 056 } 057 058 public CamelContext getContext() { 059 return camelContext; 060 } 061 062 @Override 063 public String getName() { 064 if (dataFormat instanceof DataFormatName) { 065 return ((DataFormatName) dataFormat).getDataFormatName(); 066 } 067 return null; 068 } 069 070 @Override 071 public String getCamelId() { 072 return camelContext.getName(); 073 } 074 075 @Override 076 public String getCamelManagementName() { 077 return camelContext.getManagementName(); 078 } 079 080 @Override 081 public String getState() { 082 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes. 083 if (dataFormat instanceof StatefulService) { 084 ServiceStatus status = ((StatefulService) dataFormat).getStatus(); 085 return status.name(); 086 } 087 088 // assume started if not a ServiceSupport instance 089 return ServiceStatus.Started.name(); 090 } 091 092 @Override 093 public String informationJson() { 094 String dataFormatName = getName(); 095 if (dataFormatName != null) { 096 return camelContext.explainDataFormatJson(dataFormatName, dataFormat, true); 097 } else { 098 return null; 099 } 100 } 101 102 @Override 103 public TabularData explain(boolean allOptions) { 104 String dataFormatName = getName(); 105 if (dataFormatName != null) { 106 try { 107 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainDataFormatTabularType()); 108 109 String json = camelContext.explainDataFormatJson(dataFormatName, dataFormat, allOptions); 110 List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true); 111 112 for (Map<String, String> row : rows) { 113 String name = row.get("name"); 114 String kind = row.get("kind"); 115 String label = row.get("label") != null ? row.get("label") : ""; 116 String type = row.get("type"); 117 String javaType = row.get("javaType"); 118 String deprecated = row.get("deprecated") != null ? row.get("deprecated") : ""; 119 String secret = row.get("secret") != null ? row.get("secret") : ""; 120 String value = row.get("value") != null ? row.get("value") : ""; 121 String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : ""; 122 String description = row.get("description") != null ? row.get("description") : ""; 123 124 CompositeType ct = CamelOpenMBeanTypes.explainDataFormatsCompositeType(); 125 CompositeData data = new CompositeDataSupport(ct, 126 new String[]{"option", "kind", "label", "type", "java type", "deprecated", "secret", "value", "default value", "description"}, 127 new Object[]{name, kind, label, type, javaType, deprecated, secret, value, defaultValue, description}); 128 answer.put(data); 129 } 130 131 return answer; 132 } catch (Exception e) { 133 throw ObjectHelper.wrapRuntimeCamelException(e); 134 } 135 } else { 136 return null; 137 } 138 } 139 140 @Override 141 public DataFormat getInstance() { 142 return dataFormat; 143 } 144 145}