%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.modules.PageLoader |
|
|
1 | package org.apache.turbine.modules; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | 21 | 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 | 84 | private static Log log = LogFactory.getLog(PageLoader.class); |
48 | ||
49 | /** The single instance of this class. */ |
|
50 | 42 | 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 | 42 | 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 | 0 | super(); |
65 | 0 | } |
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 | 42 | super(i); |
74 | 42 | } |
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 | 0 | if (cache()) |
85 | { |
|
86 | 0 | this.put(name, (Page) page); |
87 | } |
|
88 | 0 | } |
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 | // Execute page |
|
101 | 0 | getInstance(name).build(data); |
102 | 0 | } |
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 | 0 | 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 | 0 | Page page = null; |
132 | ||
133 | // Check if the screen is already in the cache |
|
134 | 0 | if (cache() && this.containsKey(name)) |
135 | { |
|
136 | 0 | page = (Page) this.get(name); |
137 | 0 | log.debug("Found Page " + name + " in the cache!"); |
138 | } |
|
139 | else |
|
140 | { |
|
141 | 0 | log.debug("Loading Page " + name + " from the Assembler Broker"); |
142 | ||
143 | try |
|
144 | { |
|
145 | 0 | if (ab != null) |
146 | { |
|
147 | // Attempt to load the screen |
|
148 | 0 | page = (Page) ab.getAssembler( |
149 | AssemblerBrokerService.PAGE_TYPE, name); |
|
150 | } |
|
151 | } |
|
152 | 0 | catch (ClassCastException cce) |
153 | { |
|
154 | // This can alternatively let this exception be thrown |
|
155 | // So that the ClassCastException is shown in the |
|
156 | // browser window. Like this it shows "Screen not Found" |
|
157 | 0 | page = null; |
158 | 0 | } |
159 | ||
160 | 0 | if (page == null) |
161 | { |
|
162 | // If we did not find a screen we should try and give |
|
163 | // the user a reason for that... |
|
164 | // FIX ME: The AssemblerFactories should each add it's |
|
165 | // own string here... |
|
166 | 0 | List packages = Turbine.getConfiguration() |
167 | .getList(TurbineConstants.MODULE_PACKAGES); |
|
168 | ||
169 | 0 | ObjectUtils.addOnce(packages, |
170 | GenericLoader.getBasePackage()); |
|
171 | ||
172 | 0 | 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 | 0 | else if (cache()) |
178 | { |
|
179 | // The new instance is added to the cache |
|
180 | 0 | addInstance(name, page); |
181 | } |
|
182 | } |
|
183 | 0 | 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 | 44 | return instance; |
194 | } |
|
195 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |