View Javadoc
1 package org.apache.turbine.om; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.util.HashMap; 58 import java.util.Map; 59 import org.apache.turbine.util.RunData; 60 import org.apache.turbine.util.StringUtils; 61 import org.apache.turbine.util.ParameterParser; 62 import org.apache.turbine.util.Log; 63 import org.apache.turbine.util.TurbineException; 64 import org.apache.turbine.services.resources.TurbineResources; 65 import org.apache.turbine.services.pull.ApplicationTool; 66 import org.apache.turbine.util.pool.Recyclable; 67 68 /*** 69 * A Pull tool to make om objects available to a template 70 * 71 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> 72 * @version $Id$ 73 */ 74 public class OMTool 75 implements ApplicationTool, Recyclable 76 { 77 // private RunData data; 78 private HashMap omMap; 79 80 // note the following could be a static attribute to reduce memory 81 // footprint. Might require a service to front load the 82 // PullHelpers to avoid MT issues. A multiple write is not so bad 83 // though 84 85 /*** The cache of PullHelpers. **/ 86 private static Map pullMap = new HashMap(); 87 88 /*** 89 * The Factory responsible for retrieving the 90 * objects from storage 91 */ 92 private RetrieverFactory omFactory; 93 94 public OMTool() 95 throws Exception 96 { 97 omMap = new HashMap(); 98 String className = TurbineResources.getString("tool.om.factory"); 99 // RetrieverFactory omFactory = 100 // (RetrieverFactory)Class.forName(className).newInstance(); 101 } 102 103 /*** 104 * Prepares tool for a single request 105 */ 106 public void init(Object runData) 107 { 108 // data = (RunData)runData; 109 } 110 111 /*** 112 * Implementation of ApplicationTool interface is not needed for this 113 * method as the tool is request scoped 114 */ 115 public void refresh() 116 { 117 // empty 118 } 119 120 /*** 121 * Inner class to present a nice interface to the template designer 122 */ 123 private class PullHelper 124 { 125 String omName; 126 127 private PullHelper(String omName) 128 { 129 this.omName = omName; 130 } 131 132 public Object setKey(String key) 133 throws Exception 134 { 135 Object om = null; 136 137 String inputKey = omName + key; 138 if ( omMap.containsKey(inputKey) ) 139 { 140 om = omMap.get(inputKey); 141 } 142 else 143 { 144 om = omFactory.getInstance(omName).retrieve(key); 145 omMap.put(inputKey, om); 146 } 147 148 return om; 149 } 150 } 151 152 public Object get(String omName) 153 throws Exception 154 { 155 if ( !pullMap.containsKey(omName) ) 156 { 157 // MT could overwrite a PullHelper, but that is not a problem 158 // should still synchronize to avoid two threads adding at 159 // same time 160 synchronized (this.getClass()) 161 { 162 pullMap.put(omName, new OMTool.PullHelper(omName)); 163 } 164 } 165 166 return pullMap.get(omName); 167 } 168 169 public Object get(String omName, String key) 170 throws Exception 171 { 172 return ((OMTool.PullHelper)get(omName)).setKey(key); 173 } 174 175 176 public String getName() 177 { 178 return "om"; 179 } 180 181 182 // ****************** Recyclable implementation ************************ 183 184 private boolean disposed; 185 186 /*** 187 * Recycles the object for a new client. Recycle methods with 188 * parameters must be added to implementing object and they will be 189 * automatically called by pool implementations when the object is 190 * taken from the pool for a new client. The parameters must 191 * correspond to the parameters of the constructors of the object. 192 * For new objects, constructors can call their corresponding recycle 193 * methods whenever applicable. 194 * The recycle methods must call their super. 195 */ 196 public void recycle() 197 { 198 disposed = false; 199 } 200 201 /*** 202 * Disposes the object after use. The method is called 203 * when the object is returned to its pool. 204 * The dispose method must call its super. 205 */ 206 public void dispose() 207 { 208 omMap.clear(); 209 // data = null; 210 211 disposed = true; 212 } 213 214 /*** 215 * Checks whether the recyclable has been disposed. 216 * @return true, if the recyclable is disposed. 217 */ 218 public boolean isDisposed() 219 { 220 return disposed; 221 } 222 223 } 224 225 226 227 228 229 230 231

This page was automatically generated by Maven