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.DisabledAware;
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.ManagedProcessorAware;
028import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
029import org.apache.camel.model.ProcessorDefinition;
030import org.apache.camel.model.ProcessorDefinitionHelper;
031import org.apache.camel.model.RouteDefinition;
032import org.apache.camel.model.StepDefinition;
033import org.apache.camel.spi.ManagementStrategy;
034import org.apache.camel.spi.NodeIdFactory;
035import org.apache.camel.spi.RouteIdAware;
036import org.apache.camel.support.LoggerHelper;
037import org.apache.camel.support.PluginHelper;
038import org.apache.camel.support.service.ServiceHelper;
039
040@ManagedResource(description = "Managed Processor")
041public class ManagedProcessor extends ManagedPerformanceCounter
042        implements ManagedInstance, ManagedProcessorMBean, ManagedProcessorAware {
043
044    private final CamelContext context;
045    private Processor processor;
046    private final ProcessorDefinition<?> definition;
047    private final String id;
048    private final int nodeLevel;
049    private final String stepId;
050    private Route route;
051    private String sourceLocation;
052    private String sourceLocationShort;
053
054    public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) {
055        this.context = context;
056        this.processor = processor;
057        this.definition = definition;
058        this.nodeLevel = definition.getLevel();
059        this.id = definition.idOrCreate(context.getCamelContextExtension().getContextPlugin(NodeIdFactory.class));
060        StepDefinition step;
061        if (definition instanceof StepDefinition stepDefinition) {
062            step = stepDefinition;
063        } else {
064            step = ProcessorDefinitionHelper.findFirstParentOfType(StepDefinition.class, definition, true);
065        }
066        this.stepId = step != null
067                ? step.idOrCreate(context.getCamelContextExtension().getContextPlugin(NodeIdFactory.class)) : null;
068        this.sourceLocation = definition.getLocation();
069        if (sourceLocation == null) {
070            RouteDefinition rd = ProcessorDefinitionHelper.getRoute(definition);
071            sourceLocation = rd != null ? rd.getLocation() : null;
072        }
073        this.sourceLocationShort = LoggerHelper.getLineNumberLoggerName(definition);
074    }
075
076    @Override
077    public void init(ManagementStrategy strategy) {
078        super.init(strategy);
079        boolean enabled = context.getManagementStrategy().getManagementAgent().getStatisticsLevel().isDefaultOrExtended();
080        setStatisticsEnabled(enabled);
081    }
082
083    public CamelContext getContext() {
084        return context;
085    }
086
087    @Override
088    public Object getInstance() {
089        return getProcessor();
090    }
091
092    public Processor getProcessor() {
093        return processor;
094    }
095
096    @Override
097    public void setProcessor(Processor processor) {
098        this.processor = processor;
099    }
100
101    public ProcessorDefinition<?> getDefinition() {
102        return definition;
103    }
104
105    public String getId() {
106        return id;
107    }
108
109    @Override
110    public String getStepId() {
111        return stepId;
112    }
113
114    @Override
115    public Integer getIndex() {
116        return definition.getIndex();
117    }
118
119    @Override
120    public int getLevel() {
121        return nodeLevel;
122    }
123
124    @Override
125    public String getSourceLocation() {
126        return sourceLocation;
127    }
128
129    @Override
130    public String getSourceLocationShort() {
131        return sourceLocationShort;
132    }
133
134    @Override
135    public Integer getSourceLineNumber() {
136        int line = definition.getLineNumber();
137        return line >= 0 ? line : null;
138    }
139
140    @Override
141    public Boolean getSupportExtendedInformation() {
142        return false;
143    }
144
145    public Route getRoute() {
146        return route;
147    }
148
149    public void setRoute(Route route) {
150        this.route = route;
151    }
152
153    @Override
154    public String getState() {
155        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
156        if (processor instanceof StatefulService statefulService) {
157            ServiceStatus status = statefulService.getStatus();
158            return status.name();
159        }
160
161        // assume started if not a ServiceSupport instance
162        return ServiceStatus.Started.name();
163    }
164
165    @Override
166    public String getCamelId() {
167        return context.getName();
168    }
169
170    @Override
171    public String getCamelManagementName() {
172        return context.getManagementName();
173    }
174
175    @Override
176    public String getRouteId() {
177        if (route != null) {
178            return route.getId();
179        } else if (processor instanceof RouteIdAware routeIdAware) {
180            return routeIdAware.getRouteId();
181        }
182        return null;
183    }
184
185    @Override
186    public String getNodePrefixId() {
187        if (route != null) {
188            return route.getNodePrefixId();
189        }
190        return null;
191    }
192
193    @Override
194    public String getProcessorId() {
195        return id;
196    }
197
198    @Override
199    public String getProcessorName() {
200        return definition.getShortName();
201    }
202
203    @Override
204    public String getDescription() {
205        return definition.getDescription();
206    }
207
208    @Override
209    public String getModelLabel() {
210        return definition.getLabel();
211    }
212
213    @Override
214    public String getNote() {
215        return definition.getNote();
216    }
217
218    @Override
219    public Boolean getDisabled() {
220        if (processor instanceof DisabledAware da) {
221            return da.isDisabled();
222        }
223        return false;
224    }
225
226    @Override
227    public void enable() {
228        if (processor instanceof DisabledAware da) {
229            da.setDisabled(false);
230        }
231    }
232
233    @Override
234    public void disable() {
235        if (processor instanceof DisabledAware da) {
236            da.setDisabled(true);
237        }
238    }
239
240    @Override
241    public void start() throws Exception {
242        if (!context.getStatus().isStarted()) {
243            throw new IllegalArgumentException("CamelContext is not started");
244        }
245        ServiceHelper.startService(getProcessor());
246    }
247
248    @Override
249    public void stop() throws Exception {
250        if (!context.getStatus().isStarted()) {
251            throw new IllegalArgumentException("CamelContext is not started");
252        }
253        ServiceHelper.stopService(getProcessor());
254    }
255
256    @Override
257    public String dumpProcessorAsXml() throws Exception {
258        return PluginHelper.getModelToXMLDumper(context).dumpModelAsXml(context, definition);
259    }
260}