2014-12-29

MOXy and JAXB






This articles explains how to use MOXy with JAXB to avoid needless POJO creations.

You can read it here

XML unmarshalling benchmark in Java











Here is a nice post with an actual benchmark on unmarshalling XML in java.

Read it here ..

Another benchmark here

Integrating Swagger Into JAX-RS With Java EE 6 Specification


Swagger is an awesome framework we often used to describe, consume and visualize our RESTful web services. Typically, we use Tomcat with Jersey as a servlet, then specify the Swagger package and Swagger Configuration class into web.xml, finally annotate the resources, methods and models to complete the configurations. Our team recently built a Java EE 7 application for a RESTful web service. The goal of this article is to share our experiences of configuring Swagger in Glassfish 4 without a web.xml.

Read more ...

2014-12-28

Make Maven package all dependency jars into my jar


Add the following in pom.xml


<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>
 
and you run it with
mvn clean compile assembly:single Compile goal should be added before assembly:single or otherwise the code on your own project is not included.

Source

Arquillian with NetBeans, GlassFish , JPA









A nice working example of Arquillian in Glashfish

Read it here

S.O.L.I.D. Software Development



Most professional software developers understand the academic definitions of coupling, cohesion, and encapsulation. However, many developers do not understand how to achieve the benefits of low coupling, high cohesion and strong encapsulation, as outlined in this article. Fortunately, others have created stepping stones that lead to these goals,

resulting in software that is easier to read, easier to understand and easier to change. In this article series, I will define three of the primary object-oriented principles and show how to reach them through the five S.O.L.I.D. design principles.

Have you ever played Jenga? It’s that game of wooden blocks that are stacked on top of each other in rows of three. In Jenga you try to push or pull a block out of the stack and place it on top of the stack without knocking the stack over. The player that causes the stack to fall loses.

Read more ...

How to add certificate to JVM

 

 

 

 

 

PKIX path building failed: SunCertPathBuilderException: unable to find valid certification path to requested target.

SSLHandshakeException: PKIX: unable to find valid certification path to requested target

This solution relates to the following stacktrace:

javax.net.ssl.SSLHandshakeException: sun.security.validator.
ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException
unable to find valid certification path to requested target.
    
We experienced this issue when a server changed their HTTPS SSL certificate, and our older version of java did not recognize the root certificate authority (CA).
If you can access the HTTPS URL in your browser then it is possible to update Java to recognize the root CA.
In your browser, go to the HTTPS URL that Java could not access. Click on the HTTPS certificate chain (there is lock icon in the Internet Explorer, or the domain name left of the URL in firefox) and navigate the certificate hierarchy. At the top there should be a Primary Root CA. This could be missing from your java cacerts file. Note down the Issuer and Serial Number.
To verify the root certificates, determine where the cacerts file is located. By default it is in jre/lib/security/cacerts. The default password for this keystore is 'changeit'.
e.g. on my machine, I have both JDK and JRE, here is where they are located.
./jdk1.6.0_24/jre/lib/security/cacerts
./jre1.6.0_24/lib/security/cacerts
Different versions of java can have different cacerts.
If you do not want to modify the default JRE store, you can make a copy, and use the following system properties to specify the location and password.
javax.net.ssl.trustStore
javax.net.ssl.trustStorePassword
Once you have your keystore, dump its contents by using the list option.
keytool -list -v -keystore /path/to/cacerts  > java_cacerts.txt
Enter keystore password:  changeit
In this example, /path/to/cacerts is the location of your cacerts file, and the output of the command will be saved in java_cacerts.txt.
Take a look at java_cacerts.txt. See if it includes the same certificate that is present in the browser by searching for a matching serial number. In the java_cacerts.txt file, the serial number will be in lowercase and without the ":" colon character. If it is not present, then this could be the reason for the error, and we can fix this by adding the certificate found in the browser.
Back in the browser, export the Root CA. Choose the "X.509 Certificate (DER)" type, so the exported file has a der extension.
Assuming the file is called example.der, pick the alias 'example' for this certificate. Next import the file.
keytool -import -alias example -keystore  /path/to/cacerts -file example.der
You will be prompted for a password, use 'changeit'
and response "yes" on whether to trust this key.
Dump the contents again to verify it contains your new certificate. Restart the JVM and check that it can now access the HTTPS URL. Also remove the java_cacerts.txt dump file.


Source

Using Generics To Build Fluent API's In Java











This article explains in detail the steps to create fluent API in Java using Generics.

Read it!

Code Generation using Annotation Processors in the Java language












I have found a very nice post explaining in detail how handle code generation through annotation processing.

Have a look here:

Part 1

Part 2

Part 3


Related useful articles

https://netbeans.org/kb/docs/java/annotations-custom.html

2014-12-26

Open Source Integration with Apache Camel


Take any integration project and you have multiple applications talking over multiple transports on multiple platforms. As you can imagine, in large enterprise applications this can get complex very fast. Much of the complexity stems from two issues:

1.  dealing with the specifics of applications and transports, and

2.  coming up with good solutions to integration problems.

continue reading here: http://java.dzone.com/articles/open-source-integration-apache

Useful links:

