001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Rodrigo Westrupp
028: */
029:
030: package com.caucho.amber.gen;
031:
032: import com.caucho.amber.entity.Listener;
033: import com.caucho.amber.type.ListenerType;
034: import com.caucho.bytecode.JClass;
035: import com.caucho.bytecode.JMethod;
036: import com.caucho.java.JavaWriter;
037: import com.caucho.java.gen.ClassComponent;
038: import com.caucho.util.L10N;
039:
040: import java.io.IOException;
041: import java.util.ArrayList;
042:
043: /**
044: * Generates the Java code for the wrapped entity listener object.
045: */
046: public class ListenerComponent extends ClassComponent {
047: private static final L10N L = new L10N(ListenerComponent.class);
048:
049: private String _baseClassName;
050: private String _extClassName;
051:
052: private ListenerType _listenerType;
053:
054: public ListenerComponent() {
055: }
056:
057: /**
058: * Sets the bean info for the generator
059: */
060: public void setListenerType(ListenerType listenerType) {
061: _listenerType = listenerType;
062: }
063:
064: /**
065: * Sets the base class name
066: */
067: public void setBaseClassName(String baseClassName) {
068: _baseClassName = baseClassName;
069: }
070:
071: /**
072: * Gets the base class name
073: */
074: public String getBaseClassName() {
075: return _baseClassName;
076: }
077:
078: /**
079: * Sets the ext class name
080: */
081: public void setExtClassName(String extClassName) {
082: _extClassName = extClassName;
083: }
084:
085: /**
086: * Sets the ext class name
087: */
088: public String getClassName() {
089: return _extClassName;
090: }
091:
092: /**
093: * Get bean class name.
094: */
095: public String getBeanClassName() {
096: return _baseClassName;
097: }
098:
099: /**
100: * Starts generation of the Java code
101: */
102: public void generate(JavaWriter out) throws IOException {
103: try {
104: generateHeader(out);
105:
106: generateMainCallback(out, _listenerType);
107:
108: generateCallbacks(out, Listener.PRE_PERSIST, _listenerType);
109: generateCallbacks(out, Listener.POST_PERSIST, _listenerType);
110: generateCallbacks(out, Listener.PRE_REMOVE, _listenerType);
111: generateCallbacks(out, Listener.POST_REMOVE, _listenerType);
112: generateCallbacks(out, Listener.PRE_UPDATE, _listenerType);
113: generateCallbacks(out, Listener.POST_UPDATE, _listenerType);
114: generateCallbacks(out, Listener.POST_LOAD, _listenerType);
115:
116: } catch (IOException e) {
117: throw e;
118: }
119: }
120:
121: /**
122: * Generates the class header for the generated code.
123: */
124: private void generateHeader(JavaWriter out) throws IOException {
125: out.println("/*");
126: out.println(" * Generated by Resin Amber");
127: out.println(" * " + com.caucho.Version.VERSION);
128: out.println(" */");
129: out
130: .print("private static final java.util.logging.Logger __caucho_log = ");
131: out.println("java.util.logging.Logger.getLogger(\""
132: + getBeanClassName() + "\");");
133: // if (_listenerType.getParentType() == null) {
134: out.println();
135: }
136:
137: private void generateMainCallback(JavaWriter out,
138: ListenerType listenerType) throws IOException {
139: out
140: .println("public void __caucho_callback(int callbackIndex, Object entity)");
141: out.println("{");
142: out.pushDepth();
143:
144: out.println("switch (callbackIndex) {");
145: out
146: .println("case com.caucho.amber.entity.Listener.PRE_PERSIST:");
147: out.println(" __caucho_prePersist(entity);");
148: out.println(" break;");
149: out
150: .println("case com.caucho.amber.entity.Listener.POST_PERSIST:");
151: out.println(" __caucho_postPersist(entity);");
152: out.println(" break;");
153: out
154: .println("case com.caucho.amber.entity.Listener.PRE_REMOVE:");
155: out.println(" __caucho_preRemove(entity);");
156: out.println(" break;");
157: out
158: .println("case com.caucho.amber.entity.Listener.POST_REMOVE:");
159: out.println(" __caucho_postRemove(entity);");
160: out.println(" break;");
161: out
162: .println("case com.caucho.amber.entity.Listener.PRE_UPDATE:");
163: out.println(" __caucho_preUpdate(entity);");
164: out.println(" break;");
165: out
166: .println("case com.caucho.amber.entity.Listener.POST_UPDATE:");
167: out.println(" __caucho_postUpdate(entity);");
168: out.println(" break;");
169: out.println("case com.caucho.amber.entity.Listener.POST_LOAD:");
170: out.println(" __caucho_postLoad(entity);");
171: out.println(" break;");
172: out.println("}");
173:
174: out.popDepth();
175: out.println("}");
176: }
177:
178: private void generateCallbacks(JavaWriter out, int callbackIndex,
179: ListenerType listenerType) throws IOException {
180: String name = toCallbackName(callbackIndex);
181:
182: out.println("public void __caucho_" + name + "(Object entity)");
183: out.println("{");
184: out.pushDepth();
185:
186: ArrayList<JMethod> callbacks = null;
187: ListenerType parentType;
188:
189: parentType = listenerType;
190:
191: do {
192: switch (callbackIndex) {
193: case Listener.PRE_PERSIST:
194: callbacks = parentType.getPrePersistCallbacks();
195: break;
196: case Listener.POST_PERSIST:
197: callbacks = parentType.getPostPersistCallbacks();
198: break;
199: case Listener.PRE_REMOVE:
200: callbacks = parentType.getPreRemoveCallbacks();
201: break;
202: case Listener.POST_REMOVE:
203: callbacks = parentType.getPostRemoveCallbacks();
204: break;
205: case Listener.PRE_UPDATE:
206: callbacks = parentType.getPreUpdateCallbacks();
207: break;
208: case Listener.POST_UPDATE:
209: callbacks = parentType.getPostUpdateCallbacks();
210: break;
211: case Listener.POST_LOAD:
212: callbacks = parentType.getPostLoadCallbacks();
213: break;
214: }
215:
216: if (callbacks.size() > 0)
217: break;
218:
219: parentType = parentType.getParentType();
220: } while (parentType != null);
221:
222: if (callbacks.size() > 0) {
223: for (JMethod method : callbacks) {
224: JClass params[] = method.getParameterTypes();
225:
226: out.print("this.");
227:
228: /* jpa/0r42
229: if (listenerType.getParentType() == null)
230: out.print("this.");
231: else
232: out.print("super.");
233: */
234:
235: out.print(method.getName());
236: out.println("((" + params[0].getName() + ") entity);");
237: }
238: }
239:
240: out.popDepth();
241: out.println("}");
242: }
243:
244: private static String toCallbackName(int callbackIndex) {
245: switch (callbackIndex) {
246: case Listener.PRE_PERSIST:
247: return "prePersist";
248: case Listener.POST_PERSIST:
249: return "postPersist";
250: case Listener.PRE_REMOVE:
251: return "preRemove";
252: case Listener.POST_REMOVE:
253: return "postRemove";
254: case Listener.PRE_UPDATE:
255: return "preUpdate";
256: case Listener.POST_UPDATE:
257: return "postUpdate";
258: case Listener.POST_LOAD:
259: return "postLoad";
260: }
261:
262: return null;
263: }
264: }
|