KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > AmberGetter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.gen;
31
32 import com.caucho.bytecode.JClass;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.java.JavaWriter;
35 import com.caucho.java.gen.BaseMethod;
36 import com.caucho.util.L10N;
37
38 import java.io.IOException JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Set JavaDoc;
41
42 /**
43  * Generates the skeleton for the create method.
44  */

45 public class AmberGetter extends BaseMethod {
46   private static L10N L = new L10N(AmberGetter.class);
47
48   private JMethod _method;
49   private String JavaDoc _implClassName;
50   private boolean _isReadOnly;
51   
52   public AmberGetter(JMethod method,
53              String JavaDoc implClassName)
54   {
55     super(method);
56
57     _method = method;
58     _implClassName = implClassName;
59   }
60
61   /**
62    * Set true if the field is read-only.
63    */

64   public void setReadOnly(boolean isReadOnly)
65   {
66     _isReadOnly = isReadOnly;
67   }
68
69   /**
70    * Prints the create method
71    *
72    * @param method the create method
73    */

74   public void generateCall(JavaWriter out, String JavaDoc []args)
75     throws IOException JavaDoc
76   {
77     JClass returnType = _method.getReturnType();
78
79     if (! _isReadOnly) {
80       
81       out.println("com.caucho.ejb.xa.TransactionContext xa = _xaManager.beginSingleRead();");
82       out.println();
83
84       out.println("try {");
85       out.pushDepth();
86
87       out.println("if (xa == null) {");
88       out.pushDepth();
89     }
90
91     out.print(returnType.getPrintName());
92     out.print(" value = ");
93     out.print("((" + _implClassName + ") _context.__caucho_getAmber().loadEntity(0))." + _method.getName() + "(");
94     for (int i = 0; i < args.length; i++) {
95       if (i != 0)
96     out.print(", ");
97       out.print(args[i]);
98     }
99     out.println(");");
100
101     if (returnType.isAssignableTo(Collection JavaDoc.class)) {
102       out.println("if (value == null)");
103       out.println(" return value;");
104
105       if (returnType.isAssignableTo(Set JavaDoc.class))
106     out.println("return new java.util.HashSet(value);");
107       else
108     out.println("return new java.util.ArrayList(value);");
109     }
110     else {
111       out.println("return value;");
112     }
113
114     if (! _isReadOnly) {
115       out.popDepth();
116       out.println("} else {");
117       out.println(" Bean ptr = _context._ejb_begin(xa, false, true);");
118       out.print(" return ptr." + _method.getName() + "(");
119       for (int i = 0; i < args.length; i++) {
120     if (i != 0)
121       out.print(", ");
122     out.print(args[i]);
123       }
124       out.println(");");
125       out.println("}");
126     
127       out.popDepth();
128       out.println("} finally {");
129       out.println(" if (xa != null) xa.commit();");
130       out.println("}");
131     }
132   }
133 }
134
Popular Tags