View Javadoc

1   package org.apache.turbine.modules;
2   
3   /*
4    * Copyright 2001-2004 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.turbine.Turbine;
25  import org.apache.turbine.TurbineConstants;
26  import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
27  import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
28  import org.apache.turbine.util.ObjectUtils;
29  import org.apache.turbine.util.RunData;
30  
31  /***
32   * The purpose of this class is to allow one to load and execute
33   * Layout modules.
34   *
35   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
36   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37   * @version $Id: LayoutLoader.java,v 1.9.2.3 2004/08/16 22:57:50 henning Exp $
38   */
39  public class LayoutLoader
40      extends GenericLoader
41      implements Loader
42  {
43      /*** Logging */
44      private static Log log = LogFactory.getLog(LayoutLoader.class);
45  
46      /*** The single instance of this class. */
47      private static LayoutLoader instance =
48          new LayoutLoader(Turbine.getConfiguration()
49                           .getInt(TurbineConstants.LAYOUT_CACHE_SIZE_KEY,
50                                   TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT));
51  
52      /*** The Assembler Broker Service */
53      private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
54  
55      /***
56       * These ctor's are private to force clients to use getInstance()
57       * to access this class.
58       */
59      private LayoutLoader()
60      {
61          super();
62      }
63  
64      /***
65       * These ctor's are private to force clients to use getInstance()
66       * to access this class.
67       */
68      private LayoutLoader(int i)
69      {
70          super(i);
71      }
72  
73      /***
74       * Adds an instance of an object into the hashtable.
75       *
76       * @param name Name of object.
77       * @param layout Layout to be associated with name.
78       */
79      private void addInstance(String name, Layout layout)
80      {
81          if (cache())
82          {
83              this.put(name, (Layout) layout);
84          }
85      }
86  
87      /***
88       * Attempts to load and execute the external layout.
89       *
90       * @param data Turbine information.
91       * @param name Name of object that will execute the layout.
92       * @exception Exception a generic exception.
93       */
94      public void exec(RunData data, String name)
95              throws Exception
96      {
97          // Execute layout
98          getInstance(name).build(data);
99      }
100 
101     /***
102      * Pulls out an instance of the object by name.  Name is just the
103      * single name of the object. This is equal to getInstance but
104      * returns an Assembler object and is needed to fulfil the Loader
105      * interface.
106      *
107      * @param name Name of object instance.
108      * @return A Layout with the specified name, or null.
109      * @exception Exception a generic exception.
110      */
111     public Assembler getAssembler(String name)
112         throws Exception
113     {
114         return getInstance(name);
115     }
116 
117     /***
118      * Pulls out an instance of the Layout by name.  Name is just the
119      * single name of the Layout.
120      *
121      * @param name Name of requested Layout
122      * @return A Layout with the specified name, or null.
123      * @exception Exception a generic exception.
124      */
125     public Layout getInstance(String name)
126             throws Exception
127     {
128         Layout layout = null;
129 
130         // Check if the layout is already in the cache
131         if (cache() && this.containsKey(name))
132         {
133             layout = (Layout) this.get(name);
134             log.debug("Found Layout " + name + " in the cache!");
135         }
136         else
137         {
138             log.debug("Loading Layout " + name + " from the Assembler Broker");
139 
140             try
141             {
142                 if (ab != null)
143                 {
144                     // Attempt to load the layout
145                     layout = (Layout) ab.getAssembler(
146                         AssemblerBrokerService.LAYOUT_TYPE, name);
147                 }
148             }
149             catch (ClassCastException cce)
150             {
151                 // This can alternatively let this exception be thrown
152                 // So that the ClassCastException is shown in the
153                 // browser window.  Like this it shows "Screen not Found"
154                 layout = null;
155             }
156 
157             if (layout == null)
158             {
159                 // If we did not find a screen we should try and give
160                 // the user a reason for that...
161                 // FIX ME: The AssemblerFactories should each add it's
162                 // own string here...
163                 List packages = Turbine.getConfiguration()
164                     .getList(TurbineConstants.MODULE_PACKAGES);
165 
166                 ObjectUtils.addOnce(packages,
167                         GenericLoader.getBasePackage());
168 
169                 throw new ClassNotFoundException(
170                         "\n\n\tRequested Layout not found: " + name +
171                         "\n\tTurbine looked in the following " +
172                         "modules.packages path: \n\t" + packages.toString() + "\n");
173             }
174             else if (cache())
175             {
176                 // The new instance is added to the cache
177                 addInstance(name, layout);
178             }
179         }
180         return layout;
181     }
182 
183     /***
184      * The method through which this class is accessed.
185      *
186      * @return The single instance of this class.
187      */
188     public static LayoutLoader getInstance()
189     {
190         return instance;
191     }
192 }