View Javadoc

1   package org.apache.velocity.app.event;
2   
3   import org.apache.velocity.context.Context;
4   import org.apache.velocity.util.ContextAware;
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.    
23   */
24  
25  /**
26   *  Event handler called when the RHS of #set is null.  Lets an app approve / veto
27   *  writing a log message based on the specific reference.
28   *
29   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
30   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
31   * @version $Id: NullSetEventHandler.java 685685 2008-08-13 21:43:27Z nbubna $
32   */
33  public interface NullSetEventHandler extends EventHandler
34  {
35      /**
36       * Called when the RHS of a #set() is null, which will result
37       * in a null LHS. All NullSetEventHandlers
38       * are called in sequence until a false is returned.  If no NullSetEventHandler
39       * is registered all nulls will be logged.
40       *
41       *  @param lhs  reference literal of left-hand-side of set statement
42       *  @param rhs  reference literal of right-hand-side of set statement
43       *  @return true if log message should be written, false otherwise
44       */
45      public boolean shouldLogOnNullSet( String lhs, String rhs );
46  
47      /**
48       * Defines the execution strategy for shouldLogOnNullSet
49       * @since 1.5
50       */
51      static class ShouldLogOnNullSetExecutor implements EventHandlerMethodExecutor
52      {
53          private Context context;
54          private String lhs;
55          private String rhs;
56  
57          /**
58           * when this is false, quit iterating
59           */
60          private boolean result = true;
61          private boolean executed = false;
62          
63          ShouldLogOnNullSetExecutor(
64                  Context context, 
65                  String lhs, 
66                  String rhs) 
67          {
68              this.context = context;
69              this.lhs = lhs;
70              this.rhs = rhs;
71          }
72  
73          /**
74           * Call the method shouldLogOnNullSet()
75           *  
76           * @param handler call the appropriate method on this handler
77           */
78          public void execute(EventHandler handler)
79          {
80              NullSetEventHandler eh = (NullSetEventHandler) handler;
81              
82              if (eh instanceof ContextAware)
83                  ((ContextAware) eh).setContext(context);
84  
85              executed = true;
86              result = ((NullSetEventHandler) handler).shouldLogOnNullSet(lhs, rhs); 
87          }
88  
89          public Object getReturnValue()
90          {            
91              // return new Boolean(result);
92              return result ? Boolean.TRUE : Boolean.FALSE;
93          }
94  
95          public boolean isDone()
96          {
97              return executed && !result;
98          }        
99          
100         
101     }
102 
103 }