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.component.controlbus;
018
019import org.apache.camel.Component;
020import org.apache.camel.Consumer;
021import org.apache.camel.LoggingLevel;
022import org.apache.camel.Processor;
023import org.apache.camel.Producer;
024import org.apache.camel.RuntimeCamelException;
025import org.apache.camel.impl.DefaultEndpoint;
026import org.apache.camel.spi.Language;
027import org.apache.camel.spi.Metadata;
028import org.apache.camel.spi.UriEndpoint;
029import org.apache.camel.spi.UriParam;
030import org.apache.camel.spi.UriPath;
031import org.apache.camel.util.CamelLogger;
032
033/**
034 * The controlbus component provides easy management of Camel applications based on the Control Bus EIP pattern.
035 *
036 * For example, by sending a message to an Endpoint you can control the lifecycle of routes, or gather performance statistics.
037 */
038@UriEndpoint(scheme = "controlbus", title = "Control Bus", syntax = "controlbus:command:language", producerOnly = true, label = "core,monitoring")
039public class ControlBusEndpoint extends DefaultEndpoint {
040
041    @UriPath(description = "Command can be either route or language", enums = "route,language") @Metadata(required = "true")
042    private String command;
043    @UriPath(enums = "bean,constant,el,exchangeProperty,file,groovy,header,jsonpath,jxpath,mvel,ognl,ref,simple,spel,sql,terser,tokenize,xpath,xquery,xtokenize")
044    private Language language;
045    @UriParam
046    private String routeId;
047    @UriParam(enums = "start,stop,suspend,resume,status")
048    private String action;
049    @UriParam
050    private boolean async;
051    @UriParam(defaultValue = "INFO")
052    private LoggingLevel loggingLevel = LoggingLevel.INFO;
053
054    public ControlBusEndpoint(String endpointUri, Component component) {
055        super(endpointUri, component);
056    }
057
058    @Override
059    public Producer createProducer() throws Exception {
060        CamelLogger logger = new CamelLogger(ControlBusProducer.class.getName(), loggingLevel);
061        return new ControlBusProducer(this, logger);
062    }
063
064    @Override
065    public Consumer createConsumer(Processor processor) throws Exception {
066        throw new RuntimeCamelException("Cannot consume from a ControlBusEndpoint: " + getEndpointUri());
067    }
068
069    @Override
070    public boolean isSingleton() {
071        // we dont want to be enlisted in JMX, so lets just be non-singleton
072        return false;
073    }
074
075    @Override
076    public ControlBusComponent getComponent() {
077        return (ControlBusComponent) super.getComponent();
078    }
079
080    public Language getLanguage() {
081        return language;
082    }
083
084    /**
085     * Allows you to specify the name of a Language to use for evaluating the message body.
086     * If there is any result from the evaluation, then the result is put in the message body.
087     */
088    public void setLanguage(Language language) {
089        this.language = language;
090    }
091
092    public String getRouteId() {
093        return routeId;
094    }
095
096    /**
097     * To specify a route by its id.
098     * The special keyword "current" indicates the current route.
099     */
100    public void setRouteId(String routeId) {
101        this.routeId = routeId;
102    }
103
104    public String getAction() {
105        return action;
106    }
107
108    /**
109     * To denote an action that can be either: start, stop, or status.
110     * <p/>
111     * To either start or stop a route, or to get the status of the route as output in the message body.
112     * You can use suspend and resume from Camel 2.11.1 onwards to either suspend or resume a route.
113     * And from Camel 2.11.1 onwards you can use stats to get performance statics returned in XML format;
114     * the routeId option can be used to define which route to get the performance stats for, if routeId is not defined,
115     * then you get statistics for the entire CamelContext.
116     */
117    public void setAction(String action) {
118        this.action = action;
119    }
120
121    public boolean isAsync() {
122        return async;
123    }
124
125    /**
126     * Whether to execute the control bus task asynchronously.
127     * <p/>
128     * Important: If this option is enabled, then any result from the task is not set on the Exchange.
129     * This is only possible if executing tasks synchronously.
130     */
131    public void setAsync(boolean async) {
132        this.async = async;
133    }
134
135    public LoggingLevel getLoggingLevel() {
136        return loggingLevel;
137    }
138
139    /**
140     * Logging level used for logging when task is done, or if any exceptions occurred during processing the task.
141     */
142    public void setLoggingLevel(LoggingLevel loggingLevel) {
143        this.loggingLevel = loggingLevel;
144    }
145}