NOTE: This is outdated information that applies only to Tapestry 4.

How to inject a property into an Application State Object (ASO) ?

In order to inject properties into an ASO, we implement a custom StateObjectFactory and define the method StateObjectFactory.create to create the ASO with the desired injected properties. We then use the above custom StateObjectFactory to create our ASO. The following example illustrates the above in detail. Suppose we have an ASO MyASO with property messageHolder. We would like to create an MyASO with the property messageHolder being injected by HiveMind.

package man.aso;

public class MyASO {

        private IMessageHolder messageHolder;

        public IMessageHolder getMessageHolder() {
                return messageHolder;
        }

        public void setMessageHolder(IMessageHolder message) {
                this.messageHolder = message;
        }
}

package man.aso;

public interface IMessageHolder {

        public String getMessage();

}

package man.aso;

public class HelloMessageHolder implements IMessageHolder{

        public String getMessage() {
                return "HelloMessage : Hello ";
        }

}

We shall inject HelloMessageHolder into MyASO. We now implement a custom StateObjectFactory called MyASOFactory.

package man.aso;

import org.apache.tapestry.engine.state.StateObjectFactory;

public class MyASOFactory implements StateObjectFactory{

        // To be injected by HiveMind
        private IMessageHolder messageHolder;
        
        // Contruct an MyASO with injected messageHolder
        public Object createStateObject() {     
                MyASO myASO = new MyASO();
                myASO.setMessageHolder(getMessageHolder());
                return myASO;
        }

        public IMessageHolder getMessageHolder() {              
                return messageHolder;
        }

        public void setMessageHolder(IMessageHolder message) {
                this.messageHolder = message;
        }

}

Note that the property MyASOFactory.messageHolder will be injected by HiveMind. The following configuration in HiveMind module descriptor hivemodule.xml does all the wiring.

hivemodule.xml

<service-point id="helloMessage" interface="man.aso.IMessageHolder">
        <invoke-factory>
        <construct class="man.aso.HelloMessageHolder"/>                 
       </invoke-factory>                                        
</service-point>
        
 <!-- This service creates an StateObjectFactory to create a MyASO.
         The property MyASO.messageHolder will be injected from service
         point helloMessage.    
   -->
<service-point id="myASOFactory" interface="man.aso.MyASOFactory">
    <invoke-factory>
         <construct class="man.aso.MyASOFactory">
                  <set-service property="messageHolder" service-id="helloMessage"/>
         </construct>
   </invoke-factory>  
</service-point>
        
<contribution configuration-id="tapestry.state.ApplicationObjects">
    <state-object name="myASO" scope="session">
        <invoke-factory object="service:myASOFactory" />
     </state-object>
</contribution>

The above will result in an ASO myASO being created with property messageHolder set to HelloMessageHolder.

InjectPropertyIntoASO (last edited 2011-01-02 02:29:55 by BobHarner)