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.rules;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.bam.ProcessBuilder;
021    import org.apache.camel.bam.model.ActivityDefinition;
022    import org.apache.camel.bam.model.ActivityState;
023    import org.apache.camel.bam.model.ProcessInstance;
024    import org.apache.camel.impl.ServiceSupport;
025    import org.apache.camel.util.ServiceHelper;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * Represents a activity which is typically a system or could be an endpoint
034     *
035     * @version $Revision: $
036     */
037    public class ActivityRules extends ServiceSupport {
038        private static final transient Log log = LogFactory.getLog(ActivityRules.class);
039        private int expectedMessages = 1;
040        private ProcessRules processRules;
041        private List<TemporalRule> rules = new ArrayList<TemporalRule>();
042        private ActivityDefinition activityDefinition;
043        private String activityName;
044        private final org.apache.camel.bam.ProcessBuilder builder;
045    
046        public ActivityRules(ProcessBuilder builder) {
047            this.builder = builder;
048            this.processRules = builder.getProcessRules();
049            processRules.getActivities().add(this);
050        }
051    
052        public void addRule(TemporalRule rule) {
053            rules.add(rule);
054        }
055    
056        /**
057         * Handles overdue activities
058         */
059        public void processExpired(ActivityState activityState) throws Exception {
060            for (TemporalRule rule : rules) {
061                rule.processExpired(activityState);
062            }
063        }
064    
065        public void processExchange(Exchange exchange, ProcessInstance process) {
066            for (TemporalRule rule : rules) {
067                rule.processExchange(exchange, process);
068            }
069        }
070    
071        // Properties
072        //-------------------------------------------------------------------------
073    
074        public ActivityDefinition getActivityDefinition() {
075            if (activityDefinition == null) {
076                activityDefinition = builder.findOrCreateActivityDefinition(activityName);
077            }
078            return activityDefinition;
079        }
080    
081        public void setActivityDefinition(ActivityDefinition activityDefinition) {
082            this.activityDefinition = activityDefinition;
083        }
084    
085        public int getExpectedMessages() {
086            return expectedMessages;
087        }
088    
089        public void setExpectedMessages(int expectedMessages) {
090            this.expectedMessages = expectedMessages;
091        }
092    
093        public ProcessRules getProcessRules() {
094            return processRules;
095        }
096    
097        public void setActivityName(String activityName) {
098            this.activityName = activityName;
099        }
100    
101        // Implementation methods
102        //-------------------------------------------------------------------------
103        protected void doStart() throws Exception {
104            ServiceHelper.startServices(rules);
105        }
106    
107        protected void doStop() throws Exception {
108            ServiceHelper.stopServices(rules);
109        }
110    }