Friday, December 14, 2012

jumpstart with eclipse, maven and tomcat (web/app server)

Q: What is the quick way to setup Eclipse war (possibly ear) project built with maven? 

A: In short: Eclipse + wtp + m2e + m2e-wtp

Sometimes from idea to it's realization is not an easy way.

If you're in a need to quickly setup java war project and you like the tools I do: maven, eclipse and tomcat (or any other server of your choice) following might be useful to jumpstart realizing your ideas.

My requirements

I've searched for a solution that would enable me to:
- use eclipse for application development
- manage server lifecycle directly from eclipse and
- use maven as a build tool.

My choice

Following has been found and based on the short project I've tested it with, it worked very well:
- eclipse - no doubts here (it's my favorite IDE for java development)
- wtp - fits me best for the server management directly from eclipse
- m2e - maven eclipse integration (as I'm in love with maven)
- wtp-m2e - wtp m2e integration (this one I've been missing in the puzzle, but only until today)

As  the last one mentioned was new to me the following video convicted me that it's something I need:

 
(the video is reffered on the official site as well)

After the initial project setup (depicted on the video) right clicking on the project and choosing "Run/Debug As" -> "Run/Debug on Server" does the job of publish and run/debug.

And once you change your project contents automatic republish happens in the background as well.

Sounds like the initial infrastructure setup time for my next project can be the question of seconds rather that hours.

Friday, November 30, 2012

My fedora java 7 update

Today I've migrated to java 7 (Oracle version), as our development environment has been "java 7 enabled".

The transition was "rather" smooth, I just had to follow following description: http://fedorasolved.org/Members/zcat/using-sun-java-instead-of-openjdk

When I mentioned "rather", it was not due to description present on the mentioned site :). After I rebooted I could not login to my xfce (or gnome,...), login screen was shown again even after correct password input.

The problem was caused by my laziness. I didn't really read what I copy/pasted :)

After switching to text mode (Ctrl+Alt+F<X>) I've observed some EOF reported problem right after login. And my bash didn't behave :) I had to provide full path when invoking commands.

When thinking of what I've changed, file: /etc/profile.d/sunjava.sh started to be suspicious, after opening it via:
/usr/bin/sudo /usr/bin/vi /etc/profile.d/sunjava.sh
(yes, path didn't work for me somehow :)

my suspect changed to culprit when seeing the content:
export JAVA_HOME=/usr/java/default
export PATH=\$JAVA_HOME/bin:\$PATH
EOF
after fixing it to:
export JAVA_HOME=/usr/java/default
export PATH=$JAVA_HOME/bin:$PATH
and doing the reboot afterwards, via:
/usr/bin/sudo /usr/bin/reboot
all started to work as expected.

So it seems trivial things might become challenges in my hands :)
Now I see why I became a programmer rather than a doctor :)

Friday, November 23, 2012

Maven java.lang.ClassNotFoundException: org.codehaus.plexus.classworlds.launcher.Launcher

Have you ever faced exception like this?
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.classworlds.launcher.Launcher
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.codehaus.plexus.classworlds.launcher.Launcher.  Program will exit.
My use case was that I had installed maven3 and afterwards tried to use maven2. All the environment variables updates have been setup in:
~/.bashrc
However once trying to use maven2 exception appeared (Even in the new terminal session).
Solution was found based on advice present in: http://cyntech.wordpress.com/2011/03/09/maven-2-error/

Running command:
which mvn
revealed the problem, as I still had in my $PATH the maven3 binary. Even if I appended the maven2, it was there on the later position => maven3 was to be used, but with changed $M2_HOME (reffering to maven 2).

Fixing the $PATH did the job for me.

This might not be your case, therefor as a general sum up of possible root causes, I recommend checking: http://stackoverflow.com/questions/6198677/java-lang-noclassdeffounderror-org-codehaus-plexus-classworlds-launcher-launche

Friday, November 16, 2012

Glassfish 3.1.1 OSE with Hibernate Validator 4.3.0.Final

Goal: Use hibernate validator 4.3.0.Final with Glassfish 3.1.1 OSE.

Lately I came across an issue, that I wasn't able to use the latest stable hibernate validator (hiberante validator 4.3.0.Final) with Glassfish 3.1.1 OSE.

Problem is that older version is beeing used as a glassfish module (glassfish/modules/bean-validator.jar). If you check the contents of the package (in MANIFEST.MF) you'd see that version 4.1.0.Final is beeing used.

OK, so how to deal with that? I've found out that I'm not the only one having issue here.
http://stackoverflow.com/questions/10548931/how-to-upgrade-the-hibernate-validator-4-3-0-final-to-the-glassfish-3-1-2

