01: package org.apache.velocity.app.event;
02:
03: import org.apache.velocity.context.Context;
04: import org.apache.velocity.util.ContextAware;
05:
06: /*
07: * Licensed to the Apache Software Foundation (ASF) under one
08: * or more contributor license agreements. See the NOTICE file
09: * 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: * Called when the RHS of a #set() is null, which will result
36: * in a null LHS. All NullSetEventHandlers
37: * are called in sequence until a false is returned. If no NullSetEventHandler
38: * is registered all nulls will be logged.
39: *
40: * @param lhs reference literal of left-hand-side of set statement
41: * @param rhs reference literal of right-hand-side of set statement
42: * @return true if log message should be written, false otherwise
43: */
44: public boolean shouldLogOnNullSet(String lhs, String rhs);
45:
46: /**
47: * Defines the execution strategy for shouldLogOnNullSet
48: */
49: static class ShouldLogOnNullSetExecutor implements
50: EventHandlerMethodExecutor {
51: private Context context;
52: private String lhs;
53: private String rhs;
54:
55: /**
56: * when this is false, quit iterating
57: */
58: private boolean result = true;
59: private boolean executed = false;
60:
61: ShouldLogOnNullSetExecutor(Context context, String lhs,
62: String rhs) {
63: this .context = context;
64: this .lhs = lhs;
65: this .rhs = rhs;
66: }
67:
68: /**
69: * Call the method shouldLogOnNullSet()
70: *
71: * @param handler call the appropriate method on this handler
72: */
73: public void execute(EventHandler handler) {
74: NullSetEventHandler eh = (NullSetEventHandler) handler;
75:
76: if (eh instanceof ContextAware)
77: ((ContextAware) eh).setContext(context);
78:
79: executed = true;
80: result = ((NullSetEventHandler) handler)
81: .shouldLogOnNullSet(lhs, rhs);
82: }
83:
84: public Object getReturnValue() {
85: return new Boolean(result);
86: }
87:
88: public boolean isDone() {
89: return executed && !result;
90: }
91:
92: }
93:
94: }
|