KickJava   Java API By Example, From Geeks To Geeks.

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


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.turbine.Turbine;
60 import org.apache.turbine.RunData;
61 import org.apache.turbine.TurbineException;
62 import org.apache.turbine.ValveContext;
63 import org.apache.turbine.Resolver;
64 import org.apache.turbine.modules.Module;
65
66 import org.apache.commons.logging.Log;
67 import org.apache.commons.logging.LogFactory;
68
69 /**
70  * Valve which determines the target for a request and runs the modules
71  * associated with that target. This valve is intended to model a portion
72  * of the 'Turbine classic pipeline' as defined by the behavior of turbine
73  * release 2
74  *
75  * @author <a HREF="mailto:jtaylor@apache.org">James Taylor</a>
76  * @version $Id: RunModulesValve.java,v 1.8 2003/11/23 00:42:22 mpoeschl Exp $
77  */

78 public class RunModulesValve
79     extends AbstractValve
80 {
81     private static final Log log = LogFactory.getLog( RunModulesValve.class );
82     
83     /**
84      * The default target module used for building up the
85      * context.
86      */

87     private String JavaDoc targetModuleType;
88     
89     /**
90      * Initialize this valve for use in the pipeline.
91      */

92     public void initialize()
93         throws Exception JavaDoc
94     {
95         targetModuleType = Turbine.getConfiguration()
96             .getString( "pipeline.default.targetModuleType" );
97     }
98
99     /**
100      * @see org.apache.turbine.Valve#invoke(RunData, ValveContext)
101      */

102     public void invoke( RunData data, ValveContext context )
103         throws IOException JavaDoc, TurbineException
104     {
105         try
106         {
107             runModules( targetModuleType, data );
108         }
109         catch ( Exception JavaDoc e )
110         {
111             throw new TurbineException( "Failure in RunModulesValve", e );
112         }
113
114         // Pass control to the next Valve in the Pipeline
115
context.invokeNext( data );
116     }
117     
118     /**
119      * Runs the module chain associated with the target
120      */

121     public void runModules( String JavaDoc moduleType, RunData data )
122         throws Exception JavaDoc
123     {
124         String JavaDoc target;
125         String JavaDoc oldTarget = "";
126         
127         Resolver resolver = Turbine.getResolver();
128         
129         Module module;
130         
131         target = normalizeTarget( data.getTarget() );
132         
133         while( ! oldTarget.equals( target ) && target != null )
134         {
135             module = resolver.getModule( moduleType, target );
136
137             if ( log.isDebugEnabled() )
138             {
139                 log.debug( "Executing module " + module.getClass().getName()
140                            + " for target: " + target );
141             }
142
143             module.execute( data );
144
145             // Modules might change the target:
146
oldTarget = target;
147             target = data.getTarget();
148             if ( target != null )
149             {
150                 target = normalizeTarget(target);
151             }
152         }
153         
154         // Set the target to the final normalized path, which will be used by
155
// the resolver and the appropriate TemplateEngineService to locate the
156
// template.
157

158         if ( log.isDebugEnabled() )
159         {
160             log.debug( "Setting target to " + target );
161         }
162         
163         data.setTarget( target );
164     }
165     
166     /**
167      * Normalizes the target path to the format used internally by the turbine
168      * classic style valves and resolvers. ',' will be translated to '/',
169      * multiple path seperators will be collapsed, and the result is guranteed
170      * not to begin with a path seperator.
171      */

172     private String JavaDoc normalizeTarget( String JavaDoc target )
173         throws Exception JavaDoc
174     {
175         if (target == null || target.length() == 0)
176         {
177             throw new Exception JavaDoc("RunModulesValve::normalizeTarget() target is null!");
178         }
179
180         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( target );
181
182         char c;
183
184         int len = buffer.length();
185         
186         boolean lastWasDelim = true;
187         
188         for ( int j = 0; j < len; j++ )
189         {
190             c = buffer.charAt( j );
191             
192             if ( c == ',' || c == '/' )
193             {
194                 if ( lastWasDelim )
195                 {
196                     buffer.deleteCharAt( j );
197                     
198                     j--;
199                     len--;
200                 }
201                 else
202                 {
203                     buffer.setCharAt( j, '/' );
204                 }
205                 
206                 lastWasDelim = true;
207             }
208             else
209             {
210                 lastWasDelim = false;
211             }
212         }
213         
214         if (len == 0)
215         {
216             new Exception JavaDoc( "Syntax error in target: '" + target + '\'');
217         }
218         
219         return buffer.toString();
220     }
221 }
222
Popular Tags