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