- http://camel.apache.org/
- http://stackoverflow.com/questions/8845186/what-exactly-is-apache-camel
- https://github.com/CamelCookbook/camel-cookbook-examples
- https://github.com/FuseByExample?page=2
- http://saltnlight5.blogspot.gr/2013/08/getting-started-with-apache-camel-using.html

OOP Design Patterns

Code Snippet Management With Gists



Github offers you a great tool for creating code snippets the gist.
Have a look on that video.



Source: http://code.tutsplus.com/tutorials/sexy-code-snippet-management-with-gists--net-24328

2014-12-25

Import existing source code to github



If you've got local source code you want to add to a new remote new git repository without 'cloning' the remote first, do the following (I often do this - you create your remote empty repository in bitbucket/github, then push up your source)
  1. Create the remote repository, and get the URL such as git@github.com:/youruser/somename.git or https://github.com/youruser/somename.git
If your local GIT repo is already set up, skips steps 2 and 3


  1. Locally, at the root directory of your source, git init
2a. If you initialize the repo with a .gitignore and a README.md you should do a git pull {url from step 1} to ensure you don't commit files to source that you want to ignore ;)
  1. Locally, add and commit what you want in your initial repo (for everything, git add . then git commit -m 'initial commit comment')


  1. to attach your remote repo with the name 'origin' (like cloning would do)
    git remote add origin [URL From Step 1]
  2. Execute git pull origin master to pull the remote branch so that they are in sync.
  3. to push up your master branch (change master to something else for a different branch):
    git push origin master

Camel Import Tool



         How many times have you need to import raw data in your database? In the company that I work (Cloudbiz ), this used to be a very common scenario. As a company that specializes in CRM systems, we had the responsibility to launch a new CRM model and also try to import (after doing some data healing) the data from a legacy system. Very often that legacy system used to be Excel (!) or another System that could only export data to text files which we had to process and import them to our system. Also, many times the process had to be run over and over (not just once) so the import process was look like more than an Integration process instead. Those that are familiar with this kind of Job should know how tedious this is and how much effort it takes to do it the right way.

Hopefully many things have changed since then. New tools have arisen that promise the flexibility and the "RAD" factor that was missing all those years in the field. Within this post I am going to show you the ease of implementing on a basic Data Import Scenario. I am going to demonstrate you how to use Apache Camel to import data from a text file to a REST web service. At the end of the post you will also find the source code of this demo.

Problem Definition:

Customer X has a text file that has his existing customers. Since the platform that he just bought from you also keeps customers, he asks you to import the old data inside the new database. Your platform's API is implemented with REST web service that handle JSON objects.

Approach A (the bad)

Many people in order to solve this problem they would have written a new program that would read a file and line by line, marshal the line to JSON and then do an HTTP request on the web service. This solution although it might seem simple or optimal at first sight it is actually the worst for the following reasons.
- It is not clean. You do this and after you complete it you feel dirty. Most probably on your next confession at the priest you will mention this.
- It is not reusable. If you had a new customer with a similar problem then the only way to reuse it would be to copy paste the whole project and do the modifications accordingly. This way most probably lead you to Copy paste errors and all of us know evil they are.
- The process is bound to the problem. If your next customer for example has the old data to some other form, i.e. another database or even another WS, then you either must rewrite the half of the code or must create an additional program that first transfers the old data to text and run it before.
- You have to reinvent the wheel many times. You have to create the read file process from scratch or do the http request with all the error handling involved.

Approach B (the expensive)

A well-grounded developer most probably would approach this problem differently. He/she would design the solution with a required level of abstraction in order to be reusable for the next customers without having to delete not a single line of code. If I had to deal with that, I would have done the following. A) Instead of hardcoding reading from file and sending to REST WS, I would have written something that reads from endpoint 1 and sends the result to endpoint 2. Both of those endpoints should implement the same interface and have similar behaviors. With this approach if the next project were read from a rest WS and write to a file then I should only implement the extra functionality leaving my previous code untouched and reusable. Also the data those 2 endpoints are exchanging should also be abstract. This solution although it seems more proper it has its disadvantages.
- You have to reinvent the wheel many times. Like the previous one you have to create the read file process from scratch or do the http request with all the error handling involved.
- It is too expensive. Most of the times you are not going to have the time to implement something like this. Also the customer did not pay you to create the UBER import tool module and almost certain the expense of creating it ends up to your company.

Approach C (the good)

What if someone told you that the program the developer in Approach B did already exists and does much more things that you already need? Wouldn't be great? Hopefully that tool truly exists, it is open source and it is considered the easiest to use from all the rest of its competitors - mule and Spring Integration (read this article for more ).
Having our tool ready we should only focus with things like what is the file name or what is the web service url and stop caring about how to open the file, when to close it.
In order to show you how easy it is here is a sample Apache Camel project that reads records from a csv file, transform them to JSON and sends them directly to REST web service.





Open CSV











Open CSV is a java library that is used to manipulate CSV files.

You can find the URL of the project here: http://opencsv.sourceforge.net/ and here: https://github.com/mdagis/SampleOpenCsv is a working example in case you want to download ready to play project.

Popular Posts