KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > spi > entitymanagergenerator > ApplicationManagedResourceTransactionNonInjectableInEJB


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator;
20
21 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategySupport;
22 import com.sun.source.tree.AnnotationTree;
23 import com.sun.source.tree.ClassTree;
24 import com.sun.source.tree.ExpressionTree;
25 import com.sun.source.tree.MethodTree;
26 import com.sun.source.tree.ModifiersTree;
27 import com.sun.source.tree.TypeParameterTree;
28 import java.util.Collections JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.lang.model.element.Modifier;
32 import javax.lang.model.type.TypeKind;
33 import org.netbeans.api.java.source.TreeMaker;
34 import org.netbeans.api.java.source.WorkingCopy;
35 import org.netbeans.modules.j2ee.persistence.action.GenerationOptions.*;
36 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
37
38 /**
39  * Generates the code needed for invoking an <code>EntityManager</code> in EJB 2.x
40  * environment with an application-managed persistence unit.
41  *
42  * @author Erno Mononen
43  */

44 public final class ApplicationManagedResourceTransactionNonInjectableInEJB extends EntityManagerGenerationStrategySupport {
45     
46     public ClassTree generate(){
47         
48         ClassTree modifiedClazz = null;
49         
50         ModifiersTree methodModifiers = getTreeMaker().Modifiers(
51                 Collections.<Modifier>singleton(Modifier.PUBLIC),
52                 Collections.<AnnotationTree>emptyList()
53                 );
54         
55         Set JavaDoc<Modifier> modifiers = new HashSet JavaDoc<Modifier>();
56         modifiers.add(Modifier.PRIVATE);
57         modifiers.add(Modifier.STATIC);
58         
59         
60         
61         
62         modifiedClazz = getTreeMaker().insertClassMember(getClassTree(), getIndexForField(getClassTree()), createEntityManagerFactory());
63         
64         String JavaDoc text =
65                 "javax.persistence.EntityManager em = emf.createEntityManager();\n" +
66                 "em.getTransaction().begin();\n" +
67                 "try {\n" +
68                 generateCallLines() +
69                 " em.getTransaction().commit();\n" +
70                 "} catch (Exception e) {\n" +
71                 " e.printStackTrace();\n" +
72                 " em.getTransaction().rollback();\n" +
73                 "} finally {\n" +
74                 " em.close();\n" +
75                 "}";
76         
77         MethodTree newMethod = getTreeMaker().Method(
78                 methodModifiers,
79                 computeMethodName(),
80                 getTreeMaker().PrimitiveType(TypeKind.VOID),
81                 Collections.<TypeParameterTree>emptyList(),
82                 getParameterList(),
83                 Collections.<ExpressionTree>emptyList(),
84                 "{ " + text + "}",
85                 null
86                 );
87         
88         
89         modifiedClazz = getTreeMaker().addClassMember(modifiedClazz, newMethod);
90         return modifiedClazz;
91         
92     }
93     
94 }
Popular Tags