Saturday, March 17, 2012

Ant target to start axis2 server

Here i will describe how to write ant target to start apache axis2 server directly from your "build.xml" file without calling any shell script file. For this purpose we use "org.apache.axis2.transport.SimpleAxis2Server" class and pass it few arguments to tell it about our configuration files and service folder path.

First let us add locations to class path.
(here the ${axis-homerefers to the root of your apache axis2 directory.)



<path id="axis-classpath">
 <pathelement location="${axis-home}"/>
 <pathelement location="${axis-home}/conf"/>
 <pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
 <fileset dir="${axis-home}/lib">
  <include name="*.jar"/>
 </fileset>
</path>

with adding those paths to the class path let us write ant target to start axis2 server like below.


<target name="start-axis">
 <java classname="org.apache.axis2.transport.SimpleAxis2Server" fork="true">
  <classpath refid="axis-classpath" /> 
  <jvmarg value="-Djava.endorsed.dirs=${axis-home}/lib/endorsed;${env.JAVA_HOME}/jre/lib/endorsed;${env.JAVA_HOME}/lib/endorsed" />
  <arg line="-repo ${axis-home}/repository"/>
  <arg line="-conf ${axis-home}/conf/axis2.xml"/>
 </java>
</target>


Here we pass a jvm argument stating all the endorsed folder paths and two runtime arguments which are the location for service repository and path to axis configuration file.

Friday, March 16, 2012

Ant target for Apache Tomcat

This tutorial will explain how to write an ant target to start and stop tomcat. For this task we use a special class located in "tomcat/bin/bootstrap.jar" namely "org.apache.catalina.startup.Bootstrap". Also this  jar will be depend on another jar which is located at the same location "tomcat/bin/tomcat-juli.jar".

First add these locations to a class path like below.
(here the ${tomcat} refers to the root of your tomcat directory.)


<path id="tomcat-classpath">
 <fileset dir="${tomcat}/lib">
  <include name="*.jar" />
 </fileset>
 <fileset dir="${tomcat}/bin">
  <include name="tomcat-juli.jar" />
  <include name="bootstrap.jar" />
 </fileset>
</path>


Then we write start-tomcat target to start the apache tomcat.


<target name="start-tomcat">
 <java classname="org.apache.catalina.startup.Bootstrap" fork="true">
  <classpath refid="tomcat-classpath" />
  <jvmarg value="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" />
  <jvmarg value="-Djava.util.logging.config.file=${tomcat}/conf/logging.properties" />
  <jvmarg value="-Dcatalina.home=${tomcat}" />
  <jvmarg value="-Dcatalina.base=${tomcat}" />
  <jvmarg value="-Djava.io.tmpdir=${tomcat}/temp" />
  <arg line="start"/>
 </java>
</target>


And then we write the stop-tomcat target to stop the running tomcat instance.


<target name="stop-tomcat">
 <java classname="org.apache.catalina.startup.Bootstrap" fork="true">
  <classpath refid="tomcat-classpath" />
  <jvmarg value="-Dcatalina.home=${tomcat}" />
  <arg line="stop" />
 </java>
</target>


Ok! we are done!!!

Wednesday, March 14, 2012

Configuring mysql server for ruby


Even though sqlite 3 officially ported with rails, in mass scale application production environments, the typical candidate will be MySQL due to several reasons like its open source, established an a reliability.

 
  1. If you have already installed MySQl server skip to step 2 directly other wise download it from here and run the UI installer and it will guide u through the steps. ( You need to made couple of requirements inorder to run this msi package sucessfully one is .NET 4 framework and second Microsoft C++ runtime 2010 32 bit package.)
  2. After that go to this location and download the zip file which contain MySQL c driver.
  3. UNZIP the file.
  4. Copy the dll file in "mysql-connector-c-noinstall-6.0.2-win32-vs2005\lib" location to " <your-ruby&rails-installation-directory>\ruby <version>\bin " directory.

Sunday, March 11, 2012

Install Ruby on Rails to Windows 7




Recently i have found that i like to learn new programming language and choose ruby to full fill my desire. But all the tutorials i have found how to install rails and configure gems are outdated and it took me 3 days to create a project correctly without giving any errors about missing gems or dev kit.

So that inspired me to write a guide on how to install rails correctly. ( i further wish to write more on ruby later)

So let us start the work then.

  • First go to this website and download the latest release of  "rails installer for windows" ( at the moment i m writing this blog the newest release was 2.1.0). This installer comes with current ruby language interpreter, rails framework and dev kit, all 3 in one.

  • After downloading it double click on the exe file and follow the steps to install the setup. ( i prefer a location in top level of C drive (eg: C:\ruby) because of spaces in the path will be problematic sometimes). 

  • After finishing the installer go to start menu and find "All Programs > RailsInstaller > Git Bash " and click it. This will open a command prompt like terminal (if you have previously work with git you may find out this is the same  terminal that git has provide). Or you can use windows command prompt as well.



  • Then type "ruby -v" to check the ruby version and "gem -v" to check the gem version.


  • And then type "rails -v" where the prompt say that it is unknown command and this is because even though we installed rails it has not been configured yet as a gem. 

  • Type " gem install rails --include-dependencies " to install rails gem and you are done. ( of course this may take few time )
Installing Rails
  • Re check by typing "rails -v".



  • Create a new folder in any where you like to create ruby projects. Open a command window and "cd" to that folder. Then type "rails new testApp". ( here testApp is my test applications name you can give any name and this will be your project name". After issuing this command rails will create a ruby project for you with all the dependencies, configurations and scripts.


  • Type "rails server" to start ruby server on your localhost. Then go to your web browser and type "http://localhost:3000" . You will get a page like below. (ruby server use port 3000)

You’re riding Ruby on Rails!













Saturday, March 10, 2012

Reverse engineering - Generating hbm.xml from annotated POJO

I have look for these particular scenario every where but couldnot found anything usefull other than the hibernate tools documentation. Reason the people not doing this is because hibernate - tools are mainly using for reverse engineering where this is not coming across. Only scenario i can think of where you need to generate a hibernate mapping xml from a POJO which is already annotated ( annotations are the successor of hibernate mapping xmls) is when you are in middle of the migration process from hibernate mapping xmls to annotations.

People already developing a product are not likely to suddenly adapting the changes. So they do it, step by step when it is needed so in such a case first you write annotation base POJOs but really not using them instead u generate the hbm.xml from the annotations, is the best way to work around. And also writing POJO separately and mapping separately increase the possibility of  erroneous of the code. Instead generating one automatically when the other given is much applicable which in case reduce   the time of the development and make the process more efficient.

enough talking now i will explain the process
only to generate the hbm mapping i only needed hibernate-tool-xx.jar and fremarker.jar but to run a complete hibernate base code using the generated hbm i need the following dependencies so before you start better to include all of them in to your lib directory

below jars are coming with hibernate distribution
antlr-2.7.6.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar                                                              
hibernate3.jar
slf4j-api-1.6.1.jar
jta-1.1.jar
javassist-3.12.0.GA.jar

and you have to download these separately
commons-collections-3.2.1.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jtidy-r8-21122004.jar
mysql-connector-java-5.0.4-bin.jar (if you use mysql as back end database)
slf4j-simple-1.6.1.jar
freemarker.jar
hibernate-tools-3.2.4.GA.jar

that is all you need. And lets begin coding you have to write an ant build script with following target and taskdef which need to create hbm.xml mapping for your pojo

 <taskdef name="hibernatetool"   
                classname="org.hibernate.tool.ant.HibernateToolTask"   
                classpathref="lib" />  
      <taskdef name="annotationconfiguration"   
                classname="org.hibernate.tool.ant.AnnotationConfigurationTask"   
                classpathref="lib" />  
      <target name="init" depends="copy-resources">  
           <hibernatetool destdir="${builddir}">  
                <classpath>  
                     <path location="${srcdir}" />  
                     <path location="${libdir}" />  
                     <path location="${builddir}" />  
                </classpath>  
                <annotationconfiguration configurationfile="${srcdir}/hibernate.cfg.xml" />  
                <hbm2hbmxml />  
           </hibernatetool>  
      </target>  

Put this ant file in to your project and write a POJO with annotations as they used to write in. (Everything is normal than the crazy work - trying to generate hbm.xml from annotations.)

And here is the important part. You have to include your pojo's full class path into your hibernate.cfg.xml (hibernate configuration file) as a mapping.

Below is my example hibernate.cfg.xml ,

 <?xml version='1.0' encoding='utf-8'?>  
 <!DOCTYPE hibernate-configuration PUBLIC  
 "-//Hibernate/Hibernate Configuration DTD//EN"  
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
 <hibernate-configuration>  
 <session-factory>  
  <property name="hibernate.connection.driver_class">  
 com.mysql.jdbc.Driver</property>  
  <property name="hibernate.connection.url">  
 jdbc:mysql://localhost:3306/testhib</property>  
  <property name="hibernate.connection.username">root</property>  
  <property name="hibernate.connection.password">1234</property>  
  <property name="hibernate.connection.pool_size">1</property>  
  <property name="show_sql">true</property>  
  <!-- <property name="hibernate.bytecode.provider">javassist</property> -->  
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  <property name="hibernate.hbm2ddl.auto">update</property>  
  <!-- Mapping files -->  
  <mapping class="com.example.User"/>  
  <mapping class="com.example.Task"/>  
  </session-factory>  
 </hibernate-configuration>  

here my POJOs which are  "com.example.User"  and "com.example.Task"  list under the mapping where normally hbm.xml goes. What happen here is the when the ant target "init" is called it tell hibernate tool task to read the hibernate configuration file and find the mapping class files then it goes to class path folders defined under hibernatetool task, travel the folder structure defined in package name and find the .class file, read the annotations in the .class file and generate the mappings in your directory where you defined under "destdir".

Editing XML using apache ant target - dtd validation

To acomplish this specific task and automate the xml injections there is a tool called XmlTask which can be download here.

For more information about how to write a ant target or use xmltask you can refer this guide.

This article is not about how to use xmltask actually it is a situation i have to face whilie using this. My machine connects to a proxy server when it connect to internet or other network and when inserting or updating ant task "xmltask" use the dtd declared in <!DOCTYPE >. Actually it tries to download it if it is not in a local machine. So with the proxy i got java.net.ConnectException .


 [xmltask] It looks like you've got a network error. The probable cause  
  [xmltask] is that you're trying to resolve a DTD on the internet although  
  [xmltask] you don't know it! Check your XML for DTDs external to your network  
  [xmltask] and read the Ant documentation for <xmlcatalog>. XMLTask will support  
  [xmltask] usage of <xmlcatalog>. See the following:  
  [xmltask] http://ant.apache.org/manual/CoreTypes/xmlcatalog.html  
  [xmltask] http://www.oopsconsultancy.com/software/xmltask  
  [xmltask] If this isn't the problem, then please report this error to the support  
  [xmltask] mailing list. Thanks!  
  [xmltask] Connection refused  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromStream(XmlTask.java:372)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromFile(XmlTask.java:400)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.access$000(XmlTask.java:27)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask$InputFile.getDocument(XmlTask.java:216)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.execute(XmlTask.java:651)  
  [xmltask]   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)  
  [xmltask]   at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)  
  [xmltask]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  [xmltask]   at java.lang.reflect.Method.invoke(Method.java:597)  
  [xmltask]   at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)  
  [xmltask]   at org.apache.tools.ant.Task.perform(Task.java:348)  
  [xmltask]   at org.apache.tools.ant.Target.execute(Target.java:357)  
  [xmltask]   at org.apache.tools.ant.Target.performTasks(Target.java:385)  
  [xmltask]   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)  
  [xmltask]   at org.apache.tools.ant.Project.executeTarget(Project.java:1306)  
  [xmltask]   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)  
  [xmltask]   at org.apache.tools.ant.Project.executeTargets(Project.java:1189)  
  [xmltask]   at org.apache.tools.ant.Main.runBuild(Main.java:758)  
  [xmltask]   at org.apache.tools.ant.Main.startAnt(Main.java:217)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)  
  [xmltask] Caused by: java.net.ConnectException: Connection refused  
  [xmltask]   at java.net.PlainSocketImpl.socketConnect(Native Method)  
  [xmltask]   at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)  
  [xmltask]   at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)  
  [xmltask]   at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)  
  [xmltask]   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)  
  [xmltask]   at java.net.Socket.connect(Socket.java:529)  
  [xmltask]   at java.net.Socket.connect(Socket.java:478)  
  [xmltask]   at sun.net.NetworkClient.doConnect(NetworkClient.java:163)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)  
  [xmltask]   at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:306)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:323)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromStream(XmlTask.java:356)  
  [xmltask]   ... 20 more  
  [xmltask] --- Nested Exception ---  
  [xmltask] java.net.ConnectException: Connection refused  
  [xmltask]   at java.net.PlainSocketImpl.socketConnect(Native Method)  
  [xmltask]   at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)  
  [xmltask]   at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)  
  [xmltask]   at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)  
  [xmltask]   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)  
  [xmltask]   at java.net.Socket.connect(Socket.java:529)  
  [xmltask]   at java.net.Socket.connect(Socket.java:478)  
  [xmltask]   at sun.net.NetworkClient.doConnect(NetworkClient.java:163)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)  
  [xmltask]   at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:306)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:323)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromStream(XmlTask.java:356)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromFile(XmlTask.java:400)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.access$000(XmlTask.java:27)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask$InputFile.getDocument(XmlTask.java:216)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.execute(XmlTask.java:651)  
  [xmltask]   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)  
  [xmltask]   at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)  
  [xmltask]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  [xmltask]   at java.lang.reflect.Method.invoke(Method.java:597)  
  [xmltask]   at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)  
  [xmltask]   at org.apache.tools.ant.Task.perform(Task.java:348)  
  [xmltask]   at org.apache.tools.ant.Target.execute(Target.java:357)  
  [xmltask]   at org.apache.tools.ant.Target.performTasks(Target.java:385)  
  [xmltask]   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)  
  [xmltask]   at org.apache.tools.ant.Project.executeTarget(Project.java:1306)  
  [xmltask]   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)  
  [xmltask]   at org.apache.tools.ant.Project.executeTargets(Project.java:1189)  
  [xmltask]   at org.apache.tools.ant.Main.runBuild(Main.java:758)  
  [xmltask]   at org.apache.tools.ant.Main.startAnt(Main.java:217)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)  

