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 org.apache.camel.CamelContext;
020import org.apache.camel.ExtendedCamelContext;
021import org.apache.camel.Processor;
022import org.apache.camel.Route;
023import org.apache.camel.ServiceStatus;
024import org.apache.camel.StatefulService;
025import org.apache.camel.api.management.ManagedInstance;
026import org.apache.camel.api.management.ManagedResource;
027import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
028import org.apache.camel.model.ProcessorDefinition;
029import org.apache.camel.model.ProcessorDefinitionHelper;
030import org.apache.camel.model.StepDefinition;
031import org.apache.camel.spi.ManagementStrategy;
032import org.apache.camel.spi.RouteIdAware;
033import org.apache.camel.support.service.ServiceHelper;
034
035@ManagedResource(description = "Managed Processor")
036public class ManagedProcessor extends ManagedPerformanceCounter implements ManagedInstance, ManagedProcessorMBean {
037
038    private final CamelContext context;
039    private final Processor processor;
040    private final ProcessorDefinition<?> definition;
041    private final String id;
042    private String stepId;
043    private Route route;
044
045    public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) {
046        this.context = context;
047        this.processor = processor;
048        this.definition = definition;
049        this.id = definition.idOrCreate(context.adapt(ExtendedCamelContext.class).getNodeIdFactory());
050        StepDefinition step;
051        if (definition instanceof StepDefinition) {
052            step = (StepDefinition) definition;
053        } else {
054            step = ProcessorDefinitionHelper.findFirstParentOfType(StepDefinition.class, definition, true);
055        }
056        this.stepId = step != null ? step.idOrCreate(context.adapt(ExtendedCamelContext.class).getNodeIdFactory()) : null;
057    }
058
059    @Override
060    public void init(ManagementStrategy strategy) {
061        super.init(strategy);
062        boolean enabled = context.getManagementStrategy().getManagementAgent().getStatisticsLevel().isDefaultOrExtended();
063        setStatisticsEnabled(enabled);
064    }
065
066    public CamelContext getContext() {
067        return context;
068    }
069
070    @Override
071    public Object getInstance() {
072        return processor;
073    }
074
075    public Processor getProcessor() {
076        return processor;
077    }
078
079    public ProcessorDefinition<?> getDefinition() {
080        return definition;
081    }
082
083    public String getId() {
084        return id;
085    }
086
087    @Override
088    public String getStepId() {
089        return stepId;
090    }
091
092    @Override
093    public Integer getIndex() {
094        return definition.getIndex();
095    }
096
097    @Override
098    public Boolean getSupportExtendedInformation() {
099        return false;
100    }
101
102    public Route getRoute() {
103        return route;
104    }
105
106    public void setRoute(Route route) {
107        this.route = route;
108    }
109
110    @Override
111    public String getState() {
112        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
113        if (processor instanceof StatefulService) {
114            ServiceStatus status = ((StatefulService) processor).getStatus();
115            return status.name();
116        }
117
118        // assume started if not a ServiceSupport instance
119        return ServiceStatus.Started.name();
120    }
121
122    @Override
123    public String getCamelId() {
124        return context.getName();
125    }
126
127    @Override
128    public String getCamelManagementName() {
129        return context.getManagementName();
130    }
131
132    @Override
133    public String getRouteId() {
134        if (route != null) {
135            return route.getId();
136        } else if (processor instanceof RouteIdAware) {
137            return ((RouteIdAware) processor).getRouteId();
138        }
139        return null;
140    }
141
142    @Override
143    public String getProcessorId() {
144        return id;
145    }
146
147    @Override
148    public void start() throws Exception {
149        if (!context.getStatus().isStarted()) {
150            throw new IllegalArgumentException("CamelContext is not started");
151        }
152        ServiceHelper.startService(getProcessor());
153    }
154
155    @Override
156    public void stop() throws Exception {
157        if (!context.getStatus().isStarted()) {
158            throw new IllegalArgumentException("CamelContext is not started");
159        }
160        ServiceHelper.stopService(getProcessor());
161    }
162
163    @Override
164    public String dumpProcessorAsXml() throws Exception {
165        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
166        return ecc.getModelToXMLDumper().dumpModelAsXml(context, definition);
167    }
168}