1 package org.apache.turbine.modules; 2 3 18 19 import java.util.List ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import org.apache.ecs.ConcreteElement; 25 26 import org.apache.turbine.Turbine; 27 import org.apache.turbine.TurbineConstants; 28 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService; 29 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker; 30 import org.apache.turbine.util.ObjectUtils; 31 import org.apache.turbine.util.RunData; 32 33 41 public class ScreenLoader 42 extends GenericLoader 43 implements Loader 44 { 45 46 private static Log log = LogFactory.getLog(ScreenLoader.class); 47 48 49 private static ScreenLoader instance = 50 new ScreenLoader(Turbine.getConfiguration() 51 .getInt(TurbineConstants.SCREEN_CACHE_SIZE_KEY, 52 TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT)); 53 54 55 private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService(); 56 57 61 private ScreenLoader() 62 { 63 super(); 64 } 65 66 70 private ScreenLoader(int i) 71 { 72 super(i); 73 } 74 75 81 private void addInstance(String name, Screen screen) 82 { 83 if (cache()) 84 { 85 this.put(name, (Screen) screen); 86 } 87 } 88 89 100 public ConcreteElement eval(RunData data, String name) 101 throws Exception 102 { 103 return getInstance(name).build(data); 105 } 106 107 116 public void exec(RunData data, String name) 117 throws Exception 118 { 119 this.eval(data, name); 120 } 121 122 132 public Assembler getAssembler(String name) 133 throws Exception 134 { 135 return getInstance(name); 136 } 137 138 146 public Screen getInstance(String name) 147 throws Exception 148 { 149 Screen screen = null; 150 151 if (cache() && this.containsKey(name)) 153 { 154 screen = (Screen) this.get(name); 155 log.debug("Found Screen " + name + " in the cache!"); 156 } 157 else 158 { 159 log.debug("Loading Screen " + name + " from the Assembler Broker"); 160 161 try 162 { 163 if (ab != null) 164 { 165 screen = (Screen) ab.getAssembler( 167 AssemblerBrokerService.SCREEN_TYPE, name); 168 } 169 } 170 catch (ClassCastException cce) 171 { 172 screen = null; 176 } 177 178 if (screen == null) 179 { 180 List packages = Turbine.getConfiguration() 185 .getList(TurbineConstants.MODULE_PACKAGES); 186 187 ObjectUtils.addOnce(packages, 188 GenericLoader.getBasePackage()); 189 190 throw new ClassNotFoundException ( 191 "\n\n\tRequested Screen not found: " + name + 192 "\n\tTurbine looked in the following " + 193 "modules.packages path: \n\t" + packages.toString() + "\n"); 194 } 195 else if (cache()) 196 { 197 addInstance(name, screen); 199 } 200 } 201 return screen; 202 } 203 204 209 public static ScreenLoader getInstance() 210 { 211 return instance; 212 } 213 } 214 | Popular Tags |