    001package org.apache.turbine.om;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022import java.util.concurrent.ConcurrentHashMap;
    023import java.util.concurrent.ConcurrentMap;
    024
    025import org.apache.fulcrum.pool.Recyclable;
    026import org.apache.turbine.Turbine;
    027import org.apache.turbine.services.pull.ApplicationTool;
    028
    029/**
    030 * A Pull tool to make om objects available to a template
    031 *
    032 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
    033 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    034 * @version $Id: OMTool.java 1706425 2015-10-02 14:47:51Z tv $
    035 */
    036public class OMTool implements ApplicationTool, Recyclable
    037{
    038    protected ConcurrentHashMap<String, Object> omMap;
    039
    040    // note the following could be a static attribute to reduce memory
    041    // footprint. Might require a service to front load the
    042    // PullHelpers to avoid MT issues. A multiple write is not so bad
    043    // though
    044
    045    /** The cache of PullHelpers. **/
    046    private ConcurrentMap<String, OMTool.PullHelper> pullMap =
    047            new ConcurrentHashMap<String, OMTool.PullHelper>();
    048
    049    /**
    050     *  The Factory responsible for retrieving the
    051     *  objects from storage
    052     */
    053    protected RetrieverFactory omFactory;
    054
    055    /**
    056     * Default constructor
    057     * @throws Exception if creating the factory fails
    058     */
    059    public OMTool() throws Exception
    060    {
    061        omMap = new ConcurrentHashMap<String, Object>();
    062        String className = Turbine.getConfiguration().getString("tool.om.factory");
    063        this.omFactory = (RetrieverFactory)Class.forName(className).newInstance();
    064    }
    065
    066    /**
    067     * Prepares tool for a single request
    068     *
    069     * @param data the initialization data
    070     */
    071    @Override
    072    public void init(Object data)
    073    {
    074        // data = (RunData)data;
    075    }
    076
    077    /**
    078     * Implementation of ApplicationTool interface is not needed for this
    079     * method as the tool is request scoped
    080     */
    081    @Override
    082    public void refresh()
    083    {
    084        // empty
    085    }
    086
    087    /**
    088     * Inner class to present a nice interface to the template designer
    089     */
    090    protected class PullHelper
    091    {
    092        String omName;
    093
    094        protected PullHelper(String omName)
    095        {
    096            this.omName = omName;
    097        }
    098
    099        public Object setKey(String key)
    100            throws Exception
    101        {
    102            Object om = null;
    103            String inputKey = omName + key;
    104
    105            if (omMap.containsKey(inputKey))
    106            {
    107                om = omMap.get(inputKey);
    108            }
    109            else
    110            {
    111                om = omFactory.getInstance(omName).retrieve(key);
    112                omMap.put(inputKey, om);
    113            }
    114
    115            return om;
    116        }
    117    }
    118
    119    /**
    120     * Get the {@link PullHelper} object with the given name
    121     * @param omName the object name
    122     * @return the PullHelper
    123     * @throws Exception if retrieving the object fails
    124     */
    125    public PullHelper get(String omName) throws Exception
    126    {
    127        PullHelper ph = pullMap.putIfAbsent(omName, new OMTool.PullHelper(omName));
    128        if (ph == null)
    129        {
    130            return pullMap.get(omName);
    131        }
    132
    133        return ph;
    134    }
    135
    136    /**
    137     * Get the object with the given name and key
    138     * @param omName the object name
    139     * @param key the object key
    140     * @return the object
    141     * @throws Exception if retrieving the object fails
    142     */
    143    public Object get(String omName, String key) throws Exception
    144    {
    145        return get(omName).setKey(key);
    146    }
    147
    148    // ****************** Recyclable implementation ************************
    149
    150    private boolean disposed;
    151
    152    /**
    153     * Recycles the object for a new client. Recycle methods with
    154     * parameters must be added to implementing object and they will be
    155     * automatically called by pool implementations when the object is
    156     * taken from the pool for a new client. The parameters must
    157     * correspond to the parameters of the constructors of the object.
    158     * For new objects, constructors can call their corresponding recycle
    159     * methods whenever applicable.
    160     * The recycle methods must call their super.
    161     */
    162    @Override
    163    public void recycle()
    164    {
    165        disposed = false;
    166    }
    167
    168    /**
    169     * Disposes the object after use. The method is called
    170     * when the object is returned to its pool.
    171     * The dispose method must call its super.
    172     */
    173    @Override
    174    public void dispose()
    175    {
    176        omMap.clear();
    177        disposed = true;
    178    }
    179
    180    /**
    181     * Checks whether the recyclable has been disposed.
    182     * @return true, if the recyclable is disposed.
    183     */
    184    @Override
    185    public boolean isDisposed()
    186    {
    187        return disposed;
    188    }
    189}