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: * Navigation 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: NavigationLoader.java 534527 2007-05-02 16:10:59Z tv $
043: */
044: public class NavigationLoader extends GenericLoader implements Loader {
045: /** Serial Version UID */
046: private static final long serialVersionUID = 5062299200802529612L;
047:
048: /** Logging */
049: private static Log log = LogFactory.getLog(NavigationLoader.class);
050:
051: /** The single instance of this class. */
052: private static NavigationLoader instance = new NavigationLoader(
053: Turbine.getConfiguration().getInt(
054: TurbineConstants.NAVIGATION_CACHE_SIZE_KEY,
055: TurbineConstants.NAVIGATION_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 NavigationLoader() {
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 NavigationLoader(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 navigation Navigation to be associated with name.
082: */
083: private void addInstance(String name, Navigation navigation) {
084: if (cache()) {
085: this .put(name, (Navigation) navigation);
086: }
087: }
088:
089: /**
090: * Attempts to load and execute the external Navigation. This is
091: * used when you want to execute a Navigation which returns its
092: * output via a MultiPartElement instead of out the data.getPage()
093: * value. This allows you to easily chain the execution of
094: * Navigation modules together.
095: *
096: * @param data Turbine information.
097: * @param name Name of object that will execute the navigation.
098: * @exception Exception a generic exception.
099: */
100: public ConcreteElement eval(RunData data, String name)
101: throws Exception {
102: // Execute Navigation
103: return getInstance(name).build(data);
104: }
105:
106: /**
107: * Attempts to load and execute the external Navigation.
108: *
109: * @param data Turbine information.
110: * @param name Name of object instance.
111: * @exception Exception a generic exception.
112: */
113: public void exec(RunData data, String name) throws Exception {
114: this .eval(data, name);
115: }
116:
117: /**
118: * Pulls out an instance of the object by name. Name is just the
119: * single name of the object. This is equal to getInstance but
120: * returns an Assembler object and is needed to fulfil the Loader
121: * interface.
122: *
123: * @param name Name of object instance.
124: * @return A Layout with the specified name, or null.
125: * @exception Exception a generic exception.
126: */
127: public Assembler getAssembler(String name) throws Exception {
128: return getInstance(name);
129: }
130:
131: /**
132: * Pulls out an instance of the Navigation by name. Name is just the
133: * single name of the Navigation.
134: *
135: * @param name Name of requested Navigation
136: * @return A Navigation with the specified name, or null.
137: * @exception Exception a generic exception.
138: */
139: public Navigation getInstance(String name) throws Exception {
140: Navigation navigation = null;
141:
142: // Check if the navigation is already in the cache
143: if (cache() && this .containsKey(name)) {
144: navigation = (Navigation) this .get(name);
145: log.debug("Found Navigation " + name + " in the cache!");
146: } else {
147: log.debug("Loading Navigation " + name
148: + " from the Assembler Broker");
149:
150: try {
151: if (ab != null) {
152: // Attempt to load the navigation
153: navigation = (Navigation) ab.getAssembler(
154: AssemblerBrokerService.NAVIGATION_TYPE,
155: name);
156: }
157: } catch (ClassCastException cce) {
158: // This can alternatively let this exception be thrown
159: // So that the ClassCastException is shown in the
160: // browser window. Like this it shows "Screen not Found"
161: navigation = null;
162: }
163:
164: if (navigation == null) {
165: // If we did not find a screen we should try and give
166: // the user a reason for that...
167: // FIX ME: The AssemblerFactories should each add it's
168: // own string here...
169: List packages = Turbine.getConfiguration().getList(
170: TurbineConstants.MODULE_PACKAGES);
171:
172: ObjectUtils.addOnce(packages, GenericLoader
173: .getBasePackage());
174:
175: throw new ClassNotFoundException(
176: "\n\n\tRequested Navigation not found: "
177: + name
178: + "\n\tTurbine looked in the following "
179: + "modules.packages path: \n\t"
180: + packages.toString() + "\n");
181: } else if (cache()) {
182: // The new instance is added to the cache
183: addInstance(name, navigation);
184: }
185: }
186: return navigation;
187: }
188:
189: /**
190: * The method through which this class is accessed.
191: *
192: * @return The single instance of this class.
193: */
194: public static NavigationLoader getInstance() {
195: return instance;
196: }
197: }
|