Tuesday, 18 August 2015

TDD Kickstart with Spring 4.x + Maven 3.x + MongoDB 3.x - PART 1

I guess my TDD skills has slowly melted down as I was not actively programming more than six f*king months! I need a kickstart to refresh my laggy old brain and naggy Macbook Pro Yosemite OSX (upgrades needed).

What do you need?

  • Maven 3.x
  • MongoDB 3.x
  • Java JDK 1.7.x
  • Eclipse IDE Java EE Developer (mine is Luna with m2e plugin installed)
OK assuming you've all of these installed and working, let gets dirty (I mean hands).

What are the steps?

  • Create a mongodb collection & document
  • Create a java web project And Import to Eclipse IDE
  • Add project dependencies: Spring core, Spring test, Spring data mongo etc...
  • Write you TDD code. "Red -> Green -> Refactor" cycle.

A. Create a mongodb collection & document

  1. Start your mongod: mongodb -dbpath <choose your db path i.e.: /usr/local/var/mongodb/data> &
  2. Start your mongo shell, type: mongo --shell
  3. In shell, create your db collection, type:  use <choose your db i.e.: demo>
  4. In shell, create one entry, type: db.demo.insert({name: "hamid", socialSecurity: 1234567890})
  5. In shell, verify your entry, type: db.demo.find()

