<?xml version="1.0" encoding="UTF-8"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   $Header:$
 -->
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
<document>
    <header>
        <title>Control Serialization</title>
    </header>
    <body>
        <section id="overview">
            <title>Overview</title>
            <p>
                This document describes control serialization.  Typically control serialization is initiated by
                the runtime container; however, serialization may also be initiated programmatically by a controls
                developer.  For either case the process described in the Serialization Steps section of this document
                are the same.
               <!--
                This document describes what happens when a control is serialized by its runtime container. All controls
                are serializable by default but may be marked as transient if they contain state which does not need
                to be serialized.
                -->
            </p>
        </section>
        <section id="istransient">
            <title>isTransient</title>
           <p>
               When developing a control which does not need its state serialized, the isTransient attribute of the
               ControlImplementation annotation may be set to true.  If not explicitly set isTransient will default
               to false.
          </p>
            <source>
@ControlImplementation(isTransient=true)
public class MyControlImpl implements MyControl {
                .
                .
                .
}
            </source>

            <p>
                If isTransient is set to true the control implementation will not be serialized. After deserialization
                a new instance of the control implementation will be instantiated by the controls framework upon the
                first method invocation on that control.
           </p>
        </section>

        <section id="xmlvsjava">
            <title>XML vs Java Serialization</title>
            <p>
                Beehive controls support standard Java serialization as well as XML serialization.  All of the necessary
                <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PersistenceDelegate.html">PersistenceDelegate</a>
                implementations already exist in the controls framework to support XML
                serialization using the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLEncoder.html">XMLEncoder</a>
                and <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLDecoder.html">XMLDecoder</a> classes.
            </p>
        </section>
        
        <section id="walkthrough">
           <title>Serialization Steps</title>
            <p>
                Before serialization can occur, the current ResourceContext must be ended for all controls within the
                ControlContainerContext (CCC).  Typically this is accomplished by a call to the CCC's endContext()
                method. The endContext() method causes an onRelease resource event to be fired to all controls within
                the CCC.  Any controls with onRelease event listeners then have the opportunity to release any resources
                (such as JDBC connections, JNDI contexts, sessions, etc) before serialization occurs.
            </p>
            
            <p>
                During deserialization resources can be reacquired by the control once a new ResourceContext has been
                established by the CCC. Each control then gets notified with on onAcquire resource event.
            </p>

            <p>
                The CCC performs a number of actions on serialization:
            </p>

            <ol>
                <li>
                    Service providers (
                    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/beancontext/BeanContextServiceProvider.html">
                    BeanContextServiceProvider</a>) registered with the ControlBeanContextServicesSupport instance are
                    serialized, including any registered
                    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/beancontext/BeanContextServicesListener.html">
                    BeanContextServicesListeners</a> which have been registered for those services.
                </li>
                <li>
                    Next the children of the CCC are serialized.  Children of the CCC include ControlBeans as well as
                    each ControlBean's ControlBeanContext.  Any registered
                    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/beancontext/BeanContextMembershipListener.html">
                     BeanContextMembershipListeners</a> are also serialized.
                </li>
                <li>
                    Each ControlBean contains a reference to a control implementation class which gets serialized
                    as well (unless the implementation's isTransient annotation attribute is set to true, which case it
                    will not be serialized).
                </li>
                <li>
                    The ControlBean contains the control's property state which gets serialized as part of the ControlBean.
                </li>
            </ol>
        </section>
    </body>
</document>