KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > modules > PageLoader


1 package org.apache.turbine.modules;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
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,v 1.8.2.3 2004/08/16 22:57:50 henning Exp $
38  */

39 public class PageLoader
40     extends GenericLoader
41     implements Loader
42 {
43     /** Logging */
44     private static Log log = LogFactory.getLog(PageLoader.class);
45
46     /** The single instance of this class. */
47     private static PageLoader instance =
48         new PageLoader(Turbine.getConfiguration()
49                        .getInt(TurbineConstants.PAGE_CACHE_SIZE_KEY,
50                                TurbineConstants.PAGE_CACHE_SIZE_DEFAULT));
51
52     /** The Assembler Broker Service */
53     private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
54
55     /**
56      * These ctor's are private to force clients to use getInstance()
57      * to access this class.
58      */

59     private PageLoader()
60     {
61         super();
62     }
63
64     /**
65      * These ctor's are private to force clients to use getInstance()
66      * to access this class.
67      */

68     private PageLoader(int i)
69     {
70         super(i);
71     }
72
73     /**
74      * Adds an instance of an object into the hashtable.
75      *
76      * @param name Name of object.
77      * @param page Page to be associated with name.
78      */

79     private void addInstance(String JavaDoc name, Page page)
80     {
81         if (cache())
82         {
83             this.put(name, (Page) page);
84         }
85     }
86
87     /**
88      * Attempts to load and execute the external page.
89      *
90      * @param data Turbine information.
91      * @param name Name of object that will execute the page.
92      * @exception Exception a generic exception.
93      */

94     public void exec(RunData data, String JavaDoc name)
95             throws Exception JavaDoc
96     {
97         // Execute page
98
getInstance(name).build(data);
99     }
100
101     /**
102      * Pulls out an instance of the object by name. Name is just the
103      * single name of the object. This is equal to getInstance but
104      * returns an Assembler object and is needed to fulfil the Loader
105      * interface.
106      *
107      * @param name Name of object instance.
108      * @return A Screen with the specified name, or null.
109      * @exception Exception a generic exception.
110      */

111     public Assembler getAssembler(String JavaDoc name)
112         throws Exception JavaDoc
113     {
114         return getInstance(name);
115     }
116
117     /**
118      * Pulls out an instance of the page by name. Name is just the
119      * single name of the page.
120      *
121      * @param name Name of object instance.
122      * @return A Page with the specified name, or null.
123      * @exception Exception a generic exception.
124      */

125     public Page getInstance(String JavaDoc name)
126             throws Exception JavaDoc
127     {
128         Page page = null;
129
130         // Check if the screen is already in the cache
131
if (cache() && this.containsKey(name))
132         {
133             page = (Page) this.get(name);
134             log.debug("Found Page " + name + " in the cache!");
135         }
136         else
137         {
138             log.debug("Loading Page " + name + " from the Assembler Broker");
139
140             try
141             {
142                 if (ab != null)
143                 {
144                     // Attempt to load the screen
145
page = (Page) ab.getAssembler(
146                         AssemblerBrokerService.PAGE_TYPE, name);
147                 }
148             }
149             catch (ClassCastException JavaDoc cce)
150             {
151                 // This can alternatively let this exception be thrown
152
// So that the ClassCastException is shown in the
153
// browser window. Like this it shows "Screen not Found"
154
page = null;
155             }
156
157             if (page == null)
158             {
159                 // If we did not find a screen we should try and give
160
// the user a reason for that...
161
// FIX ME: The AssemblerFactories should each add it's
162
// own string here...
163
List JavaDoc packages = Turbine.getConfiguration()
164                     .getList(TurbineConstants.MODULE_PACKAGES);
165
166                 ObjectUtils.addOnce(packages,
167                         GenericLoader.getBasePackage());
168
169                 throw new ClassNotFoundException JavaDoc(
170                         "\n\n\tRequested Page 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                 // The new instance is added to the cache
177
addInstance(name, page);
178             }
179         }
180         return page;
181     }
182
183     /**
184      * The method through which this class is accessed.
185      *
186      * @return The single instance of this class.
187      */

188     public static PageLoader getInstance()
189     {
190         return instance;
191     }
192 }
193
Popular Tags