Friday, December 28, 2007

Camila Rodriguez Teanny

JavaBeans API to copy

recent times often encounter the problem of having to create copies JavaBean'a. While written for the Java API for thousands, however, this simple action is still no ideal solution.

Let me explain briefly what I mean. Above all, such an API should provide the following functionality:

  • deeper into the copy (so-called deep copy) - Copy the reference is not an option because it is not then really the copy
  • not copy n times the same reference - if the original building there are many references to an object, then the copy should also be many references to the copied object
  • deal with the cycles - in the event of a cycle should not throw exceptions
  • supplied ClassLoader used to load classes - so that you can provide the modified class

the web I found some solutions. Among other well-known Commons Beanutils . Unfortunately, it does not meet single requirement. The second library, which I fell into the hands was Sojo. API is under development but already has many features. Supports copying into the depths and can cope with cycles. Do not copy a previously copied references. Unfortunately I have not found a way to provide your own ClassLoadera.

Due to the lack of a dream library started to look for alternatives. The first technique that was used the built-in serialization. Unfortunately, this method does not allow to provide their own ClassLoadera (ClassLoader is used for calling the method deserialize) and requires that each class implemented the Serializable interface.

Another solution that I tested was the use of encoders <-> XML JavaBean provided with the JRE (Class XMLEncoder, XMLDecoder). This technique is by far the slowest, but at first glance it seemed that has all the features that I require from this API. Unfortunately, although providing your own ClassLoader'a it is not used to load all the necessary classes.

After several days of trying various technologies found it sooner as he will implement this functionality. And in this way in 2 days I wrote a simple API to copy beanów with all functionalities. API BeanCopier bears the working name for the time being is available for download on Maven 2 repository at: http://eggframework.org/maven2 .

To use the API in its own project, Maven 2 should be added to the pom.xml:

  \u0026lt;  project  >   \u0026lt;  dependencies  >   \u0026lt;    dependency>   \u0026lt;    groupId>   com.jacekolszak  &lt;/    groupId    >  
&lt; artifactId > beancopier &lt;/ artifactId >
&lt; version > 0.9 &lt;/ version >
&lt;/ dependency >
&lt;/ dependencies >
&lt; repositories >
&lt; repository >
&lt; id > Egg Framework &lt;/ id >
&lt; url > http://eggframework.org/maven2 &lt;/ url >
&lt;/ repository >
&lt;/ repositories >
&lt;/ project >


Sposób Use BeanCopier:

 BeanCopier beanCopier = new BeanCopierImpl () ;  copy = beanCopier.copy (original, classLoader) ;  

soon throw in the CVS source. As long as they are available in the ravine beancopier-0.9-sources.jar the second repository Maven

UPDATE: Sources are available on the Bazaar repository at: http://bazaar.launchpad.net/ ~ jacekolszak / beancopier / trunk . Simply issue the command: bzr branch
http://bazaar.launchpad.net/ ~ jacekolszak / beancopier / trunk
to download the latest sources.

0 comments:

Post a Comment