Billy Yarosh Blog

Java & Ruby Development: Coding Cures, Concepts, API examples and more


The Java Enum: A Singleton Pattern


The singleton pattern restricts the instantiation of a class to one object. In Java, to enforce this, the best approach is to use an enum. This great idea comes straight from the book Effective Java by Joshua Bloch. If you don't have it in your library, get it. One of the best Java books to date.


There are a few reasons why one would use an enum as a singleton in Java:
  1. Serialization for free.
  2. Guaranteed one instance (Cannot instantiate more then one enum even through reflection.)
  3. Thread safe

Bootstrap Guide: The Virtual Tour Library

Introducing a Virtual Tour Library (using TW Bootstrap)


 A jQuery library used for designing help pages for your website. Gives the user a guided walk through of your webpage and it's functions.




Rails Associations: Always think of the user

The topic of discussion in this post is Rails associations. Primarily, we will be focusing on which association you should choose in certain advanced scenarios. If you understand Rails associations but want better insight on deciding which associations to use, then this post is for you.

The Scenario

This example may not seem practical, but I feel it is a fun and simple way to convey our association strategies. The scenario is extremely important to our decision making when designing a data model. What data is required, and in what format is usually the first step.



Java Developers Need To Be Using Lombok


Lombok is a Java library meant to simplify the development of Java code writing. This article deals with writing classes that need getters/setters, override equals, hashCode, toString and/or offer a copy constructor. Way to go Java! You just made writing a simple class cumbersome. Project Lombok will make your day if you haven't seen it yet.

Adding Groovy Sources to Maven Build Path


Say you want to run a groovy class method from your Java project in Maven. You've placed the script file at src/main/groovy/script/MyGroovyScriptClass.groovy. In Java, you would load the class and execute its method like so:
final ClassLoader parent = this.getClass().getClassLoader();
final GroovyClassLoader loader = new GroovyClassLoader(parent);
URL url = this.getClass().getResource("/script/MyGroovyScriptClass.groovy");
try {
    final Class groovyClass = loader.parseClass(url.getFile());
    this.jdbcRouteCSV = (GroovyObject) groovyClass.newInstance();
    Object[] args = {};
    this.jdbcRouteCSV.invokeMethod("myGroovyScriptMethod", args);
}
catch (IllegalAccessException iaex){
    ...
}
catch (InstantiationException iex){
    ...
}

The problem is that this won't work in Maven, unless you add the groovy folder to your resource path.


Ignore Header and Trailer Lines During Java InputStream Read

Ignore first + last lines in a file

After beating my head against the wall for some time, I was finally able to come up with a solution for this.

Say you want to hand off an InputStream or InputStreamReader to an underlying API. The problem is that your input stream may have a header or trailer. Those header/trailers differ from the underlying format. This is commonly true for CSV, EDI, and fixed length file types. So if you want a CSV file parsed by a Java API like the Smooks api, you will need to hand the framework an InputStream.


Grails Part 5 - Adding Basic Security

Click here to view the full tutorial

Continuing series: Developing a Grails Web Application.
Tutorial will: walk through steps of creating a robust Grails web application.
Application name: grails-mongo-demo
The Goal: To track and record persons addresses.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request
Previous Post: Grails Part 4 - Enhanced UI Design w/ tw Bootstrap
View Project on Github

These Instructions Will Walk Through:

  • Configuring Spring Security Plugin
  • Adding static URL maps
  • Bootstrapping in security data
  • SecurityTagLib - Conditionally display gsp content
  • Configure Security Pages with Twitter Bootstrap UI

Grails Part 4: Enhanced UI Design w/ Twitter Bootstrap

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request
Previous Post: Grails Part 3 - Building a CRUD Application
View Project on Github

These Instructions Will Walk Through:

  • Enhanced UI Design with tw bootstrap
  • Configure Bootstrap with lesscss-resources plugin
  • Adding a custom controller
  • Customizing Scaffold Templates
  • Improving Scaffold Output


Grails Part 3 - Building a CRUD Application

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request
Previous Post: Grails Part 2 - Configuring the Plugins
View Project on Github: Grails MongoDB Demo

These Instructions Will Walk Through:

  • Building a CRUD Application
    • Building the Domain Model
    • Controllers using Grails Scaffolding


Grails Part 2 - Configuring Web Application Plugins

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request

Previous Post: Grails Part 1 - Setting up a Project on Maven With IntelliJ
View Project on Github: Grails MongoDB Demo

These Instructions Will Walk Through:

  • Configuring the Plugins
    • MongoDB
    • Jetty
    • Spring Security
    • Apache CXF (WSClient)

Grails Part 1 - Setting up a Project

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration
View Project on Github: Grails MongoDB Demo
These Instructions Will Walk Through:
  • Installing the Grails SDK
  • Project Setup
    1. Creating a Grails Application
    2. Integrating with Maven
    3. Integrating with IntelliJ

Applying the Strategy Pattern in Java

Working with strategy on Arrays.sort & java.util.Comparator

All these examples and more can be found on github
In Java, you cannot pass function references through a method. With this being true, how do we then apply a strategy to a method? What is suggested, is that we define an interface with the function we want to pass. This way we can on the fly, define and pass an algorithm to a method for process.

In this demo, we will be working strictly with the strategy interface: java.util.Comparator. This is a java strategy interface, readily available in the Java API as of 1.5. It supplies a compare algorithm, where its implementation is primarily for sorting large collections of objects.


Atmosphere Websockets & Comet with Spring MVC

Since writing this guide, Spring has come out with their own Comet and Websocket implementations. I suggest you first read through their guides before deciding on atmosphere as your Comet & Websocket solution.

1. Messaging with Stomp over Websockets
2. Spring Messaging

On to the guide...

Feel free to clone the source of this project on github.
springMVC-atmosphere-comet-websockets

The Websocket protocol is defined by W3C here. The protocol is considered an HTTP upgrade protocol. Essentially what happens is, a websocket request is made by a client browser, via the HTTP protocol. Once, the request is in, the server should respond with a success. Then the browser and server establish a TCP link to handle communication from there on out. This protocol enables bi-directional communication between a client and server. Here is a link to browser and servers that support Websockets and/or Comet: comet/websocket support link