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