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 java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.bam.ProcessBuilder;
024    import org.apache.camel.bam.model.ActivityDefinition;
025    import org.apache.camel.bam.model.ActivityState;
026    import org.apache.camel.bam.model.ProcessInstance;
027    import org.apache.camel.impl.ServiceSupport;
028    import org.apache.camel.util.ServiceHelper;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
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            // lets always query for it, to avoid issues with refreshing before a commit etc
076            return builder.findOrCreateActivityDefinition(activityName);
077        }
078    
079        public void setActivityDefinition(ActivityDefinition activityDefinition) {
080            this.activityDefinition = activityDefinition;
081        }
082    
083        public int getExpectedMessages() {
084            return expectedMessages;
085        }
086    
087        public void setExpectedMessages(int expectedMessages) {
088            this.expectedMessages = expectedMessages;
089        }
090    
091        public ProcessRules getProcessRules() {
092            return processRules;
093        }
094    
095        public void setActivityName(String activityName) {
096            this.activityName = activityName;
097        }
098    
099        // Implementation methods
100        //-------------------------------------------------------------------------
101        protected void doStart() throws Exception {
102            ServiceHelper.startServices(rules);
103        }
104    
105        protected void doStop() throws Exception {
106            ServiceHelper.stopServices(rules);
107        }
108    }