1 29 30 package com.caucho.ejb.gen; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.java.JavaWriter; 34 import com.caucho.java.gen.ClassComponent; 35 import com.caucho.util.L10N; 36 37 import java.io.IOException ; 38 39 42 public class AmberBean extends EntityBean { 43 private final static L10N L = new L10N(AmberBean.class); 44 45 public AmberBean(JClass ejbClass, 46 String contextClassName, 47 String implClassName) 48 { 49 super(ejbClass, contextClassName, implClassName); 50 51 addComponent(new FlushMethod()); 52 53 if (hasMethod(ejbClass, "ejbLoad", new JClass[0])) { 54 addComponent(new LoadMethod()); 55 } 56 57 boolean hasRemove = hasMethod(ejbClass, "ejbRemove", new JClass[0]); 58 59 addComponent(new RemoveMethod(hasRemove)); 60 } 61 62 65 protected boolean isCMP() 66 { 67 return true; 68 } 69 70 73 protected void generateContext(JavaWriter out) 74 throws IOException 75 { 76 super.generateContext(out); 77 78 out.println(); 79 out.println("com.caucho.amber.entity.EntityItem _amber;"); 80 81 out.println(); 82 out.println("protected final void __caucho_setAmber(com.caucho.amber.entity.EntityItem amber)"); 83 out.println("{"); 84 out.println(" _amber = amber;"); 85 out.println("}"); 86 87 out.println(); 88 out.println("protected final com.caucho.amber.entity.EntityItem __caucho_getAmber()"); 89 out.println("{"); 90 out.println(" if (_amber != null)"); 91 out.println(" return _amber;"); 92 out.println(" else"); 93 out.println(" throw new javax.ejb.EJBException(\"object no longer exists.\");"); 94 out.println("}"); 95 } 96 97 100 protected void generateStore(JavaWriter out) 101 throws IOException 102 { 103 } 104 105 108 protected void generateLoad(JavaWriter out) 109 throws IOException 110 { 111 out.println("if (doLoad) {"); 112 out.println(" try {"); 113 out.println(" ptr.__caucho_makePersistent(trans.getAmberConnection(), __caucho_getAmber());"); 114 out.println(" ptr.__caucho_retrieve(trans.getAmberConnection());"); 115 116 120 121 out.println(" } catch (Exception e) { throw com.caucho.ejb.EJBExceptionWrapper.createRuntime(e); }"); 122 out.println(" ptr._ejb_state = QEntity._CAUCHO_IS_LOADED;"); 124 out.println("}"); 125 } 126 127 private static boolean hasMethod(Class cl, String name, Class []param) 128 { 129 try { 130 return cl.getMethod(name, param) != null; 131 } catch (Throwable e) { 132 return false; 133 } 134 } 135 136 static class FlushMethod extends ClassComponent { 137 public void generate(JavaWriter out) 138 throws IOException 139 { 140 out.println("protected void __caucho_flush_callback()"); 141 out.println(" throws java.sql.SQLException"); 142 out.println("{"); 143 out.println(" ejbStore();"); 144 out.println("}"); 145 } 146 } 147 148 static class LoadMethod extends ClassComponent { 149 public void generate(JavaWriter out) 150 throws IOException 151 { 152 out.println("protected void __caucho_load_callback()"); 153 out.println("{"); 154 out.println(" try {"); 155 out.println(" ejbLoad();"); 156 out.println(" } catch (Throwable e) {"); 157 out.println(" log.log(java.util.logging.Level.WARNING, e.toString(), e);"); 158 out.println(" }"); 159 out.println("}"); 160 } 161 } 162 163 static class RemoveMethod extends ClassComponent { 164 private boolean _hasRemove; 165 166 RemoveMethod(boolean hasRemove) 167 { 168 _hasRemove = hasRemove; 169 } 170 171 public void generate(JavaWriter out) 172 throws IOException 173 { 174 out.println("public void ejbRemove()"); 175 out.println("{"); 176 out.pushDepth(); 177 178 out.println("try {"); 179 out.pushDepth(); 180 181 if (_hasRemove) 182 out.println("super.ejbRemove();"); 183 184 out.println("_ejb_state = com.caucho.ejb.entity.EntityObject._CAUCHO_IS_DEAD;"); 185 186 out.println("_ejb_trans.getAmberConnection().delete(this);"); 187 188 out.popDepth(); 189 out.println("} catch (Throwable e) {"); 190 out.println(" log.log(java.util.logging.Level.WARNING, e.toString(), e);"); 191 out.println("}"); 192 193 out.popDepth(); 194 out.println("}"); 195 } 196 } 197 } 198 | Popular Tags |