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.ManagementStatisticsLevel;
029import org.apache.camel.Processor;
030import org.apache.camel.Route;
031import org.apache.camel.ServiceStatus;
032import org.apache.camel.StatefulService;
033import org.apache.camel.api.management.ManagedInstance;
034import org.apache.camel.api.management.ManagedResource;
035import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
036import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
037import org.apache.camel.model.ProcessorDefinition;
038import org.apache.camel.util.JsonSchemaHelper;
039import org.apache.camel.util.ObjectHelper;
040import org.apache.camel.util.ServiceHelper;
041
042/**
043 * @version 
044 */
045@ManagedResource(description = "Managed Processor")
046public class ManagedProcessor extends ManagedPerformanceCounter implements ManagedInstance, ManagedProcessorMBean {
047
048    private final CamelContext context;
049    private final Processor processor;
050    private final ProcessorDefinition<?> definition;
051    private final String id;
052    private Route route;
053
054    public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) {
055        this.context = context;
056        this.processor = processor;
057        this.definition = definition;
058        this.id = definition.idOrCreate(context.getNodeIdFactory());
059
060        boolean enabled = context.getManagementStrategy().getStatisticsLevel() == ManagementStatisticsLevel.All;
061        setStatisticsEnabled(enabled);
062    }
063
064    public CamelContext getContext() {
065        return context;
066    }
067
068    public Object getInstance() {
069        return processor;
070    }
071
072    public Processor getProcessor() {
073        return processor;
074    }
075
076    public ProcessorDefinition<?> getDefinition() {
077        return definition;
078    }
079
080    public String getId() {
081        return id;
082    }
083
084    public Integer getIndex() {
085        return definition.getIndex();
086    }
087
088    public Route getRoute() {
089        return route;
090    }
091
092    public void setRoute(Route route) {
093        this.route = route;
094    }
095
096    public String getState() {
097        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
098        if (processor instanceof StatefulService) {
099            ServiceStatus status = ((StatefulService) processor).getStatus();
100            return status.name();
101        }
102
103        // assume started if not a ServiceSupport instance
104        return ServiceStatus.Started.name();
105    }
106
107    public String getCamelId() {
108        return context.getName();
109    }
110
111    public String getCamelManagementName() {
112        return context.getManagementName();
113    }
114
115    public String getRouteId() {
116        if (route != null) {
117            return route.getId();
118        }
119        return null;
120    }
121
122    public String getProcessorId() {
123        return id;
124    }
125
126    public void start() throws Exception {
127        if (!context.getStatus().isStarted()) {
128            throw new IllegalArgumentException("CamelContext is not started");
129        }
130        ServiceHelper.startService(getProcessor());
131    }
132
133    public void stop() throws Exception {
134        if (!context.getStatus().isStarted()) {
135            throw new IllegalArgumentException("CamelContext is not started");
136        }
137        ServiceHelper.stopService(getProcessor());
138    }
139
140    public String informationJson() {
141        return context.explainEipJson(id, true);
142    }
143
144    public TabularData explain(boolean allOptions) {
145        try {
146            String json = context.explainEipJson(id, allOptions);
147            List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true);
148
149            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainEipTabularType());
150
151            for (Map<String, String> row : rows) {
152                String name = row.get("name");
153                String kind = row.get("kind");
154                String label = row.get("label") != null ? row.get("label") : "";
155                String type = row.get("type");
156                String javaType = row.get("javaType");
157                String deprecated = row.get("deprecated") != null ? row.get("deprecated") : "";
158                String value = row.get("value") != null ? row.get("value") : "";
159                String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : "";
160                String description = row.get("description") != null ? row.get("description") : "";
161
162                CompositeType ct = CamelOpenMBeanTypes.explainEipsCompositeType();
163                CompositeData data = new CompositeDataSupport(ct,
164                        new String[]{"option", "kind", "label", "type", "java type", "deprecated", "value", "default value", "description"},
165                        new Object[]{name, kind, label, type, javaType, deprecated, value, defaultValue, description});
166                answer.put(data);
167            }
168
169            return answer;
170        } catch (Exception e) {
171            throw ObjectHelper.wrapRuntimeCamelException(e);
172        }
173    }
174}