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.turbine.Turbine; 25 import org.apache.turbine.TurbineConstants; 26 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService; 27 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker; 28 import org.apache.turbine.util.ObjectUtils; 29 import org.apache.turbine.util.RunData; 30 31 39 public class LayoutLoader 40 extends GenericLoader 41 implements Loader 42 { 43 44 private static Log log = LogFactory.getLog(LayoutLoader.class); 45 46 47 private static LayoutLoader instance = 48 new LayoutLoader(Turbine.getConfiguration() 49 .getInt(TurbineConstants.LAYOUT_CACHE_SIZE_KEY, 50 TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT)); 51 52 53 private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService(); 54 55 59 private LayoutLoader() 60 { 61 super(); 62 } 63 64 68 private LayoutLoader(int i) 69 { 70 super(i); 71 } 72 73 79 private void addInstance(String name, Layout layout) 80 { 81 if (cache()) 82 { 83 this.put(name, (Layout) layout); 84 } 85 } 86 87 94 public void exec(RunData data, String name) 95 throws Exception 96 { 97 getInstance(name).build(data); 99 } 100 101 111 public Assembler getAssembler(String name) 112 throws Exception 113 { 114 return getInstance(name); 115 } 116 117 125 public Layout getInstance(String name) 126 throws Exception 127 { 128 Layout layout = null; 129 130 if (cache() && this.containsKey(name)) 132 { 133 layout = (Layout) this.get(name); 134 log.debug("Found Layout " + name + " in the cache!"); 135 } 136 else 137 { 138 log.debug("Loading Layout " + name + " from the Assembler Broker"); 139 140 try 141 { 142 if (ab != null) 143 { 144 layout = (Layout) ab.getAssembler( 146 AssemblerBrokerService.LAYOUT_TYPE, name); 147 } 148 } 149 catch (ClassCastException cce) 150 { 151 layout = null; 155 } 156 157 if (layout == null) 158 { 159 List packages = Turbine.getConfiguration() 164 .getList(TurbineConstants.MODULE_PACKAGES); 165 166 ObjectUtils.addOnce(packages, 167 GenericLoader.getBasePackage()); 168 169 throw new ClassNotFoundException ( 170 "\n\n\tRequested Layout not found: " + name + 171 "\n\tTurbine looked in the following " + 172 "modules.packages path: \n\t" + packages.toString() + "\n"); 173 } 174 else if (cache()) 175 { 176 addInstance(name, layout); 178 } 179 } 180 return layout; 181 } 182 183 188 public static LayoutLoader getInstance() 189 { 190 return instance; 191 } 192 } 193 | Popular Tags |