Saturday, 2 March 2013

Project on Sourceforge: Pojo/Bean mapper

This is a work in progress but I am creating an ANT task to generate the conversion classes to allow you to create the code that transfers data from one POJO to an other..
While at first sight this may seem similar to dozer the primary difference is that this is code generation and does not have the overhead of using introspection at run time.


Project on Sourceforge: https://sourceforge.net/projects/pojobeanmapper/


The original use for this was to take data out of a set of Hibernate DAO objects and create a variety of XML DOM objects from them. In the project that instigated this work the mapping between the DAOs and the XML DOM was not one for one and encoding the conversion into the DOAs was not a good idea.

Use

To create the task ensure the ant-pojomapper-x.x.xx.jar is on your ANT path (I suggest in ANT_HOME/lib).
Create the task with this task def command:

<property name="ant.pojomapper.dir" value="./libs/pojomapper"/>
<path id="pojomapper.lib.classpath">
<fileset dir="${ant.pojomapper.dir}" includes="*.jar" />
</path>
<taskdef resource="pojomapper_ant.properties" classpathref="pojomapper.lib.classpath" />


Once declared this will allow you to call the pojomapper task.
This will look something like this:

<beanmapper
classPath="${build.classpath}"
settingsFolder="./src/templates"
srcPackage ="myproj.dom1"
dstPackage ="myproj.api"
dstDir ="${gensrc.java.dir}/pojomapping"
outputPackageName ="myproj.dom1.converters"
/>

To work correctly there needs to be at least the following file present in the settings folder:
This file is a "property" file which contains the classes that equate to each other.

beanmapper-classpairs.properties

This file is of the form:

srcClass1=destClass1
srcClass2=destClass2

In this example two classes will be created in the package myproj.dom1.converters:

SrcClass1ToDestClass1Copier.java
SrcClass2ToDestClass2Copier.java


The generator will create these files under the folder   ${gensrc.java.dir}/pojomapping/src
along with a copy of the beanmapper-classpairs.properties file.


Additionally two other files are auto-generated.
These files can be reused if the build process failed.

beanmapper-knownmethodmappings.properties

While the this pojo mapper will match beans of the same name.
There are some that it will not beable to convert.

destClass.setter=srcClass.getter

The file defines the mappings that are already known.

PojoB.pennies=PojoA.cash
PojoB.extra=PojoA.more

At the end of the generated file are a series of mappings the pojo mapper could not determine what should be done with them.
These will appear as follows:

## Missing mappings
# destClass.???=PojoA.getter1
# destClass.???=PojoA.getter2

This allows the developer to determine quickly which methods need some attention.

beanmapper-classconverters.properties

There are conversions that the pojomapper knows implicitly but some are not known.

These need to be defined for it as follows:

SrcType:DstType=ConverterClass.convertermethod

This will create a call in any of the generated code where a SrcType class needs to be converted into a dstType.

The call will be approximatly as follows:

SrcType a ;
DstType b ;
b = ConverterClass.convertermethod(a);


If the pojomapper will detect the need for converters that it is self will be creating.

MAVEN USE

It is possible to use this ant task as a plugin.
Add the following:

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>download-files</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo>+++++++++++++++++ DOWNLOAD POJO BEAN MAPPER ++++++++++++++</echo>
                                <echo>Download pojobeanmapper to ${project.build.pojobeanmapper.jar}</echo>
                                <mkdir dir="${project.build.download.folder}" />
                                <get src="${project.build.pojobeanmapper.url}" 
                                        dest="${project.build.pojobeanmapper.jar}"
                                        verbose="false" 
                                        usetimestamp="true" />
                                <echo>Get commons-io 2.1</echo>
                                <get src="http://repo1.maven.org/maven2/commons-io/commons-io/2.1/commons-io-2.1.jar" 
                                        dest="${project.build.download.folder}"
                                        verbose="false" 
                                        usetimestamp="true" />
                                <echo>Get commons-lang 2.6</echo>
                                <get src="http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar" 
                                        dest="${project.build.download.folder}"
                                        verbose="false" 
                                        usetimestamp="true" />                                
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <phase>generate-sources</phase>
                        <configuration>
                            <target>
                                <echo>+++++++++++++++++ INVOKE POJO BEAN MAPPER ++++++++++++++</echo>
                            
                                <!-- sEE: http://maven.apache.org/plugins/maven-antrun-plugin/examples/classpaths.html -->
                                <property name="compile_classpath" refid="maven.compile.classpath"/>
                                <property name="runtime_classpath" refid="maven.runtime.classpath"/>
                                <property name="test_classpath"    refid="maven.test.classpath"/>
                                <property name="plugin_classpath"  refid="maven.plugin.classpath"/>
                                <path id="pojomapper.lib.classpath">
                                     <fileset dir="${project.build.download.folder}" includes="*.jar" />
                                </path> 
                                <taskdef resource="pojomapper_ant.properties" 
                                         classpathref="pojomapper.lib.classpath" />

                                <beanmapper 
                                    classPath="${compile_classpath}"
                                    settingsFolder="./src/templates"
                                    srcPackage ="com.proj.src"
                                    dstPackage ="com.proj.dst"
                                    dstDir ="${project.build.directory}/pojomapping"
                                    outputPackageName ="com.proj.converters"
                                    />

                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


and add the following properties:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.pojobeanmapper.url>http://sourceforge.net/projects/pojobeanmapper/files/latest/download?source=files</project.build.pojobeanmapper.url>
        <project.build.download.folder>${project.build.directory}/downloads</project.build.download.folder>
        <project.build.pojobeanmapper.jar>${project.build.download.folder}/pojobeanmapper.jar</project.build.pojobeanmapper.jar>
    </properties>


