View Javadoc
1   package org.apache.turbine.om;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ConcurrentMap;
24  
25  import org.apache.fulcrum.pool.Recyclable;
26  import org.apache.turbine.Turbine;
27  import org.apache.turbine.services.pull.ApplicationTool;
28  
29  /**
30   * A Pull tool to make om objects available to a template
31   *
32   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
33   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34   * @version $Id$
35   *
36   * @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.
37   */
38  @Deprecated
39  public class OMTool implements ApplicationTool, Recyclable
40  {
41      protected ConcurrentMap<String, Object> omMap;
42  
43      // note the following could be a static attribute to reduce memory
44      // footprint. Might require a service to front load the
45      // PullHelpers to avoid MT issues. A multiple write is not so bad
46      // though
47  
48      /** The cache of PullHelpers. **/
49      private ConcurrentMap<String, OMTool.PullHelper> pullMap =
50              new ConcurrentHashMap<>();
51  
52      /**
53       *  The Factory responsible for retrieving the
54       *  objects from storage
55       */
56      protected RetrieverFactory omFactory;
57  
58      /**
59       * Default constructor
60       * @throws Exception if creating the factory fails
61       */
62      public OMTool() throws Exception
63      {
64          omMap = new ConcurrentHashMap<>();
65          String className = Turbine.getConfiguration().getString("tool.om.factory");
66          this.omFactory = (RetrieverFactory)Class.forName(className).newInstance();
67      }
68  
69      /**
70       * Prepares tool for a single request
71       *
72       * @param data the initialization data
73       */
74      @Override
75      public void init(Object data)
76      {
77          // data = (RunData)data;
78      }
79  
80      /**
81       * Implementation of ApplicationTool interface is not needed for this
82       * method as the tool is request scoped
83       */
84      @Override
85      public void refresh()
86      {
87          // empty
88      }
89  
90      /**
91       * Inner class to present a nice interface to the template designer
92       */
93      protected class PullHelper
94      {
95          String omName;
96  
97          protected PullHelper(String omName)
98          {
99              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 }