/**
* Copyright (C) 2001-2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.generation.generator.home.ejb;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Label;
import org.objectweb.speedo.generation.generator.home.HomeGenerator;
import org.objectweb.speedo.generation.parser.ejb.EJBAnnotationParser;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoCallback;
import org.objectweb.speedo.mim.ejb.lib.EJBAbstractHomeImpl;
import org.objectweb.speedo.query.ejb.EJBQuery;
public class EJBHomeGenerator extends HomeGenerator {
private final static String LISTENER_FIELD = "eventListener";
public EJBHomeGenerator(Personality p) {
super(p);
}
protected Class getSuperClass() {
return EJBAbstractHomeImpl.class;
}
// TODO
protected Class getQueryClass() {
return EJBQuery.class;
}
protected void generatePersonalityMethods(HomeContext gc) {
if (gc.sc.callBacks.isEmpty()) {
return;
}
// public void sendEvt...
CodeVisitor mv = gc.cv.visitMethod(ACC_PUBLIC, "sendEvent",
"(ILjava/lang/Object;Ljava/lang/Object;Z)V", null, null);
int nbCase = gc.sc.callBacks.size();
int[] keys = new int[nbCase];
Label[] keyLabels = new Label[nbCase];
int i = 0;
List keysList = new ArrayList((Set<Integer>) gc.sc.callBacks.keySet());
Collections.sort(keysList);
for (int cbid : (List<Integer>) keysList) {
keys[i] = cbid;
keyLabels[i] = new Label();
i++;
}
Label switchEndLabel = new Label();
mv.visitVarInsn(ILOAD, 1);
mv.visitLookupSwitchInsn(switchEndLabel, keys, keyLabels);
String listenclass = null;
// switch (evt.type) {
for (int j = 0; j < nbCase; j++) {
// case [assoc EJB_CB(cbn) <-> evt.type]:
mv.visitLabel(keyLabels[j]);
ArrayList<SpeedoCallback> cbl = (ArrayList<SpeedoCallback>) gc.sc.callBacks.get(keys[j]);
if (cbl == null) {
continue;
}
for (SpeedoCallback actualcbn : cbl) {
if (actualcbn.listenerClassName == null) { // this a callback from the persistent class
// CALL THE CALLBACK METHOD INTO THE PERSISTENT CLASS
mv.visitVarInsn(ALOAD, 2);
mv.visitTypeInsn(CHECKCAST, gc.xJCN);
mv.visitMethodInsn(INVOKEVIRTUAL, gc.xJCN,
actualcbn.callbackName,
actualcbn.methodByteCodeSignature);
} else { // this is a callback from the listener class
if (listenclass == null) {
listenclass = actualcbn.listenerClassName;
}
// CALL THE CALLBACK METHOD INTO THE LISTENER CLASS
//this.eventListener.myMethod(param1)
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, gc.xHomeJCN, LISTENER_FIELD,
getJVMType(actualcbn.listenerClassName));
mv.visitVarInsn(ALOAD, 2);
mv.visitTypeInsn(CHECKCAST, gc.xJCN);
mv.visitMethodInsn(INVOKEVIRTUAL,
getJVMClassName(actualcbn.listenerClassName),
actualcbn.callbackName,
actualcbn.methodByteCodeSignature);
}
}
// break;
mv.visitJumpInsn(GOTO, switchEndLabel);
}
mv.visitLabel(switchEndLabel);
// }
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
if (listenclass != null) {
// ADD THE DEFINITION OF THE LISTENER CLASS VARIABLE
gc.cv.visitField(ACC_PUBLIC + ACC_FINAL,
LISTENER_FIELD, getJVMType(listenclass), null, null);
gc.ctx.put("listenClass", listenclass);
}
//public boolean hasInstanceLifeCycleListeners() {
mv = gc.cv.visitMethod(ACC_PUBLIC, "hasInstanceLifeCycleListeners",
"()Z", null, null);
//return true;
mv.visitInsn(ICONST_1);
mv.visitInsn(IRETURN);
mv.visitMaxs(0, 0);
//}
}
protected void generateNoArgConstructor(HomeContext gc) {
CodeVisitor mv = gc.cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, gc.superClassJCN, "<init>", "()V");
String listenclass = (String) gc.ctx.get("listenClass");
if (listenclass != null) {
//this.eventListener = new MyListener()
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, getJVMClassName(listenclass));
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, getJVMClassName(listenclass),
"<init>", "()V");
mv.visitFieldInsn(PUTFIELD, gc.xHomeJCN, LISTENER_FIELD,
getJVMType(listenclass));
}
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
}
}