View Javadoc

1   package org.apache.turbine.modules;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.apache.ecs.ConcreteElement;
25  
26  import org.apache.turbine.Turbine;
27  import org.apache.turbine.TurbineConstants;
28  import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
29  import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
30  import org.apache.turbine.util.ObjectUtils;
31  import org.apache.turbine.util.RunData;
32  
33  /***
34   * The purpose of this class is to allow one to load and execute
35   * Navigation modules.
36   *
37   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
38   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39   * @version $Id: NavigationLoader.java 264148 2005-08-29 14:21:04Z henning $
40   */
41  public class NavigationLoader
42      extends GenericLoader
43      implements Loader
44  {
45      /*** Serial Version UID */
46      private static final long serialVersionUID = 5062299200802529612L;
47  
48      /*** Logging */
49      private static Log log = LogFactory.getLog(NavigationLoader.class);
50  
51      /*** The single instance of this class. */
52      private static NavigationLoader instance =
53          new NavigationLoader(Turbine.getConfiguration()
54                           .getInt(TurbineConstants.NAVIGATION_CACHE_SIZE_KEY,
55                                   TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT));
56  
57      /*** The Assembler Broker Service */
58      private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
59  
60      /***
61       * These ctor's are private to force clients to use getInstance()
62       * to access this class.
63       */
64      private NavigationLoader()
65      {
66          super();
67      }
68  
69      /***
70       * These ctor's are private to force clients to use getInstance()
71       * to access this class.
72       */
73      private NavigationLoader(int i)
74      {
75          super(i);
76      }
77  
78      /***
79       * Adds an instance of an object into the hashtable.
80       *
81       * @param name Name of object.
82       * @param navigation Navigation to be associated with name.
83       */
84      private void addInstance(String name, Navigation navigation)
85      {
86          if (cache())
87          {
88              this.put(name, (Navigation) navigation);
89          }
90      }
91  
92      /***
93       * Attempts to load and execute the external Navigation. This is
94       * used when you want to execute a Navigation which returns its
95       * output via a MultiPartElement instead of out the data.getPage()
96       * value.  This allows you to easily chain the execution of
97       * Navigation modules together.
98       *
99       * @param data Turbine information.
100      * @param name Name of object that will execute the navigation.
101      * @exception Exception a generic exception.
102      */
103     public ConcreteElement eval(RunData data, String name)
104             throws Exception
105     {
106         // Execute Navigation
107         return getInstance(name).build(data);
108     }
109 
110     /***
111      * Attempts to load and execute the external Navigation.
112      *
113      * @param data Turbine information.
114      * @param name Name of object instance.
115      * @exception Exception a generic exception.
116      */
117     public void exec(RunData data, String name)
118             throws Exception
119     {
120         this.eval(data, name);
121     }
122 
123     /***
124      * Pulls out an instance of the object by name.  Name is just the
125      * single name of the object. This is equal to getInstance but
126      * returns an Assembler object and is needed to fulfil the Loader
127      * interface.
128      *
129      * @param name Name of object instance.
130      * @return A Layout with the specified name, or null.
131      * @exception Exception a generic exception.
132      */
133     public Assembler getAssembler(String name)
134         throws Exception
135     {
136         return getInstance(name);
137     }
138 
139     /***
140      * Pulls out an instance of the Navigation by name.  Name is just the
141      * single name of the Navigation.
142      *
143      * @param name Name of requested Navigation
144      * @return A Navigation with the specified name, or null.
145      * @exception Exception a generic exception.
146      */
147     public Navigation getInstance(String name)
148             throws Exception
149     {
150         Navigation navigation = null;
151 
152         // Check if the navigation is already in the cache
153         if (cache() && this.containsKey(name))
154         {
155             navigation = (Navigation) this.get(name);
156             log.debug("Found Navigation " + name + " in the cache!");
157         }
158         else
159         {
160             log.debug("Loading Navigation " + name + " from the Assembler Broker");
161 
162             try
163             {
164                 if (ab != null)
165                 {
166                     // Attempt to load the navigation
167                     navigation = (Navigation) ab.getAssembler(
168                         AssemblerBrokerService.NAVIGATION_TYPE, name);
169                 }
170             }
171             catch (ClassCastException cce)
172             {
173                 // This can alternatively let this exception be thrown
174                 // So that the ClassCastException is shown in the
175                 // browser window.  Like this it shows "Screen not Found"
176                 navigation = null;
177             }
178 
179             if (navigation == null)
180             {
181                 // If we did not find a screen we should try and give
182                 // the user a reason for that...
183                 // FIX ME: The AssemblerFactories should each add it's
184                 // own string here...
185                 List packages = Turbine.getConfiguration()
186                     .getList(TurbineConstants.MODULE_PACKAGES);
187 
188                 ObjectUtils.addOnce(packages,
189                         GenericLoader.getBasePackage());
190 
191                 throw new ClassNotFoundException(
192                         "\n\n\tRequested Navigation not found: " + name +
193                         "\n\tTurbine looked in the following " +
194                         "modules.packages path: \n\t" + packages.toString() + "\n");
195             }
196             else if (cache())
197             {
198                 // The new instance is added to the cache
199                 addInstance(name, navigation);
200             }
201         }
202         return navigation;
203     }
204 
205     /***
206      * The method through which this class is accessed.
207      *
208      * @return The single instance of this class.
209      */
210     public static NavigationLoader getInstance()
211     {
212         return instance;
213     }
214 }