Thursday, May 22, 2014

Start/stop Tomcat from ANT

<project name="Stop/start Tomcat with local deploy">

<property name="tomcat.home" value="D:/apache-tomcat-7.0.47" />
<property name="project.name" value="samara-provisioning" />
<property name="dist.dir" value="dist/artefacts" />

<!-- deploy-local: delete and copy war -->
<target name="deploy-local" depends="tomcat-stop">

<delete>
<fileset dir="${tomcat.home}/webapps">
<include name="${project.name}.war" />
</fileset>
</delete>
<delete dir="${tomcat.home}/webapps/${project.name}" />

<copy todir="${tomcat.home}/webapps">
<fileset dir="${dist.dir}">
<include name="**/${project.name}.war" />
</fileset>
</copy>

<tstamp>
<format property="TODAY" pattern="HH:mm:ss:sss zzz"
locale="en" />
</tstamp>
<echo> Build Completed At: ${TODAY}</echo>

</target>

<!-- start tomcat, it depends on deploy-local -->
<target name="tomcat-start" depends="deploy-local">
<java classname="org.apache.catalina.startup.Bootstrap"
failonerror="true" fork="true">
<classpath
path="${tomcat.home}/bin/bootstrap.jar:${tomcat.home}/bin/tomcat-juli.jar" />
<jvmarg
value="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" />
<jvmarg
value="-Djava.util.logging.config.file=${tomcat.home}/conf/logging.properties" />
<jvmarg value="-Dcatalina.home=${tomcat.home}" />
<jvmarg value="-Dcatalina.base=${tomcat.home}" />
<jvmarg value="-Djava.io.tmpdir=${tomcat.home}/temp" />
<arg line="start" />
</java>
</target>

<!--stopping tomcat , if it is found running -->
<target name="tomcat-stop" if="tomcat.running" depends="check-port">
<echo message="Tomcat is running...stopping it" />
<java classname="org.apache.catalina.startup.Bootstrap" fork="true">
<classpath
path="${tomcat.home}/bin/bootstrap.jar:${tomcat.home}/bin/tomcat-juli.jar" />
<jvmarg value="-Dcatalina.home=${tomcat.home}" />
<arg line="stop" />
</java>
</target>

<!-- Check whether Tomcat is running -->

<target name="check-port" description="Check whether Tomcat is running">
<echo message="Checking whether Tomcat is running" />
<condition property="tomcat.running">
<socket server="localhost" port="8080" />
</condition>
</target>

</project>


Ref:
http://techfindouts.blogspot.com/2013/08/how-to-restart-stopstart-tomcat-using.html
http://linuxclicks.blogspot.com/2011/04/java-ant-target-to-startstop-apache.html

Wednesday, May 7, 2014

Hibernate/persistence annotations

Some useful annotations when using
hibernate.hbm2ddl.auto to update | create | create-drop
If hbm2ddl.auto is set to update it will not change type of field!
     postgres:  ALTER TABLE users ALTER COLUMN role TYPE varchar(16);

javax.persistence.Lob with/without org.hibernate.annotations.Type

Using postgres and omitting Type for text lobs will cause numbers in database tables. 
Like operator will not work!
Solution:
@Lob
@Type(type = "org.hibernate.type.TextType")

If there was version without @Type and you want to update/migrate, please share your expirience.
(If you add @Type, old records will be read as plain strings.)
// This was tested with hibernate 3.6.10. and 5.1.0

org.hibernate.annotations.ForeignKey

Very useful for not having generated names as foreign keys.

In JPA 2.1 you can use javax.persistence.ForeignKey
@JoinColumn(foreignKey = @ForeignKey(name = "FK_ORDER_CUSTOMER"))

javax.persistence.Enumerated(javax.persistence.EnumType.STRING)

When using enum, store it in db as string, not a number

javax.persistence.Id with javax.persistence.Column

This combination will cause a lot of problems with hsqldb (column annotation is same like Id constraint). Postgres will not complain.
@Id
@Column(name = "ID", nullable = false, unique = true)

Solution:
remove nullable and unique


Tested on hsqldb 2.3.2 (as in memory) and postgres 9.2/9.3

Ref: