Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Events"

m (Replacing page with ''''Warning For the current release, see [http://www.eclipse.org/eclipselink/documentation/2.4/moxy Developing JAXB Applications Using EclipseLink ...')
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] For the current release, see [http://www.eclipse.org/eclipselink/documentation/2.4/moxy Developing JAXB Applications Using EclipseLink MOXy, EclipseLink 2.4]
|info=y
+
'''
|eclipselink=y
+
|eclipselinktype=MOXy
+
|toc=y
+
}}
+
= Events =
+
  
JAXB offers several mechanisms to get event callbacks during the marshalling and unmarshalling processes.  You can specify callback methods directly on your mapped objects, or define separate '''Listener''' classes and register them with the JAXB runtime.
+
http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime007.htm
 
+
 
+
== Event Listener Methods on JAXB Mapped Objects ==
+
 
+
On any of your objects you have mapped with JAXB, you have the option of specifying some special methods to allow you to recieve event notification when that object is marshalled or unmarshalled.  The methods must have the following signatures:
+
 
+
<source lang="java">
+
/**
+
* Invoked by Marshaller after it has created an instance of this object.
+
*/
+
void beforeMarshal(Marshaller m);
+
 
+
/**
+
* Invoked by Marshaller after it has marshalled all properties of this object.
+
*/
+
void afterMarshal(Marshaller m);
+
 
+
/**
+
* This method is called immediately after the object is created and before the unmarshalling of this
+
* object begins. The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
+
*/
+
void beforeUnmarshal(Unmarshaller u, Object parent);
+
 
+
/**
+
* This method is called after all the properties (except IDREF) are unmarshalled for this object,
+
* but before this object is set to the parent object.
+
*/
+
void afterUnmarshal(Unmarshaller u, Object parent);
+
</source>
+
 
+
 
+
=== Example ===
+
 
+
The following example shows how to write to a log every time a '''Company''' object is processed.
+
 
+
<source lang="java">
+
package example;
+
 
+
import java.util.Date;
+
import java.util.logging.Logger;
+
import javax.xml.bind.annotation.*;
+
 
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Company {
+
 
+
  @XmlAttribute
+
  private String id;
+
 
+
  void beforeMarshal(Marshaller m) {
+
      Logger.getLogger("example").info("COMPANY:[id=" + id + "] " + Thread.currentThread());
+
  }
+
 
+
  void afterMarshal(Marshaller m) {
+
      Logger.getLogger("example").info("COMPANY:[id=" + id + "] " + Thread.currentThread());
+
  }
+
 
+
  void beforeUnmarshal(Unmarshaller u, Object parent) {
+
      Logger.getLogger("example").info("COMPANY:[id=" + id + "] " + Thread.currentThread());
+
  }
+
 
+
  void afterUnmarshal(Unmarshaller u, Object parent) {
+
      Logger.getLogger("example").info("COMPANY:[id=" + id + "] " + Thread.currentThread());
+
  }
+
 
+
}
+
</source>
+
 
+
 
+
== Registering Listeners on Marshallers and Unmarshallers ==
+
 
