1 23 package com.sun.ejb.codegen; 24 25 import java.lang.reflect.Method ; 26 import java.io.*; 27 import java.util.*; 28 import java.util.logging.*; 29 import com.sun.logging.*; 30 import com.sun.ejb.EJBUtils; 31 32 import sun.rmi.rmic.IndentingWriter; 33 34 import javax.ejb.EnterpriseBean ; 35 import javax.ejb.SessionBean ; 36 import javax.ejb.EntityBean ; 37 import com.sun.enterprise.deployment.*; 38 import com.sun.enterprise.util.JarClassLoader; 39 import com.sun.enterprise.util.LocalStringManagerImpl; 40 41 import static java.lang.reflect.Modifier .*; 42 import static com.sun.corba.ee.spi.codegen.Wrapper.*; 43 import com.sun.corba.ee.spi.codegen.Type; 44 import com.sun.corba.ee.impl.codegen.ClassGenerator; 45 46 50 51 public class SerializableBeanGenerator extends Generator 52 implements ClassGeneratorFactory { 53 54 private static LocalStringManagerImpl localStrings = 55 new LocalStringManagerImpl(SerializableBeanGenerator.class); 56 private static Logger _logger=null; 57 static{ 58 _logger=LogDomains.getLogger(LogDomains.DPL_LOGGER); 59 } 60 61 private Class beanClass; 62 private String generatedSerializableClassName; 63 64 private ClassLoader loader; 65 66 70 public String getGeneratedClass() { 71 return generatedSerializableClassName; 72 } 73 74 public String className() { 76 return getGeneratedClass(); 77 } 78 79 public SerializableBeanGenerator(ClassLoader cl, 80 String beanClassName) 81 throws GeneratorException 82 { 83 super(); 84 85 loader = cl; 86 try { 87 beanClass = cl.loadClass(beanClassName); 88 } catch (ClassNotFoundException ex) { 89 throw new InvalidBean( 90 localStrings.getLocalString( 91 "generator.remote_interface_not_found", 92 "Remote interface not found ")); 93 } 94 95 generatedSerializableClassName = 96 EJBUtils.getGeneratedSerializableClassName(beanClassName); 97 98 } 99 100 public ClassGenerator evaluate() { 101 102 _clear(); 103 104 String packageName = getPackageName(generatedSerializableClassName); 105 String simpleName = getBaseName(generatedSerializableClassName); 106 107 if( packageName != null ) { 108 _package(packageName); 109 } else { 110 _package(); 112 } 113 114 List toImplement = new LinkedList<Type>(); 115 toImplement.add(_t("java.io.Serializable")); 116 _class(PUBLIC, simpleName, _t(beanClass.getName()), toImplement); 117 118 _constructor( PUBLIC ) ; 119 _body(); 120 _expr(_super(_s(_void()))) ; 121 _end(); 122 123 _method( PRIVATE, _void(), "writeObject", _t("java.io.IOException")); 124 _arg( _t("java.io.ObjectOutputStream"), "oos" ); 125 _body(); 126 _expr(_call( _t("com.sun.ejb.EJBUtils"), "serializeObjectFields", 127 _s(_void(), 128 _Class(), _Object(), _t("java.io.ObjectOutputStream")), 129 _const(_t(beanClass.getName())), _this(), 130 _v("oos"))); 131 _end(); 132 133 134 _method( PRIVATE, _void(), "readObject", _t("java.io.IOException"), 135 _t("java.lang.ClassNotFoundException") ); 136 _arg( _t("java.io.ObjectInputStream"), "ois" ); 137 _body(); 138 _expr(_call( _t("com.sun.ejb.EJBUtils"), "deserializeObjectFields", 139 _s(_void(), 140 _Class(), _Object(), _t("java.io.ObjectInputStream")), 141 _const(_t(beanClass.getName())), _this(), 142 _v("ois"))); 143 _end(); 144 145 _end(); 146 147 return _classGenerator() ; 148 } 149 150 156 public void generate(OutputStream out) 157 throws GeneratorException, IOException 158 { 159 IndentingWriter p = new IndentingWriter(new OutputStreamWriter(out)); 160 161 String packageName = getPackageName(generatedSerializableClassName); 162 String simpleName = getBaseName(generatedSerializableClassName); 163 164 p.pln(""); 165 166 if (packageName != null) { 167 p.pln("package " + packageName + ";"); 168 } 169 170 p.pln(""); 171 172 p.plnI("public class " + simpleName + " extends " + beanClass.getName() 173 + " implements java.io.Serializable { "); 174 175 p.pln(""); 176 177 p.plnI("private void writeObject(java.io.ObjectOutputStream oos) throws java.io.IOException {"); 178 p.pln(" com.sun.ejb.EJBUtils.serializeObjectFields(" + 179 beanClass.getName() + ".class, this, oos);"); 180 p.pln("}"); 181 182 p.plnI("private void readObject(java.io.ObjectInputStream ois) throws java.io.IOException, java.lang.ClassNotFoundException {"); 183 p.pln(" com.sun.ejb.EJBUtils.deserializeObjectFields(" + 184 beanClass.getName() + ".class, this, ois);"); 185 p.pln("}"); 186 187 p.pOln("}"); 188 p.close(); 189 } 190 191 192 193 } 194 | Popular Tags |