KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 JavaDoc;
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 /**
37  * Generates the code needed for invoking an <code>EntityManager</code> in J2EE 1.4
38  * web components with a container-managed persistence unit.
39  *
40  * @author Erno Mononen
41  */

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                 // TODO: RETOUCHE
60
// TODO: add this automatically
61
// "// add this to web.xml:\n" +
62
// "// <persistence-context-ref>\n" +
63
// "// <persistence-context-ref-name>persistence/LogicalName</persistence-context-ref-name>\n" +
64
// "// < persistence-unit-name>PUName</persistence-unit-name>\n" +
65
// "// </persistence-context-ref>\n" +
66
// "// <resource-ref>\n" +
67
// "// <res-ref-name>UserTransaction</res-ref-name>\n" +
68
// "// <res-type>javax.transaction.UserTransaction</res-type>\n" +
69
// "// <res-auth>Container</res-auth>\n" +
70
// "// </resource-ref>\n" +
71
"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