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 State{{`Object}}Factory 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 Hive`Mind.

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 Hell{{`oMessage}}Holder into MyASO. We now implement a custom StateObjectFactory called MyASO`Factory.

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 My{{`ASO}}Factory.messageHolder will be injected by HiveMind. The following configuration in Hive`Mind 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 my{{`ASO being created with property messageHolder set to Hello}}Message`Holder.

  • No labels