KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > JacObjectOutputStream


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak, Lionel Seinturier.
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.rtti.MethodItem;
26
27 /**
28  * <code>JacObjectOutputStream</code> is used to write JAC objects
29  * into an output stream during a serialization process.
30  *
31  * <p>This stream is used when serializing a JAC object into an array
32  * of bytes when calling <code>JacObject.serialize()</code>. All the
33  * objects that are not JAC objects are serialized with the default
34  * procedure. When a JAC object is encountered, a
35  * <code>whenSerialized</code> event is thrown on the current AC
36  * manager so that the aspect components can parametrize the
37  * serialization process.
38  *
39  * <p>A symetric process for deserialization is implemented by
40  * <code>JacObjectInputStream</code>.
41  *
42  * @see ACManager#whenSerialized(Wrappee,SerializedJacObject)
43  * @see JacObjectInputStream
44  *
45  * @author Renaud Pawlak
46  * @author Lionel Seinturier
47  */

48  
49 public class JacObjectOutputStream extends ObjectOutputStream JavaDoc {
50     static Logger logger = Logger.getLogger("serialization");
51
52     /**
53     * Creates a JacObjectInputStream.
54     *
55     * @param os the output stream where the bytes are written.
56     */

57     public JacObjectOutputStream(OutputStream JavaDoc os) throws IOException JavaDoc {
58         super(os);
59         enableReplaceObject(true);
60     }
61
62     /**
63     * This method is upcalled by the Java serialization process each
64     * time a new object to serialize is encountered.
65     *
66     * <p>If a JAC object is encountered (instance of
67     * <code>Wrappee</code>), the aspect component manager is upcalled
68     * to parametrize the serialization.
69     *
70     * @param obj the encountered JAC object
71     * @return the final serialized JAC object
72     *
73     * @see SerializedJacObject
74     * @see ACManager#whenSerialized(Wrappee,SerializedJacObject) */

75    
76     protected Object JavaDoc replaceObject(Object JavaDoc obj) throws IOException JavaDoc {
77         if (obj instanceof Wrappee) {
78             SerializedJacObject sjo =
79                 new SerializedJacObject(obj.getClass().getName());
80             if (obj.getClass().getName().startsWith("org.objectweb.jac.lib.java.util")) {
81                 sjo.disableForwarding();
82             }
83             ((ACManager)ACManager.get()).whenSerialized((Wrappee)obj,sjo);
84             return sjo;
85         } else if (obj instanceof AspectComponent) {
86             SerializedJacObject sjo =
87                 new SerializedJacObject(obj.getClass().getName());
88             return sjo;
89         } else if (obj instanceof MethodItem) {
90             return new SerializedMethodItem((MethodItem)obj);
91         } else if (obj.getClass().getName().equals("org.objectweb.jac.aspects.gui.DisplayContext")) {
92             // <HACK reason="Don't crash when trying to serialize displayContext">
93
logger.debug("replaceObject "+obj+" -> null");
94             return null;
95             // </HACK>
96
}
97         return obj;
98     }
99
100 }
101
Popular Tags