Author: davsclaus Date: Thu Feb 26 15:01:10 2009 New Revision: 748170 URL: http://svn.apache.org/viewvc?rev=748170&view=rev Log: CAMEL-1373: CamelCase keys and polished code.
Added: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisConstants.java (with props) Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisComponent.java camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisPollingConsumer.java camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisProducer.java camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueueTest.java camel/trunk/components/camel-ibatis/src/test/resources/org/apache/camel/component/ibatis/Account.xml Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisComponent.java?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisComponent.java (original) +++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisComponent.java Thu Feb 26 15:01:10 2009 @@ -23,12 +23,9 @@ import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; -import org.apache.camel.Endpoint; import org.apache.camel.component.ResourceBasedComponent; -import org.apache.camel.impl.DefaultComponent; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /** @@ -77,6 +74,22 @@ this.sqlMapClient = sqlMapClient; } + /** + * Creates an IbatisEndpoint for use by an IbatisConsumer or IbatisProducer. + */ + @Override + protected IBatisEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { + IBatisEndpoint answer = new IBatisEndpoint(uri, this, remaining); + setProperties(answer, parameters); + return answer; + } + + private SqlMapClient createSqlMapClient() throws IOException { + Resource resource = resolveMandatoryResource(sqlMapConfig); + InputStream is = resource.getInputStream(); + return SqlMapClientBuilder.buildSqlMapClient(new InputStreamReader(is)); + } + // Properties //------------------------------------------------------------------------- @@ -96,7 +109,6 @@ /** * Sets the SqlMapClient - * @param sqlMapClient The client */ public void setSqlMapClient(SqlMapClient sqlMapClient) { this.sqlMapClient = sqlMapClient; @@ -104,26 +116,11 @@ /** * The Spring uri of the SqlMapConfig - * @return java.lang.String */ public String getSqlMapConfig() { return sqlMapConfig; } - /** - * Creates an IbatisEndpoint for use by an IbatisConsumer or IbatisProducer. - */ - @Override - protected IBatisEndpoint createEndpoint(String uri, String remaining, Map params) throws Exception { - return new IBatisEndpoint(uri, this, remaining, params); - } - - private SqlMapClient createSqlMapClient() throws IOException { - Resource resource = resolveMandatoryResource(sqlMapConfig); - InputStream is = resource.getInputStream(); - return SqlMapClientBuilder.buildSqlMapClient(new InputStreamReader(is)); - } - public boolean isUseTransactions() { return useTransactions; } Added: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisConstants.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisConstants.java?rev=748170&view=auto ============================================================================== --- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisConstants.java (added) +++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisConstants.java Thu Feb 26 15:01:10 2009 @@ -0,0 +1,29 @@ +/** + * 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.ibatis; + +/** + * IBatis Constants + */ +public final class IBatisConstants { + + public static final String IBATIS_STATEMENT_NAME = "CamelIBatisStatementName"; + + private IBatisConstants() { + // Utility class + } +} Propchange: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisConstants.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisConstants.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java (original) +++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisEndpoint.java Thu Feb 26 15:01:10 2009 @@ -17,23 +17,13 @@ package org.apache.camel.component.ibatis; import java.io.IOException; -import java.sql.SQLException; -import java.util.List; -import java.util.Map; import com.ibatis.sqlmap.client.SqlMapClient; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.PollingConsumer; import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.component.ibatis.strategy.DefaultIBatisProcessingStategy; import org.apache.camel.component.ibatis.strategy.IBatisProcessingStrategy; import org.apache.camel.impl.DefaultPollingEndpoint; -import org.apache.camel.util.ObjectHelper; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; /** * An <a href="http://camel.apache.org/ibatis.html>iBatis Endpoint</a> @@ -42,33 +32,15 @@ * @version $Revision$ */ public class IBatisEndpoint extends DefaultPollingEndpoint { - private static final transient Log LOG = LogFactory.getLog(IBatisEndpoint.class); - private IBatisProcessingStrategy strategy; - /** - * Indicates if transactions are necessary. Defaulted in IBatisComponent. - */ private boolean useTransactions; - /** - * Statement to run when polling or processing - */ private String statement; - /** - * Name of a strategy to use for dealing w/ - * polling a database and consuming the message. Can be a bean name - * or a class name. - */ - private String consumeStrategyName; - /** - * URI parameters - */ - private Map params; - public IBatisEndpoint(String uri, IBatisComponent component, - String statement, Map params) throws Exception { + public IBatisEndpoint() { + } + public IBatisEndpoint(String uri, IBatisComponent component, String statement) throws Exception { super(uri, component); - this.params = params; setUseTransactions(component.isUseTransactions()); setStatement(statement); } @@ -92,37 +64,30 @@ configureConsumer(consumer); return consumer; } -/* - @Override - public PollingConsumer<Exchange> createPollingConsumer() throws Exception { - return new IBatisPollingConsumer(this); - } -*/ + /** - * @return SqlMapClient - * @throws IOException if the component is configured with a SqlMapConfig - * and there is a problem reading the file + * Gets the iBatis SqlMapClient */ public SqlMapClient getSqlMapClient() throws IOException { return getComponent().getSqlMapClient(); } /** - * Gets the IbatisProcessingStrategy to to use when consuming messages+ * from the database - * @return IbatisProcessingStrategy - * @throws Exception + * Gets the IbatisProcessingStrategy to to use when consuming messages from the database */ public IBatisProcessingStrategy getProcessingStrategy() throws Exception { if (strategy == null) { - String strategyName = (String) params.get("consumeStrategy"); - strategy = getStrategy(strategyName, new DefaultIBatisProcessingStategy()); + strategy = new DefaultIBatisProcessingStategy(); } return strategy; } + public void setStrategy(IBatisProcessingStrategy strategy) { + this.strategy = strategy; + } + /** * Statement to run when polling or processing - * @return name of the statement */ public String getStatement() { return statement; @@ -130,45 +95,14 @@ /** * Statement to run when polling or processing - * @param statement */ public void setStatement(String statement) { this.statement = statement; } /** - * Resolves a strategy in the camelContext or by class name - * @param name - * @param defaultStrategy - * @return IbatisProcessingStrategy - * @throws Exception - */ - private IBatisProcessingStrategy getStrategy(String name, IBatisProcessingStrategy defaultStrategy) throws Exception { - - if (name == null) { - return defaultStrategy; - } - - IBatisProcessingStrategy strategy = getComponent().getCamelContext().getRegistry().lookup(name, IBatisProcessingStrategy.class); - if (strategy == null) { - try { - Class<?> clazz = ObjectHelper.loadClass(name); - if (clazz != null) { - strategy = ObjectHelper.newInstance(clazz, IBatisProcessingStrategy.class); - } - } catch (Exception e) { - LOG.error("Failed to resolve/create processing strategy (" + name + ")", e); - throw e; - } - } - - return strategy != null ? strategy : defaultStrategy; - } - - /** * Indicates if transactions should be used when calling statements. Useful if using a comma separated list when * consuming records. - * @return boolean */ public boolean isUseTransactions() { return useTransactions; @@ -176,17 +110,9 @@ /** * Sets indicator to use transactions for consuming and error handling statements. - * @param useTransactions */ public void setUseTransactions(boolean useTransactions) { this.useTransactions = useTransactions; } - public String getConsumeStrategyName() { - return consumeStrategyName; - } - - public void setConsumeStrategyName(String consumeStrategyName) { - this.consumeStrategyName = consumeStrategyName; - } } Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisPollingConsumer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisPollingConsumer.java?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisPollingConsumer.java (original) +++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisPollingConsumer.java Thu Feb 26 15:01:10 2009 @@ -28,7 +28,6 @@ import org.apache.commons.logging.LogFactory; /** - * @version $Revision$ * <pre> * Ibatis Camel Component used to read data from a database. * @@ -91,14 +90,16 @@ * </tr> * <tbody> </table> * - * @see strategy.IBatisProcessingStrategy + * @see org.apache.camel.component.ibatis.strategy.IBatisProcessingStrategy */ public class IBatisPollingConsumer extends ScheduledPollConsumer { - private static Log logger = LogFactory.getLog(IBatisPollingConsumer.class); + private static final Log LOG = LogFactory.getLog(IBatisPollingConsumer.class); + /** * Statement to run after data has been processed in the route */ private String onConsume; + /** * Process resultset individually or as a list */ @@ -118,6 +119,9 @@ @Override protected void poll() throws Exception { IBatisEndpoint endpoint = getEndpoint(); + if (LOG.isTraceEnabled()) { + LOG.trace("Polling: " + endpoint); + } List data = endpoint.getProcessingStrategy().poll(this, getEndpoint()); if (useIterator) { for (Object object : data) { @@ -131,11 +135,9 @@ } /** - * delivers the content + * Delivers the content * - * @param data - * a single row object if useIterator=true otherwise the entire - * result set + * @param data a single row object if useIterator=true otherwise the entire result set */ protected void process(final Object data) throws Exception { final IBatisEndpoint endpoint = getEndpoint(); @@ -143,9 +145,11 @@ Message msg = exchange.getIn(); msg.setBody(data); - msg.setHeader("org.apache.camel.ibatis.queryName", endpoint.getStatement()); + msg.setHeader(IBatisConstants.IBATIS_STATEMENT_NAME, endpoint.getStatement()); - logger.debug("Setting message"); + if (LOG.isDebugEnabled()) { + LOG.debug("Processing exchange: " + exchange); + } getAsyncProcessor().process(exchange, new AsyncCallback() { public void done(boolean sync) { @@ -161,16 +165,16 @@ } /** - * Gets the statement to run after successful processing - * @return Name of the statement + * Gets the statement(s) to run after successful processing. + * Use comma to separate multiple statements. */ public String getOnConsume() { return onConsume; } /** - * Sets the statement to run after successful processing - * @param onConsume The name of the statement + * Sets the statement to run after successful processing. + * Use comma to separate multiple statements. */ public void setOnConsume(String onConsume) { this.onConsume = onConsume; @@ -179,7 +183,6 @@ /** * Indicates how resultset should be delivered to the route - * @return boolean */ public boolean isUseIterator() { return useIterator; @@ -189,7 +192,6 @@ * Sets how resultset should be delivered to route. * Indicates delivery as either a list or individual object. * defaults to true. - * @param useIterator */ public void setUseIterator(boolean useIterator) { this.useIterator = useIterator; Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisProducer.java?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisProducer.java (original) +++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/IBatisProducer.java Thu Feb 26 15:01:10 2009 @@ -21,28 +21,25 @@ import com.ibatis.sqlmap.client.SqlMapClient; -import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.impl.DefaultProducer; import org.apache.camel.util.ObjectHelper; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * @version $Revision$ */ public class IBatisProducer extends DefaultProducer { + private static final Log LOG = LogFactory.getLog(IBatisProducer.class); private String statement; private IBatisEndpoint endpoint; public IBatisProducer(IBatisEndpoint endpoint) { super(endpoint); - statement = endpoint.getStatement(); this.endpoint = endpoint; - } - - @Override - public Endpoint getEndpoint() { - return (IBatisEndpoint) super.getEndpoint(); + this.statement = endpoint.getStatement(); } /** @@ -54,14 +51,21 @@ if (body == null) { // must be a poll so lets do a query Message msg = exchange.getOut(true); + if (LOG.isTraceEnabled()) { + LOG.trace("Querying for list: " + statement); + } List list = client.queryForList(statement); msg.setBody(list); - msg.setHeader("org.apache.camel.ibatis.queryName", statement); + msg.setHeader(IBatisConstants.IBATIS_STATEMENT_NAME, statement); } else { // lets handle arrays or collections of objects Iterator iter = ObjectHelper.createIterator(body); while (iter.hasNext()) { - client.insert(statement, iter.next()); + Object value = iter.next(); + if (LOG.isTraceEnabled()) { + LOG.trace("Inserting: " + value + " using statement: " + statement); + } + client.insert(statement, value); } } } Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java (original) +++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/DefaultIBatisProcessingStategy.java Thu Feb 26 15:01:10 2009 @@ -29,16 +29,11 @@ * Default strategy for consuming messages for a route */ public class DefaultIBatisProcessingStategy implements IBatisProcessingStrategy { - /** - * Calls update on the SqlMapClient using the consumeStatement. - * Will call multiple statements if the consumeStatement is a comma separated list. - * The parameter passed to the statement is the original data delivered to the route. - */ - public void commit(IBatisEndpoint endpoint, Exchange exchange, Object data, String consumeStatement) throws Exception { + public void commit(IBatisEndpoint endpoint, Exchange exchange, Object data, String consumeStatements) throws Exception { SqlMapClient client = endpoint.getSqlMapClient(); boolean useTrans = endpoint.isUseTransactions(); - String[] statements = consumeStatement.split(","); + String[] statements = consumeStatements.split(","); try { if (useTrans) { client.startTransaction(Connection.TRANSACTION_REPEATABLE_READ); Modified: camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java (original) +++ camel/trunk/components/camel-ibatis/src/main/java/org/apache/camel/component/ibatis/strategy/IBatisProcessingStrategy.java Thu Feb 26 15:01:10 2009 @@ -23,7 +23,7 @@ import org.apache.camel.component.ibatis.IBatisPollingConsumer; /** - * Processing strategy for dealing with IBatis records + * Processing strategy for dealing with IBatis. */ public interface IBatisProcessingStrategy { @@ -33,19 +33,18 @@ * @param consumer The Ibatis Polling Consumer * @param endpoint The Ibatis Endpoint * @return Results of the query as a java.util.List - * @throws Exception + * @throws Exception can be thrown in case of error */ List poll(IBatisPollingConsumer consumer, IBatisEndpoint endpoint) throws Exception; /** - * Called if there is a statement to be run after processing + * Commit callback if there are a statements to be run after processing. * * @param endpoint The Ibatis Enpoint * @param exchange The exchange after it has been processed * @param data The original data delivered to the route - * @param consumeStatement The update statement to run - * @throws Exception + * @param consumeStatements Name of the statement(s) to run, will use SQL update. Use comma to provide multiple statements to run. + * @throws Exception can be thrown in case of error */ - void commit(IBatisEndpoint endpoint, Exchange exchange, Object data, String consumeStatement) - throws Exception; + void commit(IBatisEndpoint endpoint, Exchange exchange, Object data, String consumeStatements) throws Exception; } Modified: camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueueTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueueTest.java?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueueTest.java (original) +++ camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueueTest.java Thu Feb 26 15:01:10 2009 @@ -69,7 +69,9 @@ protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { + // START SNIPPET: e1 from("ibatis:selectUnprocessedAccounts?consumer.onConsume=consumeAccount").to("mock:results"); + // END SNIPPET: e1 from("direct:start").to("ibatis:insertAccount"); } Modified: camel/trunk/components/camel-ibatis/src/test/resources/org/apache/camel/component/ibatis/Account.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ibatis/src/test/resources/org/apache/camel/component/ibatis/Account.xml?rev=748170&r1=748169&r2=748170&view=diff ============================================================================== --- camel/trunk/components/camel-ibatis/src/test/resources/org/apache/camel/component/ibatis/Account.xml (original) +++ camel/trunk/components/camel-ibatis/src/test/resources/org/apache/camel/component/ibatis/Account.xml Thu Feb 26 15:01:10 2009 @@ -80,16 +80,20 @@ delete from ACCOUNT where ACC_ID = #id# </delete> + <!-- START SNIPPET: e1 --> <select id="selectUnprocessedAccounts" resultMap="AccountResult"> select * from ACCOUNT where PROCESSED = false </select> - + <!-- END SNIPPET: e1 --> + <select id="selectProcessedAccounts" resultMap="AccountResult"> select * from ACCOUNT where PROCESSED = true </select> - + + <!-- START SNIPPET: e2 --> <update id="consumeAccount" parameterClass="Account"> update ACCOUNT set PROCESSED = true where ACC_ID = #id# </update> + <!-- END SNIPPET: e2 --> </sqlMap> \ No newline at end of file