View Javadoc

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