1 package org.apache.turbine.modules;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 /***
32 * The purpose of this class is to allow one to load and execute Page
33 * modules.
34 *
35 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
36 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37 * @version $Id: PageLoader.java 264148 2005-08-29 14:21:04Z henning $
38 */
39 public class PageLoader
40 extends GenericLoader
41 implements Loader
42 {
43 /*** Serial Version UID */
44 private static final long serialVersionUID = 272783554001103941L;
45
46 /*** Logging */
47 private static Log log = LogFactory.getLog(PageLoader.class);
48
49 /*** The single instance of this class. */
50 private static PageLoader instance =
51 new PageLoader(Turbine.getConfiguration()
52 .getInt(TurbineConstants.PAGE_CACHE_SIZE_KEY,
53 TurbineConstants.PAGE_CACHE_SIZE_DEFAULT));
54
55 /*** The Assembler Broker Service */
56 private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
57
58 /***
59 * These ctor's are private to force clients to use getInstance()
60 * to access this class.
61 */
62 private PageLoader()
63 {
64 super();
65 }
66
67 /***
68 * These ctor's are private to force clients to use getInstance()
69 * to access this class.
70 */
71 private PageLoader(int i)
72 {
73 super(i);
74 }
75
76 /***
77 * Adds an instance of an object into the hashtable.
78 *
79 * @param name Name of object.
80 * @param page Page to be associated with name.
81 */
82 private void addInstance(String name, Page page)
83 {
84 if (cache())
85 {
86 this.put(name, (Page) page);
87 }
88 }
89
90 /***
91 * Attempts to load and execute the external page.
92 *
93 * @param data Turbine information.
94 * @param name Name of object that will execute the page.
95 * @exception Exception a generic exception.
96 */
97 public void exec(RunData data, String name)
98 throws Exception
99 {
100
101 getInstance(name).build(data);
102 }
103
104 /***
105 * Pulls out an instance of the object by name. Name is just the
106 * single name of the object. This is equal to getInstance but
107 * returns an Assembler object and is needed to fulfil the Loader
108 * interface.
109 *
110 * @param name Name of object instance.
111 * @return A Screen with the specified name, or null.
112 * @exception Exception a generic exception.
113 */
114 public Assembler getAssembler(String name)
115 throws Exception
116 {
117 return getInstance(name);
118 }
119
120 /***
121 * Pulls out an instance of the page by name. Name is just the
122 * single name of the page.
123 *
124 * @param name Name of object instance.
125 * @return A Page with the specified name, or null.
126 * @exception Exception a generic exception.
127 */
128 public Page getInstance(String name)
129 throws Exception
130 {
131 Page page = null;
132
133
134 if (cache() && this.containsKey(name))
135 {
136 page = (Page) this.get(name);
137 log.debug("Found Page " + name + " in the cache!");
138 }
139 else
140 {
141 log.debug("Loading Page " + name + " from the Assembler Broker");
142
143 try
144 {
145 if (ab != null)
146 {
147
148 page = (Page) ab.getAssembler(
149 AssemblerBrokerService.PAGE_TYPE, name);
150 }
151 }
152 catch (ClassCastException cce)
153 {
154
155
156
157 page = null;
158 }
159
160 if (page == null)
161 {
162
163
164
165
166 List packages = Turbine.getConfiguration()
167 .getList(TurbineConstants.MODULE_PACKAGES);
168
169 ObjectUtils.addOnce(packages,
170 GenericLoader.getBasePackage());
171
172 throw new ClassNotFoundException(
173 "\n\n\tRequested Page not found: " + name +
174 "\n\tTurbine looked in the following " +
175 "modules.packages path: \n\t" + packages.toString() + "\n");
176 }
177 else if (cache())
178 {
179
180 addInstance(name, page);
181 }
182 }
183 return page;
184 }
185
186 /***
187 * The method through which this class is accessed.
188 *
189 * @return The single instance of this class.
190 */
191 public static PageLoader getInstance()
192 {
193 return instance;
194 }
195 }