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.Collections ; 30 import javax.lang.model.element.Modifier; 31 import javax.lang.model.type.TypeKind; 32 import org.netbeans.api.java.source.TreeMaker; 33 import org.netbeans.api.java.source.WorkingCopy; 34 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 35 36 42 public final class ContainerManagedJTANonInjectableInWeb extends EntityManagerGenerationStrategySupport { 43 44 public ClassTree generate() { 45 46 ModifiersTree methodModifiers = getTreeMaker().Modifiers( 47 Collections.<Modifier>singleton(Modifier.PUBLIC), 48 Collections.<AnnotationTree>emptyList() 49 ); 50 51 MethodTree newMethod = getTreeMaker().Method( 52 methodModifiers, 53 computeMethodName(), 54 getTreeMaker().PrimitiveType(TypeKind.VOID), 55 Collections.<TypeParameterTree>emptyList(), 56 getParameterList(), 57 Collections.<ExpressionTree>emptyList(), 58 "{ " + 59 "try {\n" + 72 " javax.naming.Context ctx = new javax.naming.InitialContext();\n" + 73 " javax.transaction.UserTransaction utx = (javax.transaction.UserTransaction) ctx.lookup(\"java:comp/env/UserTransaction\");\n" + 74 " utx.begin();\n" + 75 " javax.persistence.EntityManager em = (javax.persistence.EntityManager) ctx.lookup(\"java:comp/env/persistence/LogicalName\");\n" + 76 generateCallLines() + 77 " utx.commit();\n" + 78 "} catch(Exception e) {\n" + 79 " java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,\"exception caught\", e);\n" + 80 " throw new RuntimeException(e);\n" + 81 "}" 82 + "}", 83 null 84 ); 85 return getTreeMaker().addClassMember(getClassTree(), newMethod); 86 } 87 88 } 89 | Popular Tags |