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.

Monday, December 17, 2007

Blood Pressure Over 100

Fluent Interface in Egg Framework 2.0

One of the most exciting features introduced to Egg Framework 2.0 is a Fluent Interface - API, you can use to create readable code web applications in Java - to my surprise looking very similar to HTML.

Fluent Interface is a concept introduced by Martin Fowler and Eric Evans. They named this term to create a style of programming interfaces. More can be read on a blog Martin Fowler.

Here is an example web page created using the Fluent Interface implemented in Egg Framework 2.0 :

   package     org    .    eggframework2    .    view    .    examples    .    xhtml    ;  

import static org . eggframework2 . view . elements . xhtml . ext . FluentInterfaceMethods . * ;

import org . eggframework2 . view . elements . IElement ;
import org . eggframework2 . view . elements . xhtml . tags . Html ;

public class FluentInterfaceUsageExample extends Html {

public FluentInterfaceUsageExample ( ) {
IElement body = //
body ( //
p ( "Hello World" ) , //
a ( "Egg Framework Page" , href ( "http://eggframework.org" ) ) , //
form ( action ( "/action.do" ) , method ( "post" ) , //
input ( type ( "text" ) , name ( "Login" ) ) , br ( ) , //
input ( type ( "password" ) , name ( "Password" ) ) , br ( ) , //
input ( type ( "submit" ) , value ( "Please log me in" ) ) //
) , //
table ( //
tr ( //
td ( "Cell 1" ) , td ( "Cell 2" ) , td ( "Cell 3" ) //
) , //
tr ( //
td ( "Cell 1" ) , td ( "Cell 2" ) , td ( "Cell 3" ) //
) //
) ) ;

add ( body ) ;
}

public static void main ( String [ ] args ) { system. out. println ( new FluentInterfaceUsageExample ( ) ) ; }}

As you can see Java code is very clear and similar to the resulting HTML code. All thanks to the use of static methods and static imports. By importing all the classes FluentInterfaceMethods methods you can use them on your page as if they were part of it. We do not need so precede the name of the method name of the class in which it is defined. This makes code is much more concise.

FluentInterfaceMethods class methods in addition to creating instances of the elements (tags) HTML has a method of forming attributes such as type , name, id . This allows you to create a one line item and set its attributes.

If you are frequently placing the source code (Pretty Print) then you must be prepared for the fact that most of the IDE will work out the code that page with one line (because all the content by creating a nested method calls). It certainly does not help to increase readability of the code, just the opposite - it significantly worse. To guard against this situation, use the comments ("//") in places where the IDE is to break the line. Thanks to them, even after the laying operation code, the code will be nicely presented.

Fluent Interface in Egg Framework is a way to increase the readability of the code is created. It is a kind of overlay, and uses the existing API. After several / several minutes of work you should get used to this "strange style of programming." Depends on you whether you used it or not - in many cases may prove to be better, but there may be situations where the creation of content pages "in the usual way" would be easier and faster.