1 package org.apache.turbine.modules;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 // Java Core Classes
58 import java.util.Vector;
59
60 // Turbine Utility Classes
61 import org.apache.turbine.TurbineConstants;
62 import org.apache.turbine.services.resources.TurbineResources;
63 import org.apache.turbine.services.TurbineServices;
64 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
65 import org.apache.turbine.util.ObjectUtils;
66 import org.apache.turbine.util.RunData;
67
68 // ECS
69 import org.apache.ecs.ConcreteElement;
70
71 /***
72 * The purpose of this class is to allow one to load and execute
73 * Navigation modules.
74 *
75 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
76 * @version $Id: NavigationLoader.java,v 1.1.1.1 2001/08/16 05:08:29 jvanzyl Exp $
77 */
78 public class NavigationLoader extends GenericLoader
79 {
80 /*** The single instance of this class. */
81 private static NavigationLoader instance = new NavigationLoader(
82 TurbineResources.getInt(TurbineConstants.NAVIGATION_CACHE_SIZE, 10));
83
84 /***
85 * These ctor's are private to force clients to use getInstance()
86 * to access this class.
87 */
88 private NavigationLoader()
89 {
90 super();
91 }
92
93 /***
94 * These ctor's are private to force clients to use getInstance()
95 * to access this class.
96 */
97 private NavigationLoader(int i)
98 {
99 super(i);
100 }
101
102 /***
103 * Adds an instance of an object into the hashtable.
104 *
105 * @param name Name of object.
106 * @param navigation Navigation to be associated with name.
107 */
108 private void addInstance( String name, Navigation navigation )
109 {
110 if ( cache() )
111 this.put ( name, (Navigation) navigation );
112 }
113
114 /***
115 * Attempts to load and execute the external Navigation. This is
116 * used when you want to execute a Navigation which returns its
117 * output via a MultiPartElement instead of out the data.getPage()
118 * value. This allows you to easily chain the execution of
119 * Navigation modules together.
120 *
121 * @param data Turbine information.
122 * @param name Name of object that will execute the navigation.
123 * @exception Exception a generic exception.
124 */
125 public ConcreteElement eval( RunData data, String name )
126 throws Exception
127 {
128 // Execute Navigation
129 return getInstance(name).build(data);
130 }
131
132 /***
133 * Attempts to load and execute the external Navigation.
134 *
135 * @param data Turbine information.
136 * @param name Name of object instance.
137 * @exception Exception a generic exception.
138 */
139 public void exec( RunData data, String name )
140 throws Exception
141 {
142 this.eval( data, name );
143 }
144
145 /***
146 * Pulls out an instance of the object by name. Name is just the
147 * single name of the object.
148 *
149 * @param name Name of object instance.
150 * @return A Layout with the specified name, or null.
151 * @exception Exception a generic exception.
152 */
153 public Navigation getInstance(String name)
154 throws Exception
155 {
156 Navigation navigation = null;
157
158 if ( cache() && this.containsKey( name ) )
159 {
160 navigation = (Navigation) this.get( name );
161 }
162 else
163 {
164 // We get the broker service
165 AssemblerBrokerService ab =
166 (AssemblerBrokerService)TurbineServices.getInstance()
167 .getService (AssemblerBrokerService.SERVICE_NAME);
168
169 try
170 {
171 // Attempt to load the screen
172 navigation = (Navigation) ab.getAssembler(
173 AssemblerBrokerService.NAVIGATION_TYPE,name);
174 }
175 catch (ClassCastException cce)
176 {
177 // This can alternatively let this exception be thrown
178 // So that the ClassCastException is shown in the
179 // browser window. Like this it shows "Screen not Found"
180 navigation = null;
181 }
182
183 if (navigation == null)
184 {
185 // If we did not find a screen we should try and give
186 // the user a reason for that...
187 // FIX ME: The AssemblerFactories should each add it's own
188 // string here...
189 Vector packages = TurbineResources.getVector(
190 TurbineConstants.MODULE_PACKAGES);
191 ObjectUtils.addOnce( packages,
192 GenericLoader.getBasePackage() );
193
194 throw new ClassNotFoundException(
195 "\n\n\tRequested Navigation not found: " + name + "\n" +
196 "\tTurbine looked in the following modules.packages " +
197 "path: \n\t" + packages.toString() + "\n");
198 }
199 else if ( cache() )
200 {
201 // The new instance is added to the cache
202 addInstance( name, navigation );
203 }
204 }
205 return navigation;
206 }
207
208 /***
209 * The method through which this class is accessed.
210 *
211 * @return The single instance of this class.
212 */
213 public static NavigationLoader getInstance()
214 {
215 return instance;
216 }
217 }
This page was automatically generated by Maven