CAMEL-7982: camel-git - A generic git component, make git Consumer abstract


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

Branch: refs/heads/master
Commit: 8239d6f7cd21eea3763fad80024e8f7799244035
Parents: 8205bbe
Author: Andrea Cosentino <[email protected]>
Authored: Sat Jul 18 11:06:50 2015 +0200
Committer: Andrea Cosentino <[email protected]>
Committed: Sat Jul 18 11:08:15 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/component/git/GitConsumer.java |  65 ----
 .../apache/camel/component/git/GitEndpoint.java |  18 +-
 .../camel/component/git/GitOperation.java       |  33 --
 .../apache/camel/component/git/GitProducer.java | 334 -------------------
 .../git/consumer/AbstractGitConsumer.java       |  66 ++++
 .../git/consumer/GitCommitConsumer.java         |  36 ++
 .../camel/component/git/consumer/GitType.java   |  23 ++
 .../component/git/producer/GitOperation.java    |  33 ++
 .../component/git/producer/GitProducer.java     | 331 ++++++++++++++++++
 .../github/consumer/GitConsumerTest.java        |   4 +-
 10 files changed, 507 insertions(+), 436 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
deleted file mode 100644
index ac3a5f4..0000000
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
+++ /dev/null
@@ -1,65 +0,0 @@
-package org.apache.camel.component.git;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
-import org.apache.camel.impl.ScheduledPollConsumer;
-import org.eclipse.jgit.api.Git;
-import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-
-public class GitConsumer extends ScheduledPollConsumer {
-       
-       private GitEndpoint endpoint;
-       
-       private Repository repository;
-       
-       private Git git;
-       
-       private List used;
-
-       public GitConsumer(GitEndpoint endpoint, Processor processor) {
-               super(endpoint, processor);
-               this.endpoint = endpoint;
-               this.repository = getLocalRepository();
-               this.git = new Git(repository);
-               this.used = new ArrayList();
-       }
-
-       @Override
-       protected int poll() throws Exception {
-               int count = 0;
-               Iterable<RevCommit> commits = git.log().all().call();
-        for (RevCommit commit : commits) {
-               if (!used.contains(commit.getId())) {
-            Exchange e = getEndpoint().createExchange();
-            e.getOut().setBody(commit.getShortMessage());
-            getProcessor().process(e);
-            used.add(commit.getId());
-            count++;
-               }
-        }
-        return count;
-       }
-       
-    private Repository getLocalRepository(){
-        FileRepositoryBuilder builder = new FileRepositoryBuilder();
-        Repository repo = null;
-               try {
-                       repo = builder.setGitDir(new 
File(endpoint.getLocalPath(), ".git"))
-                               .readEnvironment() // scan environment GIT_* 
variables
-                               .findGitDir() // scan up the file system tree
-                               .build();
-               } catch (IOException e) {
-                       //LOG.error("There was an error, cannot open " + 
endpoint.getLocalPath() + " repository");
-                       e.printStackTrace();
-               }
-               return repo;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
index de09f95..247b60d 100644
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
@@ -19,6 +19,9 @@ package org.apache.camel.component.git;
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.component.git.consumer.GitCommitConsumer;
+import org.apache.camel.component.git.consumer.GitType;
+import org.apache.camel.component.git.producer.GitProducer;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriEndpoint;
@@ -30,8 +33,10 @@ public class GitEndpoint extends DefaultEndpoint {
 
     @UriPath @Metadata(required = "true")
     private String localPath;
-    @UriPath(label = "consumer")
+    @UriPath
     private String branchName;
+    @UriPath(label = "consumer")
+    private GitType type;
     @UriParam
     private String username;
     @UriParam
@@ -52,7 +57,8 @@ public class GitEndpoint extends DefaultEndpoint {
 
        @Override
        public Consumer createConsumer(Processor processor) throws Exception {
-        return new GitConsumer(this, processor);
+           if (type == GitType.COMMIT) return new GitCommitConsumer(this, 
processor);
+           else throw new IllegalArgumentException("Cannot create producer 
with type " + type);
        }
 
        @Override
@@ -108,4 +114,12 @@ public class GitEndpoint extends DefaultEndpoint {
        public void setOperation(String operation) {
                this.operation = operation;
        }
+
+    public GitType getType() {
+        return type;
+    }
+
+    public void setType(GitType type) {
+        this.type = type;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
deleted file mode 100644
index 8d7b69a..0000000
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * 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.git;
-
-public interface GitOperation {
-
-    public final static String CLONE_OPERATION = "clone";
-    public final static String INIT_OPERATION = "init";
-    public final static String ADD_OPERATION = "add";
-    public final static String REMOVE_OPERATION = "remove";
-    public final static String COMMIT_OPERATION = "commit";
-    public final static String COMMIT_ALL_OPERATION = "commitAll";
-    public final static String CREATE_BRANCH_OPERATION = "createBranch";
-    public final static String DELETE_BRANCH_OPERATION = "deleteBranch";
-    public final static String STATUS_OPERATION = "status";
-    public final static String LOG_OPERATION = "log";
-    public final static String PUSH_OPERATION = "push";
-    public final static String PULL_OPERATION = "pull";
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
deleted file mode 100644
index 42a55e6..0000000
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
+++ /dev/null
@@ -1,334 +0,0 @@
-package org.apache.camel.component.git;
-
-import java.io.File;
-import java.io.IOException;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.impl.DefaultProducer;
-import org.apache.camel.util.ObjectHelper;
-import org.eclipse.jgit.api.Git;
-import org.eclipse.jgit.api.PullResult;
-import org.eclipse.jgit.api.Status;
-import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-import org.eclipse.jgit.transport.PushResult;
-import org.eclipse.jgit.transport.RefSpec;
-import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class GitProducer extends DefaultProducer{
-
-    private static final Logger LOG = 
LoggerFactory.getLogger(GitProducer.class);
-    private final GitEndpoint endpoint;
-    
-       public GitProducer(GitEndpoint endpoint) {
-               super(endpoint);
-               this.endpoint = endpoint;
-       }
-
-       @Override
-       public void process(Exchange exchange) throws Exception {
-        String operation;      
-        Repository repo;
-           if (ObjectHelper.isEmpty(endpoint.getOperation())) {
-               operation = 
exchange.getIn().getHeader(GitConstants.GIT_OPERATION, String.class);
-           } else {
-               operation = endpoint.getOperation();
-           }
-       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
-               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
-       }
-       repo = getLocalRepository();
-           
-           switch (operation) {
-           case GitOperation.CLONE_OPERATION:
-               doClone(exchange, operation);
-               break;
-               
-           case GitOperation.INIT_OPERATION:
-               doInit(exchange, operation);
-               break;
-
-           case GitOperation.ADD_OPERATION:
-               doAdd(exchange, operation, repo);
-               break;
-               
-            case GitOperation.REMOVE_OPERATION:
-                doRemove(exchange, operation, repo);
-                break;
-               
-           case GitOperation.COMMIT_OPERATION:
-               doCommit(exchange, operation, repo);
-               break;
-           
-            case GitOperation.COMMIT_ALL_OPERATION:
-                doCommitAll(exchange, operation, repo);
-                break;
-                
-            case GitOperation.CREATE_BRANCH_OPERATION:
-                doCreateBranch(exchange, operation, repo);
-                break;
-                
-            case GitOperation.DELETE_BRANCH_OPERATION:
-                doDeleteBranch(exchange, operation, repo);
-                break;
-                
-            case GitOperation.STATUS_OPERATION:
-                doStatus(exchange, operation, repo);
-                break;
-                
-            case GitOperation.LOG_OPERATION:
-                doLog(exchange, operation, repo);
-                break;
-                
-            case GitOperation.PUSH_OPERATION:
-                doPush(exchange, operation, repo);
-                break;
-                            
-            case GitOperation.PULL_OPERATION:
-                doPull(exchange, operation, repo);
-                break;
-           }
-           repo.close();
-       }
-       
-    protected void doClone(Exchange exchange, String operation) {
-       Git result = null;
-       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
-               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
-       }
-       try {
-               File localRepo = new File(endpoint.getLocalPath(), "");
-               if (!localRepo.exists()) {
-                          result = 
Git.cloneRepository().setURI(endpoint.getRemotePath()).setDirectory(new 
File(endpoint.getLocalPath(),"")).call();
-               } else {
-               throw new IllegalArgumentException("The local repository 
directory already exists");
-               }
-               } catch (Exception e) {
-                       LOG.error("There was an error in Git " + operation + " 
operation");
-                       e.printStackTrace();
-               } finally {
-                       result.close();
-               }
-    }
-
-    protected void doInit(Exchange exchange, String operation) {
-       Git result = null;
-       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
-               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
-       }
-       try {
-                       result = Git.init().setDirectory(new 
File(endpoint.getLocalPath(),"")).setBare(false).call();
-               } catch (Exception e) {
-                       LOG.error("There was an error in Git " + operation + " 
operation");
-                       e.printStackTrace();
-               } finally {
-                       result.close();
-               }
-    }
-    
-    protected void doAdd(Exchange exchange, String operation, Repository repo) 
{
-       Git git = null;
-       String fileName = null;
-       if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME)))
 {
-               fileName = 
exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME, String.class);
-       } else {
-               throw new IllegalArgumentException("File name must be specified 
to execute " + operation);
-       }
-       try {
-               git = new Git(repo);
-                if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                    
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-                }
-                       git.add().addFilepattern(fileName).call();
-               } catch (Exception e) {
-                       LOG.error("There was an error in Git " + operation + " 
operation");
-                       e.printStackTrace();
-               }
-    }
-    
-    protected void doRemove(Exchange exchange, String operation, Repository 
repo) {
-        Git git = null;
-        String fileName = null;
-        if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME)))
 {
-                fileName = 
exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME, String.class);
-        } else {
-                throw new IllegalArgumentException("File name must be 
specified to execute " + operation);
-        }
-        try {
-                git = new Git(repo);
-                if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                    
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-                }
-                        git.rm().addFilepattern(fileName).call();
-                } catch (Exception e) {
-                        LOG.error("There was an error in Git " + operation + " 
operation");
-                        e.printStackTrace();
-                }
-    }
-    
-    protected void doCommit(Exchange exchange, String operation, Repository 
repo) {
-       Git git = null;
-       String commitMessage = null;
-       if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE)))
 {
-               commitMessage = 
exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE, String.class);
-       } else {
-               throw new IllegalArgumentException("Commit message must be 
specified to execute " + operation);
-       }
-       try {
-            git = new Git(repo);
-            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-            }
-               git.commit().setMessage(commitMessage).call();
-               } catch (Exception e) {
-                       LOG.error("There was an error in Git " + operation + " 
operation");
-                       e.printStackTrace();
-               }
-    }
-    
-    protected void doCommitAll(Exchange exchange, String operation, Repository 
repo) {
-        Git git = null;
-        String commitMessage = null;
-        if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE)))
 {
-                commitMessage = 
exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE, String.class);
-        } else {
-                throw new IllegalArgumentException("Commit message must be 
specified to execute " + operation);
-        }
-        try {
-            git = new Git(repo);
-            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-            }
-                git.commit().setAll(true).setMessage(commitMessage).call();
-                } catch (Exception e) {
-                        LOG.error("There was an error in Git " + operation + " 
operation");
-                        e.printStackTrace();
-                }
-    }
-    
-    protected void doCreateBranch(Exchange exchange, String operation, 
Repository repo) {
-        Git git = null;
-        if (ObjectHelper.isEmpty(endpoint.getBranchName())) {
-            throw new IllegalArgumentException("Branch Name must be specified 
to execute " + operation);
-        } 
-        try {
-            git = new Git(repo);
-            git.branchCreate().setName(endpoint.getBranchName()).call();
-        } catch (Exception e) {
-            LOG.error("There was an error in Git " + operation + " operation");
-            e.printStackTrace();
-        }
-    }
-    
-    protected void doDeleteBranch(Exchange exchange, String operation, 
Repository repo) {
-        Git git = null;
-        if (ObjectHelper.isEmpty(endpoint.getBranchName())) {
-            throw new IllegalArgumentException("Branch Name must be specified 
to execute " + operation);
-        } 
-        try {
-            git = new Git(repo);
-            git.branchDelete().setBranchNames(endpoint.getBranchName()).call();
-        } catch (Exception e) {
-            LOG.error("There was an error in Git " + operation + " operation");
-            e.printStackTrace();
-        }
-    }
-    
-    protected void doStatus(Exchange exchange, String operation, Repository 
repo) {
-        Git git = null;
-        Status status = null;
-        try {
-            git = new Git(repo);
-            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-            }
-                status = git.status().call();
-                } catch (Exception e) {
-                        LOG.error("There was an error in Git " + operation + " 
operation");
-                        e.printStackTrace();
-                }
-        exchange.getOut().setBody(status);
-    }
-    
-    protected void doLog(Exchange exchange, String operation, Repository repo) 
{
-        Git git = null;
-        Iterable<RevCommit> revCommit = null;
-        try {
-            git = new Git(repo);
-            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-            }
-                revCommit = git.log().call();
-                } catch (Exception e) {
-                        LOG.error("There was an error in Git " + operation + " 
operation");
-                        e.printStackTrace();
-                }
-        exchange.getOut().setBody(revCommit);
-    }
-    
-    protected void doPush(Exchange exchange, String operation, Repository 
repo) {
-        Git git = null;
-        Iterable<PushResult> result = null;
-        try {
-            git = new Git(repo);
-            if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
-                throw new IllegalArgumentException("Remote path must be 
specified to execute " + operation);
-            } 
-            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-            } 
-            if (ObjectHelper.isNotEmpty(endpoint.getUsername()) && 
ObjectHelper.isNotEmpty(endpoint.getPassword())) {
-                UsernamePasswordCredentialsProvider credentials = new 
UsernamePasswordCredentialsProvider(endpoint.getUsername(), 
endpoint.getPassword());
-                result = 
git.push().setCredentialsProvider(credentials).setRemote(endpoint.getRemotePath()).call();
-            } else {
-                result = git.push().setRemote(endpoint.getRemotePath()).call();
-            }
-                } catch (Exception e) {
-                        LOG.error("There was an error in Git " + operation + " 
operation");
-                        e.printStackTrace();
-                }
-        exchange.getOut().setBody(result);
-    }
-    
-    protected void doPull(Exchange exchange, String operation, Repository 
repo) {
-        Git git = null;
-        PullResult result = null;
-        try {
-            git = new Git(repo);
-            if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
-                throw new IllegalArgumentException("Remote path must be 
specified to execute " + operation);
-            } 
-            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
-                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
-            } 
-            if (ObjectHelper.isNotEmpty(endpoint.getUsername()) && 
ObjectHelper.isNotEmpty(endpoint.getPassword())) {
-                UsernamePasswordCredentialsProvider credentials = new 
UsernamePasswordCredentialsProvider(endpoint.getUsername(), 
endpoint.getPassword());
-                result = 
git.pull().setCredentialsProvider(credentials).setRemote(endpoint.getRemotePath()).call();
-            } else {
-                result = git.pull().setRemote(endpoint.getRemotePath()).call();
-            }
-                } catch (Exception e) {
-                        LOG.error("There was an error in Git " + operation + " 
operation");
-                        e.printStackTrace();
-                }
-        exchange.getOut().setBody(result);
-    }
-    
-    private Repository getLocalRepository(){
-        FileRepositoryBuilder builder = new FileRepositoryBuilder();
-        Repository repo = null;
-               try {
-                       repo = builder.setGitDir(new 
File(endpoint.getLocalPath(), ".git"))
-                               .readEnvironment() // scan environment GIT_* 
variables
-                               .findGitDir() // scan up the file system tree
-                               .build();
-               } catch (IOException e) {
-                       LOG.error("There was an error, cannot open " + 
endpoint.getLocalPath() + " repository");
-                       e.printStackTrace();
-               }
-               return repo;
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java
new file mode 100644
index 0000000..5ceec5d
--- /dev/null
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java
@@ -0,0 +1,66 @@
+package org.apache.camel.component.git.consumer;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.camel.Processor;
+import org.apache.camel.component.git.GitEndpoint;
+import org.apache.camel.impl.ScheduledPollConsumer;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+public abstract class AbstractGitConsumer extends ScheduledPollConsumer {
+    
+    private final GitEndpoint endpoint;
+    
+    private Repository repo;
+    
+    private Git git;
+
+    public AbstractGitConsumer(GitEndpoint endpoint, Processor processor) {
+        super(endpoint, processor);
+        this.endpoint = endpoint;
+        this.repo = getLocalRepository();
+        this.git = new Git(repo);
+    }
+    
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        this.repo = getLocalRepository();
+        this.git = new Git(repo);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        repo.close();
+        git.close();
+    }
+
+    private Repository getLocalRepository(){
+        FileRepositoryBuilder builder = new FileRepositoryBuilder();
+        Repository repo = null;
+                try {
+                        repo = builder.setGitDir(new 
File(endpoint.getLocalPath(), ".git"))
+                                .readEnvironment() // scan environment GIT_* 
variables
+                                .findGitDir() // scan up the file system tree
+                                .build();
+                } catch (IOException e) {
+                        //LOG.error("There was an error, cannot open " + 
endpoint.getLocalPath() + " repository");
+                        e.printStackTrace();
+                }
+                return repo;
+    }
+    
+    protected Repository getRepository() {
+        return repo;
+    }
+    
+    protected Git getGit() {
+        return git;
+    }
+    
+    protected abstract int poll() throws Exception;
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java
new file mode 100644
index 0000000..d289c26
--- /dev/null
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java
@@ -0,0 +1,36 @@
+package org.apache.camel.component.git.consumer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.git.GitEndpoint;
+import org.eclipse.jgit.revwalk.RevCommit;
+
+public class GitCommitConsumer extends AbstractGitConsumer {
+       
+       private List used;
+
+       public GitCommitConsumer(GitEndpoint endpoint, Processor processor) {
+               super(endpoint, processor);
+               this.used = new ArrayList();
+       }
+
+       @Override
+       protected int poll() throws Exception {
+               int count = 0;
+               Iterable<RevCommit> commits = getGit().log().all().call();
+        for (RevCommit commit : commits) {
+               if (!used.contains(commit.getId())) {
+            Exchange e = getEndpoint().createExchange();
+            e.getOut().setBody(commit.getShortMessage());
+            getProcessor().process(e);
+            used.add(commit.getId());
+            count++;
+               }
+        }
+        return count;
+       }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java
new file mode 100644
index 0000000..af3729e
--- /dev/null
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java
@@ -0,0 +1,23 @@
+/**
+ * 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.git.consumer;
+
+public enum GitType {
+
+    COMMIT
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
new file mode 100644
index 0000000..de03fc7
--- /dev/null
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
@@ -0,0 +1,33 @@
+/**
+ * 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.git.producer;
+
+public interface GitOperation {
+
+    public final static String CLONE_OPERATION = "clone";
+    public final static String INIT_OPERATION = "init";
+    public final static String ADD_OPERATION = "add";
+    public final static String REMOVE_OPERATION = "remove";
+    public final static String COMMIT_OPERATION = "commit";
+    public final static String COMMIT_ALL_OPERATION = "commitAll";
+    public final static String CREATE_BRANCH_OPERATION = "createBranch";
+    public final static String DELETE_BRANCH_OPERATION = "deleteBranch";
+    public final static String STATUS_OPERATION = "status";
+    public final static String LOG_OPERATION = "log";
+    public final static String PUSH_OPERATION = "push";
+    public final static String PULL_OPERATION = "pull";
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
new file mode 100644
index 0000000..b9d88fe
--- /dev/null
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
@@ -0,0 +1,331 @@
+package org.apache.camel.component.git.producer;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.git.GitConstants;
+import org.apache.camel.component.git.GitEndpoint;
+import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.PullResult;
+import org.eclipse.jgit.api.Status;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.jgit.transport.PushResult;
+import org.eclipse.jgit.transport.RefSpec;
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class GitProducer extends DefaultProducer{
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(GitProducer.class);
+    private final GitEndpoint endpoint;
+    
+    private Repository repo;
+    
+    private Git git;
+    
+       public GitProducer(GitEndpoint endpoint) {
+               super(endpoint);
+               this.endpoint = endpoint;
+       }
+       
+           @Override
+           protected void doStart() throws Exception {
+               super.doStart();
+               this.repo = getLocalRepository();
+               this.git = new Git(repo);
+           }
+
+           @Override
+           protected void doStop() throws Exception {
+               super.doStop();
+               repo.close();
+               git.close();
+           }
+
+       @Override
+       public void process(Exchange exchange) throws Exception {
+        String operation;      
+           if (ObjectHelper.isEmpty(endpoint.getOperation())) {
+               operation = 
exchange.getIn().getHeader(GitConstants.GIT_OPERATION, String.class);
+           } else {
+               operation = endpoint.getOperation();
+           }
+       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
+               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
+       }
+           
+           switch (operation) {
+           case GitOperation.CLONE_OPERATION:
+               doClone(exchange, operation);
+               break;
+               
+           case GitOperation.INIT_OPERATION:
+               doInit(exchange, operation);
+               break;
+
+           case GitOperation.ADD_OPERATION:
+               doAdd(exchange, operation);
+               break;
+               
+            case GitOperation.REMOVE_OPERATION:
+                doRemove(exchange, operation);
+                break;
+               
+           case GitOperation.COMMIT_OPERATION:
+               doCommit(exchange, operation);
+               break;
+           
+            case GitOperation.COMMIT_ALL_OPERATION:
+                doCommitAll(exchange, operation);
+                break;
+                
+            case GitOperation.CREATE_BRANCH_OPERATION:
+                doCreateBranch(exchange, operation);
+                break;
+                
+            case GitOperation.DELETE_BRANCH_OPERATION:
+                doDeleteBranch(exchange, operation);
+                break;
+                
+            case GitOperation.STATUS_OPERATION:
+                doStatus(exchange, operation);
+                break;
+                
+            case GitOperation.LOG_OPERATION:
+                doLog(exchange, operation);
+                break;
+                
+            case GitOperation.PUSH_OPERATION:
+                doPush(exchange, operation);
+                break;
+                            
+            case GitOperation.PULL_OPERATION:
+                doPull(exchange, operation);
+                break;
+           }
+       }
+       
+    protected void doClone(Exchange exchange, String operation) throws 
Exception {
+       Git result = null;
+       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
+               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
+       }
+       try {
+               File localRepo = new File(endpoint.getLocalPath(), "");
+               if (!localRepo.exists()) {
+                          result = 
Git.cloneRepository().setURI(endpoint.getRemotePath()).setDirectory(new 
File(endpoint.getLocalPath(),"")).call();
+               } else {
+               throw new IllegalArgumentException("The local repository 
directory already exists");
+               }
+               } catch (Exception e) {
+                       LOG.error("There was an error in Git " + operation + " 
operation");
+                       throw e;
+               } finally {
+                       result.close();
+               }
+    }
+
+    protected void doInit(Exchange exchange, String operation) throws 
Exception {
+       Git result = null;
+       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
+               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
+       }
+       try {
+                       result = Git.init().setDirectory(new 
File(endpoint.getLocalPath(),"")).setBare(false).call();
+               } catch (Exception e) {
+                       LOG.error("There was an error in Git " + operation + " 
operation");
+                       throw e;
+               } finally {
+                       result.close();
+               }
+    }
+    
+    protected void doAdd(Exchange exchange, String operation) throws Exception 
{
+       String fileName = null;
+       if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME)))
 {
+               fileName = 
exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME, String.class);
+       } else {
+               throw new IllegalArgumentException("File name must be specified 
to execute " + operation);
+       }
+       try {
+                if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                    
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+                }
+                       git.add().addFilepattern(fileName).call();
+               } catch (Exception e) {
+                       LOG.error("There was an error in Git " + operation + " 
operation");
+                       throw e;
+               }
+    }
+    
+    protected void doRemove(Exchange exchange, String operation) throws 
Exception {
+        String fileName = null;
+        if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME)))
 {
+                fileName = 
exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME, String.class);
+        } else {
+                throw new IllegalArgumentException("File name must be 
specified to execute " + operation);
+        }
+        try {
+                if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                    
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+                }
+                        git.rm().addFilepattern(fileName).call();
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " 
operation");
+                        throw e;
+                }
+    }
+    
+    protected void doCommit(Exchange exchange, String operation) throws 
Exception {
+       String commitMessage = null;
+       if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE)))
 {
+               commitMessage = 
exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE, String.class);
+       } else {
+               throw new IllegalArgumentException("Commit message must be 
specified to execute " + operation);
+       }
+       try {
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+               git.commit().setMessage(commitMessage).call();
+               } catch (Exception e) {
+                       LOG.error("There was an error in Git " + operation + " 
operation");
+                       throw e;
+               }
+    }
+    
+    protected void doCommitAll(Exchange exchange, String operation) throws 
Exception {
+        String commitMessage = null;
+        if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE)))
 {
+                commitMessage = 
exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE, String.class);
+        } else {
+                throw new IllegalArgumentException("Commit message must be 
specified to execute " + operation);
+        }
+        try {
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+                git.commit().setAll(true).setMessage(commitMessage).call();
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " 
operation");
+                        throw e;
+                }
+    }
+    
+    protected void doCreateBranch(Exchange exchange, String operation) throws 
Exception {
+        if (ObjectHelper.isEmpty(endpoint.getBranchName())) {
+            throw new IllegalArgumentException("Branch Name must be specified 
to execute " + operation);
+        } 
+        try {
+            git.branchCreate().setName(endpoint.getBranchName()).call();
+        } catch (Exception e) {
+            LOG.error("There was an error in Git " + operation + " operation");
+            throw e;
+        }
+    }
+    
+    protected void doDeleteBranch(Exchange exchange, String operation) throws 
Exception {
+        if (ObjectHelper.isEmpty(endpoint.getBranchName())) {
+            throw new IllegalArgumentException("Branch Name must be specified 
to execute " + operation);
+        } 
+        try {
+            git.branchDelete().setBranchNames(endpoint.getBranchName()).call();
+        } catch (Exception e) {
+            LOG.error("There was an error in Git " + operation + " operation");
+            throw e;
+        }
+    }
+    
+    protected void doStatus(Exchange exchange, String operation) throws 
Exception {
+        Status status = null;
+        try {
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+                status = git.status().call();
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " 
operation");
+                        throw e;
+                }
+        exchange.getOut().setBody(status);
+    }
+    
+    protected void doLog(Exchange exchange, String operation) throws Exception 
{
+        Iterable<RevCommit> revCommit = null;
+        try {
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+                revCommit = git.log().call();
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " 
operation");
+                        throw e;
+                }
+        exchange.getOut().setBody(revCommit);
+    }
+    
+    protected void doPush(Exchange exchange, String operation) throws 
Exception {
+        Iterable<PushResult> result = null;
+        try {
+            if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
+                throw new IllegalArgumentException("Remote path must be 
specified to execute " + operation);
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getUsername()) && 
ObjectHelper.isNotEmpty(endpoint.getPassword())) {
+                UsernamePasswordCredentialsProvider credentials = new 
UsernamePasswordCredentialsProvider(endpoint.getUsername(), 
endpoint.getPassword());
+                result = 
git.push().setCredentialsProvider(credentials).setRemote(endpoint.getRemotePath()).call();
+            } else {
+                result = git.push().setRemote(endpoint.getRemotePath()).call();
+            }
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " 
operation");
+                        throw e;
+                }
+        exchange.getOut().setBody(result);
+    }
+    
+    protected void doPull(Exchange exchange, String operation) throws 
Exception {
+        PullResult result = null;
+        try {
+            if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
+                throw new IllegalArgumentException("Remote path must be 
specified to execute " + operation);
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getUsername()) && 
ObjectHelper.isNotEmpty(endpoint.getPassword())) {
+                UsernamePasswordCredentialsProvider credentials = new 
UsernamePasswordCredentialsProvider(endpoint.getUsername(), 
endpoint.getPassword());
+                result = 
git.pull().setCredentialsProvider(credentials).setRemote(endpoint.getRemotePath()).call();
+            } else {
+                result = git.pull().setRemote(endpoint.getRemotePath()).call();
+            }
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " 
operation");
+                        throw e;
+                }
+        exchange.getOut().setBody(result);
+    }
+    
+    private Repository getLocalRepository(){
+        FileRepositoryBuilder builder = new FileRepositoryBuilder();
+        Repository repo = null;
+               try {
+                       repo = builder.setGitDir(new 
File(endpoint.getLocalPath(), ".git"))
+                               .readEnvironment() // scan environment GIT_* 
variables
+                               .findGitDir() // scan up the file system tree
+                               .build();
+               } catch (IOException e) {
+                       LOG.error("There was an error, cannot open " + 
endpoint.getLocalPath() + " repository");
+                       e.printStackTrace();
+               }
+               return repo;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8239d6f7/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
 
b/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
index 6071d23..d45591b 100644
--- 
a/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
+++ 
b/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
@@ -33,7 +33,7 @@ import org.junit.Test;
 public class GitConsumerTest extends GitTestSupport {
     
     @Test
-    public void commitTest() throws Exception {
+    public void commitConsumerTest() throws Exception {
 
        Repository repository = getTestRepository();
         MockEndpoint added = getMockEndpoint("mock:result");
@@ -116,7 +116,7 @@ public class GitConsumerTest extends GitTestSupport {
                         .to("git://" + GIT_LOCAL_REPO + "?operation=add");
                 from("direct:commit")
                         .to("git://" + GIT_LOCAL_REPO + "?operation=commit");
-                from("git://" + GIT_LOCAL_REPO)
+                from("git://" + GIT_LOCAL_REPO + "?type=commit")
                         .to("mock:result");
             } 
         };

Reply via email to