+
JAXB's '''Marshaller''' and '''Unmarshaller''' interfaces both define a '''setListener()''' method, which allows you to set your own custom '''Listener''' to intercept marshal and unmarshal events.
+
 
+
<source lang="java">
+
package javax.xml.bind;
+
 
+
public interface Marshaller {
+
  ...
+
  public static abstract class Listener {
+
    /**
+
      * Callback method invoked before marshalling from source to XML.
+
      *
+
      * This method is invoked just before marshalling process starts to marshal source.
+
      * Note that if the class of source defines its own beforeMarshal method,
+
      * the class specific callback method is invoked just before this method is invoked.
+
      *
+
      * @param source instance of JAXB mapped class prior to marshalling from it.
+
      */
+
      public void beforeMarshal(Object source) {}
+
 
+
    /**
+
      * Callback method invoked after marshalling source to XML.
+
      *
+
      * This method is invoked after source and all its descendants have been marshalled.
+
      * Note that if the class of source defines its own afterMarshal method,
+
      * the class specific callback method is invoked just before this method is invoked.
+
      *
+
      * @param source instance of JAXB mapped class after marshalling it.
+
      */
+
      public void afterMarshal(Object source) {}
+
  }
+
}
+
</source>
+
 
+
<source lang="java">
+
package javax.xml.bind;
+
 
+
public interface Unmarshaller {
+
  ...
+
  public static abstract class Listener {
+
 
+
    /**
+
      * Callback method invoked before unmarshalling into target.
+
      *
+
      * This method is invoked immediately after target was created and
+
      * before the unmarshalling of this object begins. Note that
+
      * if the class of target defines its own beforeUnmarsha method,
+
      * the class specific callback method is invoked before this method is invoked.
+
      *
+
      * @param target non-null instance of JAXB mapped class prior to unmarshalling into it.
+
      * @param parent instance of JAXB mapped class that will eventually reference target.
+
      *              null when target is root element.
+
      */
+
      public void beforeUnmarshal(Object target, Object parent) {}
+
 
+
    /**
+
      * Callback method invoked after unmarshalling XML data into target.
+
      *
+
      * This method is invoked after all the properties (except IDREF) are unmarshalled into target,
+
      * but before target is set into its parent object.
+
      * Note that if the class of target defines its own afterUnmarshal method,
+
      * the class specific callback method is invoked before this method is invoked.
+
      *
+
      * @param target non-null instance of JAXB mapped class prior to unmarshalling into it.
+
      * @param parent instance of JAXB mapped class that will reference target.
+
      *              null when target is root element.
+
      */
+
      public void afterUnmarshal(Object target, Object parent) {}
+
  }
+
}
+
</source>
+
 
+
 
+
=== Example ===
+
 
+
This example performs the same logging as above, but using generic '''Listener''' classes.  This makes it easier to log all JAXB objects in the system.
+
 
+
<source lang="java">
+
package example;
+
 
+
import java.util.logging.Logger;
+
 
+
private class MarshalLogger extends Marshaller.Listener {
+
  @Override
+
  public void afterMarshal(Object source) {
+
      Logger.getLogger("example").info(source + " "  + Thread.currentThread());
+
  }
+
 
+
  @Override
+
  public void beforeMarshal(Object source) {
+
      Logger.getLogger("example").info(source + " "  + Thread.currentThread());
+
  }
+
}
+
</source>
+
 
+
<source lang="java">
+
package example;
+
 
+
import java.util.logging.Logger;
+
 
+
private class UnmarshalLogger extends Unmarshaller.Listener {
+
  @Override
+
  public void afterUnmarshal(Object target, Object parent) {
+
      Logger.getLogger("example").info(target + " "  + Thread.currentThread());
+
  }
+
 
+
  @Override
+
  public void beforeUnmarshal(Object target, Object parent) {
+
      Logger.getLogger("example").info(target + " "  + Thread.currentThread());
+
  }
+
}
+
</source>
+
 
+
The following code sets up the listeners:
+
 
+
<source lang="java">
+
Marshaller marshaller = jaxbContext.createMarshaller();
+
marshaller.setListener(new MarshalLogger());
+
 
+
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
+
unmarshaller.setListener(new UnmarshalLogger());
+
</source>
+
 
+
An example of a typical marshal / unmarshal example, showing both the class-level and '''Marshaller/Unmarshaller'''-level event output:
+
 
+
<tt>
+
<span style="color:#009000">
+
Jun 2, 2011 6:31:59 PM example.Company beforeMarshal<br>
+
INFO: COMPANY:[id=Zoltrix] Thread[main,5,main]<br>
+
</span>
+
<span style="color:#000090">
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger beforeMarshal<br>
+
INFO: example.Company@10e790c Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger beforeMarshal<br>
+
INFO: example.Employee@1db7df8 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger afterMarshal<br>
+
INFO: example.Employee@1db7df8 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger beforeMarshal<br>
+
INFO: example.Employee@3570b0 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger afterMarshal<br>
+
INFO: example.Employee@3570b0 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger beforeMarshal<br>
+
INFO: example.Employee@79717e Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger afterMarshal<br>
+
INFO: example.Employee@79717e Thread[main,5,main]<br>
+
</span>
+
<span style="color:#009000">
+
Jun 2, 2011 6:31:59 PM example.Company afterMarshal<br>
+
INFO: COMPANY:[id=Zoltrix] Thread[main,5,main]<br>
+
</span>
+
<span style="color:#000090">
+
Jun 2, 2011 6:31:59 PM example.Tester$MarshalLogger afterMarshal<br>
+
INFO: example.Company@10e790c Thread[main,5,main]<br>
+
</span>
+
<span style="color:#009000">
+
Jun 2, 2011 6:31:59 PM example.Company beforeUnmarshal<br>
+
INFO: COMPANY:[id=null] Thread[main,5,main]<br>
+
</span>
+
<span style="color:#000090">
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger beforeUnmarshal<br>
+
INFO: example.Company@f0c0d3 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger beforeUnmarshal<br>
+
INFO: example.Employee@4f80d6 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger afterUnmarshal<br>
+
INFO: example.Employee@4f80d6 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger beforeUnmarshal<br>
+
INFO: example.Employee@1ea0252 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger afterUnmarshal<br>
+
INFO: example.Employee@1ea0252 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger beforeUnmarshal<br>
+
INFO: example.Employee@3e89c3 Thread[main,5,main]<br>
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger afterUnmarshal<br>
+
INFO: example.Employee@3e89c3 Thread[main,5,main]<br>
+
</span>
+
<span style="color:#009000">
+
Jun 2, 2011 6:31:59 PM example.Company afterUnmarshal<br>
+
INFO: COMPANY:[id=Zoltrix] Thread[main,5,main]<br>
+
</span>
+
<span style="color:#000090">
+
Jun 2, 2011 6:31:59 PM example.Tester$UnmarshalLogger afterUnmarshal<br>
+
INFO: example.Company@f0c0d3 Thread[main,5,main]<br>
+
</span>
+
</tt>
+

Latest revision as of 16:30, 6 November 2012

Warning For the current release, see Developing JAXB Applications Using EclipseLink MOXy, EclipseLink 2.4

http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime007.htm

Back to the top