25 Ağustos 2011 Perşembe

how to continue build with ant despite failure

I am aware of two ways to continue build process despite failure.. The first way is the easy one.. If the task that may fail has failonerror property, simply set it to false. However, not all tasks have that property. In those situations, subant task( http://ant.apache.org/manual/Tasks/subant.html) which is generally used for batch builds come into play. In order to use subant, you have to redesign your build file by moving targets which may-fail during build process in seperate build files. The target names that will be executed wihin subant in main build.xml must be identical..

Here is a subant example:


<subant failonerror="false">
            <fileset dir="." includes="**/build.xml" excludes="build.xml"/>
            <target name="clean"/>
            <target name="build"/>
        </subant>

By the way, if the execution order is important, filelist must be used instead of fileset as follows:


<subant failonerror="false">
<filelist dir="${parent.dir}">
    <file name="project1/build.xml"/>
    <file name="project2/bıild.xml"/>
    ...
    <file name="projectn/build.xml"/>
  </filelist>
            <target name="clean"/>
            <target name="build"/>
        </subant>




warning: non-varargs call of varargs method with inexact argument type for last parameter

The method i explained in http://hilaltarakci.blogspot.com/2011/01/printing-object-with-reflection-api-for.html is my favorite debug mechanism..
However, it results in the following warning:


warning: non-varargs call of varargs method with inexact argument type for last parameter;
    [javac] cast to java.lang.Object for a varargs call
    [javac] cast to java.lang.Object[] for a non-varargs call and to suppress this warning
    [javac]                             logger.debug("METHOD: " + mBean[i].getName() + " : " + mBean[i].invoke(classItself, null));

In order to prevent the error, simply modify the following line by casting null to Object []:

System.out.println("METHOD: " + m[i].getName() + " : " + m[i].invoke(classItself, (Object [])null));

24 Ağustos 2011 Çarşamba

ant deploy to glassfish for service assemblies

The previous post is about ant deploy of web services to glassfish v2.1 application server. If you are curious, please read http://hilaltarakci.blogspot.com/2011/08/ant-deploy-to-glassfish.html.
This post is about deploying service assemblies to glassfish via ant..

Here is my solution:
jbi_admin.xml, copied from ${glassfish.home}/jbi/bin, modified as follows:


<?xml version="1.0" encoding="UTF-8"?>
<!--
 # BEGIN_HEADER - DO NOT EDIT
 #
 # The contents of this file are subject to the terms
 # of the Common Development and Distribution License
 # (the "License").  You may not use this file except
 # in compliance with the License.
 #
 # You can obtain a copy of the license at
 # https://open-esb.dev.java.net/public/CDDLv1.0.html.
 # See the License for the specific language governing
 # permissions and limitations under the License.
 #
 # When distributing Covered Code, include this CDDL
 # HEADER in each file and include the License file at
 # https://open-esb.dev.java.net/public/CDDLv1.0.html.
 # If applicable add the following below this CDDL HEADER,
 # with the fields enclosed by brackets "[]" replaced with
 # your own identifying information: Portions Copyright
 # [year] [name of copyright owner]
-->


<!--
 # @(#)jbi_admin.xml
 # Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
 #
 # END_HEADER - DO NOT EDIT
-->
<!--
 Copyright (c) 2004-2007 Sun Microsystems Inc., All Rights Reserved.
-->
<!--
@author chikkala
-->
<project basedir="." name="jbi_admin" xmlns:jbi="http://www.sun.com/ns/jbi/jbi-ant/1">
<target name="-pre-jbi-admin-setup">
</target>


<target name="-jbi-admin-setup" depends="-pre-jbi-admin-setup">
<taskdef resource="com/sun/jbi/ui/ant/antlib.xml" />
<!-- properties used as attribute values for ant tasks -->
<property name="jbi.target" value="server" />
<property name="jbi.secure" value="false" />
<property name="jbi.username" value="admin" />
<property name="jbi.password" value="adminadmin" />
<property name="jbi.host" value="localhost" />
<property name="jbi.port" value="4848" />
<property name="jbi.show.descriptor" value="false" />
<property name="jbi.task.fail.on.error" value="true" />
<property name="jbi.force.task" value="false" />
<property name="jbi.keep.archive" value="false" />
</target>


<target name="init-jbi-admin" depends="-jbi-admin-setup" />


<!-- deployment targets -->
<!-- deploy service assembly targets -->


<target name="-init-deploy-sa">
<condition property="deploy.sa">
<and>
<isset property="jbi.deploy.file" />
<not>
<isset property="jbi.service.assembly.name" />
</not>
</and>
</condition>
<condition property="deploy.sa.from.domain">
<and>
<isset property="jbi.service.assembly.name" />
<not>
<isset property="jbi.deploy.file" />
</not>
</and>
</condition>
<condition property="deploy.sa.option.error1">
<and>
<isset property="jbi.service.assembly.name" />
<isset property="jbi.deploy.file" />
</and>
</condition>
<condition property="deploy.sa.option.error2">
<and>
<not>
<isset property="jbi.service.assembly.name" />
</not>
<not>
<isset property="jbi.deploy.file" />
</not>
</and>
</condition>
</target>


<target name="-do-deploy-sa" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" file="${jbi.deploy.file}" />
</target>


<target name="-do-deploy-sa-from-domain" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa.from.domain">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" name="${jbi.service.assembly.name}" />
</target>


<target name="-do-deploy-sa-option-error1" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa.option.error1">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" file="${jbi.deploy.file}" name="${jbi.service.assembly.name}" />
</target>


<target name="-do-deploy-sa-option-error2" depends="init-jbi-admin, -init-deploy-sa" if="deploy.sa.option.error2">
<jbi-deploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" />
</target>


<!-- main install target for deploy service assembly -->
<target name="deploy-service-assembly" depends="init-jbi-admin, -init-deploy-sa,
        -do-deploy-sa, -do-deploy-sa-from-domain, 
        -do-deploy-sa-option-error1, -do-deploy-sa-option-error2" description="Deploys Service assembly">
</target>


<!-- undeploy service assembly target -->
<target name="-init-undeploy-sa" depends="init-jbi-admin">
<!-- default service assembly name value. null or empty for no service assembly dependency in query tasks-->
<property name="jbi.service.assembly.name" value="" />
</target>


<target name="undeploy-service-assembly" depends="init-jbi-admin, -init-undeploy-sa" description="Undeploys service assembly">
<jbi-undeploy-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" force="${jbi.force.task}" keepArchive="${jbi.keep.archive}" name="${jbi.service.assembly.name}" />
</target>


<target name="-init-sa-lifecycle-options" depends="init-jbi-admin">
<!-- default service assembly name value. null or empty for no service assembly dependency in query tasks-->
<property name="jbi.service.assembly.name" value="" />
</target>


<!-- start service assembly target -->
<target name="start-service-assembly" depends="init-jbi-admin, -init-sa-lifecycle-options" description="starts service assembly">
<jbi-start-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" name="${jbi.service.assembly.name}" />
</target>


<!-- stop service assembly target -->
<target name="stop-service-assembly" depends="init-jbi-admin, -init-sa-lifecycle-options" description="stops service assembly">
<jbi-stop-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" name="${jbi.service.assembly.name}" />
</target>


<!-- shutdown service assembly target -->
<target name="shut-down-service-assembly" depends="init-jbi-admin, -init-sa-lifecycle-options" description="shuts down service assembly">
<jbi-shut-down-service-assembly username="${jbi.username}" password="${jbi.password}" secure="${jbi.secure}" host="${jbi.host}" port="${jbi.port}" target="${jbi.target}" 
failOnError="${jbi.task.fail.on.error}" force="${jbi.force.task}" name="${jbi.service.assembly.name}" />
</target>


</project>

and the actual build.xml is as follows:

<?xml version="1.0"?>
<project name="myproject" default="zip" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">


<import file="./jbi_admin.xml" />
        <!-- some stuff deleted.. -->
<!-- ==================================== -->
<!-- Glassfish tasks                      -->
<!-- ==================================== -->
<target name="glassfish-init" unless="glassfish.initialized">
<path id="jbi.ant.tasks.classpath.id">
<pathelement location="${glassfish.home}/lib/sun-appserv-ant.jar" />
<pathelement location="${glassfish.home}/jbi/lib/jbi-ant-tasks.jar" />
</path>
<!-- Load jbi task definitions -->
<taskdef resource="com/sun/jbi/ui/ant/antlib.xml" classpathref="jbi.ant.tasks.classpath.id" />
<taskdef name="unset" classname="ise.antelope.tasks.Unset">
<classpath path="${build.lib}/antelopetasks-3.2.10.jar" />
</taskdef>
<!-- ensure that glassfish taskdef is only run once, otw ant will error -->
<property name="glassfish.initialized" value="true" />
</target>


<target name="glassfish-password-init" depends="init, glassfish-init">
<!-- do not forget to delete this! -->
<echo message="AS_ADMIN_PASSWORD=adminadmin" file="${build.dir}/password.file" />
</target>


<target name="glassfish-prod-deploy-pre" depends="glassfish-init">
<!--<property name="jbi.service.assembly.name" value="" />-->
<unset name="jbi.service.assembly.name"/>
<property name="jbi.task.fail.on.error" value="true" />
<property name="jbi.host" value="${prod.host}" />
<property name="jbi.deploy.file" value="${build.dir}/${name}-${SURUM}.zip" />
</target>


<target name="glassfish-start-assembly-pre" depends="glassfish-init">
<property name="jbi.task.fail.on.error" value="true" />
<property name="jbi.service.assembly.name" value="${name}-${SURUM}" />
<property name="jbi.host" value="${prod.host}" />
<unset name="jbi.deploy.file"/>
<!--<property name="jbi.deploy.file" value="" />-->
</target>


<target name="glassfish-prod-undeploy-pre" depends="glassfish-init">
<property name="jbi.task.fail.on.error" value="false" />
<property name="jbi.service.assembly.name" value="${name}-${SURUM}" />
<property name="jbi.host" value="${prod.host}" />
<unset name="jbi.deploy.file"/>
<!--<property name="jbi.deploy.file" value="" />-->
</target>


<target name="exec-glassfish-prod-deploy" depends="zip, glassfish-prod-deploy-pre, deploy-service-assembly">
</target>


<target name="exec-glassfish-start-assembly" depends="glassfish-start-assembly-pre, start-service-assembly">
</target>


<target name="glassfish-prod-deploy" depends="exec-glassfish-prod-deploy, exec-glassfish-start-assembly">
</target>


<target name="glassfish-prod-undeploy" depends="glassfish-prod-undeploy-pre, shut-down-component, undeploy-service-assembly">
</target>


</project>


glassfish-prod-deploy target deploys the specified assembly to the application server and starts it.
Likewise, glassfish-prod-undeploy target shutdowns the assembly and undeploys it.

19 Ağustos 2011 Cuma

ant deploy to glassfish

In order to deploy the web service war to glassfish using ant, you can use the following ant snippet:



<target name="glassfish-init" unless="glassfish.initialized">
<taskdef name="sun-appserv-deploy" classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.DeployTask">
<classpath path="${glassfish.home}/lib/sun-appserv-ant.jar" />
</taskdef>
<taskdef name="sun-appserv-undeploy" classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.UndeployTask">
<classpath path="${glassfish.home}/lib/sun-appserv-ant.jar" />
</taskdef>
<!-- ensure that glassfish taskdef is only run once, otw ant will error -->
<property name="glassfish.initialized" value="true" />
</target>


<target name="glassfish-password-init" depends="init, glassfish-init">
<!-- do not forget to delete this! -->
<echo message="AS_ADMIN_PASSWORD=adminadmin" file="${build.dir}/password.file" />
</target>


<target name="glassfish-remote-deploy" depends="glassfish-init, glassfish-password-init, war">
<sun-appserv-deploy file="${build.dir}/${name}-${version}.war" name="${name}-${version}" 
user="admin" passwordfile="${build.dir}/password.file" host="remotemachine" port="4848"
asinstalldir="${glassfish.home}"/>
</target>


<target name="glassfish-remote-undeploy" depends="glassfish-init, glassfish-password-init">
<sun-appserv-undeploy name="${name}-${version}" 
user="admin" passwordfile="${build.dir}/password.file" host="remotemachine" port="4848" 
asinstalldir="${glassfish.home}"/>
</target>



The point is to use sun-appserv-ant.jar file, which includes glassfish specific ant task definitions, inside glassfish v2.1 home directory. Otherwise, there will be ClassCouldNotBeFound exceptions..


18 Ağustos 2011 Perşembe

hibernate on glassfish

A way of  powering glassfish with hibernate support (instead of toplink) is directly copying hibernate runtime.jar files under glassfish's domain1/lib folder. However, this forces all deployed applications to use the same hibernate version. Therefore, the best solution is packaging hibernate .jars with the application itself.

Here is a useful link which explains how to use hibernate JPA on glassfish: http://javafromthetrenches.wordpress.com/2011/01/15/using-hibernate-jpa-with-glassfish/

The following dependencies worked for me:

<dependency org="antlr"
name="antlr"
rev="2.7.6"/>

<dependency org="asm"
name="asm"
rev="1.5.3"/>

<dependency org="asm"
name="asm-attrs"
rev="1.5.3"/>

<dependency org="cglib"
name="cglib"
rev="2.2"/>

<dependency org="commons-collections"
name="commons-collections"
rev="2.1.1"/>

<dependency org="commons-logging"
name="commons-logging"
rev="1.1"/>

<dependency org="dom4j"
name="dom4j"
rev="1.6.1"/>

<dependency org="ehcache"
name="ehcache"
rev="1.2.3"/>

<dependency org="javax.transaction"
name="jta"
rev="1.1"/>

<dependency org="org.hibernate"
name="hibernate-tools"
rev="3.2.0.ga"/>

<dependency org="org.hibernate"
name="hibernate-annotations"
rev="3.3.1.GA"/>

<dependency org="org.hibernate"
name="hibernate-commons-annotations"
rev="3.3.0.ga"/>

<dependency org="org.hibernate"
name="hibernate-entitymanager"
rev="3.3.2.GA"/>

<dependency org="javassist"
name="javassist"
rev="3.4.GA"/>

<dependency org="org.hibernate"
name="ejb3-persistence"
rev="3.3.2.Beta1"/>

<-- if hibernate cloning api beanlib is going to be used -->
<dependency org="net.sf.beanlib"
name="beanlib"
rev="5.0.2beta"/>

<-- if hibernate cloning api beanlib is going to be used -->
<dependency org="net.sf.beanlib"
name="beanlib-hibernate"
rev="5.0.2beta"/>

<!-- if postgresql driver is going to be used -->
<dependency org="it.cnr.isti.domoware"
name="org.postgresql.jdbc4"
rev="8.3-603"/>


The listed dependencies are not latest version of hibernate, however it is a working hibernate runtime set of jars. Here is an ongoing discussion about the topic: http://www.java.net/forum/topic/glassfish/glassfish/updating-support-hibernate-glassfish-need-communitys-help

14 Ağustos 2011 Pazar

Latex - referencing a web site

While writing .tex document, the following is a valid web site reference.


@ONLINE{w1_MPEG7,
author = {W3C Incubator Group},
title = {MPEG-7 and the Semantic Web},
month = aug,
year = {2011},
url = {http://www.w3.org/2005/Incubator/mmsem/XGR-mpeg7/}
}


and latex output is:

MPEG-7 [1] is one of ..
...
[1] W3C Incubator Group. Mpeg-7 and the semantic web, August 2011.

9 Ağustos 2011 Salı

reading from properties file

a simple code snippet for reading from properties file:


import java.util.ResourceBundle;

Locale locale = new Locale("en_US");
ResourceBundle bundle = ResourceBundle.getBundle("mypropertiesfile", locale);
String name = bundle.getString("name");
String surname = bundle.getString("surname");

.. and the corresponding mypropertiesfile.properties should be in the classpath:


name = buffy
surname = vampireSlayer



3 Ağustos 2011 Çarşamba

java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/String;

After successfully deploying web services to glassfish v2.1 server, the following error comes up while running test client code:
java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/String;
at com.sun.xml.ws.model.RuntimeModeler.processExceptions(RuntimeModeler.java:1162)
at com.sun.xml.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:898)
at com.sun.xml.ws.model.RuntimeModeler.processMethod(RuntimeModeler.java:666)
at com.sun.xml.ws.model.RuntimeModeler.processClass(RuntimeModeler.java:420)
at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:254)
at com.sun.xml.ws.client.WSServiceDelegate.createSEIPortInfo(WSServiceDelegate.java:661)
at com.sun.xml.ws.client.WSServiceDelegate.addSEI(WSServiceDelegate.java:649)
at com.sun.xml.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:343)
at com.sun.xml.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:326)
at com.sun.xml.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:308)
at javax.xml.ws.Service.getPort(Service.java:92)


According to this, current JAVA_HOME is pointing to Java 6 SE, which now has a version of WebFault WITHOUT the messageName element ..


In order to fix this situation, endorse mechanism should be applied, similar to this http://weblogs.java.net/blog/ramapulavarthi/archive/2009/04/tip_for_using_j.html

my solution is to add the following to junit ant task:

<jvmarg value="-Djava.endorsed.dirs=${lib.endorsed.dir}"/>


// endorsed folder consists of webservices-api.jar..

2 Ağustos 2011 Salı

running ant JUnitTask

message from ant:


BUILD FAILED
.../build.xml:346: Problem: failed to create task or type junit
Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
        This looks like one of Ant's optional components.
....



this is due to lack of  ant-junit.jar file.

one method is to download ant-junit.jar from here: http://www.java2s.com/Code/Jar/ABC/Downloadantjunitjar.htm
and copy it under $ANT_HOME/lib

another method is to download a newer version of ant (http://ant.apache.org/)
ant 1.8.2 comes with that jar file..

http://ant.apache.org/manual/Tasks/junit.html explains how to run junit task..