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