1 package org.apache.velocity.app.event; 2 3 18 19 import org.apache.velocity.context.InternalEventContext; 20 import org.apache.velocity.context.Context; 21 22 29 public class EventCartridge implements ReferenceInsertionEventHandler, 30 NullSetEventHandler, 31 MethodExceptionEventHandler 32 { 33 private ReferenceInsertionEventHandler rieh = null; 34 private NullSetEventHandler nseh = null; 35 private MethodExceptionEventHandler meeh = null; 36 37 45 public boolean addEventHandler( EventHandler ev ) 46 { 47 if (ev == null) 48 { 49 return false; 50 } 51 52 boolean found = false; 53 54 if ( ev instanceof ReferenceInsertionEventHandler) 55 { 56 rieh = (ReferenceInsertionEventHandler) ev; 57 found = true; 58 } 59 60 if ( ev instanceof NullSetEventHandler ) 61 { 62 nseh = (NullSetEventHandler) ev; 63 found = true; 64 } 65 66 if ( ev instanceof MethodExceptionEventHandler ) 67 { 68 meeh = (MethodExceptionEventHandler) ev; 69 found = true; 70 } 71 72 return found; 73 } 74 75 83 public boolean removeEventHandler(EventHandler ev) 84 { 85 if ( ev == null ) 86 { 87 return false; 88 } 89 90 boolean found = false; 91 92 if (ev == rieh) 93 { 94 rieh = null; 95 found = true; 96 } 97 98 if (ev == nseh) 99 { 100 nseh = null; 101 found = true; 102 } 103 104 if (ev == meeh) 105 { 106 meeh = null; 107 found = true; 108 } 109 110 return found; 111 } 112 113 124 public Object referenceInsert( String reference, Object value ) 125 { 126 if (rieh == null) 127 { 128 return value; 129 } 130 131 return rieh.referenceInsert( reference, value ); 132 } 133 134 145 public boolean shouldLogOnNullSet( String lhs, String rhs ) 146 { 147 if ( nseh == null) 148 { 149 return true; 150 } 151 152 return nseh.shouldLogOnNullSet( lhs, rhs ); 153 } 154 155 167 public Object methodException( Class claz, String method, Exception e ) 168 throws Exception 169 { 170 173 if (meeh == null) 174 { 175 throw e; 176 } 177 178 181 return meeh.methodException( claz, method, e ); 182 } 183 184 192 public final boolean attachToContext( Context context ) 193 { 194 if ( context instanceof InternalEventContext ) 195 { 196 InternalEventContext iec = (InternalEventContext) context; 197 198 iec.attachEventCartridge( this ); 199 200 return true; 201 } 202 else 203 { 204 return false; 205 } 206 } 207 } 208 | Popular Tags |