1 29 30 package com.caucho.amber.gen; 31 32 import com.caucho.amber.entity.Listener; 33 import com.caucho.amber.type.ListenerType; 34 import com.caucho.bytecode.JClass; 35 import com.caucho.bytecode.JMethod; 36 import com.caucho.java.JavaWriter; 37 import com.caucho.java.gen.ClassComponent; 38 import com.caucho.util.L10N; 39 40 import java.io.IOException ; 41 import java.util.ArrayList ; 42 43 46 public class ListenerComponent extends ClassComponent { 47 private static final L10N L = new L10N(ListenerComponent.class); 48 49 private String _baseClassName; 50 private String _extClassName; 51 52 private ListenerType _listenerType; 53 54 public ListenerComponent() 55 { 56 } 57 58 61 public void setListenerType(ListenerType listenerType) 62 { 63 _listenerType = listenerType; 64 } 65 66 69 public void setBaseClassName(String baseClassName) 70 { 71 _baseClassName = baseClassName; 72 } 73 74 77 public String getBaseClassName() 78 { 79 return _baseClassName; 80 } 81 82 85 public void setExtClassName(String extClassName) 86 { 87 _extClassName = extClassName; 88 } 89 90 93 public String getClassName() 94 { 95 return _extClassName; 96 } 97 98 101 public String getBeanClassName() 102 { 103 return _baseClassName; 104 } 105 106 109 public void generate(JavaWriter out) 110 throws IOException 111 { 112 try { 113 generateHeader(out); 114 115 generateMainCallback(out, _listenerType); 116 117 generateCallbacks(out, Listener.PRE_PERSIST, _listenerType); 118 generateCallbacks(out, Listener.POST_PERSIST, _listenerType); 119 generateCallbacks(out, Listener.PRE_REMOVE, _listenerType); 120 generateCallbacks(out, Listener.POST_REMOVE, _listenerType); 121 generateCallbacks(out, Listener.PRE_UPDATE, _listenerType); 122 generateCallbacks(out, Listener.POST_UPDATE, _listenerType); 123 generateCallbacks(out, Listener.POST_LOAD, _listenerType); 124 125 } catch (IOException e) { 126 throw e; 127 } 128 } 129 130 133 private void generateHeader(JavaWriter out) 134 throws IOException 135 { 136 out.println("/*"); 137 out.println(" * Generated by Resin Amber"); 138 out.println(" * " + com.caucho.Version.VERSION); 139 out.println(" */"); 140 out.print("private static final java.util.logging.Logger __caucho_log = "); 141 out.println("java.util.logging.Logger.getLogger(\"" + getBeanClassName() + "\");"); 142 out.println(); 144 } 145 146 private void generateMainCallback(JavaWriter out, 147 ListenerType listenerType) 148 throws IOException 149 { 150 out.println("public void __caucho_callback(int callbackIndex, Object entity)"); 151 out.println("{"); 152 out.pushDepth(); 153 154 out.println("switch (callbackIndex) {"); 155 out.println("case com.caucho.amber.entity.Listener.PRE_PERSIST:"); 156 out.println(" __caucho_prePersist(entity);"); 157 out.println(" break;"); 158 out.println("case com.caucho.amber.entity.Listener.POST_PERSIST:"); 159 out.println(" __caucho_postPersist(entity);"); 160 out.println(" break;"); 161 out.println("case com.caucho.amber.entity.Listener.PRE_REMOVE:"); 162 out.println(" __caucho_preRemove(entity);"); 163 out.println(" break;"); 164 out.println("case com.caucho.amber.entity.Listener.POST_REMOVE:"); 165 out.println(" __caucho_postRemove(entity);"); 166 out.println(" break;"); 167 out.println("case com.caucho.amber.entity.Listener.PRE_UPDATE:"); 168 out.println(" __caucho_preUpdate(entity);"); 169 out.println(" break;"); 170 out.println("case com.caucho.amber.entity.Listener.POST_UPDATE:"); 171 out.println(" __caucho_postUpdate(entity);"); 172 out.println(" break;"); 173 out.println("case com.caucho.amber.entity.Listener.POST_LOAD:"); 174 out.println(" __caucho_postLoad(entity);"); 175 out.println(" break;"); 176 out.println("}"); 177 178 out.popDepth(); 179 out.println("}"); 180 } 181 182 private void generateCallbacks(JavaWriter out, 183 int callbackIndex, 184 ListenerType listenerType) 185 throws IOException 186 { 187 String name = toCallbackName(callbackIndex); 188 189 out.println("public void __caucho_" + name + "(Object entity)"); 190 out.println("{"); 191 out.pushDepth(); 192 193 ArrayList <JMethod> callbacks = null; 194 ListenerType parentType; 195 196 parentType = listenerType; 197 198 do { 199 switch (callbackIndex) { 200 case Listener.PRE_PERSIST: 201 callbacks = parentType.getPrePersistCallbacks(); 202 break; 203 case Listener.POST_PERSIST: 204 callbacks = parentType.getPostPersistCallbacks(); 205 break; 206 case Listener.PRE_REMOVE: 207 callbacks = parentType.getPreRemoveCallbacks(); 208 break; 209 case Listener.POST_REMOVE: 210 callbacks = parentType.getPostRemoveCallbacks(); 211 break; 212 case Listener.PRE_UPDATE: 213 callbacks = parentType.getPreUpdateCallbacks(); 214 break; 215 case Listener.POST_UPDATE: 216 callbacks = parentType.getPostUpdateCallbacks(); 217 break; 218 case Listener.POST_LOAD: 219 callbacks = parentType.getPostLoadCallbacks(); 220 break; 221 } 222 223 if (callbacks.size() > 0) 224 break; 225 226 parentType = parentType.getParentType(); 227 } 228 while (parentType != null); 229 230 if (callbacks.size() > 0) { 231 for (JMethod method : callbacks) { 232 JClass params[] = method.getParameterTypes(); 233 234 if (listenerType.getParentType() == null) 235 out.print("this."); 236 else 237 out.print("super."); 238 239 out.print(method.getName()); 240 out.println("((" + params[0].getName() + ") entity);"); 241 } 242 } 243 244 out.popDepth(); 245 out.println("}"); 246 } 247 248 private static String toCallbackName(int callbackIndex) 249 { 250 switch (callbackIndex) { 251 case Listener.PRE_PERSIST: 252 return "prePersist"; 253 case Listener.POST_PERSIST: 254 return "postPersist"; 255 case Listener.PRE_REMOVE: 256 return "preRemove"; 257 case Listener.POST_REMOVE: 258 return "postRemove"; 259 case Listener.PRE_UPDATE: 260 return "preUpdate"; 261 case Listener.POST_UPDATE: 262 return "postUpdate"; 263 case Listener.POST_LOAD: 264 return "postLoad"; 265 } 266 267 return null; 268 } 269 } 270 | Popular Tags |