tuscany-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Millies, Sebastian" <sebastian.mill...@ids-scheer.com>
Subject RE: JAXBHelperContext, the POJO/SDO bridge for Tuscany SDO
Date Wed, 15 May 2013 12:10:30 GMT
No, I’ve never measured the performance. Of course it’s going to be very slow, going via
the shared XML representation.

Oh, and I’d forgotten one important technical detail: In order to make the XML representation
of the pojo convertible to an SDO, you need to force the presence of an xsi:type attribute.
I think that can be done this way:

// Convert to an XML representation with a specified root element. This class should not have
an @XmlRootElement.
// Use the superclass as the second argument for the JAXBElement, which forces the generation
of an
// xsi:type attribute in the XML, which is needed for unmarshalling into an SDO of the appropriate
type.
QName qName = new QName( namespaceURI, localPart, prefix );
JAXBElement<AbstractJaxbBean> jaxbElement = new JAXBElement<Pojo>( qName, Pojo.class,
this );


n  Sebastian

From: Malisetti, Ramanjaneyulu [mailto:Ramanjaneyulu.Malisetti@ca.com]
Sent: Wednesday, May 15, 2013 1:52 PM
To: user@tuscany.apache.org
Subject: RE: JAXBHelperContext, the POJO/SDO bridge for Tuscany SDO

Thank you Sebastian. This approach seem expensive but better than nothing. What is the overhead,
any idea?

Regards
Raman

From: Millies, Sebastian [mailto:sebastian.millies@ids-scheer.com]
Sent: Wednesday, May 15, 2013 3:59 PM
To: user@tuscany.apache.org<mailto:user@tuscany.apache.org>
Subject: RE: JAXBHelperContext, the POJO/SDO bridge for Tuscany SDO

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( "\\.<file:///\\.>" )
), '.' );
    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.Malisetti@ca.com]
Sent: Wednesday, May 15, 2013 10:35 AM
To: user@tuscany.apache.org<mailto: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


Mime
View raw message