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.ecs.ConcreteElement;
028:
029: import org.apache.turbine.Turbine;
030: import org.apache.turbine.TurbineConstants;
031: import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
032: import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
033: import org.apache.turbine.util.ObjectUtils;
034: import org.apache.turbine.util.RunData;
035:
036: /**
037: * The purpose of this class is to allow one to load and execute
038: * Screen modules.
039: *
040: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
041: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
042: * @version $Id: ScreenLoader.java 534527 2007-05-02 16:10:59Z tv $
043: */
044: public class ScreenLoader extends GenericLoader implements Loader {
045: /** Serial Version UID */
046: private static final long serialVersionUID = -4825216334500657398L;
047:
048: /** Logging */
049: private static Log log = LogFactory.getLog(ScreenLoader.class);
050:
051: /** The single instance of this class. */
052: private static ScreenLoader instance = new ScreenLoader(Turbine
053: .getConfiguration().getInt(
054: TurbineConstants.SCREEN_CACHE_SIZE_KEY,
055: TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT));
056:
057: /** The Assembler Broker Service */
058: private static AssemblerBrokerService ab = TurbineAssemblerBroker
059: .getService();
060:
061: /**
062: * These ctor's are private to force clients to use getInstance()
063: * to access this class.
064: */
065: private ScreenLoader() {
066: super ();
067: }
068:
069: /**
070: * These ctor's are private to force clients to use getInstance()
071: * to access this class.
072: */
073: private ScreenLoader(int i) {
074: super (i);
075: }
076:
077: /**
078: * Adds an instance of an object into the hashtable.
079: *
080: * @param name Name of object.
081: * @param screen Screen to be associated with name.
082: */
083: private void addInstance(String name, Screen screen) {
084: if (cache()) {
085: this .put(name, (Screen) screen);
086: }
087: }
088:
089: /**
090: * Attempts to load and execute the external Screen. This is used
091: * when you want to execute a Screen which returns its output via
092: * a MultiPartElement instead of out the data.getPage() value.
093: * This allows you to easily chain the execution of Screen modules
094: * together.
095: *
096: * @param data Turbine information.
097: * @param name Name of object that will execute the screen.
098: * @exception Exception a generic exception.
099: */
100: public ConcreteElement eval(RunData data, String name)
101: throws Exception {
102: // Execute screen
103: return getInstance(name).build(data);
104: }
105:
106: /**
107: * Attempts to load and execute the Screen. This is used when you
108: * want to execute a Screen which returns its output via the
109: * data.getPage() object.
110: *
111: * @param data Turbine information.
112: * @param name Name of object that will execute the screen.
113: * @exception Exception a generic exception.
114: */
115: public void exec(RunData data, String name) throws Exception {
116: this .eval(data, name);
117: }
118:
119: /**
120: * Pulls out an instance of the object by name. Name is just the
121: * single name of the object. This is equal to getInstance but
122: * returns an Assembler object and is needed to fulfil the Loader
123: * interface.
124: *
125: * @param name Name of object instance.
126: * @return A Screen with the specified name, or null.
127: * @exception Exception a generic exception.
128: */
129: public Assembler getAssembler(String name) throws Exception {
130: return getInstance(name);
131: }
132:
133: /**
134: * Pulls out an instance of the Screen by name. Name is just the
135: * single name of the Screen.
136: *
137: * @param name Name of requested Screen.
138: * @return A Screen with the specified name, or null.
139: * @exception Exception a generic exception.
140: */
141: public Screen getInstance(String name) throws Exception {
142: Screen screen = null;
143:
144: // Check if the screen is already in the cache
145: if (cache() && this .containsKey(name)) {
146: screen = (Screen) this .get(name);
147: log.debug("Found Screen " + name + " in the cache!");
148: } else {
149: log.debug("Loading Screen " + name
150: + " from the Assembler Broker");
151:
152: try {
153: if (ab != null) {
154: // Attempt to load the screen
155: screen = (Screen) ab.getAssembler(
156: AssemblerBrokerService.SCREEN_TYPE, name);
157: }
158: } catch (ClassCastException cce) {
159: // This can alternatively let this exception be thrown
160: // So that the ClassCastException is shown in the
161: // browser window. Like this it shows "Screen not Found"
162: screen = null;
163: }
164:
165: if (screen == null) {
166: // If we did not find a screen we should try and give
167: // the user a reason for that...
168: // FIX ME: The AssemblerFactories should each add it's
169: // own string here...
170: List packages = Turbine.getConfiguration().getList(
171: TurbineConstants.MODULE_PACKAGES);
172:
173: ObjectUtils.addOnce(packages, GenericLoader
174: .getBasePackage());
175:
176: throw new ClassNotFoundException(
177: "\n\n\tRequested Screen not found: "
178: + name
179: + "\n\tTurbine looked in the following "
180: + "modules.packages path: \n\t"
181: + packages.toString() + "\n");
182: } else if (cache()) {
183: // The new instance is added to the cache
184: addInstance(name, screen);
185: }
186: }
187: return screen;
188: }
189:
190: /**
191: * The method through which this class is accessed.
192: *
193: * @return The single instance of this class.
194: */
195: public static ScreenLoader getInstance() {
196: return instance;
197: }
198: }
|