what i did is as the tutorial dtd section suggested i have define <xmlcatalog> tag and map the local dtd source file to the public id like below example.


 <xmlcatalog id="dtd">  
   <dtd   
    publicId="-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"   
    location="./servlet-2.3.dtd"/>   
 </xmlcatalog>  


and then it gives another error when the edited xml file is parsing by my another program. The error is due to <!DOCTYPE> declaration is missing.
What happen is when we use xmlcatalog to define the local path , and when we insert a xml code to the document and write it back to a file , it elemenates the <!DOCTYPE > declaration in the newly writing file. If we overide the exisiting file we loose it from the only file we have. So to overcome this problem what i did was insert public id and system uril inside the <xmltask> tag itself like below.

 <xmltask source="etc/hibernate.cfg.xml" dest="etc/hibernate.cfg.xml" public="-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
          system="http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
                <xmlcatalog id="dtd">  

tomcat 7 taglib definition not consist issue

I have encountered this when i tried to port a java war file which is previously deployed in tomcat 6.0.13 to newest tomcat version tomcat 7.0.14

when the web container trying to deploy the .war file it throws an exception saying that

jndi:/localhost/application/WEB-INF/web.xml
java.lang.IllegalArgumentException: taglib definition not consistent with application version

and i when i googled for this problem i found only one mail thread regarding this tomcat 7 issue 

and i tried with all the options described their and finally found the solution that is write tags inside of which resolve the problem


  
  <jsp-config>
      <taglib>
          <taglib-uri>
              http://metawerx.net/mapp/taglibs
          </taglib-uri>
          <taglib-location>
              /WEB-INF/mapp.tld
          </taglib-location>
     </taglib>
  </jsp-config>


for more details about refer this site