Author: davsclaus Date: Thu Nov 1 15:42:39 2012 New Revision: 1404647 URL: http://svn.apache.org/viewvc?rev=1404647&view=rev Log: CAMEL-5651: Initial spike of control bus EIP / component.
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusComponent.java (with props) camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java (with props) camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java (with props) camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/package.html - copied, changed from r1404502, camel/trunk/camel-core/src/main/java/org/apache/camel/component/binding/package.html camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/controlbus - copied, changed from r1404498, camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/binding camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusLanguageSimpleStartRouteTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStartRouteTest.java (with props) Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusComponent.java?rev=1404647&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusComponent.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusComponent.java Thu Nov 1 15:42:39 2012 @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.controlbus; + +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.impl.DefaultComponent; + +/** + * The <a href="http://camel.apache.org/controlbus.html">control bus</a> component. + */ +public class ControlBusComponent extends DefaultComponent { + + // TODO: allow to use a thread pool for tasks so you dont have to wait + // TODO: management command, to use the JMX mbeans easier + // TODO: Bulk status in POJO / JSON format + // TODO: a header with the action to do instead of uri, as we may want to be lenient + + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + ControlBusEndpoint answer = new ControlBusEndpoint(uri, this); + + // does the control bus use a language + if (remaining != null && remaining.startsWith("language:")) { + String lan = remaining.substring(9); + if (lan != null) { + answer.setLanguage(getCamelContext().resolveLanguage(lan)); + } else { + throw new IllegalArgumentException("Language must be configured in endpoint uri: " + uri); + } + } + + setProperties(answer, parameters); + return answer; + } +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusComponent.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusComponent.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java?rev=1404647&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java Thu Nov 1 15:42:39 2012 @@ -0,0 +1,79 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.controlbus; + +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.spi.Language; + +/** + * The control bus endpoint. + */ +public class ControlBusEndpoint extends DefaultEndpoint { + + private Language language; + private String routeId; + private String action; + + public ControlBusEndpoint(String endpointUri, Component component) { + super(endpointUri, component); + } + + @Override + public Producer createProducer() throws Exception { + return new ControlBusProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + throw new RuntimeCamelException("Cannot consume from a ControlBusEndpoint: " + getEndpointUri()); + } + + @Override + public boolean isSingleton() { + // we dont want to be enlisted in JMX, so lets just be non-singleton + return false; + } + + public Language getLanguage() { + return language; + } + + public void setLanguage(Language language) { + this.language = language; + } + + public String getRouteId() { + return routeId; + } + + public void setRouteId(String routeId) { + this.routeId = routeId; + } + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusEndpoint.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java?rev=1404647&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java Thu Nov 1 15:42:39 2012 @@ -0,0 +1,89 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.controlbus; + +import org.apache.camel.AsyncCallback; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.ServiceStatus; +import org.apache.camel.impl.DefaultAsyncProducer; +import org.apache.camel.spi.Language; +import org.apache.camel.util.ExchangeHelper; + +/** + * The control bus producer. + */ +public class ControlBusProducer extends DefaultAsyncProducer { + + public ControlBusProducer(Endpoint endpoint) { + super(endpoint); + } + + @Override + public ControlBusEndpoint getEndpoint() { + return (ControlBusEndpoint) super.getEndpoint(); + } + + @Override + public boolean process(Exchange exchange, AsyncCallback callback) { + if (getEndpoint().getLanguage() != null) { + try { + processByLanguage(exchange, getEndpoint().getLanguage()); + } catch (Exception e) { + exchange.setException(e); + } + } else if (getEndpoint().getAction() != null) { + try { + processByAction(exchange, getEndpoint().getRouteId(), getEndpoint().getAction()); + } catch (Exception e) { + exchange.setException(e); + } + } + + callback.done(true); + return true; + } + + protected void processByLanguage(Exchange exchange, Language language) throws Exception { + // create dummy exchange + Exchange dummy = ExchangeHelper.createCopy(exchange, true); + + String body = dummy.getIn().getMandatoryBody(String.class); + if (body != null) { + Expression exp = language.createExpression(body); + Object out = exp.evaluate(dummy, Object.class); + if (out != null) { + exchange.getIn().setBody(out); + } + } + } + + protected void processByAction(Exchange exchange, String id, String action) throws Exception { + if ("start".equals(action)) { + getEndpoint().getCamelContext().startRoute(id); + } else if ("stop".equals(action)) { + getEndpoint().getCamelContext().stopRoute(id); + } else if ("status".equals(action)) { + ServiceStatus status = getEndpoint().getCamelContext().getRouteStatus(id); + if (status != null) { + exchange.getIn().setBody(status.name()); + } + } + } + +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/ControlBusProducer.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Copied: camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/package.html (from r1404502, camel/trunk/camel-core/src/main/java/org/apache/camel/component/binding/package.html) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/package.html?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/package.html&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/component/binding/package.html&r1=1404502&r2=1404647&rev=1404647&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/binding/package.html (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/controlbus/package.html Thu Nov 1 15:42:39 2012 @@ -19,8 +19,8 @@ </head> <body> -The <a href="http://camel.apache.org/binding.html">Binding Component</a> which is -a way of wrapping an Endpoint in a contract; such as a Data Format, a Content Enricher or validation step. +The <a href="http://camel.apache.org/controlbus.html">Control Bus Component</a> which +allows to manage Camel at runtime. </body> </html> Copied: camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/controlbus (from r1404498, camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/binding) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/controlbus?p2=camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/controlbus&p1=camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/binding&r1=1404498&r2=1404647&rev=1404647&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/binding (original) +++ camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/controlbus Thu Nov 1 15:42:39 2012 @@ -15,4 +15,4 @@ # limitations under the License. # -class=org.apache.camel.component.binding.BindingNameComponent \ No newline at end of file +class=org.apache.camel.component.controlbus.ControlBusComponent \ No newline at end of file Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusLanguageSimpleStartRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusLanguageSimpleStartRouteTest.java?rev=1404647&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusLanguageSimpleStartRouteTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusLanguageSimpleStartRouteTest.java Thu Nov 1 15:42:39 2012 @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.controlbus; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * + */ +public class ControlBusLanguageSimpleStartRouteTest extends ContextTestSupport { + + public void testControlBusStartStop() throws Exception { + assertEquals("Stopped", context.getRouteStatus("foo").name()); + + // store a pending message + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); + template.sendBody("seda:foo", "Hello World"); + + // start the route using control bus + template.sendBody("controlbus:language:simple", "camelContext.startRoute('foo')"); + + assertMockEndpointsSatisfied(); + + // now stop the route, using a header + template.sendBodyAndHeader("controlbus:language:simple", "camelContext.stopRoute(header.me)", "me", "foo"); + + assertEquals("Stopped", context.getRouteStatus("foo").name()); + } + + public void testControlBusStatus() throws Exception { + assertEquals("Stopped", context.getRouteStatus("foo").name()); + + String status = template.requestBody("controlbus:language:simple", "camelContext.getRouteStatus('foo')", String.class); + assertEquals("Stopped", status); + + context.startRoute("foo"); + + status = template.requestBody("controlbus:language:simple", "camelContext.getRouteStatus('foo')", String.class); + assertEquals("Started", status); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:foo").routeId("foo").noAutoStartup() + .to("mock:foo"); + } + }; + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusLanguageSimpleStartRouteTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusLanguageSimpleStartRouteTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStartRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStartRouteTest.java?rev=1404647&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStartRouteTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStartRouteTest.java Thu Nov 1 15:42:39 2012 @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.controlbus; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * + */ +public class ControlBusStartRouteTest extends ContextTestSupport { + + public void testControlBusStartStop() throws Exception { + assertEquals("Stopped", context.getRouteStatus("foo").name()); + + // store a pending message + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); + template.sendBody("seda:foo", "Hello World"); + + // start the route using control bus + template.sendBody("controlbus:route?routeId=foo&action=start", null); + + assertMockEndpointsSatisfied(); + + // now stop the route, using a header + template.sendBody("controlbus:route?routeId=foo&action=stop", null); + + assertEquals("Stopped", context.getRouteStatus("foo").name()); + } + + public void testControlBusStatus() throws Exception { + assertEquals("Stopped", context.getRouteStatus("foo").name()); + + String status = template.requestBody("controlbus:route?routeId=foo&action=status", null, String.class); + assertEquals("Stopped", status); + + context.startRoute("foo"); + + status = template.requestBody("controlbus:route?routeId=foo&action=status", null, String.class); + assertEquals("Started", status); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:foo").routeId("foo").noAutoStartup() + .to("mock:foo"); + } + }; + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStartRouteTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/controlbus/ControlBusStartRouteTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date