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/Converting XML to Objects/Events"

Line 14: Line 14:
  
 
<source lang="java">
 
<source lang="java">
  // Invoked by Marshaller after it has created an instance of this object.
+
// Invoked by Marshaller after it has created an instance of this object.
  boolean beforeMarshal(Marshaller m);
+
boolean beforeMarshal(Marshaller m);
  
  // Invoked by Marshaller after it has marshalled all properties of this object.
+
// Invoked by Marshaller after it has marshalled all properties of this object.
  void afterMarshal(Marshaller m);
+
void afterMarshal(Marshaller m);
  
  // This method is called immediately after the object is created and before the unmarshalling of this  
+
// 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.
+
// object begins. The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
  void beforeUnmarshal(Unmarshaller u, Object parent);
+
void beforeUnmarshal(Unmarshaller u, Object parent);
  
  //This method is called after all the properties (except IDREF) are unmarshalled for this object,  
+
//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.
+
//but before this object is set to the parent object.
  void afterUnmarshal(Unmarshaller u, Object parent);
+
void afterUnmarshal(Unmarshaller u, Object parent);
 
</source>
 
</source>
  

Revision as of 17:29, 2 June 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Events

JAXB contains several mechanisms to get event callbacks during the marshalling and unmarshalling processes.

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:

// Invoked by Marshaller after it has created an instance of this object.
boolean 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);

Example

The following example shows how to add to a log file every time a Customer object is marshalled or unmarshalled.

package example;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
public class Company {
 
   ...
 
   boolean beforeMarshal(Marshaller m) {
      GlobalLogManager.logMessage(new java.util.Date() + " [COMPANY Pre-Marshal] " + Thread.currentThread())
      return true;
   }
 
   void afterMarshal(Marshaller m) {
      GlobalLogManager.logMessage(java.util.Date() + " [COMPANY Post-Marshal] " + Thread.currentThread());
   }
 
   void beforeUnmarshal(Unmarshaller u, Object parent) {
      GlobalLogManager.logMessage(new java.util.Date() + " [COMPANY Pre-Unmarshal] " + Thread.currentThread());
   }
 
   void afterUnmarshal(Unmarshaller u, Object parent) {
      GlobalLogManager.logMessage(new java.util.Date() + " [COMPANY Pre-Unmarshal] " + Thread.currentThread());
   }
 
}

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. Marshaller.Listener and Unmarshaller.Listener

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) {}
   }
}
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) {}
   }
}

Back to the top