KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > gen > ListenerComponent


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

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 JavaDoc;
41 import java.util.ArrayList JavaDoc;
42
43 /**
44  * Generates the Java code for the wrapped entity listener object.
45  */

46 public class ListenerComponent extends ClassComponent {
47   private static final L10N L = new L10N(ListenerComponent.class);
48
49   private String JavaDoc _baseClassName;
50   private String JavaDoc _extClassName;
51
52   private ListenerType _listenerType;
53
54   public ListenerComponent()
55   {
56   }
57
58   /**
59    * Sets the bean info for the generator
60    */

61   public void setListenerType(ListenerType listenerType)
62   {
63     _listenerType = listenerType;
64   }
65
66   /**
67    * Sets the base class name
68    */

69   public void setBaseClassName(String JavaDoc baseClassName)
70   {
71     _baseClassName = baseClassName;
72   }
73
74   /**
75    * Gets the base class name
76    */

77   public String JavaDoc getBaseClassName()
78   {
79     return _baseClassName;
80   }
81
82   /**
83    * Sets the ext class name
84    */

85   public void setExtClassName(String JavaDoc extClassName)
86   {
87     _extClassName = extClassName;
88   }
89
90   /**
91    * Sets the ext class name
92    */

93   public String JavaDoc getClassName()
94   {
95     return _extClassName;
96   }
97
98   /**
99    * Get bean class name.
100    */

101   public String JavaDoc getBeanClassName()
102   {
103     return _baseClassName;
104   }
105
106   /**
107    * Starts generation of the Java code
108    */

109   public void generate(JavaWriter out)
110     throws IOException JavaDoc
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 JavaDoc e) {
126       throw e;
127     }
128   }
129
130   /**
131    * Generates the class header for the generated code.
132    */

133   private void generateHeader(JavaWriter out)
134     throws IOException JavaDoc
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     // if (_listenerType.getParentType() == null) {
143
out.println();
144   }
145
146   private void generateMainCallback(JavaWriter out,
147                                     ListenerType listenerType)
148     throws IOException JavaDoc
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 JavaDoc
186   {
187     String JavaDoc name = toCallbackName(callbackIndex);
188
189     out.println("public void __caucho_" + name + "(Object entity)");
190     out.println("{");
191     out.pushDepth();
192
193     ArrayList JavaDoc<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 JavaDoc 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