org.milyn.javabean
Class Bean

java.lang.Object
  extended by org.milyn.javabean.BindingAppender
      extended by org.milyn.javabean.Bean
All Implemented Interfaces:
org.milyn.delivery.VisitorAppender

public class Bean
extends BindingAppender

Programmatic Bean Configurator.

This class can be used to programmatically configure a Smooks instance for performing a Java Bindings on a specific class. To populate a graph, you simply create a graph of Bean instances by binding Beans onto Beans.

This class uses a Fluent API (all methods return the Bean instance), making it easy to string configurations together to build up a graph of Bean configuration.

Example

Taking the "classic" Order message as an example and binding it into a corresponding Java Object model.

The Message

 <order xmlns="http://x">
     <header>
         <y:date xmlns:y="http://y">Wed Nov 15 13:45:28 EST 2006</y:date>
         <customer number="123123">Joe</customer>
         <privatePerson></privatePerson>
     </header>
     <order-items>
         <order-item>
             <product>111</product>
             <quantity>2</quantity>
             <price>8.90</price>
         </order-item>
         <order-item>
             <product>222</product>
             <quantity>7</quantity>
             <price>5.20</price>
         </order-item>
     </order-items>
 </order>
 

The Java Model

(Not including getters and setters):
 public class Order {
     private Header header;
     private List<OrderItem> orderItems;
 }
 public class Header {
     private Long customerNumber;
     private String customerName;
 }
 public class OrderItem {
     private long productId;
     private Integer quantity;
     private double price;
 }
 

The Binding Configuration and Execution Code

The configuration code (Note: Smooks instance defined and instantiated globally):
 Smooks smooks = new Smooks();

 Bean orderBean = new Bean(Order.class, "order", "/order");
 orderBean.bindTo("header",
     orderBean.newBean(Header.class, "/order")
         .bindTo("customerNumber", "header/customer/@number")
         .bindTo("customerName", "header/customer")
     ).bindTo("orderItems",
     orderBean.newBean(ArrayList.class, "/order")
         .bindTo(orderBean.newBean(OrderItem.class, "order-item")
             .bindTo("productId", "order-item/product")
             .bindTo("quantity", "order-item/quantity")
             .bindTo("price", "order-item/price"))
     );

 smooks.addVisitors(orderBean);
 

And the execution code:

 JavaResult result = new JavaResult();

 smooks.filterSource(new StreamSource(orderMessageStream), result);
 Order order = (Order) result.getBean("order");
 

Author:
tom.fennelly@jboss.com
See Also:
Value

Constructor Summary
Bean(Class<?> beanClass, String beanId)
          Create a Bean binding configuration.
Bean(Class<?> beanClass, String beanId, String createOnElement)
          Create a Bean binding configuration.
Bean(Class<?> beanClass, String beanId, String createOnElement, String createOnElementNS)
          Create a Bean binding configuration.
Bean(Class<T> beanClass, String beanId, Factory<? extends T> factory)
          Create a Bean binding configuration.
Bean(Class<T> beanClass, String beanId, String createOnElement, Factory<? extends T> factory)
          Create a Bean binding configuration.
Bean(Class<T> beanClass, String beanId, String createOnElement, String createOnElementNS, Factory<? extends T> factory)
          Create a Bean binding configuration.
 
Method Summary
 void addVisitors(org.milyn.delivery.VisitorConfigMap visitorMap)
          Add the visitors, associated with this Bean instance, to the visitor map.
 Bean bindTo(Bean bean)
          Add a bean binding configuration to this Collection/array bean binding config.
 Bean bindTo(String dataSelector)
          Create a binding configuration to bind the data, selected from the message by the dataSelector, to the target Collection/array Bean beanclass instance.
 Bean bindTo(String bindingMember, Bean bean)
          Add a bean binding configuration for the specified bindingMember (field/method) to this bean binding config.
 Bean bindTo(String dataSelector, org.milyn.javabean.DataDecoder dataDecoder)
          Create a binding configuration to bind the data, selected from the message by the dataSelector, to the target Collection/array Bean beanclass instance.
 Bean bindTo(String bindingMember, String dataSelector)
          Create a binding configuration to bind the data, selected from the message by the dataSelector, to the specified bindingMember (field/method).
 Bean bindTo(String bindingMember, String dataSelector, org.milyn.javabean.DataDecoder dataDecoder)
          Create a binding configuration to bind the data, selected from the message by the dataSelector, to the target Bean member specified by the bindingMember param.
