001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.bam;
018
019 import java.util.Date;
020
021 import org.apache.camel.Endpoint;
022 import org.apache.camel.Expression;
023 import org.apache.camel.Processor;
024 import org.apache.camel.Route;
025 import org.apache.camel.bam.model.ActivityState;
026 import org.apache.camel.bam.model.ProcessInstance;
027 import org.apache.camel.bam.rules.ActivityRules;
028 import org.apache.camel.builder.ProcessorFactory;
029 import org.apache.camel.impl.EventDrivenConsumerRoute;
030
031 /**
032 * @version $Revision: $
033 */
034 public class ActivityBuilder implements ProcessorFactory {
035 private ProcessBuilder processBuilder;
036 private Endpoint endpoint;
037 private ActivityRules activityRules;
038 private Expression correlationExpression;
039
040 public ActivityBuilder(ProcessBuilder processBuilder, Endpoint endpoint) {
041 this.processBuilder = processBuilder;
042 this.endpoint = endpoint;
043 this.activityRules = new ActivityRules(processBuilder);
044 this.activityRules.setActivityName(endpoint.getEndpointUri());
045 }
046
047 public Endpoint getEndpoint() {
048 return endpoint;
049 }
050
051 public Processor createProcessor() throws Exception {
052 return processBuilder.createActivityProcessor(this);
053 }
054
055 public Route createRoute() throws Exception {
056 Processor processor = createProcessor();
057 if (processor == null) {
058 throw new IllegalArgumentException("No processor created for ActivityBuilder: " + this);
059 }
060 return new EventDrivenConsumerRoute(getEndpoint(), processor);
061 }
062
063 // Builder methods
064 //-----------------------------------------------------------------------
065 public ActivityBuilder correlate(Expression correlationExpression) {
066 this.correlationExpression = correlationExpression;
067 return this;
068 }
069
070 public ActivityBuilder name(String name) {
071 activityRules.setActivityName(name);
072 return this;
073 }
074
075 /**
076 * Create a temporal rule for when this step starts
077 */
078 public TimeExpression starts() {
079 return new TimeExpression(this, ActivityLifecycle.Started) {
080 public Date evaluate(ProcessInstance instance, ActivityState state) {
081 return state.getTimeStarted();
082 }
083 };
084 }
085
086 /**
087 * Create a temporal rule for when this step completes
088 */
089 public TimeExpression completes() {
090 return new TimeExpression(this, ActivityLifecycle.Completed) {
091 public Date evaluate(ProcessInstance instance, ActivityState state) {
092 return state.getTimeCompleted();
093 }
094 };
095 }
096
097 // Properties
098 //-----------------------------------------------------------------------
099 public Expression getCorrelationExpression() {
100 return correlationExpression;
101 }
102
103 public ActivityRules getActivityRules() {
104 return activityRules;
105 }
106
107 public ProcessBuilder getProcessBuilder() {
108 return processBuilder;
109 }
110 }