    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 1812628 2017-10-19 12:34:25Z gk $
    035 *
    036 * @deprecated This class is probably not used anymore, it may have been intended for cacheable Torque OM or might be used with Fulcrum Security Torque.
    037 */
    038@Deprecated
    039public class OMTool implements ApplicationTool, Recyclable
    040{
    041    protected ConcurrentMap<String, Object> omMap;
    042
    043    // note the following could be a static attribute to reduce memory
    044    // footprint. Might require a service to front load the
    045    // PullHelpers to avoid MT issues. A multiple write is not so bad
    046    // though
    047
    048    /** The cache of PullHelpers. **/
    049    private ConcurrentMap<String, OMTool.PullHelper> pullMap =
    050            new ConcurrentHashMap<String, OMTool.PullHelper>();
    051
    052    /**
    053     *  The Factory responsible for retrieving the
    054     *  objects from storage
    055     */
    056    protected RetrieverFactory omFactory;
    057
    058    /**
    059     * Default constructor
    060     * @throws Exception if creating the factory fails
    061     */
    062    public OMTool() throws Exception
    063    {
    064        omMap = new ConcurrentHashMap<String, Object>();
    065        String className = Turbine.getConfiguration().getString("tool.om.factory");
    066        this.omFactory = (RetrieverFactory)Class.forName(className).newInstance();
    067    }
    068
    069    /**
    070     * Prepares tool for a single request
    071     *
    072     * @param data the initialization data
    073     */
    074    @Override
    075    public void init(Object data)
    076    {
    077        // data = (RunData)data;
    078    }
    079
    080    /**
    081     * Implementation of ApplicationTool interface is not needed for this
    082     * method as the tool is request scoped
    083     */
    084    @Override
    085    public void refresh()
    086    {
    087        // empty
    088    }
    089
    090    /**
    091     * Inner class to present a nice interface to the template designer
    092     */
    093    protected class PullHelper
    094    {
    095        String omName;
    096
    097        protected PullHelper(String omName)
    098        {
    099            this.omName = omName;
    100        }
    101
    102        public Object setKey(String key)
    103            throws Exception
    104        {
    105            Object om = null;
    106            String inputKey = omName + key;
    107
    108            if (omMap.containsKey(inputKey))
    109            {
    110                om = omMap.get(inputKey);
    111            }
    112            else
    113            {
    114                om = omFactory.getInstance(omName).retrieve(key);
    115                omMap.put(inputKey, om);
    116            }
    117
    118            return om;
    119        }
    120    }
    121
    122    /**
    123     * Get the {@link PullHelper} object with the given name
    124     * @param omName the object name
    125     * @return the PullHelper
    126     * @throws Exception if retrieving the object fails
    127     */
    128    public PullHelper get(String omName) throws Exception
    129    {
    130        PullHelper ph = pullMap.putIfAbsent(omName, new OMTool.PullHelper(omName));
    131        if (ph == null)
    132        {
    133            return pullMap.get(omName);
    134        }
    135
    136        return ph;
    137    }
    138
    139    /**
    140     * Get the object with the given name and key
    141     * @param omName the object name
    142     * @param key the object key
    143     * @return the object
    144     * @throws Exception if retrieving the object fails
    145     */
    146    public Object get(String omName, String key) throws Exception
    147    {
    148        return get(omName).setKey(key);
    149    }
    150
    151    // ****************** Recyclable implementation ************************
    152
    153    private boolean disposed;
    154
    155    /**
    156     * Recycles the object for a new client. Recycle methods with
    157     * parameters must be added to implementing object and they will be
    158     * automatically called by pool implementations when the object is
    159     * taken from the pool for a new client. The parameters must
    160     * correspond to the parameters of the constructors of the object.
    161     * For new objects, constructors can call their corresponding recycle
    162     * methods whenever applicable.
    163     * The recycle methods must call their super.
    164     */
    165    @Override
    166    public void recycle()
    167    {
    168        disposed = false;
    169    }
    170
    171    /**
    172     * Disposes the object after use. The method is called
    173     * when the object is returned to its pool.
    174     * The dispose method must call its super.
    175     */
    176    @Override
    177    public void dispose()
    178    {
    179        omMap.clear();
    180        disposed = true;
    181    }
    182
    183    /**
    184     * Checks whether the recyclable has been disposed.
    185     * @return true, if the recyclable is disposed.
    186     */
    187    @Override
    188    public boolean isDisposed()
    189    {
    190        return disposed;
    191    }
    192}