camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [9/9] camel git commit: CAMEL-4725: Polished
Date Sat, 09 Jan 2016 10:44:52 GMT
CAMEL-4725: Polished


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e9f3f60d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e9f3f60d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e9f3f60d

Branch: refs/heads/master
Commit: e9f3f60d2a5d373b5e93c9a4ef4b50e71a2165ea
Parents: 83ef4e6
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Jan 9 11:42:29 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Jan 9 11:43:29 2016 +0100

----------------------------------------------------------------------
 .../sql/stored/SqlStoredComponent.java          | 11 ++-
 .../component/sql/stored/SqlStoredEndpoint.java | 82 ++++++++++++++++----
 .../component/sql/stored/SqlStoredProducer.java |  2 -
 .../template/TemplateStoredProcedure.java       |  5 ++
 .../TemplateStoredProcedureFactory.java         | 14 +++-
 .../sql/stored/template/ast/ParseHelper.java    |  2 +-
 .../template/ast/ParseRuntimeException.java     |  1 +
 7 files changed, 95 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e9f3f60d/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
index 7012186..3391450 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
@@ -21,6 +21,7 @@ import javax.sql.DataSource;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
+import org.apache.camel.component.sql.stored.template.TemplateStoredProcedureFactory;
 import org.apache.camel.impl.UriEndpointComponent;
 import org.apache.camel.util.CamelContextHelper;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -58,7 +59,14 @@ public class SqlStoredComponent extends UriEndpointComponent {
             throw new IllegalArgumentException("DataSource must be configured");
         }
 
