Hello Raman,

I think there's nothing out of the box for Tuscany SDOs. I did experiment with 
Pojo/SDO-conversion a while back based on the Eclipse example you cite.
I use a common base class Pojo for all the pojos and customize my jaxb bindings 
with a <xjc:superclass> annotation in bindings.xjb. This base class
contains toXML() and fromXML() methods implemented in the usual JAXB way.

A PojoSDOBridge for Tuscany then can be implemented like this:

/**
   * Convert an SDO to a bean via the shared XML representation.
   * <p>
   * Implementation note: Does not depend on EclipseLink JAXBContextHelper, 
only on standard JAXB.
   * @param sdo
   * @return the bean of the corresponding type
   * @throws JAXBException
   * @throws ClassNotFoundException
   */
  public static Pojo toPojo( DataObject sdo ) throws JAXBException, 
ClassNotFoundException
  {
    SDOHelper helper = SDOHelperFactory.getHelper();
    String xml = helper.toXML( sdo, null );

    Class<? extends Pojo> clazz = Pojo.load( sdo.getType().getURI(), 
sdo.getType().getName() );
    Pojo bean = Pojo.fromXML( xml, clazz );
    return bean;
  }

  /**
   * Convert a bean to an SDO via the shared XML representation.
   * <p>
   * Implementation note: Does not depend on EclipseLink JAXBContextHelper, 
only on Tuscany's SDO implementation.
   * @param bean
   * @return the SDO of the corresponding type
   * @throws JAXBException
   * @throws ClassNotFoundException
   */
  public static DataObject toSDO( Pojo bean ) throws JAXBException, 
ClassNotFoundException
  {
    String xml = bean.toXML();

    // The generated XML contains an xsi:type element, so the XML can be 
unmarshalled to an SDO of that type
    XMLHelper xmlHelper = HelperProvider.getDefaultContext().getXMLHelper();
    XMLDocument xmlDoc = xmlHelper.load( xml );
    DataObject sdo = xmlDoc.getRootObject();

    return sdo;
  }

For loading the pojo class, one must find the FQCN corresponding to the given 
type and namespace. That can be a challenge.
XJC does it, but I haven't had time to figure out how exactly. I came up with a 
simplification that works in most cases. I'd be
grateful for a better suggestion:

/**
   * Constructs the fully qualified class name corresponding to the given 
type's namespace and name.
   * <p>
   * Intended as an analogue to what xjc does.
   * @param namespace must be a valid HTTP-URL. Used to contruct the package 
name.
   * @param typeName the simple name of a type belonging to the namespace. 
Corresponds to the simple class name.
   * @return the fully qualified class name
   */
  // TODO find a utility library that does EXACTLY the same as XJC, even for 
arbitrary URL's.
  private static final String fullyQualifiedClassName( String namespace, String 
typeName )
  {
    URL url;
    try {
      url = new URL( namespace );
    }
    catch( MalformedURLException e ) {
      throw new IllegalStateException( "Namespace must be a valid URL", e );
    }

    if( !url.getProtocol().equals( "http" ) ) {
      throw new IllegalArgumentException( "Namespace must be an HTTP-URL, not " 
+ url );
    }

    String packagePrefix = join( reverse( url.getHost().split( "\\." ) ), '.' );
    String packageSuffix = url.getPath().replaceAll( "/", "." );
    String className = packagePrefix + packageSuffix + "." + typeName;

    return className;
  }

Hope that gets you started. Perhaps something like this would be worth 
including in a Tuscany utility class?

Best Regards,
Sebastian

From: Malisetti, Ramanjaneyulu [mailto:ramanjaneyulu.malise...@ca.com]
Sent: Wednesday, May 15, 2013 10:35 AM
To: user@tuscany.apache.org
Subject: JAXBHelperContext, the POJO/SDO bridge for Tuscany SDO

Hi,
        Is there POJO/SDO bridge for Tuscany SDO API like one discussed at 
http://wiki.eclipse.org/EclipseLink/Examples/SDO/JAXB.

We use SDO as data model for services, but some people have problem with 
dealing dynamic DataObjects. So, I am looking for conversion from static 
DataObject to dynamic DataObject or POJO to SDO.

Regards
Raman

Software AG - Sitz/Registered office: Uhlandstra?e 12, 64297 Darmstadt, Germany 
- Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management 
Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd 
Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. 
Andreas Bereczky - http://www.softwareag.com

Reply via email to