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