Suggestion gave me some motivation, as it seems to be possible to achieve :)

As I'm using ear, the simple solution provided in answer was not feasable for me (as it was a war case). OK, let's see how far we can get. There are multiple references (finally I ended up with the following 2 links):
I read the later one, as it seemd like a good idea to learn creating modules deployable to glassfish, however even if I created maven osgi bundle it neither contained all the dependencies required nor the descriptor seemed to be OK. Sounds like a wrong way for me then.

Going back to the 1.st link, I found following svn repository and tried to build things: https://svn.java.net/svn/glassfish~svn/trunk/external/source-build
However only once it has been achieved (and the way there was quite long) I realized that this just rebuilds the hibernate validator with it's dependencies, and that is of course not needed, as these are already built and available in public maven repositories :).

OK, going via next steps in the article, I ended up with the following solution:
  1. checked out glassfish sources
    svn co https://svn.java.net/svn/hk2~svn/branches/hk2-gf-3.1.1/
    
  2. updated bean-validation related pom, see my diff:
    Index: pom.xml
    ===================================================================
    --- pom.xml (revision 4105)
    +++ pom.xml (working copy)
    @@ -54,9 +54,10 @@
              maven-bundle-plugin and hk2-maven-plugin together -->
         <packaging>jar</packaging>
         <properties>
    -      <hibernate-validator.version>4.2.0.Final</hibernate-validator.version>
    +      <hibernate-validator.version>4.3.0.Final</hibernate-validator.version>
           <javax.validation.version>1.0</javax.validation.version>
           <slf4j.version>1.6.1</slf4j.version>
    +      <jboss-logging.version>3.1.0.CR2</jboss-logging.version>
         </properties>
         <name>Validation API (JSR 303) version ${javax.validation.version}, Hibernate Validator version ${hibernate-validator.version} and its dependencies repackaged as OSGi bundle</name>
     
    @@ -89,7 +90,7 @@
                             <Embed-Dependency>
                                 <!-- Only specify root artifacts that need to be embedded, everything else
                                      will be pulled in automatically based on Private-Package settings. -->
    -                            *; artifactId=hibernate-validator|slf4j-api|slf4j-jdk14; inline=true
    +                            *; artifactId=hibernate-validator|jboss-logging|slf4j-api|slf4j-jdk14; inline=true
                             </Embed-Dependency>
                             <Export-Package>
                                  <!-- 
    @@ -105,7 +106,7 @@
     
                             <Private-Package>
                                  <!-- Have a private copy of external non-standard dependencies -->
    -                             org.slf4j.*; com.googlecode.jtype.*; org.joda.time.*; org.jsoup.*
    +                             org.jboss.logging.*; org.slf4j.*; com.googlecode.jtype.*; org.joda.time.*; org.jsoup.*
                             </Private-Package>
     
                             <Import-Package>
    @@ -117,6 +118,12 @@
                                    which is a JPA 2 class.
                                -->
                                org.slf4j; org.slf4j.spi; org.slf4j.helpers; version=${slf4j.version}; resolution:=optional,
    +                           org.jboss.logging; version=${jboss-logging.version}; resolution:=optional,
    +                           org.apache.log4j;resolution:=optional,
    +                           org.jboss.logmanager;resolution:=optional,
    +                           com.ibm.uvm.tools;resolution:=optional,
    +                           com.sun.jdmk.comm;resolution:=optional, 
    +                           javax.jmdns;resolution:=optional,
                                javax.persistence.*; version="2.0"; resolution:=optional,
                                *
                            </Import-Package>
    @@ -263,6 +270,13 @@
             </dependency>
     
     
    + <dependency>
    +  <groupId>org.jboss.logging</groupId>
    +  <artifactId>jboss-logging</artifactId>
    +  <version>${jboss-logging.version}</version>
    +            <optional>true</optional>
    + </dependency>
    +
             <!-- We bundle jdk binding inside this OSGi bundle -->
             <dependency>
                <groupId>org.slf4j</groupId>
    
  3. built:
    cd hk2-gf-3.1.1/external/bean-validator
    mvn clean install
    
  4. copied to glassfish modules and removed old one:
    rm ${GLASSFISH_HOME}/glassfish/modules/bean-validator.jar
    cp hk2-gf-3.1.1/external/bean-validator/target/bean-validator-1.1.15-SNAPSHOT.jar ${GLASSFISH_HOME}/glassfish/modules/
    
  5. done :)

Issues? well, we'll see later, during testing :) But very first try worked perfectly.

