001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.gen21;
031:
032: import com.caucho.ejb.cfg.*;
033: import com.caucho.java.JavaWriter;
034: import com.caucho.java.gen.BaseMethod;
035: import com.caucho.util.L10N;
036:
037: import java.io.IOException;
038: import java.util.Collection;
039: import java.util.Set;
040:
041: /**
042: * Generates the skeleton for the create method.
043: */
044: public class AmberGetter extends BaseMethod {
045: private static L10N L = new L10N(AmberGetter.class);
046:
047: private ApiMethod _method;
048: private String _implClassName;
049: private boolean _isReadOnly;
050:
051: public AmberGetter(ApiMethod method, String implClassName) {
052: super (method.getMethod());
053:
054: _method = method;
055: _implClassName = implClassName;
056: }
057:
058: /**
059: * Set true if the field is read-only.
060: */
061: public void setReadOnly(boolean isReadOnly) {
062: _isReadOnly = isReadOnly;
063: }
064:
065: /**
066: * Prints the create method
067: *
068: * @param method the create method
069: */
070: public void generateCall(JavaWriter out, String[] args)
071: throws IOException {
072: Class returnType = _method.getReturnType();
073:
074: if (!_isReadOnly) {
075:
076: out
077: .println("com.caucho.ejb.xa.TransactionContext xa = _xaManager.beginSingleRead();");
078: out.println();
079:
080: out.println("try {");
081: out.pushDepth();
082:
083: out.println("if (xa == null) {");
084: out.pushDepth();
085: }
086:
087: out.printClass(returnType);
088: out.print(" value = ");
089: out
090: .print("(("
091: + _implClassName
092: + ") _context.__caucho_getAmberCacheItem().loadEntity(0))."
093: + _method.getName() + "(");
094: for (int i = 0; i < args.length; i++) {
095: if (i != 0)
096: out.print(", ");
097: out.print(args[i]);
098: }
099: out.println(");");
100:
101: if (Collection.class.isAssignableFrom(returnType)) {
102: out.println("if (value == null)");
103: out.println(" return value;");
104:
105: if (Set.class.isAssignableFrom(returnType))
106: out.println("return new java.util.HashSet(value);");
107: else
108: out.println("return new java.util.ArrayList(value);");
109: } else {
110: out.println("return value;");
111: }
112:
113: if (!_isReadOnly) {
114: out.popDepth();
115: out.println("} else {");
116: out
117: .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: }
|