KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > codegen > SerializableBeanGenerator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.ejb.codegen;
24
25 import java.lang.reflect.Method JavaDoc;
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 JavaDoc;
35 import javax.ejb.SessionBean JavaDoc;
36 import javax.ejb.EntityBean JavaDoc;
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 JavaDoc.*;
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 /**
47  * This class is used to generate a Serializable sub-class
48  * of a 3.0 stateful session bean.
49  */

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 JavaDoc beanClass;
62     private String JavaDoc generatedSerializableClassName;
63
64     private ClassLoader JavaDoc loader;
65
66     /**
67      * Get the fully qualified name of the generated class.
68      * @return the name of the generated class.
69      */

70     public String JavaDoc getGeneratedClass() {
71         return generatedSerializableClassName;
72     }
73
74     // For corba codegen infrastructure
75
public String JavaDoc className() {
76         return getGeneratedClass();
77     }
78
79     public SerializableBeanGenerator(ClassLoader JavaDoc cl,
80                                      String JavaDoc beanClassName)
81     throws GeneratorException
82     {
83     super();
84
85         loader = cl;
86     try {
87         beanClass = cl.loadClass(beanClassName);
88     } catch (ClassNotFoundException JavaDoc 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 JavaDoc packageName = getPackageName(generatedSerializableClassName);
105         String JavaDoc simpleName = getBaseName(generatedSerializableClassName);
106         
107         if( packageName != null ) {
108             _package(packageName);
109         } else {
110             // no-arg _package() call is required for default package
111
_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     /**
151      * Generate the code to the specified output stream.
152      * @param the output stream
153      * @exception GeneratorException on a generation error
154      * @exception IOException on an IO error
155      */

156     public void generate(OutputStream out)
157     throws GeneratorException, IOException
158     {
159     IndentingWriter p = new IndentingWriter(new OutputStreamWriter(out));
160
161     String JavaDoc packageName = getPackageName(generatedSerializableClassName);
162         String JavaDoc 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