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