Comments? Feel free to share, as I'm not the osgi expert (yet :)), so some of my updates might ... let's say won't make too much sense.

Monday, October 22, 2012

Solving Eclipse 4.2 unresponsive UI problems

Q: How to fix Eclipse 4.2 unresponsive UI problems?
A: Reserving more memory for it.

Now the long answer comes:
While using Eclipse 4.2 on Fedora 17 I got into problems, that opening Find dialog within the file took around 5-10 seconds.
That is crazy and not the way the things can work to be productive.

However following forum entry gave some hope to me:
http://www.eclipse.org/forums/index.php/mv/msg/367243/896690/#msg_896690
And after applying the suggestion ecilpse came "back to life" :)

So these are the args that worked for me:
-vmargs
-Xms512m
-Xmx1024m

Sunday, September 16, 2012

Generating graphical representation of XSD (XML schema).

Question: Have you ever seen the graphical representaiton of XSD in a way XMLSpy is capable of generating?


Did you ever need to generate similar output via free/open source tools?

Answer: XSD Diagram (http://regis.cosnier.free.fr/?page=XSDDiagram).

When searching for the alternative couple years ago, that was my choice and I didn't regret since then,

Usage is very simple, let's see how works. I'll go step by step in a usage. For a real world example I use as a sample for the xsd the following one (standard web application descriptor schema): http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd

  1. Start XSDDiagram.exe and choose File -> Open (open the downloaded xsd file)
  2. Error is thrown: "Could not find file 'dir/web-common_3_0.xsd'."
  3. OK, so we need reffered xsds as well. Let's get the rest then (web-common_3_0.xsd, javaee_6.xsd, jsp_2_2.xsd and javaee_web_services_client_1_3.xsd).
  4. Copy them to the same dir and reopen the original xsd again.
  5. Click the button in toolbar "Add all toplevel elements"

    or just choose the element you are interested in and click "Add selected toplevel element"
  6. Click multiple times (till all the levels are expanded) the button in toolbar "Expand one level"

    or just click the + sign for those elements you're interested in.
  7. Choose File -> Export Diagram -> (choose the file type feasable for your use case, I chose svg) and save.
  8. You're done!
Output could look like this (please note that schema graphical representation in this case is huge):

Monday, July 30, 2012

fixing incorrect dates in JPEGs on (x)ubuntu

Have you ever faced the problem of incorrectly set date in your camera?
I have, and realized it only after copying data to my laptop.

In my case year was set to previous one. For the solution description, please keep this in mind as I fix the date shift only, however the tools used could fix any other incorrect date setup as well.

Fixing it for the future pics is easy, just update your settings in camera, however how to fix it for the already created pics?
There are 2 places where date update is required:
- fix file properties as well as
- fix EXIF data.

Fixing file date
In linux it's easy. My case was I set up last year instead of this one => after having all the jpegs in one dir, I just ran:
touch -d '+0 year' *
(solution ideas were inspired by: http://superuser.com/questions/122863/linux-shell-change-a-files-modify-timestamp-relatively)

How to fix EXIF data?
Exiftool (http://www.sno.phy.queensu.ca/~phil/exiftool/) seems to do the job easily. Date shift feature using exiftool is described in more detail on: http://www.sno.phy.queensu.ca/~phil/exiftool/Shift.html

I Instaled it first:
sudo apt-get install libimage-exiftool-perl
Moreover there are multiple dates in EXIF, to list them I ran this on the particular file (using exiftool):
exiftool -k IMGP0184.JPG | grep Date
File Modification Date/Time     : 2012:07:30 22:27:57+02:00
Modify Date                     : 2011:07:22 12:48:12
Date/Time Original              : 2011:07:22 12:48:12
Create Date                     : 2011:07:22 12:48:12
Date                            : 2011:07:22
Manufacture Date                : 2011:10:07
-- press RETURN --
To fix EXIF dates on all the relevant files I ran:
exiftool "-AllDates+=1:0:0 0:0:0" *.JPG
That should be it. Let's check the result rerunning the EXIF data listing command:
exiftool -k IMGP0184.JPG | grep Date
File Modification Date/Time     : 2012:07:30 22:28:16+02:00
Modify Date                     : 2012:07:22 12:48:12
Date/Time Original              : 2012:07:22 12:48:12
Create Date                     : 2012:07:22 12:48:12
Date                            : 2011:07:22
Manufacture Date                : 2011:10:07
-- press RETURN --
OK, that worked for me :) Hope it helps you as well.

One more thing, to cleanup backup files created by exiftool I ran following:
rm *.JPG_original