Thursday, September 18, 2014

Tomcat 7 JNDI DataSource with Groovy Grails


Tomcat 7 JNDI DataSource with Groovy Grails


Intro

This article describes how to configure JNDI DataSource to integrate Tomcat 7 with application built in Groovy on Grails framework.

Environment setup

I suppose that you have properly set following environment variables:
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
export CATALINA_HOME=/opt/apache-tomcat-7.0.55

Open shell for Tomcat user

sudo su tomcat7
cp $ORACLE_HOME/jdbc/lib/ojdbc6.jar $CATALINA_HOME/lib
# in my case:
# cp /u01/app/oracle/product/11.2.0/xe/jdbc/lib/ojdbc6.jar /opt/apache-tomcat-7.0.55/lib

Edit server.xml

Open file $CATALINA_HOME/conf/server.xml

Find appropriate Host section
Add following data source definition:

<Context docBase="YourApplication-0.1" path="/YourApplication-0.1" reloadable="true">
 <Resource name="jdbc/yourApplication" auth="Container"
  type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
  url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
  username="DatabaseUser" password="DatabasePassword" maxActive="20" maxIdle="10"
  maxWait="-1"/>
</Context>

Restart tomcat7

sudo /etc/init.d/tomcat7 restart

Open your DataSource.groovy

Add or modify dataSource section:

environments {
 demo {
  dataSource {
   dialect = "org.hibernate.dialect.Oracle10gDialect"
   dbCreate = "none"
   pooled = false
   jndiName = "java:comp/env/jdbc/yourApplication"
  }
 }
}

Note

Do not forget prefix java:comp/env/ in your jndiName in Grails, otherwise it will not match your Tomcat settings and may result to following (not much explaining) error:
Failed to retrieve JNDI naming context for container [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/YourApplication-0.1]]  so no cleanup was performed for that container

Note2

You can change schema by setting:
hibernate.default_schema = 'schemaName'
(Note that I have not yet tested this config parameter)

Rebuild your application

cd YourApplication
grails -Dgrails.env=demo war

Deploy your application

cd target
cp -f YourApplication-0.1.war $CATALINA_HOME/webapps

Done

Tuesday, September 09, 2014

Tomcat 7 Linux CentOS installation


Tomcat 7 Linux CentOS installation


# get app
Get Tomcat7
wget http://www.eu.apache.org/dist/tomcat/tomcat-7/v7.0.55/bin/apache-tomcat-7.0.55.tar.gz

# check md5
md5sum apache-tomcat-7.0.55.tar.gz
xmd5sum=3c46fc0f608c1280dcd65100a983f285
echo -n Checking md5 ...' '
if [ "`md5sum apache-tomcat-7.0.55.tar.gz | awk '{print $1}'`" != "$xmd5sum" ] ; then echo INCORRECT md5! Exiting. ; exit 1 ; else echo md5 OK ; fi

# add user
echo Adding users
groupadd tomcat7
useradd -s /bin/bash -g tomcat7 tomcat7

# unpack
echo Unpacking Tomcat
tar -zxf apache-tomcat-7.0.55.tar.gz
mv apache-tomcat-7.0.55 /opt/
ln -s apache-tomcat-7.0.55 /opt/tomcat7
chown -Rf tomcat7.tomcat7 /opt/apache-tomcat-7.0.55



# create init script
echo Creating init script

cat > /etc/init.d/tomcat7 <<EOF
#!/bin/bash
#
# Tomcat 7
#
# chkconfig: 234 80 20
# description:  Start up the Tomcat servlet engine.
# Source function library.
. /etc/init.d/functions
RETVAL=$?
CATALINA_HOME="/opt/tomcat7"
# export JAVA_HOME=/usr/java/latest
case "$1" in
 start)
  if [ -f $CATALINA_HOME/bin/startup.sh ];
   then
    echo $"Starting Tomcat"
    /bin/su -s /bin/bash tomcat7 $CATALINA_HOME/bin/startup.sh
  fi
  ;;
 stop)
  if [ -f $CATALINA_HOME/bin/shutdown.sh ];
   then
    echo $"Stopping Tomcat"
     /bin/su -s /bin/bash tomcat7 $CATALINA_HOME/bin/shutdown.sh
  fi
  ;;
 *)
  echo $"Usage: $0 {start|stop}"
  exit 1
  ;;
esac
exit $RETVAL
EOF
# end of init script



# register init script
chmod a+x /etc/init.d/tomcat7
chmod 755 /etc/init.d/tomcat7

# register init script
echo Registering Tomcat7 to run on system startup
chkconfig --level 234 jboss off
chkconfig --add tomcat7
chkconfig --level 234 tomcat7 on

# Final message
echo
echo Installation DONE
echo
echo To start Tomcat7 service, type:
echo service tomcat7 start
echo
echo To stop Tomcat7 service, type:
echo service tomcat7 stop
echo

# make tomcat users
# source http://tecadmin.net/steps-to-install-tomcat-server-on-centos-rhel/
cat > /opt/tomcat7/conf/tomcat-users-example.xml <<EOF
<!-- user manager can access only manager section -->
<role rolename="manager-gui" />
<user username="manager" password="manager_password" roles="manager-gui" />
<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="admin_password" roles="manager-gui,admin-gui" />
EOF