------------------------------------------------------------------------------
Please return to the is blog entry for full information at a later date.
It will be updated over the month of decemper 2013.

Thursday, 28 February 2013

Ant tasks with Eclipse

When managing an eclipse project and building it with ANT it can be a real pain to constantly manage the class paths in the ANT file and your project. The EBuilder ant tasks make this easier.

There is a project on source forge (http://ebuild.firefang.net/) which does this nicely however there are a couple of flaws in that version and I have created a new release (Sourceforge version TBA post a request to me and I will send it to you).

After downloading and placing the JAR into your ANT_HOME\lib directory, the way the project works is the typical task definition:

<taskdef       resource="firefang_ant.properties"      classpathref="ant.lib.classpath" />

... followed by a series of tasks the most useful of which is the eclipse_cp  task.
This will read a specified .classpath file from your project and scan the entries it finds there.
ie.

<eclipse_cp action="libs" rootdir="${project.dir}" project="." result="proj.libs" />

See Eclipse Help

Your .classpath will look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" path="test"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="lib" path="conf"/>
    <classpathentry kind="con" path="org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?ivyXmlPath=ivy.xml&amp;confs=*"/>
    <classpathentry kind="output" path="build"/>
</classpath>

The action attribute on the task can be any of the following values:
lib : Which reads the library entries;
src: Which reads the source entries;
output : Which reads the library entries;

This produces a ';' separated list which can be used directly as a class path or it can have the libaries turned into fully qualified paths (the next iteration of the project).

ie.

<for list="${proj.libs}" param="lib" delimiter=";">
                    <sequential>
                        <echo message="    checking @{lib}"/>
                        <!--
                        <var name="build.classpath" value="${build.classpath};${project.dir}/@{lib}" />
                        -->
                        <if>
                            <available file="@{lib}"/>
                            <then>
                                <var name="build.classpath" value="${build.classpath};@{lib}" />
                            </then>
                            <else>
                                <var name="build.classpath" value="${build.classpath};${project.dir}/@{lib}" />
                            </else>
                        </if>
                       
                    </sequential>
                </for>


NB. The above uses ant-contrib v1 (http://ant-contrib.sourceforge.net/)






Setting up Eclipse check list (Part 3) - Plugins Extras

Previously in this series

Following on from the previous blog of a year ago I found my self setting up Juno and I thought it might be an idea to update the list of plugins I use.
So this is the set I find most useful:

SubEclipse

Links to subversion.
Juno comes with the GIT integration but for a few ludites this is still nice.
The update site is http://subclipse.tigris.org/update_1.8.x 


StartExplorer Eclipse Plug-in

This plug-in offers tight integration of system file manager (Windows Explorer, Gnome Nautilus, KDE Konqueror, Mac Finder, ...) and shell (cmd.exe, Linux/Mac terminal) in Eclipse.
The update site is http://basti1302.github.com/startexplorer/update/

Log4E

Log4E is an Eclipse Plugin which helps you to use your logger easily in Java Projects.
The Plugin Log4E is not bound to any special logging framework. Thus you might be able to adapt to your own logger by defining your own templates using the preferences. It has active support for Log4j, SLF4J, Commons Logging and JDK 1.4 logging.

Aptana

Aptana Studio 3.2.2 is a complete environment that includes extensive capabilities to build Ruby and Rails, PHP, and Python applications, along with complete HTML, CSS and JavaScript editing.

Or preferably by adding the following update site http://download.aptana.com/studio3/plugin/install

Monday, 4 February 2013

Cute tool of the day - Lorum Ipsum

“Lorem ipsum” dummy text is used by many web-developers to test how their HTML templates will look with real data. Often, developers use third-party services to generate “Lorem ipsum” text, but now you can do that right in your editor.
The website will just create html for you to use.
BUT if you want to get some random data in your code try http://loremipsum.sourceforge.net/ which you can connect to your code directly.

Tool tip of the day - Colour Scheme Designer

For all you budding web disigners ... a nice easy way of determining what coulours you should have on your website.

http://colorschemedesigner.com
This site based on a colour wheel allows you to design a harmonious web site.
I particularly liked the ability to export the CSS & to get a colour chart for your web site.

Saturday, 29 December 2012

Stream music from your computer to Xbox 360

Over xmas my son ask me for some music on his xbox.
Easy I thought ... no so!
it is easy if you have Vista, W7 & W8 but not if you have a dinosaur media system based on xp.
But you can get there in the end.
Follow these steps:
  1. Read this post on the XBoz site.http://windows.microsoft.com/en-GB/windows-xp/help/windows-media-player/11/stream-xbox.
    This should get you up to speed on what you need to do.
  2. Download media Player or Zune (if you can stand the Metro look and feel).
  3. Start the sharing process as described in the first article.
  4. Test the connection from the xbox;
  5. If it fails (as it did for me) check the service "Windows Media Player Network Sharing Service".
    -Open the task manager. (ctrl+alt+del)
    -Go to the Services Tab.
    -Click on the Services button and minimize task manager.
    -In new Services Window, right click on Windows Media Player Network Sharing Service.
    -Select Properties.
    -go to Log On tab.
    -if Local System Account is not highlighted, then you've most likely found the problem. Select Local System Account radio button, click Apply and OK, then right click on Windows Media Player Network Sharing Service again and select restart service.
That should sort it!

Tuesday, 4 December 2012

Pre-configured stacks ... nice!


http://bitnami.org/stack/tomcatstack
I haven't tried these yet but they look worth a go ...
There are a number of different pre configured stacks for your downloading and installing pleasure.

Including installers for vm-ware ... nice!