Coverage Report - org.apache.camel.bam.ProcessBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessBuilder
85% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.bam;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.camel.Endpoint;
 23  
 import org.apache.camel.Processor;
 24  
 import org.apache.camel.Route;
 25  
 import org.apache.camel.bam.model.ActivityDefinition;
 26  
 import org.apache.camel.bam.model.ProcessDefinition;
 27  
 import org.apache.camel.bam.model.ProcessInstance;
 28  
 import org.apache.camel.bam.processor.ActivityMonitorEngine;
 29  
 import org.apache.camel.bam.processor.JpaBamProcessor;
 30  
 import org.apache.camel.bam.rules.ProcessRules;
 31  
 import org.apache.camel.builder.RouteBuilder;
 32  
 
 33  
 import org.springframework.orm.jpa.JpaTemplate;
 34  
 import org.springframework.transaction.TransactionStatus;
 35  
 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
 36  
 import org.springframework.transaction.support.TransactionTemplate;
 37  
 
 38  
 import static org.apache.camel.util.ObjectHelper.notNull;
 39  
 
 40  
 /**
 41  
  * A builder of a process definition
 42  
  * 
 43  
  * @version $Revision: $
 44  
  */
 45  4
 public abstract class ProcessBuilder extends RouteBuilder {
 46  
     private static int processCounter;
 47  
     private JpaTemplate jpaTemplate;
 48  
     private final TransactionTemplate transactionTemplate;
 49  
     private final String processName;
 50  2
     private List<ActivityBuilder> activityBuilders = new ArrayList<ActivityBuilder>();
 51  2
     private Class entityType = ProcessInstance.class;
 52  2
     private ProcessRules processRules = new ProcessRules();
 53  
     private ProcessDefinition processDefinition;
 54  
 
 55  
     protected ProcessBuilder(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate) {
 56  2
         this(jpaTemplate, transactionTemplate, createProcessName());
 57  2
     }
 58  
 
 59  2
     protected ProcessBuilder(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate, String processName) {
 60  2
         this.jpaTemplate = jpaTemplate;
 61  2
         this.transactionTemplate = transactionTemplate;
 62  2
         this.processName = processName;
 63  2
     }
 64  
 
 65  
     protected static synchronized String createProcessName() {
 66  2
         return "Process-" + (++processCounter);
 67  
     }
 68  
 
 69  
     public ActivityBuilder activity(String endpointUri) {
 70  4
         return activity(endpoint(endpointUri));
 71  
     }
 72  
 
 73  
     public ActivityBuilder activity(Endpoint endpoint) {
 74  4
         ActivityBuilder answer = new ActivityBuilder(this, endpoint);
 75  4
         activityBuilders.add(answer);
 76  4
         return answer;
 77  
     }
 78  
 
 79  
     /**
 80  
      * Sets the process entity type used to perform state management
 81  
      */
 82  
     public ProcessBuilder entityType(Class entityType) {
 83  0
         this.entityType = entityType;
 84  0
         return this;
 85  
     }
 86  
 
 87  
     public Processor createActivityProcessor(ActivityBuilder activityBuilder) {
 88  4
         notNull(jpaTemplate, "jpaTemplate");
 89  4
         transactionTemplate.execute(new TransactionCallbackWithoutResult() {
 90  4
             protected void doInTransactionWithoutResult(TransactionStatus status) {
 91  4
                 processRules.setProcessDefinition(getProcessDefinition());
 92  4
             }
 93  
         });
 94  4
         return new JpaBamProcessor(getTransactionTemplate(), getJpaTemplate(), activityBuilder.getCorrelationExpression(), activityBuilder.getActivityRules(), getEntityType());
 95  
     }
 96  
 
 97  
     // Properties
 98  
     // -----------------------------------------------------------------------
 99  
     public List<ActivityBuilder> getActivityBuilders() {
 100  0
         return activityBuilders;
 101  
     }
 102  
 
 103  
     public Class getEntityType() {
 104  4
         return entityType;
 105  
     }
 106  
 
 107  
     public JpaTemplate getJpaTemplate() {
 108  6
         return jpaTemplate;
 109  
     }
 110  
 
 111  
     public void setJpaTemplate(JpaTemplate jpaTemplate) {
 112  0
         this.jpaTemplate = jpaTemplate;
 113  0
     }
 114  
 
 115  
     public TransactionTemplate getTransactionTemplate() {
 116  6
         return transactionTemplate;
 117  
     }
 118  
 
 119  
     public ProcessRules getProcessRules() {
 120  6
         return processRules;
 121  
     }
 122  
 
 123  
     public String getProcessName() {
 124  0
         return processName;
 125  
     }
 126  
 
 127  
     public ProcessDefinition getProcessDefinition() {
 128  26
         if (processDefinition == null) {
 129  2
             processDefinition = findOrCreateProcessDefinition();
 130  
         }
 131  26
         return processDefinition;
 132  
     }
 133  
 
 134  
     public void setProcessDefinition(ProcessDefinition processDefinition) {
 135  0
         this.processDefinition = processDefinition;
 136  0
     }
 137  
 
 138  
     // Implementation methods
 139  
     // -------------------------------------------------------------------------
 140  
     protected void populateRoutes(List<Route> routes) throws Exception {
 141  2
         boolean first = true;
 142  2
         for (ActivityBuilder builder : activityBuilders) {
 143  4
             Route route = builder.createRoute();
 144  4
             if (first) {
 145  2
                 route.getServices().add(new ActivityMonitorEngine(getJpaTemplate(), getTransactionTemplate(), getProcessRules()));
 146  2
                 first = false;
 147  
             }
 148  4
             routes.add(route);
 149  4
         }
 150  2
     }
 151  
 
 152  
     // Implementation methods
 153  
     // -------------------------------------------------------------------------
 154  
     public ActivityDefinition findOrCreateActivityDefinition(String activityName) {
 155  22
         ProcessDefinition definition = getProcessDefinition();
 156  22
         List<ActivityDefinition> list = jpaTemplate.find("select x from " + ActivityDefinition.class.getName() + " x where x.processDefinition = ?1 and x.name = ?2", definition, activityName);
 157  22
         if (!list.isEmpty()) {
 158  18
             return list.get(0);
 159  
         } else {
 160  4
             ActivityDefinition answer = new ActivityDefinition();
 161  4
             answer.setName(activityName);
 162  4
             answer.setProcessDefinition(ProcessDefinition.getRefreshedProcessDefinition(jpaTemplate, definition));
 163  4
             jpaTemplate.persist(answer);
 164  4
             return answer;
 165  
         }
 166  
     }
 167  
 
 168  
     protected ProcessDefinition findOrCreateProcessDefinition() {
 169  2
         List<ProcessDefinition> list = jpaTemplate.find("select x from " + ProcessDefinition.class.getName() + " x where x.name = ?1", processName);
 170  2
         if (!list.isEmpty()) {
 171  0
             return list.get(0);
 172  
         } else {
 173  2
             ProcessDefinition answer = new ProcessDefinition();
 174  2
             answer.setName(processName);
 175  2
             jpaTemplate.persist(answer);
 176  2
             return answer;
 177  
         }
 178  
     }
 179  
 }