View Javadoc
1   package org.apache.turbine.modules.pages;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.apache.turbine.annotation.TurbineLoader;
28  import org.apache.turbine.modules.Action;
29  import org.apache.turbine.modules.ActionLoader;
30  import org.apache.turbine.modules.Layout;
31  import org.apache.turbine.modules.LayoutLoader;
32  import org.apache.turbine.modules.Page;
33  import org.apache.turbine.modules.Screen;
34  import org.apache.turbine.modules.ScreenLoader;
35  import org.apache.turbine.pipeline.PipelineData;
36  import org.apache.turbine.util.RunData;
37  
38  /**
39   * When building sites using templates, Screens need only be defined
40   * for templates which require dynamic (database or object) data.
41   *
42   * <p>
43   *
44   * This page can be used on sites where the number of Screens can be
45   * much less than the number of templates.  The templates can be
46   * grouped in directories with common layouts.  Screen modules are
47   * then expected to be placed in packages corresponding with the
48   * templates' directories and follow a specific naming scheme.
49   *
50   * <p>
51   *
52   * The template parameter is parsed and and a Screen whose package
53   * matches the templates path and shares the same name minus any
54   * extension and beginning with a capital letter is searched for.  If
55   * not found, a Screen in a package matching the template's path with
56   * name Default is searched for.  If still not found, a Screen with
57   * name Default is looked for in packages corresponding to parent
58   * directories in the template's path until a match is found.
59   *
60   * <p>
61   *
62   * For example if data.getParameters().getString("template") returns
63   * /about_us/directions/driving.wm, the search follows
64   * about_us.directions.Driving, about_us.directions.Default,
65   * about_us.Default, Default, VelocitySiteScreen.
66   *
67   * <p>
68   *
69   * Only one Layout module is used, since it is expected that any
70   * dynamic content will be placed in navigations and screens.  The
71   * layout template to be used is found in a similar way to the Screen.
72   * For example the following paths will be searched in the layouts
73   * subdirectory: /about_us/directions/driving.wm,
74   * /about_us/directions/default.wm, /about_us/default.wm, /default.wm.
75   *
76   * <p>
77   *
78   * This approach allows a site with largely static content to be
79   * updated and added to regularly by those with little Java
80   * experience.
81   *
82   * <p>
83   *
84   * The code is an almost a complete clone of the FreeMarkerSitePage
85   * written by John McNally.  I've only modified it for Template use.
86   *
87   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
88   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
89   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
90   * @version $Id$
91   */
92  public class DefaultPage implements Page
93  {
94      /** Logging */
95      protected final Logger log = LogManager.getLogger(this.getClass());
96  
97      /** Injected loader instance */
98      @TurbineLoader( Action.class )
99      protected ActionLoader actionLoader;
100 
101     /** Injected loader instance */
102     @TurbineLoader( Screen.class )
103     protected ScreenLoader screenLoader;
104 
105     /** Injected loader instance */
106     @TurbineLoader( Layout.class )
107     protected LayoutLoader layoutLoader;
108 
109     /**
110      * Builds the Page.
111      *
112      * @param pipelineData Turbine information.
113      * @throws Exception a generic exception.
114      */
115     @Override
116     public void doBuild(PipelineData pipelineData)
117             throws Exception
118     {
119         RunData data = pipelineData.getRunData();
120         // Template pages can use this to set up the context, so it is
121         // available to the Action and Screen.  It does nothing here.
122         doBuildBeforeAction(pipelineData);
123 
124         // If an action has been defined, execute it here.  Actions
125         // can re-define the template definition.
126         if (data.hasAction())
127         {
128             actionLoader.exec(pipelineData, data.getAction());
129         }
130 
131         // if a redirect was setup in data, don't do anything else
132         if (StringUtils.isNotEmpty(data.getRedirectURI()))
133         {
134             return;
135         }
136 
137         // Template pages can use this to set up default templates and
138         // associated class modules.  It does nothing here.
139         doBuildAfterAction(pipelineData);
140 
141         String screenName = data.getScreen();
142 
143         log.debug("Building {}", screenName);
144 
145         // Ask the Screen for its Layout and then execute the Layout.
146         // The Screen can override the getLayout() method to re-define
147         // the Layout depending on data passed in via the
148         // data.parameters object.
149         Screen aScreen = screenLoader.getAssembler(screenName);
150         String layout = aScreen.getLayout(pipelineData);
151 
152         // If the Layout has been set to be null, attempt to execute
153         // the Screen that has been defined.
154         if (layout != null)
155         {
156             layoutLoader.exec(pipelineData, layout);
157         }
158         else
159         {
160             screenLoader.exec(pipelineData, screenName);
161         }
162 
163         // Do any post build actions (overridable by subclasses -
164         // does nothing here).
165         doPostBuild(pipelineData);
166     }
167 
168     /**
169      * Can be used by template Pages to stuff the Context into the
170      * PipelineData so that it is available to the Action module and the
171      * Screen module via getContext().  It does nothing here.
172      *
173      * @param pipelineData Turbine information.
174      * @throws Exception a generic exception.
175      */
176     protected void doBuildBeforeAction(PipelineData pipelineData)
177             throws Exception
178     {
179         // do nothing by default
180     }
181 
182     /**
183      * Can be overridden by template Pages to set up data needed to
184      * process a template.  It does nothing here.
185      *
186      * @param pipelineData Turbine information.
187      * @throws Exception a generic exception.
188      */
189     protected void doBuildAfterAction(PipelineData pipelineData)
190             throws Exception
191     {
192         // do nothing by default
193     }
194 
195     /**
196      * Can be overridden to perform actions when the request is
197      * fully processed. It does nothing here.
198      *
199      * @param pipelineData Turbine information.
200      * @throws Exception a generic exception.
201      */
202     protected void doPostBuild(PipelineData pipelineData)
203             throws Exception
204     {
205         // do nothing by default
206     }
207 }