From e48637d0d7ee40224ccbafd24b71a1dc70751495 Mon Sep 17 00:00:00 2001 From: dvirgiln Date: Tue, 25 Nov 2014 16:12:25 +0100 Subject: [PATCH] SWITCHYARD-2433 Use consumer.delay and consumer.initialDelay instead of the internal timer in the SQL binding --- .../sql/deploy/CamelSqlInboundHandler.java | 18 +++-- .../sql/deploy/SQLCamelComponentMessages.java | 21 +++-- .../camel/sql/model/CamelSqlBindingModel.java | 46 ++++++----- .../model/CamelSqlConsumerBindingModel.java | 53 +++++++++++++ .../sql/model/v1/V1CamelSqlBindingModel.java | 79 ++++++++++++++++--- .../v1/V1CamelSqlConsumerBindingModel.java | 69 ++++++++++++++++ .../camel/sql/model/v2/camel-sql_2_0.xsd | 8 +- 7 files changed, 248 insertions(+), 46 deletions(-) create mode 100644 camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlConsumerBindingModel.java create mode 100644 camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlConsumerBindingModel.java diff --git a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/CamelSqlInboundHandler.java b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/CamelSqlInboundHandler.java index 765da4773..b4dd14d79 100644 --- a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/CamelSqlInboundHandler.java +++ b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/CamelSqlInboundHandler.java @@ -6,7 +6,7 @@ * 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, + * 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. @@ -16,6 +16,7 @@ import javax.xml.namespace.QName; import org.apache.camel.model.RouteDefinition; +import org.apache.commons.lang.StringUtils; import org.switchyard.ServiceDomain; import org.switchyard.common.camel.SwitchYardCamelContext; import org.switchyard.common.lang.Strings; @@ -33,7 +34,7 @@ public class CamelSqlInboundHandler extends InboundHandler /** * Sole constructor. - * + * * @param camelBindingModel The CamelBindingModel. * @param camelContext The camel context instance. * @param serviceName The target service name. @@ -49,15 +50,16 @@ protected RouteDefinition createRouteDefinition() { CamelSqlBindingModel bindingModel = getBindingModel(); QName serviceName = getServiceName(); - String period = bindingModel.getPeriod(); if (bindingModel.isServiceBinding()) { - if (Strings.trimToNull(period) == null) { - throw SQLCamelComponentMessages.MESSAGES.periodAttributeMandatory(); + if (bindingModel.getConsumer()!=null && bindingModel.getConsumer().getDelay()!=null && StringUtils.isNotEmpty(bindingModel.getPeriod())) { + throw SQLCamelComponentMessages.MESSAGES.multipleDelayDefined(); } - + if (bindingModel.getConsumer()!=null && bindingModel.getConsumer().getDelay()!=null && bindingModel.getInitialDelay()!=null) { + throw SQLCamelComponentMessages.MESSAGES.multipleInitialDelayDefined(); + } + RouteDefinition definition = new RouteDefinition(); - definition.routeId(getRouteId()) - .from(getBindingModel().getTimerURI(getRouteId()).toString()); + definition.routeId(getRouteId()).from(getComponentUri().toString()); return addTransactionPolicy(definition) .to(getBindingModel().getComponentURI().toString()) .setProperty(ExchangeCompletionEvent.GATEWAY_NAME).simple(getBindingModel().getName(), String.class) diff --git a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/SQLCamelComponentMessages.java b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/SQLCamelComponentMessages.java index 87675e80c..86232e336 100644 --- a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/SQLCamelComponentMessages.java +++ b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/deploy/SQLCamelComponentMessages.java @@ -6,7 +6,7 @@ * 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, + * 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. @@ -30,13 +30,22 @@ public interface SQLCamelComponentMessages { * The default messages. */ SQLCamelComponentMessages MESSAGES = Messages.getBundle(SQLCamelComponentMessages.class); - + + /** + * multipleInitialDelayDefined method definition. + * + * @return SwitchYardException + */ + @Message(id = 34000, value = "The initial delay has been defined twice.") + SwitchYardException multipleInitialDelayDefined(); + /** - * periodAttributeMandatory method definition. + * multipleDelayDefined method definition. + * * @return SwitchYardException */ - @Message(id = 34000, value = "Period attribute is mandatory for SQL service bindings") - SwitchYardException periodAttributeMandatory(); + @Message(id = 34001, value = "The delay has been defined twice.") + SwitchYardException multipleDelayDefined(); + - } diff --git a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlBindingModel.java b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlBindingModel.java index 63fab9d79..e0c3463cd 100644 --- a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlBindingModel.java +++ b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlBindingModel.java @@ -6,7 +6,7 @@ * 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, + * 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. @@ -22,14 +22,14 @@ public interface CamelSqlBindingModel extends CamelBindingModel { /** * Get SQL query to be executed. - * + * * @return SQL query. */ String getQuery(); /** * Specify sql query to execute. - * + * * @param query SQL query. * @return a reference to this Camel binding model */ @@ -37,14 +37,14 @@ public interface CamelSqlBindingModel extends CamelBindingModel { /** * Reference to a DataSource to look up in the registry. - * + * * @return Data source bean name. */ String getDataSourceRef(); /** * Specify data source bean name. - * + * * @param dataSourceRef Bean name. * @return a reference to this Camel binding model */ @@ -52,14 +52,14 @@ public interface CamelSqlBindingModel extends CamelBindingModel { /** * Execute SQL batch update statements. - * + * * @return True if jdbc batch update should be performed. */ Boolean isBatch(); /** * Turn on/off JDBC batching support. - * + * * @param batch Batch flag. * @return a reference to this Camel binding model */ @@ -67,16 +67,16 @@ public interface CamelSqlBindingModel extends CamelBindingModel { /** * Get placeholder character. - * + * * @return Paceholder character. */ String getPlaceholder(); /** - * Specifies a character that will be replaced to ? in SQL query. + * Specifies a character that will be replaced to ? in SQL query. * Notice, that it is simple String.replaceAll() operation and no SQL parsing is * involved (quoted strings will also change). - * + * * @param placeholder Placeholder in query. * @return a reference to this Camel binding model */ @@ -84,14 +84,14 @@ public interface CamelSqlBindingModel extends CamelBindingModel { /** * Period between polls. - * + * * @return Period between polls. */ String getPeriod(); /** * Specifies delays between pools. Possible values are long (millis) and string, eg: 1s, 1m. - * + * * @param period Period between polls. * @return a reference to this Camel binding model */ @@ -99,7 +99,7 @@ public interface CamelSqlBindingModel extends CamelBindingModel { /** * Specifies delay in millis before first poll. - * + * * @param initialDelay First poll delay. * @return a reference to this Camel binding model */ @@ -107,17 +107,25 @@ public interface CamelSqlBindingModel extends CamelBindingModel { /** * Get first poll delay. - * + * * @return First poll delay. */ Long getInitialDelay(); /** - * Returns timer uri used to call component uri. - * - * @param timerId Name of timer endpoint. - * @return Camel timer uri. + * The consumer's configurations. + * + * @return an instance of the camel sql consumer binding model */ - String getTimerURI(String timerId); + CamelSqlConsumerBindingModel getConsumer(); + + /** + * Specify the consumer binding model. + * + * @param consumer + * The consumer binding model + * @return a reference to this binding model + */ + CamelSqlBindingModel setConsumer(CamelSqlConsumerBindingModel consumer); } diff --git a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlConsumerBindingModel.java b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlConsumerBindingModel.java new file mode 100644 index 000000000..6d9de0ecf --- /dev/null +++ b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/CamelSqlConsumerBindingModel.java @@ -0,0 +1,53 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed 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.switchyard.component.camel.sql.model; + +/** + * Represents the consumer configuration settings for a SQL endpoint in Camel. + * + * @author David Virgil Naranjo + */ +public interface CamelSqlConsumerBindingModel { + /** + * Delay in milliseconds. + * + * @return Delay. + */ + Long getDelay(); + + /** + * Set the delay in milliseconds. + * + * @param delay + * Delay to use. + * @return a reference to this binding model + */ + CamelSqlConsumerBindingModel setDelay(Long delay); + + /** + * Initial delay in milliseconds. + * + * @return the initial delay. + */ + Long getInitialDelay(); + + /** + * Set the delay in milliseconds. + * + * @param delay + * Initial delay to use. + * @return a reference to this binding model + */ + CamelSqlConsumerBindingModel setInitialDelay(Long initialDelay); +} diff --git a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlBindingModel.java b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlBindingModel.java index d8216c161..09784c721 100644 --- a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlBindingModel.java +++ b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlBindingModel.java @@ -6,7 +6,7 @@ * 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, + * 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. @@ -17,14 +17,16 @@ import java.util.List; import org.apache.camel.util.UnsafeUriCharactersEncoder; +import org.apache.commons.lang.StringUtils; import org.switchyard.component.camel.common.QueryString; import org.switchyard.component.camel.common.model.v1.V1BaseCamelBindingModel; import org.switchyard.component.camel.sql.model.CamelSqlBindingModel; +import org.switchyard.component.camel.sql.model.CamelSqlConsumerBindingModel; import org.switchyard.config.Configuration; import org.switchyard.config.model.Descriptor; /** * Implementation of sql configuration binding. - * + * * @author Lukasz Dywicki */ public class V1CamelSqlBindingModel extends V1BaseCamelBindingModel @@ -43,6 +45,8 @@ public class V1CamelSqlBindingModel extends V1BaseCamelBindingModel private static final String PERIOD = "period"; private static final String INITIAL_DELAY = "initialDelay"; + private CamelSqlConsumerBindingModel _consume; + /** * Create a new CamelSqlBindingModel. * @param namespace namespace @@ -50,12 +54,12 @@ public class V1CamelSqlBindingModel extends V1BaseCamelBindingModel public V1CamelSqlBindingModel(String namespace) { super(SQL, namespace); - setModelChildrenOrder(QUERY, DATA_SOURCE_REF, BATCH, PLACEHOLDER); + setModelChildrenOrder(QUERY, DATA_SOURCE_REF, BATCH, PLACEHOLDER, CONSUME); } /** * Create a V1CamelSqlBindingModel from the specified configuration and descriptor. - * + * * @param config The switchyard configuration instance. * @param desc The switchyard descriptor instance. */ @@ -126,25 +130,76 @@ public Long getInitialDelay() { return value == null ? null : Long.valueOf(value); } - @Override - public String getTimerURI(String name) { - QueryString queryStr = new QueryString(); - queryStr.add("period", getPeriod()); - queryStr.add("delay", getInitialDelay()); - return UnsafeUriCharactersEncoder.encode("timer://" + name + queryStr); - } - @Override public URI getComponentURI() { + if (StringUtils.isNotBlank(getPeriod())) { + if (getConsumer() == null) { + _consume = new V1CamelSqlConsumerBindingModel(getModelConfiguration(), getModelDescriptor()); + } + _consume.setDelay(convertPeriodToLongMilliseconds(this.getPeriod())); + } + if (getInitialDelay() != null) { + if (getConsumer() == null) { + _consume = new V1CamelSqlConsumerBindingModel(getModelConfiguration(), getModelDescriptor()); + } + _consume.setInitialDelay(getInitialDelay()); + } Configuration modelConfiguration = getModelConfiguration(); List children = modelConfiguration.getChildren(); String baseUri = SQL + "://" + getQuery(); QueryString queryStr = new QueryString(); + traverseConfiguration(children, queryStr, QUERY); return URI.create(UnsafeUriCharactersEncoder.encode(baseUri + queryStr.toString())); } + @Override + public CamelSqlConsumerBindingModel getConsumer() { + if (_consume == null) { + Configuration config = getModelConfiguration().getFirstChild(CONSUME); + if (config != null) { + _consume = new V1CamelSqlConsumerBindingModel(config, getModelDescriptor()); + } + } + return _consume; + } + + @Override + public CamelSqlBindingModel setConsumer(CamelSqlConsumerBindingModel consumer) { + Configuration config = getModelConfiguration().getFirstChild(CONSUME); + if (config != null) { + // set an existing config value + getModelConfiguration().removeChildren(CONSUME); + getModelConfiguration().addChild(((V1CamelSqlConsumerBindingModel) consumer).getModelConfiguration()); + } else { + setChildModel((V1CamelSqlConsumerBindingModel) consumer); + } + _consume = consumer; + return this; + } + + private Long convertPeriodToLongMilliseconds(String period){ + if(isLong(period)){ + return new Long(period); + } + else if(period.endsWith("s")){ + if(isLong(period.substring(0,period.length()-1))){ + return new Long(period.substring(0, period.length() - 1) + "000"); + } + } + return null; + } + + private boolean isLong(String s) { + try { + Long.parseLong(s); + } catch (NumberFormatException e) { + return false; + } + // only got here if we didn't return false + return true; + } } diff --git a/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlConsumerBindingModel.java b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlConsumerBindingModel.java new file mode 100644 index 000000000..1fb8dbea6 --- /dev/null +++ b/camel/camel-sql/src/main/java/org/switchyard/component/camel/sql/model/v1/V1CamelSqlConsumerBindingModel.java @@ -0,0 +1,69 @@ +/* + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors. + * + * Licensed 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.switchyard.component.camel.sql.model.v1; + +import org.switchyard.component.camel.common.model.v1.V1BaseCamelModel; +import org.switchyard.component.camel.sql.model.CamelSqlConsumerBindingModel; +import org.switchyard.config.Configuration; +import org.switchyard.config.model.Descriptor; + +/** + * Implementation of sql consumer configuration binding. + * + * @author David Virgil Naranjo + */ +public class V1CamelSqlConsumerBindingModel extends V1BaseCamelModel implements CamelSqlConsumerBindingModel { + + private static final String DELAY = "consumer.delay"; + private static final String INITIAL_DELAY = "consumer.initialDelay"; + + protected V1CamelSqlConsumerBindingModel(Configuration config, Descriptor desc) { + super(config, desc); + setModelChildrenOrder(INITIAL_DELAY, DELAY); + } + + /** + * Creates new binding model. + * + * @param namespace + * namespace + */ + public V1CamelSqlConsumerBindingModel(String namespace) { + super(namespace, V1CamelSqlBindingModel.CONSUME); + } + + @Override + public Long getDelay() { + return getLongConfig(DELAY); + } + + @Override + public V1CamelSqlConsumerBindingModel setDelay(Long delay) { + return setConfig(DELAY, delay); + } + + @Override + public Long getInitialDelay() { + return getLongConfig(INITIAL_DELAY); + } + + @Override + public V1CamelSqlConsumerBindingModel setInitialDelay(Long initialDelay) { + return setConfig(INITIAL_DELAY, initialDelay); + } + + + + +} diff --git a/camel/camel-sql/src/main/resources/org/switchyard/component/camel/sql/model/v2/camel-sql_2_0.xsd b/camel/camel-sql/src/main/resources/org/switchyard/component/camel/sql/model/v2/camel-sql_2_0.xsd index 47b5030a8..539a5ee26 100644 --- a/camel/camel-sql/src/main/resources/org/switchyard/component/camel/sql/model/v2/camel-sql_2_0.xsd +++ b/camel/camel-sql/src/main/resources/org/switchyard/component/camel/sql/model/v2/camel-sql_2_0.xsd @@ -30,6 +30,7 @@ + @@ -37,5 +38,10 @@ - + + + + + +