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.ArrayList;
23  import java.util.List;
24  
25  import org.apache.turbine.Turbine;
26  import org.apache.turbine.TurbineConstants;
27  import org.apache.turbine.pipeline.PipelineData;
28  import org.apache.turbine.services.TurbineServices;
29  import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
30  
31  /**
32   * This is the base class for the loaders. It contains code that is
33   * used across all of the loaders. It also specifies the interface
34   * that is required to be called a Loader.
35   *
36   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
37   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
39   * @param <T> the specialized assembler type
40   */
41  public abstract class GenericLoader<T extends Assembler>
42  {
43      /** The Assembler Broker Service */
44      protected AssemblerBrokerService ab;
45  
46      /** @serial This can be serialized */
47      private boolean reload = false;
48  
49      /** Base packages path for Turbine */
50      private static final String TURBINE_PACKAGE = "org.apache.turbine.modules";
51  
52      /** Packages paths for Turbine */
53      private static List<String> TURBINE_PACKAGES = null;
54  
55      /**
56       * Basic constructor for creating a loader.
57       */
58      public GenericLoader()
59      {
60          super();
61          ab = (AssemblerBrokerService)TurbineServices.getInstance().getService(AssemblerBrokerService.SERVICE_NAME);
62      }
63  
64      /**
65       * Attempts to load and execute the external action that has been
66       * set.
67       * @param pipelineData the Turbine request
68       * @param name the name of the assembler module
69       * @throws Exception a generic exception.
70       */
71      public abstract void exec(PipelineData pipelineData, String name)
72              throws Exception;
73  
74      /**
75       * Returns whether or not this external action is reload itself.
76       * This is in cases where the Next button would be clicked, but
77       * since we are checking for that, we would go into an endless
78       * loop.
79       *
80       * @return True if the action is reload.
81       */
82      public boolean reload()
83      {
84          return this.reload;
85      }
86  
87      /**
88       * Sets whether or not this external action is reload itself.
89       * This is in cases where the Next button would be clicked, but
90       * since we are checking for that, we would go into an endless
91       * loop.
92       *
93       * @param reload True if the action must be marked as reload.
94       * @return Itself.
95       */
96      public GenericLoader<T> setReload(boolean reload)
97      {
98          this.reload = reload;
99          return this;
100     }
101 
102     /**
103      * Gets the base package where Turbine should find its default
104      * modules.
105      *
106      * @return A String with the base package name.
107      */
108     public static String getBasePackage()
109     {
110         return TURBINE_PACKAGE;
111     }
112 
113     /**
114      * Gets the package list where Turbine should find its
115      * modules.
116      *
117      * @return A List with the package names (including the base package).
118      */
119     public static List<String> getPackages()
120     {
121         if (TURBINE_PACKAGES == null)
122         {
123             List<String> turbinePackages = new ArrayList<String>();
124             List<Object> configTurbinePackages =
125                 Turbine.getConfiguration()
126                 	.getList(TurbineConstants.MODULE_PACKAGES);
127             for (Object o : configTurbinePackages)
128             {
129             	turbinePackages.add((String)o);
130             }
131 
132             TURBINE_PACKAGES = turbinePackages;
133         }
134 
135         List<String> packages = TURBINE_PACKAGES;
136 
137         if (!packages.contains(TURBINE_PACKAGE))
138         {
139             packages.add(TURBINE_PACKAGE);
140         }
141 
142         return packages;
143     }
144 
145     /**
146      * Pulls out an instance of the object by name.  Name is just the
147      * single name of the object.
148      *
149      * @param type Type of the assembler.
150      * @param name Name of object instance.
151      * @return A Screen with the specified name, or null.
152      * @throws Exception a generic exception.
153      */
154     protected T getAssembler(Class<T> type, String name)
155         throws Exception
156     {
157         T asm = null;
158 
159         try
160         {
161             if (ab != null)
162             {
163                 // Attempt to load the assembler
164                 asm = ab.getAssembler(type, name);
165             }
166         }
167         catch (ClassCastException cce)
168         {
169             // This can alternatively let this exception be thrown
170             // So that the ClassCastException is shown in the
171             // browser window.  Like this it shows "Screen not Found"
172             asm = null;
173         }
174 
175         if (asm == null)
176         {
177             // If we did not find a screen we should try and give
178             // the user a reason for that...
179             // FIX ME: The AssemblerFactories should each add it's
180             // own string here...
181             List<String> packages = GenericLoader.getPackages();
182 
183             throw new ClassNotFoundException(
184                     "\n\n\tRequested " + type + " not found: " + name +
185                     "\n\tTurbine looked in the following " +
186                     "modules.packages path: \n\t" + packages.toString() + "\n");
187         }
188 
189         return asm;
190     }
191 }