KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > om > OMTool


1 package org.apache.turbine.om;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.turbine.Turbine;
23 import org.apache.turbine.services.pull.ApplicationTool;
24 import org.apache.turbine.util.pool.Recyclable;
25
26 /**
27  * A Pull tool to make om objects available to a template
28  *
29  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
30  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
31  * @version $Id: OMTool.java,v 1.4.2.2 2004/05/20 03:05:19 seade Exp $
32  */

33 public class OMTool implements ApplicationTool, Recyclable
34 {
35     // private RunData data;
36
private HashMap JavaDoc omMap;
37
38     // note the following could be a static attribute to reduce memory
39
// footprint. Might require a service to front load the
40
// PullHelpers to avoid MT issues. A multiple write is not so bad
41
// though
42

43     /** The cache of PullHelpers. **/
44     private static Map JavaDoc pullMap = new HashMap JavaDoc();
45
46     /**
47      * The Factory responsible for retrieving the
48      * objects from storage
49      */

50     private RetrieverFactory omFactory;
51
52     public OMTool()throws Exception JavaDoc
53     {
54         omMap = new HashMap JavaDoc();
55         String JavaDoc className = Turbine.getConfiguration()
56                 .getString("tool.om.factory");
57         // RetrieverFactory omFactory =
58
// (RetrieverFactory)Class.forName(className).newInstance();
59
}
60
61     /**
62      * Prepares tool for a single request
63      */

64     public void init(Object JavaDoc runData)
65     {
66         // data = (RunData)runData;
67
}
68
69     /**
70      * Implementation of ApplicationTool interface is not needed for this
71      * method as the tool is request scoped
72      */

73     public void refresh()
74     {
75         // empty
76
}
77
78     /**
79      * Inner class to present a nice interface to the template designer
80      */

81     private class PullHelper
82     {
83         String JavaDoc omName;
84
85         private PullHelper(String JavaDoc omName)
86         {
87             this.omName = omName;
88         }
89
90         public Object JavaDoc setKey(String JavaDoc key)
91             throws Exception JavaDoc
92         {
93             Object JavaDoc om = null;
94
95             String JavaDoc inputKey = omName + key;
96             if (omMap.containsKey(inputKey))
97             {
98                 om = omMap.get(inputKey);
99             }
100             else
101             {
102                 om = omFactory.getInstance(omName).retrieve(key);
103                 omMap.put(inputKey, om);
104             }
105
106             return om;
107         }
108     }
109
110     public Object JavaDoc get(String JavaDoc omName) throws Exception JavaDoc
111     {
112         if (!pullMap.containsKey(omName))
113         {
114             // MT could overwrite a PullHelper, but that is not a problem
115
// should still synchronize to avoid two threads adding at
116
// same time
117
synchronized (this.getClass())
118             {
119                 pullMap.put(omName, new OMTool.PullHelper(omName));
120             }
121         }
122
123         return pullMap.get(omName);
124     }
125
126     public Object JavaDoc get(String JavaDoc omName, String JavaDoc key) throws Exception JavaDoc
127     {
128         return ((OMTool.PullHelper) get(omName)).setKey(key);
129     }
130
131
132     public String JavaDoc getName()
133     {
134         return "om";
135     }
136
137
138     // ****************** Recyclable implementation ************************
139

140     private boolean disposed;
141
142     /**
143      * Recycles the object for a new client. Recycle methods with
144      * parameters must be added to implementing object and they will be
145      * automatically called by pool implementations when the object is
146      * taken from the pool for a new client. The parameters must
147      * correspond to the parameters of the constructors of the object.
148      * For new objects, constructors can call their corresponding recycle
149      * methods whenever applicable.
150      * The recycle methods must call their super.
151      */

152     public void recycle()
153     {
154         disposed = false;
155     }
156
157     /**
158      * Disposes the object after use. The method is called
159      * when the object is returned to its pool.
160      * The dispose method must call its super.
161      */

162     public void dispose()
163     {
164         omMap.clear();
165         // data = null;
166
disposed = true;
167     }
168
169     /**
170      * Checks whether the recyclable has been disposed.
171      * @return true, if the recyclable is disposed.
172      */

173     public boolean isDisposed()
174     {
175         return disposed;
176     }
177 }
178
179
180
181
182
183
184
185
186
Popular Tags