##### Coverage Report - org.apache.turbine.om.OMTool

Classes in this File | Line Coverage | Branch Coverage | Complexity
OMTool |
57% | 11/19
100% | 2/2
| 1.3;1,3
OMTool$PullHelper |
100% | 10/10
100% | 2/2
| 1.3;1,3

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: OMTool.java 1706425 2015-10-02 14:47:51Z tv $
35 |  |
      */
36 |  |
     public class OMTool implements ApplicationTool, Recyclable
37 |  |
     {
38 |  |
         protected ConcurrentHashMap<String, Object> 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 | 2 |
         private ConcurrentMap<String, OMTool.PullHelper> pullMap =
47 |  |
                 new ConcurrentHashMap<String, OMTool.PullHelper>();
48 |  |
49 |  |
         /**
50 |  |
          *  The Factory responsible for retrieving the
51 |  |
          *  objects from storage
52 |  |
          */
53 |  |
         protected RetrieverFactory omFactory;
54 |  |
55 |  |
         /**
56 |  |
          * Default constructor
57 |  |
          * @throws Exception if creating the factory fails
58 |  |
          */
59 |  |
         public OMTool() throws Exception
60 | 2 |
         {
61 | 2 |
             omMap = new ConcurrentHashMap<String, Object>();
62 | 2 |
             String className = Turbine.getConfiguration().getString("tool.om.factory");
63 | 2 |
             this.omFactory = (RetrieverFactory)Class.forName(className).newInstance();
64 | 2 |
         }
65 |  |
66 |  |
         /**
67 |  |
          * Prepares tool for a single request
68 |  |
          *
69 |  |
          * @param data the initialization data
70 |  |
          */
71 |  |
         @Override
72 |  |
         public void init(Object data)
73 |  |
         {
74 |  |
             // data = (RunData)data;
75 | 0 |
         }
76 |  |
77 |  |
         /**
78 |  |
          * Implementation of ApplicationTool interface is not needed for this
79 |  |
          * method as the tool is request scoped
80 |  |
          */
81 |  |
         @Override
82 |  |
         public void refresh()
83 |  |
         {
84 |  |
             // empty
85 | 0 |
         }
86 |  |
87 |  |
         /**
88 |  |
          * Inner class to present a nice interface to the template designer
89 |  |
          */
90 |  |
         protected class PullHelper
91 |  |
         {
92 |  |
             String omName;
93 |  |
94 |  |
             protected PullHelper(String omName)
95 | 5 |
             {
96 | 5 |
                 this.omName = omName;
97 | 5 |
             }
98 |  |
99 |  |
             public Object setKey(String key)
100 |  |
                 throws Exception
101 |  |
             {
102 | 2 |
                 Object om = null;
103 | 2 |
                 String inputKey = omName + key;
104 |  |
105 | 2 |
                 if (omMap.containsKey(inputKey))
106 |  |
                 {
107 | 1 |
                     om = omMap.get(inputKey);
108 |  |
                 }
109 |  |
                 else
110 |  |
                 {
111 | 1 |
                     om = omFactory.getInstance(omName).retrieve(key);
112 | 1 |
                     omMap.put(inputKey, om);
113 |  |
                 }
114 |  |
115 | 2 |
                 return om;
116 |  |
             }
117 |  |
         }
118 |  |
119 |  |
         /**
120 |  |
          * Get the {@link PullHelper} object with the given name
121 |  |
          * @param omName the object name
122 |  |
          * @return the PullHelper
123 |  |
          * @throws Exception if retrieving the object fails
124 |  |
          */
125 |  |
         public PullHelper get(String omName) throws Exception
126 |  |
         {
127 | 5 |
             PullHelper ph = pullMap.putIfAbsent(omName, new OMTool.PullHelper(omName));
128 | 5 |
             if (ph == null)
129 |  |
             {
130 | 3 |
                 return pullMap.get(omName);
131 |  |
             }
132 |  |
133 | 2 |
             return ph;
134 |  |
         }
135 |  |
136 |  |
         /**
137 |  |
          * Get the object with the given name and key
138 |  |
          * @param omName the object name
139 |  |
          * @param key the object key
140 |  |
          * @return the object
141 |  |
          * @throws Exception if retrieving the object fails
142 |  |
          */
143 |  |
         public Object get(String omName, String key) throws Exception
144 |  |
         {
145 | 2 |
             return get(omName).setKey(key);
146 |  |
         }
147 |  |
148 |  |
         // ****************** Recyclable implementation ************************
149 |  |
150 |  |
         private boolean disposed;
151 |  |
152 |  |
         /**
153 |  |
          * Recycles the object for a new client. Recycle methods with
154 |  |
          * parameters must be added to implementing object and they will be
155 |  |
          * automatically called by pool implementations when the object is
156 |  |
          * taken from the pool for a new client. The parameters must
157 |  |
          * correspond to the parameters of the constructors of the object.
158 |  |
          * For new objects, constructors can call their corresponding recycle
159 |  |
          * methods whenever applicable.
160 |  |
          * The recycle methods must call their super.
161 |  |
          */
162 |  |
         @Override
163 |  |
         public void recycle()
164 |  |
         {
165 | 0 |
             disposed = false;
166 | 0 |
         }
167 |  |
168 |  |
         /**
169 |  |
          * Disposes the object after use. The method is called
170 |  |
          * when the object is returned to its pool.
171 |  |
          * The dispose method must call its super.
172 |  |
          */
173 |  |
         @Override
174 |  |
         public void dispose()
175 |  |
         {
176 | 0 |
             omMap.clear();
177 | 0 |
             disposed = true;
178 | 0 |
         }
179 |  |
180 |  |
         /**
181 |  |
          * Checks whether the recyclable has been disposed.
182 |  |
          * @return true, if the recyclable is disposed.
183 |  |
          */
184 |  |
         @Override
185 |  |
         public boolean isDisposed()
186 |  |
         {
187 | 0 |
             return disposed;
188 |  |
         }
189 |  |
     }