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 ApplicationManagedResourceTransactionInjectableInWeb extends EntityManagerGenerationStrategySupport{ 43 44 public ClassTree generate() { 45 46 ClassTree modifiedClazz = getTreeMaker().insertClassMember(getClassTree(), getIndexForField(getClassTree()), createEntityManagerFactory()); 47 48 ModifiersTree methodModifiers = getTreeMaker().Modifiers( 49 Collections.<Modifier>singleton(Modifier.PUBLIC), 50 Collections.<AnnotationTree>emptyList() 51 ); 52 53 String text = 54 "javax.persistence.EntityManager em = emf.createEntityManager();\n" + 55 "try {\n" + 56 " em.getTransaction().begin();\n" + 57 generateCallLines() + 58 " em.getTransaction().commit();\n" + 59 "} catch(Exception e) {\n" + 60 " java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,\"exception caught\", e);\n" + 61 " em.getTransaction().rollback();\n" + 62 "} finally {\n" + 63 " em.close();\n" + 64 "}"; 65 66 MethodTree newMethod = getTreeMaker().Method( 67 methodModifiers, 68 computeMethodName(), 69 getTreeMaker().PrimitiveType(TypeKind.VOID), 70 Collections.<TypeParameterTree>emptyList(), 71 getParameterList(), 72 Collections.<ExpressionTree>emptyList(), 73 "{ " + text + "}", 74 null 75 ); 76 77 return getTreeMaker().addClassMember(modifiedClazz, newMethod); 78 } 79 80 } 81 | Popular Tags |