static Method getBindingMethod(String bindingMember, Class<?> beanClass)
          Get the bean binding class Member (field/method).
 Bean newBean(Class<?> beanClass, String createOnElement)
          Create a Bean binding configuration.
 Bean newBean(Class<?> beanClass, String beanId, String createOnElement)
          Create a Bean binding configuration.
static Bean newBean(Class<?> beanClass, String beanId, String createOnElement, String createOnElementNS)
          Create a Bean binding configuration.
<T> Bean
newBean(Class<T> beanClass, String createOnElement, Factory<T> factory)
          Create a Bean binding configuration.
<T> Bean
newBean(Class<T> beanClass, String beanId, String createOnElement, Factory<T> factory)
          Create a Bean binding configuration.
static
<T> Bean
newBean(Class<T> beanClass, String beanId, String createOnElement, String createOnElementNS, Factory<T> factory)
          Create a Bean binding configuration.
 
Methods inherited from class org.milyn.javabean.BindingAppender
getBeanId
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Bean

public Bean(Class<?> beanClass,
            String beanId)
Create a Bean binding configuration.

The bean instance is created on the root/document fragment.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.

Bean

public Bean(Class<T> beanClass,
            String beanId,
            Factory<? extends T> factory)
Create a Bean binding configuration.

The bean instance is created on the root/document fragment.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.
factory - The factory that will create the runtime object

Bean

public Bean(Class<?> beanClass,
            String beanId,
            String createOnElement)
Create a Bean binding configuration.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.
createOnElement - The element selector used to create the bean instance.

Bean

public Bean(Class<T> beanClass,
            String beanId,
            String createOnElement,
            Factory<? extends T> factory)
Create a Bean binding configuration.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.
createOnElement - The element selector used to create the bean instance.
factory - The factory that will create the runtime object

Bean

public Bean(Class<?> beanClass,
            String beanId,
            String createOnElement,
            String createOnElementNS)
Create a Bean binding configuration.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.
createOnElement - The element selector used to create the bean instance.
createOnElementNS - The namespace for the element selector used to create the bean instance.

Bean

public Bean(Class<T> beanClass,
            String beanId,
            String createOnElement,
            String createOnElementNS,
            Factory<? extends T> factory)
Create a Bean binding configuration.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.
createOnElement - The element selector used to create the bean instance.
createOnElementNS - The namespace for the element selector used to create the bean instance.
factory - The factory that will create the runtime object
Method Detail

newBean

public static Bean newBean(Class<?> beanClass,
                           String beanId,
                           String createOnElement,
                           String createOnElementNS)
Create a Bean binding configuration.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.
createOnElement - The element selector used to create the bean instance.
createOnElementNS - The namespace for the element selector used to create the bean instance.

newBean

public static <T> Bean newBean(Class<T> beanClass,
                               String beanId,
                               String createOnElement,
                               String createOnElementNS,
                               Factory<T> factory)
Create a Bean binding configuration.

Parameters:
beanClass - The bean runtime class.
beanId - The bean ID.
createOnElement - The element selector used to create the bean instance.
createOnElementNS - The namespace for the element selector used to create the bean instance.
factory - The factory that will create the runtime object

newBean

public Bean newBean(Class<?> beanClass,
                    String createOnElement)
Create a Bean binding configuration.

This method binds the configuration to the same Smooks instance supplied in the constructor. The beanId is generated.

Parameters:
beanClass - The bean runtime class.
createOnElement - The element selector used to create the bean instance.
Returns:
this Bean configuration instance.

newBean

public <T> Bean newBean(Class<T> beanClass,
                        String createOnElement,
                        Factory<T> factory)
Create a Bean binding configuration.

This method binds the configuration to the same Smooks instance supplied in the constructor. The beanId is generated.

