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.gen.*;
033: import com.caucho.ejb.cfg.*;
034: import com.caucho.java.JavaWriter;
035: import com.caucho.java.gen.ClassComponent;
036: import com.caucho.util.L10N;
037:
038: import java.io.IOException;
039:
040: /**
041: * Generates the skeleton for an Amber-based entity bean.
042: */
043: public class AmberBean extends EntityBean {
044: private final static L10N L = new L10N(AmberBean.class);
045:
046: public AmberBean(ApiClass ejbClass, String contextClassName,
047: String implClassName) {
048: super (ejbClass, contextClassName, implClassName);
049:
050: addComponent(new FlushMethod());
051:
052: if (ejbClass.hasMethod("ejbLoad", new Class[0])) {
053: addComponent(new LoadMethod());
054: }
055:
056: boolean hasRemove = ejbClass.hasMethod("ejbRemove",
057: new Class[0]);
058:
059: addComponent(new RemoveMethod(hasRemove));
060: }
061:
062: /**
063: * Returns true for CMP.
064: */
065: protected boolean isCMP() {
066: return true;
067: }
068:
069: /**
070: * Generates the amber context stuff.
071: */
072: protected void generateContext(JavaWriter out) throws IOException {
073: super .generateContext(out);
074:
075: out.println();
076: out
077: .println("private com.caucho.amber.entity.EntityItem __amber_cacheItem;");
078:
079: out.println();
080: out
081: .println("protected final void __caucho_setAmberCacheItem(com.caucho.amber.entity.EntityItem amber)");
082: out.println("{");
083: out.println(" __amber_cacheItem = amber;");
084: out.println("}");
085:
086: out.println();
087: out
088: .println("protected final com.caucho.amber.entity.EntityItem __caucho_getAmberCacheItem()");
089: out.println("{");
090: out.println(" if (__amber_cacheItem == null)");
091: out
092: .println(" __amber_cacheItem = _server.getAmberCacheItem(getPrimaryKey());");
093: out.println(" return __amber_cacheItem;");
094: out.println("}");
095:
096: out.println();
097: out.println("protected final " + getImplClassName()
098: + " __caucho_getAmberCacheEntity()");
099: out.println("{");
100: out.println(" if (__amber_cacheItem == null)");
101: out
102: .println(" __amber_cacheItem = _server.getAmberCacheItem(getPrimaryKey());");
103: out.println(" return (" + getImplClassName()
104: + ") __amber_cacheItem.getEntity();");
105: out.println("}");
106: }
107:
108: /**
109: * Generates the load code.
110: */
111: protected void generateStore(JavaWriter out) throws IOException {
112: }
113:
114: /**
115: * Generates the load code.
116: */
117: protected void generateLoad(JavaWriter out) throws IOException {
118: out.println("if (doLoad) {");
119: out.println(" try {");
120: //out.println(" ptr.__caucho_makePersistent(trans.getAmberConnection(), __caucho_getAmber());");
121: out
122: .println(" ptr.__caucho_retrieve_eager(trans.getAmberConnection());");
123:
124: /*
125: if (hasMethod("ejbLoad", new Class[0]))
126: out.println(" ptr.ejbLoad();");
127: */
128:
129: out
130: .println(" } catch (Exception e) { throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e); }");
131: // out.println(" ptr._ejb_state = QEntity._CAUCHO_IS_DIRTY;");
132: out.println(" ptr._ejb_state = QEntity._CAUCHO_IS_LOADED;");
133: out.println("}");
134: }
135:
136: private static boolean hasMethod(Class cl, String name,
137: Class[] param) {
138: try {
139: return cl.getMethod(name, param) != null;
140: } catch (Throwable e) {
141: return false;
142: }
143: }
144:
145: static class FlushMethod extends ClassComponent {
146: public void generate(JavaWriter out) throws IOException {
147: out.println("protected void __caucho_flush_callback()");
148: out.println(" throws java.sql.SQLException");
149: out.println("{");
150: out.println(" ejbStore();");
151: out.println("}");
152: }
153: }
154:
155: static class LoadMethod extends ClassComponent {
156: public void generate(JavaWriter out) throws IOException {
157: out.println("protected void __caucho_load_callback()");
158: out.println("{");
159: out.println(" try {");
160: out.println(" ejbLoad();");
161: out.println(" } catch (Throwable e) {");
162: out
163: .println(" __caucho_log.log(java.util.logging.Level.WARNING, e.toString(), e);");
164: out.println(" }");
165: out.println("}");
166: }
167: }
168:
169: static class RemoveMethod extends ClassComponent {
170: private boolean _hasRemove;
171:
172: RemoveMethod(boolean hasRemove) {
173: _hasRemove = hasRemove;
174: }
175:
176: public void generate(JavaWriter out) throws IOException {
177: out.println("public void ejbRemove()");
178: out.println("{");
179: out.pushDepth();
180:
181: out.println("try {");
182: out.pushDepth();
183:
184: if (_hasRemove)
185: out.println("super.ejbRemove();");
186:
187: out
188: .println("_ejb_state = com.caucho.ejb.entity.EntityObject._CAUCHO_IS_DEAD;");
189:
190: //out.println("_ejb_trans.getAmberConnection().delete(this);");
191: out.println("__caucho_delete();");
192:
193: out.popDepth();
194: out.println("} catch (Exception e) {");
195: out
196: .println(" __caucho_log.log(java.util.logging.Level.WARNING, e.toString(), e);");
197: out.println("}");
198:
199: out.popDepth();
200: out.println("}");
201: }
202: }
203: }
|