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

This page was automatically generated by Maven