001package org.apache.turbine.pipeline;
002
003import java.util.HashMap;
004import java.util.Map;
005
006import org.apache.turbine.services.TurbineServices;
007import org.apache.turbine.services.rundata.RunDataService;
008import org.apache.turbine.util.RunData;
009
010
011/*
012 * Licensed to the Apache Software Foundation (ASF) under one
013 * or more contributor license agreements.  See the NOTICE file
014 * distributed with this work for additional information
015 * regarding copyright ownership.  The ASF licenses this file
016 * to you under the Apache License, Version 2.0 (the
017 * "License"); you may not use this file except in compliance
018 * with the License.  You may obtain a copy of the License at
019 *
020 *   http://www.apache.org/licenses/LICENSE-2.0
021 *
022 * Unless required by applicable law or agreed to in writing,
023 * software distributed under the License is distributed on an
024 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
025 * KIND, either express or implied.  See the License for the
026 * specific language governing permissions and limitations
027 * under the License.
028 */
029
030
031/**
032 * <p>A <b>PipelineData</b> is a holder for data being passed from one
033 * Valve to the next.
034 * The detailed contract for a Valve is included in the description of
035 * the <code>invoke()</code> method below.</p>
036 *
037 * <b>HISTORICAL NOTE</b>:  The "PipelineData" name was assigned to this
038 * holder as it functions similarly to the RunData object, but without
039 * the additional methods
040 *
041 * @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
042 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
043 */
044public class DefaultPipelineData implements PipelineData
045{
046    private final Map<Class<?>, Map<Class<?>, ? super Object>> map =
047        new HashMap<>();
048
049    /**
050     * Put a configured map of objects into the pipeline data object
051     *
052     * @param key the key class
053     * @param value the value map
054     */
055    @Override
056    public void put(Class<?> key, Map<Class<?>, ? super Object> value)
057    {
058        map.put(key, value);
059    }
060
061    /**
062     * Get the configured map of objects for the given key
063     *
064     * @param key the key class
065     * @return the value map or null if no such key exists
066     */
067    @Override
068    public Map<Class<?>, ? super Object> get(Class<?> key)
069    {
070        return map.get(key);
071    }
072
073    /**
074     * Get a value from the configured map of objects for the given keys
075     *
076     * @param key the key class
077     * @param innerKey the key into the value map
078     * @return the inner value or null if no such keys exist
079     */
080    @Override
081    @SuppressWarnings("unchecked")
082    public <T> T get(Class<?> key, Class<T> innerKey)
083    {
084        Map<Class<?>, ? super Object> innerMap = get(key);
085        if (innerMap == null)
086        {
087            return null;
088        }
089        return (T) innerMap.get(innerKey);
090    }
091
092    /**
093     * Put object back into RunDataService for recycling
094     */
095    @Override
096    public void close() throws Exception
097    {
098        RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
099        if (rds != null)
100        {
101            rds.putRunData((RunData) this);
102        }
103    }
104}