Saturday, February 19, 2011

[Java] JAXB to create Java Stubs from XML schema

One of the ways to create client stubs in Java is through XML schema. Once we have server side artifacts, we can generate fully annotated Java classes from an XML schema file by using the JAXB schema compiler, i.e. xjc command-line tool. The resulting JAXB objects/Java Stubs can be used in Java applications to manipulate the XML request and response data.

Steps to follow:

1. Copy the XML schema to a file <schema-fileName>.xsd in <source-directory>. Or create XSD schema for web service with .xsd.

2. From <source-directory>, use following command to create java stubs from that xsd

xjc <source-directory>\<schema-fileName>.xsd -p <destination-package> -d <destination-directory>

3. Use the generated JAXB objects within your Java application to manipulate XML content through the generated JAXB classes.

4. Use following command to compile and generate .class files

javac -d <directory-for-classFiles> <destination-directory>\*.java

5. Now to have .jar file for these generated stubs, we can use following command

cd <directory-for-classFiles>
jar cvf <jar-filename>.jar

6. Copy .jar file in lib folder of your application for further access.

Wednesday, February 16, 2011

[Linux - Ubuntu] Creating Shell Script

Below is an easy and simple way to create a shell script for Ubuntu (Linux). 

1. Example is to stop Tomcat, copy some application files and start Tomcat. Write following sample Linux commands in a file and save it as <shell-script>.sh

#!/bin/sh
echo Deploying JAVA Application

echo Stopping Tomcat Service...
cd /usr/local/tomcat/bin
./catalina.sh stop

echo
echo

echo Copying Application Files...
cd <source-directory>
cp -R * <destination-directory>
rm <some-file-name>.<extension>
mv <source-file-name>.<extension>  <destination-file-name>.<extension>

echo
echo

echo Deployment Compelted. Starting Tomcat Service...
cd /usr/local/tomcat/bin
./catalina.sh start

echo
echo

2. Grant permissions to this file:
chmod 777 <shell-script>.sh

3.  Run this shell script as:
./<shell-script>.sh

4. Now, use following command to get the Catalina Logs to assure everything is working fine:
tail -f /usr/local/tomcat/logs/catalina.out

... and that's it.