View Javadoc
1 package org.apache.turbine.modules; 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 // Java Core Classes 58 import java.util.Vector; 59 60 // Turbine Utility Classes 61 import org.apache.turbine.TurbineConstants; 62 import org.apache.turbine.services.TurbineServices; 63 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService; 64 import org.apache.turbine.services.resources.TurbineResources; 65 import org.apache.turbine.util.ObjectUtils; 66 import org.apache.turbine.util.RunData; 67 68 /*** 69 * The purpose of this class is to allow one to load and execute 70 * Action modules. 71 * 72 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> 73 * @version $Id: ActionLoader.java,v 1.1.1.1 2001/08/16 05:08:29 jvanzyl Exp $ 74 */ 75 public class ActionLoader extends GenericLoader 76 { 77 /*** The single instance of this class. */ 78 private static ActionLoader instance = new ActionLoader( 79 TurbineResources.getInt(TurbineConstants.ACTION_CACHE_SIZE, 20)); 80 81 /*** 82 * These ctor's are private to force clients to use getInstance() 83 * to access this class. 84 */ 85 private ActionLoader() 86 { 87 super(); 88 } 89 90 /*** 91 * These ctor's are private to force clients to use getInstance() 92 * to access this class. 93 */ 94 private ActionLoader(int i) 95 { 96 super(i); 97 } 98 99 /*** 100 * Adds an instance of an object into the hashtable. 101 * 102 * @param name Name of object. 103 * @param action Action to be associated with name. 104 */ 105 private void addInstance( String name, Action action ) 106 { 107 if ( cache() ) 108 this.put( name, (Action) action ); 109 } 110 111 /*** 112 * Attempts to load and execute the external action. 113 * 114 * @param data Turbine information. 115 * @param name Name of object that will execute the action. 116 * @exception Exception a generic exception. 117 */ 118 public void exec( RunData data, String name ) 119 throws Exception 120 { 121 // Execute action 122 getInstance(name).perform(data); 123 } 124 125 /*** 126 * Pulls out an instance of the object by name. Name is just the 127 * single name of the object. 128 * 129 * @param name Name of object instance. 130 * @return An Action with the specified name, or null. 131 * @exception Exception a generic exception. 132 */ 133 public Action getInstance(String name) 134 throws Exception 135 { 136 Action action = null; 137 138 if ( cache() && this.containsKey( name ) ) 139 { 140 action = (Action) this.get( name ); 141 } 142 else 143 { 144 // We get the broker service 145 AssemblerBrokerService ab = 146 (AssemblerBrokerService)TurbineServices.getInstance() 147 .getService(AssemblerBrokerService.SERVICE_NAME); 148 149 try 150 { 151 // Attempt to load the screen 152 action = (Action) ab.getAssembler( 153 AssemblerBrokerService.ACTION_TYPE,name); 154 } 155 catch (ClassCastException cce) 156 { 157 // This can alternatively let this exception be thrown 158 // So that the ClassCastException is shown in the 159 // browser window. Like this it shows "Screen not Found" 160 action = null; 161 } 162 163 if (action == null) 164 { 165 // If we did not find a screen we should try and give 166 // the user a reason for that... 167 // FIX ME: The AssemblerFactories should each add it's own 168 // string here... 169 Vector packages = TurbineResources.getVector( 170 TurbineConstants.MODULE_PACKAGES); 171 ObjectUtils.addOnce( packages, 172 GenericLoader.getBasePackage() ); 173 174 throw new ClassNotFoundException( 175 "\n\n\tRequested Action not found: " + name + "\n" + 176 "\tTurbine looked in the following modules.packages " + 177 "path: \n\t" + packages.toString() + "\n"); 178 } 179 else if ( cache() ) 180 { 181 // The new instance is added to the cache 182 addInstance( name, action ); 183 } 184 } 185 return action; 186 } 187 188 /*** 189 * The method through which this class is accessed. 190 * 191 * @return The single instance of this class. 192 */ 193 public static ActionLoader getInstance() 194 { 195 return instance; 196 } 197 }

This page was automatically generated by Maven