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.assemblerbroker.AssemblerBrokerService;
29  import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
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 = TurbineAssemblerBroker.getService();
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      }
62  
63      /**
64       * Attempts to load and execute the external action that has been
65       * set.
66       * @param pipelineData the Turbine request
67       * @param name the name of the assembler module
68       * @exception Exception a generic exception.
69       */
70      public abstract void exec(PipelineData pipelineData, String name)
71              throws Exception;
72  
73      /**
74       * Returns whether or not this external action is reload itself.
75       * This is in cases where the Next button would be clicked, but
76       * since we are checking for that, we would go into an endless
77       * loop.
78       *
79       * @return True if the action is reload.
80       */
81      public boolean reload()
82      {
83          return this.reload;
84      }
85  
86      /**
87       * Sets whether or not this external action is reload itself.
88       * This is in cases where the Next button would be clicked, but
89       * since we are checking for that, we would go into an endless
90       * loop.
91       *
92       * @param reload True if the action must be marked as reload.
93       * @return Itself.
94       */
95      public GenericLoader<T> setReload(boolean reload)
96      {
97          this.reload = reload;
98          return this;
99      }
100 
101     /**
102      * Gets the base package where Turbine should find its default
103      * modules.
104      *
105      * @return A String with the base package name.
106      */
107     public static String getBasePackage()
108     {
109         return TURBINE_PACKAGE;
110     }
111 
112     /**
113      * Gets the package list where Turbine should find its
114      * modules.
115      *
116      * @return A List with the package names (including the base package).
117      */
118     public static List<String> getPackages()
119     {
120         if (TURBINE_PACKAGES == null)
121         {
122             List<String> turbinePackages = new ArrayList<String>();
123             List<Object> configTurbinePackages =
124                 Turbine.getConfiguration()
125                 	.getList(TurbineConstants.MODULE_PACKAGES);
126             for (Object o : configTurbinePackages)
127             {
128             	turbinePackages.add((String)o);
129             }
130 
131             TURBINE_PACKAGES = turbinePackages;
132         }
133 
134         List<String> packages = TURBINE_PACKAGES;
135 
136         if (!packages.contains(TURBINE_PACKAGE))
137         {
138             packages.add(TURBINE_PACKAGE);
139         }
140 
141         return packages;
142     }
143 
144     /**
145      * Pulls out an instance of the object by name.  Name is just the
146      * single name of the object.
147      *
148      * @param type Type of the assembler.
149      * @param name Name of object instance.
150      * @return A Screen with the specified name, or null.
151      * @exception Exception a generic exception.
152      */
153     protected T getAssembler(Class<T> type, String name)
154         throws Exception
155     {
156         T asm = null;
157 
158         try
159         {
160             if (ab != null)
161             {
162                 // Attempt to load the assembler
163                 asm = ab.getAssembler(type, name);
164             }
165         }
166         catch (ClassCastException cce)
167         {
168             // This can alternatively let this exception be thrown
169             // So that the ClassCastException is shown in the
170             // browser window.  Like this it shows "Screen not Found"
171             asm = null;
172         }
173 
174         if (asm == null)
175         {
176             // If we did not find a screen we should try and give
177             // the user a reason for that...
178             // FIX ME: The AssemblerFactories should each add it's
179             // own string here...
180             List<String> packages = GenericLoader.getPackages();
181 
182             throw new ClassNotFoundException(
183                     "\n\n\tRequested " + type + " not found: " + name +
184                     "\n\tTurbine looked in the following " +
185                     "modules.packages path: \n\t" + packages.toString() + "\n");
186         }
187 
188         return asm;
189     }
190 }