Repository: camel Updated Branches: refs/heads/master bd92689f2 -> 7920a773b
CAMEL-7386: camel-openshift component. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9d537003 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9d537003 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9d537003 Branch: refs/heads/master Commit: 9d53700317986898565c4d462644d116420fe616 Parents: bd92689 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Apr 23 11:48:25 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Apr 23 11:48:25 2014 +0200 ---------------------------------------------------------------------- .../component/openshift/OpenShiftConsumer.java | 67 ++++++++++++++++++++ .../component/openshift/OpenShiftEndpoint.java | 19 ++++-- .../component/openshift/OpenShiftHelper.java | 61 ++++++++++++++++++ .../component/openshift/OpenShiftProducer.java | 24 +------ .../openshift/OpenShiftConsumerTest.java | 58 +++++++++++++++++ 5 files changed, 203 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9d537003/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftConsumer.java b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftConsumer.java new file mode 100644 index 0000000..9cbefd8 --- /dev/null +++ b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftConsumer.java @@ -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.openshift; + +import java.util.List; + +import com.openshift.client.IApplication; +import com.openshift.client.IDomain; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.impl.ScheduledPollConsumer; + +public class OpenShiftConsumer extends ScheduledPollConsumer { + + public OpenShiftConsumer(Endpoint endpoint, Processor processor) { + super(endpoint, processor); + } + + @Override + public OpenShiftEndpoint getEndpoint() { + return (OpenShiftEndpoint) super.getEndpoint(); + } + + @Override + protected int poll() throws Exception { + String openshiftServer = OpenShiftHelper.getOpenShiftServer(getEndpoint()); + IDomain domain = OpenShiftHelper.loginAndGetDomain(getEndpoint(), openshiftServer); + if (domain == null) { + return 0; + } + + List<IApplication> apps = domain.getApplications(); + + // TODO grab state + // TODO: option to only emit if state changes + + for (IApplication app : apps) { + Exchange exchange = getEndpoint().createExchange(app); + try { + getProcessor().process(exchange); + } catch (Exception e) { + exchange.setException(e); + } + if (exchange.getException() != null) { + getExceptionHandler().handleException("Error during processing exchange.", exchange, exchange.getException()); + } + } + + return apps.size(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/9d537003/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftEndpoint.java b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftEndpoint.java index 7439a48..0735762 100644 --- a/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftEndpoint.java +++ b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftEndpoint.java @@ -16,17 +16,20 @@ */ package org.apache.camel.component.openshift; +import com.openshift.client.IApplication; import org.apache.camel.Component; import org.apache.camel.Consumer; +import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.Producer; -import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.impl.DefaultExchange; +import org.apache.camel.impl.ScheduledPollEndpoint; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.util.ObjectHelper; -@UriEndpoint(scheme = "openshift") -public class OpenShiftEndpoint extends DefaultEndpoint { +@UriEndpoint(scheme = "openshift", consumerClass = OpenShiftConsumer.class) +public class OpenShiftEndpoint extends ScheduledPollEndpoint { @UriParam private String username; @@ -57,7 +60,15 @@ public class OpenShiftEndpoint extends DefaultEndpoint { @Override public Consumer createConsumer(Processor processor) throws Exception { - throw new UnsupportedOperationException("Consumer not supported on this component"); + Consumer consumer = new OpenShiftConsumer(this, processor); + configureConsumer(consumer); + return consumer; + } + + public Exchange createExchange(IApplication application) { + Exchange exchange = new DefaultExchange(this); + exchange.getIn().setBody(application); + return exchange; } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/9d537003/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftHelper.java b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftHelper.java new file mode 100644 index 0000000..cb7db5e --- /dev/null +++ b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftHelper.java @@ -0,0 +1,61 @@ +/** + * 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.openshift; + +import java.io.IOException; + +import com.openshift.client.IDomain; +import com.openshift.client.IOpenShiftConnection; +import com.openshift.client.IUser; +import com.openshift.client.OpenShiftConnectionFactory; +import com.openshift.client.configuration.OpenShiftConfiguration; + +public final class OpenShiftHelper { + + private static final String DEFAULT_OPENSHIFT_SERVER = "openshift.redhat.com"; + + private OpenShiftHelper() { + } + + public static String getOpenShiftServer(OpenShiftEndpoint endpoint) throws IOException { + String answer = endpoint.getServer(); + if (answer == null) { + answer = new OpenShiftConfiguration().getLibraServer(); + } + if (answer == null) { + answer = DEFAULT_OPENSHIFT_SERVER; + } + return answer; + } + + public static IDomain loginAndGetDomain(OpenShiftEndpoint endpoint, String openshiftServer) { + // grab all applications + IOpenShiftConnection connection = + new OpenShiftConnectionFactory().getConnection(endpoint.getClientId(), endpoint.getUsername(), endpoint.getPassword(), openshiftServer); + + IUser user = connection.getUser(); + + IDomain domain; + if (endpoint.getDomain() != null) { + domain = user.getDomain(endpoint.getDomain()); + } else { + domain = user.getDefaultDomain(); + } + + return domain; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/9d537003/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftProducer.java b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftProducer.java index ae218ff..d453be4 100644 --- a/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftProducer.java +++ b/components/camel-openshift/src/main/java/org/apache/camel/component/openshift/OpenShiftProducer.java @@ -25,11 +25,7 @@ import com.openshift.client.IApplication; import com.openshift.client.IDomain; import com.openshift.client.IGear; import com.openshift.client.IGearGroup; -import com.openshift.client.IOpenShiftConnection; -import com.openshift.client.IUser; -import com.openshift.client.OpenShiftConnectionFactory; import com.openshift.client.cartridge.IEmbeddedCartridge; -import com.openshift.client.configuration.OpenShiftConfiguration; import org.apache.camel.CamelExchangeException; import org.apache.camel.Endpoint; import org.apache.camel.Exchange; @@ -50,24 +46,8 @@ public class OpenShiftProducer extends DefaultProducer { @Override public void process(Exchange exchange) throws Exception { - String openshiftServer; - if (getEndpoint().getServer() != null) { - openshiftServer = getEndpoint().getServer(); - } else { - openshiftServer = new OpenShiftConfiguration().getLibraServer(); - } - - IOpenShiftConnection connection = - new OpenShiftConnectionFactory().getConnection(getEndpoint().getClientId(), getEndpoint().getUsername(), getEndpoint().getPassword(), openshiftServer); - - IUser user = connection.getUser(); - - IDomain domain; - if (getEndpoint().getDomain() != null) { - domain = user.getDomain(getEndpoint().getDomain()); - } else { - domain = user.getDefaultDomain(); - } + String openshiftServer = OpenShiftHelper.getOpenShiftServer(getEndpoint()); + IDomain domain = OpenShiftHelper.loginAndGetDomain(getEndpoint(), openshiftServer); if (domain == null) { throw new CamelExchangeException("User has no domain with id " + getEndpoint().getDomain(), exchange); } http://git-wip-us.apache.org/repos/asf/camel/blob/9d537003/components/camel-openshift/src/test/java/org/apache/camel/component/openshift/OpenShiftConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-openshift/src/test/java/org/apache/camel/component/openshift/OpenShiftConsumerTest.java b/components/camel-openshift/src/test/java/org/apache/camel/component/openshift/OpenShiftConsumerTest.java new file mode 100644 index 0000000..0d9157b --- /dev/null +++ b/components/camel-openshift/src/test/java/org/apache/camel/component/openshift/OpenShiftConsumerTest.java @@ -0,0 +1,58 @@ +/** + * 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.openshift; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class OpenShiftConsumerTest extends CamelTestSupport { + + private String username; + private String password; + + @Override + public void setUp() throws Exception { + // INSERT credentials here + username = null; + password = null; + super.setUp(); + } + + @Test + public void testConsumer() throws Exception { + if (username == null) { + return; + } + + getMockEndpoint("mock:result").expectedMinimumMessageCount(1); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + fromF("openshift:myApp?username=%s&password=%s&delay=5s", username, password) + .log("App ${body.name}") + .to("mock:result"); + } + }; + } +}