Parameters:
beanClass - The bean runtime class.
createOnElement - The element selector used to create the bean instance.
factory - The factory that will create the runtime object
Returns:
this Bean configuration instance.

newBean

public Bean newBean(Class<?> beanClass,
                    String beanId,
                    String createOnElement)
Create a Bean binding configuration.

This method binds the configuration to the same Smooks instance supplied in the constructor.

Parameters:
beanClass - The bean runtime class.
beanId - The beanId.
createOnElement - The element selector used to create the bean instance.
Returns:
this Bean configuration instance.

newBean

public <T> Bean newBean(Class<T> beanClass,
                        String beanId,
                        String createOnElement,
                        Factory<T> factory)
Create a Bean binding configuration.

This method binds the configuration to the same Smooks instance supplied in the constructor.

Parameters:
beanClass - The bean runtime class.
beanId - The beanId.
createOnElement - The element selector used to create the bean instance.
factory - The factory that will create the runtime object
Returns:
this Bean configuration instance.

bindTo

public Bean bindTo(String bindingMember,
                   String dataSelector)
Create a binding configuration to bind the data, selected from the message by the dataSelector, to the specified bindingMember (field/method).

Discovers the DataDecoder through the specified bindingMember.

Parameters:
bindingMember - The name of the binding member. This is a bean property (field) or method name.
dataSelector - The data selector for the data value to be bound.
Returns:
The Bean configuration instance.

bindTo

public Bean bindTo(String bindingMember,
                   String dataSelector,
                   org.milyn.javabean.DataDecoder dataDecoder)
Create a binding configuration to bind the data, selected from the message by the dataSelector, to the target Bean member specified by the bindingMember param.

Parameters:
bindingMember - The name of the binding member. This is a bean property (field) or method name.
dataSelector - The data selector for the data value to be bound.
dataDecoder - The DataDecoder to be used for decoding the data value.
Returns:
this Bean configuration instance.

bindTo

public Bean bindTo(String bindingMember,
                   Bean bean)
Add a bean binding configuration for the specified bindingMember (field/method) to this bean binding config.

This method is used to build a binding configuration graph, which in turn configures Smooks to build a Java Object Graph (ala <jb:wiring> configurations).

Parameters:
bindingMember - The name of the binding member. This is a bean property (field) or method name. The bean runtime class should match the
bean - The Bean instance to be bound
Returns:
this Bean configuration instance.

bindTo

public Bean bindTo(Bean bean)
            throws IllegalArgumentException
Add a bean binding configuration to this Collection/array bean binding config.

This method checks that this bean's beanClass is a Collection/array, generating an IllegalArgumentException if the check fails.

Parameters:
bean - The Bean instance to be bound
Returns:
this Bean configuration instance.
Throws:
IllegalArgumentException - this Bean's beanClass (not the supplied bean!) is not a Collection/array. You cannot call this method on Bean configurations whose beanClass is not a Collection/array. For non Collection/array types, you must use one of the bindTo meths that specify a 'bindingMember'.

bindTo

public Bean bindTo(String dataSelector)
Create a binding configuration to bind the data, selected from the message by the dataSelector, to the target Collection/array Bean beanclass instance.

Parameters:
dataSelector - The data selector for the data value to be bound.
Returns:
this Bean configuration instance.

bindTo

public Bean bindTo(String dataSelector,
                   org.milyn.javabean.DataDecoder dataDecoder)
Create a binding configuration to bind the data, selected from the message by the dataSelector, to the target Collection/array Bean beanclass instance.

Parameters:
dataSelector - The data selector for the data value to be bound.
dataDecoder - The DataDecoder to be used for decoding the data value.
Returns:
this Bean configuration instance.

addVisitors

public void addVisitors(org.milyn.delivery.VisitorConfigMap visitorMap)
Add the visitors, associated with this Bean instance, to the visitor map.

Parameters:
visitorMap - The visitor Map.

getBindingMethod

public static Method getBindingMethod(String bindingMember,
                                      Class<?> beanClass)
Get the bean binding class Member (field/method).

Parameters:
bindingMember - Binding member name.
Returns:
The binding member, or null if not found.


Copyright © 2018. All rights reserved.