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