1 19 20 package org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator; 21 22 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategySupport; 23 import com.sun.source.tree.AnnotationTree; 24 import com.sun.source.tree.ClassTree; 25 import com.sun.source.tree.ExpressionTree; 26 import com.sun.source.tree.MethodTree; 27 import com.sun.source.tree.ModifiersTree; 28 import com.sun.source.tree.TypeParameterTree; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import javax.lang.model.element.Modifier; 33 import javax.lang.model.type.TypeKind; 34 import org.netbeans.api.java.source.TreeMaker; 35 import org.netbeans.api.java.source.WorkingCopy; 36 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 37 38 44 public final class ContainerManagedJTAInjectableInWeb extends EntityManagerGenerationStrategySupport { 45 46 47 public ClassTree generate() { 48 49 ClassTree modifiedClazz = getClassTree(); 50 51 List <ExpressionTree> attribs = new ArrayList <ExpressionTree>(); 52 attribs.add(getGenUtils().createAnnotationArgument("name", "persistence/LogicalName")); attribs.add(getGenUtils().createAnnotationArgument("unitName", getPersistenceUnitName())); 55 modifiedClazz = getGenUtils().addAnnotation(modifiedClazz, getGenUtils().createAnnotation(PERSISTENCE_CONTEXT_FQN, attribs)); 56 modifiedClazz = getTreeMaker().insertClassMember(modifiedClazz, getIndexForField(modifiedClazz), createUserTransaction()); 57 58 ModifiersTree methodModifiers = getTreeMaker().Modifiers( 59 Collections.<Modifier>singleton(Modifier.PROTECTED), 60 Collections.<AnnotationTree>emptyList() 61 ); 62 MethodTree newMethod = getTreeMaker().Method( 63 methodModifiers, 64 computeMethodName(), 65 getTreeMaker().PrimitiveType(TypeKind.VOID), 66 Collections.<TypeParameterTree>emptyList(), 67 getParameterList(), 68 Collections.<ExpressionTree>emptyList(), 69 "{ " + 70 "try {\n" + 71 " javax.naming.Context ctx = (javax.naming.Context) new javax.naming.InitialContext().lookup(\"java:comp/env\");\n" + 72 " utx.begin();\n" + 73 " javax.persistence.EntityManager em = (javax.persistence.EntityManager) ctx.lookup(\"persistence/LogicalName\");\n" + 74 generateCallLines() + 75 " utx.commit();\n" + 76 "} catch(Exception e) {\n" + 77 " java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,\"exception caught\", e);\n" + 78 " throw new RuntimeException(e);\n" + 79 80 "}", 81 null 82 ); 83 84 return getTreeMaker().addClassMember(modifiedClazz, newMethod); 85 } 86 87 } 88 | Popular Tags |