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 470256 2006-11-02 07:20:36Z wglass $
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 */
50 static class ShouldLogOnNullSetExecutor implements EventHandlerMethodExecutor
51 {
52 private Context context;
53 private String lhs;
54 private String rhs;
55
56 /**
57 * when this is false, quit iterating
58 */
59 private boolean result = true;
60 private boolean executed = false;
61
62 ShouldLogOnNullSetExecutor(
63 Context context,
64 String lhs,
65 String rhs)
66 {
67 this.context = context;
68 this.lhs = lhs;
69 this.rhs = rhs;
70 }
71
72 /**
73 * Call the method shouldLogOnNullSet()
74 *
75 * @param handler call the appropriate method on this handler
76 */
77 public void execute(EventHandler handler)
78 {
79 NullSetEventHandler eh = (NullSetEventHandler) handler;
80
81 if (eh instanceof ContextAware)
82 ((ContextAware) eh).setContext(context);
83
84 executed = true;
85 result = ((NullSetEventHandler) handler).shouldLogOnNullSet(lhs, rhs);
86 }
87
88 public Object getReturnValue()
89 {
90 return new Boolean(result);
91 }
92
93 public boolean isDone()
94 {
95 return executed && !result;
96 }
97
98
99 }
100
101 }