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