KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > pipeline > DefaultTargetValve


1 package org.apache.turbine.pipeline;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.IOException JavaDoc;
58
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61 import org.apache.commons.configuration.Configuration;
62 import org.apache.turbine.RunData;
63 import org.apache.turbine.TemplateContext;
64 import org.apache.turbine.Turbine;
65 import org.apache.turbine.TurbineException;
66 import org.apache.turbine.ValveContext;
67 import org.apache.turbine.modules.Module;
68
69 /**
70  * Implements the RunData target portion of the "Turbine classic"
71  * processing pipeline (from the Turbine 2.x series).
72  *
73  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
74  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
75  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
76  * @author <a HREF="mailto:mikeh@apache.org">Mike Haberman</a>
77  * @author <a HREF="mailto:james@jamestaylor.org">James Taylor</a>
78  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
79  * @version $Id: DefaultTargetValve.java,v 1.19 2004/11/01 20:08:41 epugh Exp $
80  */

81 public class DefaultTargetValve
82     extends AbstractValve
83 {
84     private static final Log log = LogFactory.getLog(DefaultTargetValve.class);
85
86     protected static final String JavaDoc DEFAULT_MODULE_TYPE = "screens";
87
88     protected String JavaDoc targetModuleType = DEFAULT_MODULE_TYPE;
89
90     /**
91      * Creates a new instance with the <code>targetModuleType</code>
92      * and <code>runner</code> members filled in from Turbine's
93      * configuration.
94      */

95     public DefaultTargetValve()
96     {
97
98     }
99     
100     
101
102     /* (non-Javadoc)
103      * @see org.apache.turbine.Valve#initialize()
104      */

105     public void initialize() throws Exception JavaDoc {
106         Configuration cfg = Turbine.getConfiguration();
107
108         if (cfg != null)
109         {
110             // Get the module type
111
targetModuleType =
112                 cfg.getString("pipeline.default.targetModuleType",
113                               DEFAULT_MODULE_TYPE);
114         }
115     }
116     /**
117      * @see org.apache.turbine.Valve#invoke(RunData, ValveContext)
118      */

119     public void invoke(RunData data, ValveContext context)
120         throws IOException JavaDoc, TurbineException
121     {
122         try
123         {
124             execute(data);
125         }
126         catch (Exception JavaDoc e)
127         {
128             throw new TurbineException(e);
129         }
130
131         // Pass control to the next Valve in the Pipeline
132
context.invokeNext(data);
133     }
134
135     /**
136      * Executes the <code>Valve</code>.
137      *
138      * @param data The run-time data.
139      */

140     protected void execute(RunData data)
141         throws Exception JavaDoc
142     {
143         // Get the target that will determine the template / layout
144
String JavaDoc target = data.getTarget();
145         if ( target != null )
146         {
147             data.getResponse().setLocale(data.getLocale());
148             data.getResponse().setContentType(data.getContentType());
149
150             // Get the template context, already populated by any context
151
// builders that were found for this target
152
TemplateContext context = Module.getTemplateContext( data );
153             context.put( "template", target );
154
155             // Resolve the layout template for this target
156

157             String JavaDoc layout = Turbine.getResolver()
158                 .getTemplate("layouts", target);
159
160             // Use the renderer to render the layout,
161

162             render( data, context, layout );
163         }
164         else
165         {
166             if ( log.isDebugEnabled() )
167             {
168                 log.debug( "Target was null." );
169             }
170         }
171     }
172
173     protected void render( RunData data, TemplateContext context, String JavaDoc target )
174         throws Exception JavaDoc
175     {
176         if ( log.isDebugEnabled() )
177         {
178             log.debug( "Rendering target " + target );
179         }
180
181         Renderer r = new Renderer( data );
182
183         // FIXME: Can we remove hardcoding here?
184

185         context.put( "renderer", r );
186
187         // Render the target
188

189         String JavaDoc out = r.render( target );
190
191         // Write the composed string to the response
192

193         data.getOut().print( out );
194     }
195 }
196
197
Popular Tags