-        return new SqlStoredEndpoint(new JdbcTemplate(target), remaining);
+        JdbcTemplate template = new JdbcTemplate(target);
+        TemplateStoredProcedureFactory factory = new TemplateStoredProcedureFactory(template);
+
+        SqlStoredEndpoint answer = new SqlStoredEndpoint(uri, this);
+        answer.setJdbcTemplate(template);
+        answer.setTemplate(remaining);
+        answer.setTemplateStoredProcedureFactory(factory);
+        return answer;
     }
 
     public DataSource getDataSource() {
@@ -71,4 +79,5 @@ public class SqlStoredComponent extends UriEndpointComponent {
     public void setDataSource(DataSource dataSource) {
         this.dataSource = dataSource;
     }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e9f3f60d/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
index 8ded03a..e65fa8b 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
@@ -16,46 +16,96 @@
  */
 package org.apache.camel.component.sql.stored;
 
+import org.apache.camel.Component;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.sql.stored.template.TemplateStoredProcedureFactory;
-import org.apache.camel.impl.DefaultPollingEndpoint;
+import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriPath;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ServiceHelper;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
 import org.springframework.jdbc.core.JdbcTemplate;
 
-@UriEndpoint(scheme = "sql-stored", title = "SQL stored", syntax = "sql-stored:template",
label = "database,sql")
-public class SqlStoredEndpoint extends DefaultPollingEndpoint {
+@UriEndpoint(scheme = "sql-stored", title = "SQL StoredProcedure", syntax = "sql-stored:template",
producerOnly = true, label = "database,sql")
+public class SqlStoredEndpoint extends DefaultEndpoint {
 
-    @UriPath(description = "Sets the stored procedure template to perform")
-    @Metadata(required = "true")
-    private String template;
-
-    private final TemplateStoredProcedureFactory templateStoredProcedureFactory;
-
-    private final JdbcTemplate jdbcTemplate;
+    private JdbcTemplate jdbcTemplate;
+    private TemplateStoredProcedureFactory templateStoredProcedureFactory;
 
+    @UriPath @Metadata(required = "true")
+    private String template;
 
-    public SqlStoredEndpoint(JdbcTemplate jdbcTemplate,
-                             String template) {
-        this.templateStoredProcedureFactory = new TemplateStoredProcedureFactory(jdbcTemplate);
-        this.jdbcTemplate = jdbcTemplate;
-        this.template = template;
+    public SqlStoredEndpoint(String endpointUri, Component component) {
+        super(endpointUri, component);
     }
 
     @Override
     public Producer createProducer() throws Exception {
+        ObjectHelper.notNull(template, "template");
+        ObjectHelper.notNull(templateStoredProcedureFactory, "templateStoredProcedureFactory");
+
         return new SqlStoredProducer(this, template, templateStoredProcedureFactory);
     }
 
     @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new UnsupportedOperationException("This component does not support consumer");
+    }
+
+    @Override
     public boolean isSingleton() {
-        return false;
+        return true;
     }
 
     @Override
     protected String createEndpointUri() {
         return "sql-stored:" + UnsafeUriCharactersEncoder.encode(template);
     }
+
+    @Override
+    protected void doStart() throws Exception {
+        ServiceHelper.startService(templateStoredProcedureFactory);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        ServiceHelper.stopService(templateStoredProcedureFactory);
+    }
+
+    public String getTemplate() {
+        return template;
+    }
+
+    /**
+     * The stored procedure template to perform
+     */
+    public void setTemplate(String template) {
+        this.template = template;
+    }
+
+    public TemplateStoredProcedureFactory getTemplateStoredProcedureFactory() {
+        return templateStoredProcedureFactory;
+    }
+
+    /**
+     * To use a custom instance of TemplateStoredProcedureFactory
+     */
+    public void setTemplateStoredProcedureFactory(TemplateStoredProcedureFactory templateStoredProcedureFactory)
{
+        this.templateStoredProcedureFactory = templateStoredProcedureFactory;
+    }
+
+    public JdbcTemplate getJdbcTemplate() {
+        return jdbcTemplate;
+    }
+
+    /**
+     * to use a custom instance of JdbcTemplate
+     */
+    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
+        this.jdbcTemplate = jdbcTemplate;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e9f3f60d/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
index 81ff287..4a7ccc4 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
@@ -24,12 +24,10 @@ import org.apache.camel.impl.DefaultProducer;
 
 public class SqlStoredProducer extends DefaultProducer {
 
-    private TemplateStoredProcedureFactory templateStoredProcedureFactory;
     private TemplateStoredProcedure defaultTemplateStoredProcedure;
 
     public SqlStoredProducer(Endpoint endpoint, String template, TemplateStoredProcedureFactory
templateStoredProcedureFactory) {
         super(endpoint);
-        this.templateStoredProcedureFactory = templateStoredProcedureFactory;
         this.defaultTemplateStoredProcedure = templateStoredProcedureFactory.createFromString(template);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/e9f3f60d/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
index e0821f5..180c1b2 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
@@ -23,6 +23,8 @@ import org.apache.camel.Exchange;
 import org.apache.camel.component.sql.stored.template.ast.InputParameter;
 import org.apache.camel.component.sql.stored.template.ast.OutParameter;
 import org.apache.camel.component.sql.stored.template.ast.Template;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.SqlOutParameter;
 import org.springframework.jdbc.core.SqlParameter;
@@ -30,6 +32,7 @@ import org.springframework.jdbc.object.StoredProcedure;
 
 public class TemplateStoredProcedure extends StoredProcedure {
 
+    private static final Logger LOG = LoggerFactory.getLogger(TemplateStoredProcedure.class);
     private final Template template;
 
     public TemplateStoredProcedure(JdbcTemplate jdbcTemplate, Template template) {
@@ -47,6 +50,7 @@ public class TemplateStoredProcedure extends StoredProcedure {
             setFunction(false);
         }
 
+        LOG.debug("Compiling stored procedure: {}", template.getProcedureName());
         compile();
     }
 
@@ -58,6 +62,7 @@ public class TemplateStoredProcedure extends StoredProcedure {
             params.put(inputParameter.getName(), inputParameter.getValueExpression().evaluate(exchange,
inputParameter.getJavaType()));
         }
 
+        LOG.debug("Invoking stored procedure: {}", template.getProcedureName());
         Map<String, Object> ret = super.execute(params);
 
         for (OutParameter out : template.getOutParameterList()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/e9f3f60d/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
index 5d5a329..f096dc8 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
@@ -22,14 +22,15 @@ import org.apache.camel.component.sql.stored.template.ast.ParseRuntimeException;
 import org.apache.camel.component.sql.stored.template.ast.Template;
 import org.apache.camel.component.sql.stored.template.generated.ParseException;
 import org.apache.camel.component.sql.stored.template.generated.SSPTParser;
+import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.util.LRUCache;
 import org.springframework.jdbc.core.JdbcTemplate;
 
-public class TemplateStoredProcedureFactory {
+public class TemplateStoredProcedureFactory extends ServiceSupport {
 
     public static final int TEMPLATE_CACHE_DEFAULT_SIZE = 200;
     private final JdbcTemplate jdbcTemplate;
-    private LRUCache<String, TemplateStoredProcedure> templateCache = new LRUCache<String,
TemplateStoredProcedure>(TEMPLATE_CACHE_DEFAULT_SIZE);
+    private final LRUCache<String, TemplateStoredProcedure> templateCache = new LRUCache<String,
TemplateStoredProcedure>(TEMPLATE_CACHE_DEFAULT_SIZE);
 
     public TemplateStoredProcedureFactory(JdbcTemplate jdbcTemplate) {
         this.jdbcTemplate = jdbcTemplate;
@@ -63,4 +64,13 @@ public class TemplateStoredProcedureFactory {
         return input;
     }
 
+    @Override
+    protected void doStart() throws Exception {
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        // clear cache when we are stopping
+        templateCache.clear();
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e9f3f60d/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseHelper.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseHelper.java
index 19417de..dd926ef 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseHelper.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseHelper.java
@@ -43,7 +43,7 @@ public final class ParseHelper {
     public static Class sqlTypeToJavaType(int sqlType, String sqlTypeStr) {
         //TODO: as rest of types.
         //TODO: add test for each type.
-        Class ret = null;
+        Class ret;
         switch (sqlType) {
         case Types.INTEGER:
             ret = Integer.class;

http://git-wip-us.apache.org/repos/asf/camel/blob/e9f3f60d/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseRuntimeException.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseRuntimeException.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseRuntimeException.java
index 2092afa..2fdea24 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseRuntimeException.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/ast/ParseRuntimeException.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.sql.stored.template.ast;
 
 public class ParseRuntimeException extends RuntimeException {
+
     public ParseRuntimeException(String message) {
         super(message);
     }


Mime
View raw message