001: package org.apache.turbine.modules;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.List;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import org.apache.turbine.Turbine;
025: import org.apache.turbine.TurbineConstants;
026: import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
027: import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
028: import org.apache.turbine.util.ObjectUtils;
029: import org.apache.turbine.util.RunData;
030:
031: /**
032: * The purpose of this class is to allow one to load and execute Page
033: * modules.
034: *
035: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
036: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037: * @version $Id: PageLoader.java 264148 2005-08-29 14:21:04Z henning $
038: */
039: public class PageLoader extends GenericLoader implements Loader {
040: /** Serial Version UID */
041: private static final long serialVersionUID = 272783554001103941L;
042:
043: /** Logging */
044: private static Log log = LogFactory.getLog(PageLoader.class);
045:
046: /** The single instance of this class. */
047: private static PageLoader instance = new PageLoader(Turbine
048: .getConfiguration().getInt(
049: TurbineConstants.PAGE_CACHE_SIZE_KEY,
050: TurbineConstants.PAGE_CACHE_SIZE_DEFAULT));
051:
052: /** The Assembler Broker Service */
053: private static AssemblerBrokerService ab = TurbineAssemblerBroker
054: .getService();
055:
056: /**
057: * These ctor's are private to force clients to use getInstance()
058: * to access this class.
059: */
060: private PageLoader() {
061: super ();
062: }
063:
064: /**
065: * These ctor's are private to force clients to use getInstance()
066: * to access this class.
067: */
068: private PageLoader(int i) {
069: super (i);
070: }
071:
072: /**
073: * Adds an instance of an object into the hashtable.
074: *
075: * @param name Name of object.
076: * @param page Page to be associated with name.
077: */
078: private void addInstance(String name, Page page) {
079: if (cache()) {
080: this .put(name, (Page) page);
081: }
082: }
083:
084: /**
085: * Attempts to load and execute the external page.
086: *
087: * @param data Turbine information.
088: * @param name Name of object that will execute the page.
089: * @exception Exception a generic exception.
090: */
091: public void exec(RunData data, String name) throws Exception {
092: // Execute page
093: getInstance(name).build(data);
094: }
095:
096: /**
097: * Pulls out an instance of the object by name. Name is just the
098: * single name of the object. This is equal to getInstance but
099: * returns an Assembler object and is needed to fulfil the Loader
100: * interface.
101: *
102: * @param name Name of object instance.
103: * @return A Screen with the specified name, or null.
104: * @exception Exception a generic exception.
105: */
106: public Assembler getAssembler(String name) throws Exception {
107: return getInstance(name);
108: }
109:
110: /**
111: * Pulls out an instance of the page by name. Name is just the
112: * single name of the page.
113: *
114: * @param name Name of object instance.
115: * @return A Page with the specified name, or null.
116: * @exception Exception a generic exception.
117: */
118: public Page getInstance(String name) throws Exception {
119: Page page = null;
120:
121: // Check if the screen is already in the cache
122: if (cache() && this .containsKey(name)) {
123: page = (Page) this .get(name);
124: log.debug("Found Page " + name + " in the cache!");
125: } else {
126: log.debug("Loading Page " + name
127: + " from the Assembler Broker");
128:
129: try {
130: if (ab != null) {
131: // Attempt to load the screen
132: page = (Page) ab.getAssembler(
133: AssemblerBrokerService.PAGE_TYPE, name);
134: }
135: } catch (ClassCastException cce) {
136: // This can alternatively let this exception be thrown
137: // So that the ClassCastException is shown in the
138: // browser window. Like this it shows "Screen not Found"
139: page = null;
140: }
141:
142: if (page == null) {
143: // If we did not find a screen we should try and give
144: // the user a reason for that...
145: // FIX ME: The AssemblerFactories should each add it's
146: // own string here...
147: List packages = Turbine.getConfiguration().getList(
148: TurbineConstants.MODULE_PACKAGES);
149:
150: ObjectUtils.addOnce(packages, GenericLoader
151: .getBasePackage());
152:
153: throw new ClassNotFoundException(
154: "\n\n\tRequested Page not found: "
155: + name
156: + "\n\tTurbine looked in the following "
157: + "modules.packages path: \n\t"
158: + packages.toString() + "\n");
159: } else if (cache()) {
160: // The new instance is added to the cache
161: addInstance(name, page);
162: }
163: }
164: return page;
165: }
166:
167: /**
168: * The method through which this class is accessed.
169: *
170: * @return The single instance of this class.
171: */
172: public static PageLoader getInstance() {
173: return instance;
174: }
175: }
|