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