Add a base Inmplementation for StageContext
-------------------------------------------

                 Key: SANDBOX-249
                 URL: https://issues.apache.org/jira/browse/SANDBOX-249
             Project: Commons Sandbox
          Issue Type: Improvement
          Components: Pipeline
            Reporter: Sheldon Robinson


/*
 * 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.commons.pipeline;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This class provide an instance inmplementation of StageContext and maybe
 * extended to provided additional functionality of included in other classes 
 * wishing to in the StageContext Interface
 *  
 * @author sheldon
 *
 */
public class BaseContext implements StageContext {
        
        // Map of DownstreamFeeder having the Stage as key
    private final Map<Stage, Feeder> feeders;
        
        // Map of pipeline branches where the keys are branch names.
    private final Map<String,Pipeline> branches;
        
    // The list of listeners registered with the pipeline.
    private final List<StageEventListener> listeners;
    
         // Global environment variables
    private Map<String,Object> env = Collections.synchronizedMap(new 
HashMap<String,Object>());
    
    public BaseContext(){
        this.feeders = new HashMap<Stage, Feeder>();
        this.branches = new HashMap<String,Pipeline>();
        this.listeners = Collections.synchronizedList(new 
ArrayList<StageEventListener>());
    }

        @Override
        public Feeder getBranchFeeder(String branch) {
        return branches.get(branch).getSourceFeeder();
        }
        
        /**
         * Add a branch [EMAIL PROTECTED] Pipeline} to the Stage
         * @param key a string allowing the look-up of a branch
         * @param branch a Pipeline added to Stage 
         * @return Pipeline The prior Pipeline corresponding to the key 
         */
        public Pipeline addBranch(String key, Pipeline branch){
                return branches.put(key,branch);
        }

        @Override
        public Feeder getDownstreamFeeder(Stage stage) {
                return feeders.get(stage)!= null?feeders.get(stage):Feeder.VOID;
        }
        
        public Feeder addDownstreamFeeder(Stage stage, Feeder feeder) throws 
IllegalArgumentException{
                if(stage != null)
                        return feeders.put(stage, feeder);
                else
                        throw new IllegalArgumentException("Attempted insert of 
null stage");
        }

        @Override
        public Object getEnv(String key) {
                return env.get(key);
        }
        
        /**
         * Sets a global environment variable for the stage.
         * @param key a string to look-up the an environment variable
         * @param value The Object to set for the variable 
         * @return Object The prior object corresponding to the key
         */
        public Object setEnv(String key, Object value){
                return env.put(key, value);
        }

        @Override
        public Collection<StageEventListener> getRegisteredListeners() {
                return listeners;
        }

        @Override
        public void raise(final EventObject ev) {
                for (final StageEventListener listener : listeners) {
                new Thread() {
                    public void run() {
                        listener.notify(ev);
                    }
                }.start();                      
                }


        }

        @Override
        public void registerListener(StageEventListener listener) {
                listeners.add(listener);

        }

}


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to