View Javadoc

1   package org.apache.turbine.om;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.turbine.services.pull.ApplicationTool;
23  import org.apache.turbine.util.pool.Recyclable;
24  
25  /***
26   * A Pull tool to make om objects available to a template
27   *
28   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
29   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
30   * @version $Id: OMTool.java 278824 2005-09-05 20:01:15Z henning $
31   */
32  public class OMTool implements ApplicationTool, Recyclable
33  {
34      // private RunData data;
35      private HashMap omMap;
36  
37      // note the following could be a static attribute to reduce memory
38      // footprint. Might require a service to front load the
39      // PullHelpers to avoid MT issues. A multiple write is not so bad
40      // though
41  
42      /*** The cache of PullHelpers. **/
43      private static Map pullMap = new HashMap();
44  
45      /***
46       *  The Factory responsible for retrieving the
47       *  objects from storage
48       */
49      private RetrieverFactory omFactory;
50  
51      public OMTool()throws Exception
52      {
53          omMap = new HashMap();
54          // String className = Turbine.getConfiguration()
55          //         .getString("tool.om.factory");
56          //         RetrieverFactory omFactory =
57          //             (RetrieverFactory)Class.forName(className).newInstance();
58      }
59  
60      /***
61       * Prepares tool for a single request
62       */
63      public void init(Object runData)
64      {
65          // data = (RunData)runData;
66      }
67  
68      /***
69       * Implementation of ApplicationTool interface is not needed for this
70       * method as the tool is request scoped
71       */
72      public void refresh()
73      {
74          // empty
75      }
76  
77      /***
78       * Inner class to present a nice interface to the template designer
79       */
80      private class PullHelper
81      {
82          String omName;
83  
84          private PullHelper(String omName)
85          {
86              this.omName = omName;
87          }
88  
89          public Object setKey(String key)
90              throws Exception
91          {
92              Object om = null;
93  
94              String inputKey = omName + key;
95              if (omMap.containsKey(inputKey))
96              {
97                  om = omMap.get(inputKey);
98              }
99              else
100             {
101                 om = omFactory.getInstance(omName).retrieve(key);
102                 omMap.put(inputKey, om);
103             }
104 
105             return om;
106         }
107     }
108 
109     public Object get(String omName) throws Exception
110     {
111         if (!pullMap.containsKey(omName))
112         {
113             // MT could overwrite a PullHelper, but that is not a problem
114             // should still synchronize to avoid two threads adding at
115             // same time
116             synchronized (this.getClass())
117             {
118                 pullMap.put(omName, new OMTool.PullHelper(omName));
119             }
120         }
121 
122         return pullMap.get(omName);
123     }
124 
125     public Object get(String omName, String key) throws Exception
126     {
127         return ((OMTool.PullHelper) get(omName)).setKey(key);
128     }
129 
130 
131     public String getName()
132     {
133         return "om";
134     }
135 
136 
137     // ****************** Recyclable implementation ************************
138 
139     private boolean disposed;
140 
141     /***
142      * Recycles the object for a new client. Recycle methods with
143      * parameters must be added to implementing object and they will be
144      * automatically called by pool implementations when the object is
145      * taken from the pool for a new client. The parameters must
146      * correspond to the parameters of the constructors of the object.
147      * For new objects, constructors can call their corresponding recycle
148      * methods whenever applicable.
149      * The recycle methods must call their super.
150      */
151     public void recycle()
152     {
153         disposed = false;
154     }
155 
156     /***
157      * Disposes the object after use. The method is called
158      * when the object is returned to its pool.
159      * The dispose method must call its super.
160      */
161     public void dispose()
162     {
163         omMap.clear();
164         // data = null;
165         disposed = true;
166     }
167 
168     /***
169      * Checks whether the recyclable has been disposed.
170      * @return true, if the recyclable is disposed.
171      */
172     public boolean isDisposed()
173     {
174         return disposed;
175     }
176 }
177 
178 
179 
180 
181 
182 
183 
184