B. Create java web project (please make sure you're connected to the internet)

  1. In command line, navigate to your eclipse workspace folder. If there isn't existing, create one.
  2. Create java web project (just hit enter on interactive prompt), type: mvn archetype:generate -DgroupId=<choose one i.e.: com.pointcut.demo> -DartifactId=<choose one i.e.: springmongo> -DarchetypeArtifactId=maven-archetype-webapp
  3. On "BUILD SUCCESS" (after a long while), navigate to your project folder (i.e.: springmongo) to get your project eclipse ready, type: mvn eclipse:clean eclipse:eclipse
  4. Import into eclipse IDE: Goto menu File->Import->Maven->Existing Maven Projects and locate your project in your workspace folder.
  5. Make sure you project points to the right JDK runtime in Java Build Path: Right click on project name->Build Path->Configure Build Path...

C. Add project dependencies: Spring core, Spring test, Spring data mongo

  1. Add project dependencies: Right click on project name->Maven->Add Dependency
  2. In "Add Dependency" window, type "spring-core" in "Enter groupId, artifactId or sha1 prefix or pattern (*)" field
  3. Choose related library from "Search Results" list choose "org.springframework spring-core"-> 4.0.2.RELEASE version
  4. Repeat previous two (2) steps for spring-test (4.0.2.RELEASE), spring-context (4.0.2.RELEASE), spring-context-support (4.0.2.RELEASE), spring-beans (4.0.2.RELEASE), spring-data-mongodb (1.2.0.RELEASE), spring-beans (4.0.2.RELEASE), mongo-java-driver (2.9.1)
  5. Update your maven project: Right click on project name ->Maven->Update Project...

What's next? checkout PART 2 coming soon...


-----------------------------✄----------------------------

Installation Guide

How to install? I love using command line if all possible. ok briefly this is what I did via command line, at least.

For Maven 3.x

  1. Download the archive from here. In command line, wget <archive url address>
  2. Unzip it. For my case at /usr/local. Depending on your archive (.zip or tar.gz) u may want to user unzip or tar zxvf in command line.
  3. Set the home var and path. Update your .bash_profile (I use vi), scroll all the way down and insert the following lines: 
          export M2_HOME=/usr/local/<Maven Extracted Folder>
          export PATH=$PATH:$MAVEN_HOME/bin

For Mongo DB 3.x (recommend 64-bit)

  1. Download the archive from here. Again, in command line wget <archive url address>
  2. Unzip it. For my case at /usr/local. Depending on your archive (.zip or tar.gz) u may want to user unzip or tar zxvf in command line.
  3. Set the home var and path. Update your .bash_profile (I use vi), scroll all the way down and insert the following:

          export MONGO_HOME=/usr/local/<Mongo Extracted Folder>
          export PATH=$PATH:$MONGO_HOME/bin

For Java JDK 1.7 here for Mac

For Eclipse Luna Java EE here 


Monday, 17 August 2015

Class not found: com.mchange.v2.ser.Indirector

I was trying to setup CAS server for Liferay CE 6.2  based on "Liferay 6.x Portal Enterprise Intranets Cookbook" but stumbled with the above error. I did follow all the steps diligently, but I guess the author might have missed additional library to add to ${TOMCAT_HOME}/webapps/cas-server-webapp-4.0.0/WEB-INF/lib that is "mchange-commons-java".

I quickly search for this library in my machine if any as I was so lazy to search in out in google and found it in Liferay's tomcat bundle under webapps/ROOT/WEB-IF/lib folder.

Tips: You may want to use find command in Centos (or any linux-based OS) search by filename with "sudo find / -name 'mchange-commons-java*' -print".


I hope that helps someone outthere.

Happy Godek'king!

Remote JMX Connection Issue

Recently I tried to remotely connect to Liferay 6.2 's tomcat bundle on JMX via JConsole and JVisualVM with no success. Both applications repeatedly unable to connect via rmi. This boggled me as this never happened to me if the same to be done on Windows or my OSX macbook.

The only difference this time is I have Centos 7 running on my macbook. Yes, Centos 7, trying to get my hands dirty back on Linux. Installing Centos 7 on Macbook is another story. It took me a few days of misery and frustration until LiveCD saved me. OK, enough about me.

So, as usual you would need to supply VM arguments as follow:

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false


Tips: You can quickly kill the process that is listening to port 9999 with "fuser -k 9999/tcp" command.

Normally this is would suffice, but not this time.
Further tests to narrow down to possible root cause includes:

1. Telnet to port - OK
# telnet <host-ip> 9999

2. Run local JConsole and JVisualVM - OK (although unneccessary)

Tips: You may completely disable firewall in Centos 7 with "sudo systemctl disable firewall-d" or
open up specific port with "sudo firewall-cmd --zone=public --add-port=9999/tcp && sudo firewall-cmd --reload"

After exhaustively searching through google and stackoverflow finally I found a blog page that suggest additional argument. Ironically, the blog post entry has nothing to do with my problem, duhh...additionally I found this via a comment in stackoverflow entry.

So, the additional argument that make it work is -Dcom.sun.management.jmxremote.rmi.port=9999
Feel me? yeah, how silly is that and I don't know why and how that works. So, help me god if anyone would generously want to explain.
'
-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.rmi.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false


Made the changes then, I fired JConsole/JVisualVM and it works!

I hope that helps some one outthere.

Happy Godek'king!!

FYI "Godek" slang means "hack" in Malay :)




java.io.IOException: Cannot recover key

For those who are NOT familiar with configuring tomcat HTTPS connector (I had apache-tomcat-7.0.34), like me, this error can be frustrating. It turned out to be a simple fix. I am assuming you have properly generated a tomcat key, a keystore and imported them successfully into JAVA cacerts keystore. As for my case I chose my own keystore filename, my own kesytore pass(word) and my own tomcat key pass(word). Unfortunately, these require explicitly be entered in your tomcat's server.xml. 

Consider the following template in the tomcat's server.xml file after you uncommented it.



If you named your keystore as ".keystore" with key and keystore pass(word) as "changeit" and left it reside in your home dir (i.e.: /home/hamid) , then above setup would work happily just like that.

Unfortunately, for most of the time it is not the case as you would name your keystore to some other name, located to some other location (folder) and both key & keystore with some other pass(word).
For example:

Keystore name: keystore.jks
Keystore folder: /home/hamid/secret/store
Keystore password: password1
Key password: password2

Then you would want to do the following:



I hope that helps.

Happy Godek'king!

FYI "Godek" slang means "hack" in Malay :)