Wednesday, December 24, 2008

Merry Christmas...

Merry Christmas everyone....

God Bless...

Friday, December 19, 2008

Eclipse Code Templates - toString, equals, hashCode

Finally I decided to write a code template that I can use in order to create those infamous methods that should be a part of everyone's DTO. Those methods are toString(), equals(), and hashCode().

The template does assume that you are using the commons-lang library. Feel free to use it and pay it forward:


${:import(org.apache.commons.lang.builder.EqualsBuilder,
org.apache.commons.lang.builder.HashCodeBuilder,
org.apache.commons.lang.builder.ReflectionToStringBuilder)} @Override public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } @Override public String toString() { return ReflectionToStringBuilder.toString(this); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); }
Enjoy.

Wednesday, October 22, 2008

Eclipse - Bringing Back Run Last Launched

If you upgraded to the Eclipse 3.4, you may have noticed that the shortcut key for Running the Last Launched does not work properly. In older versions of Eclipse, CTRL+F11 used to run the last launched application or Junit test. By default, in Eclipse 3.4, this is used to run the *.java file that currently has focus.

If you would like to change this functionality back, just follow these instructions:

1. Open Eclipse Preferences by Navigating to Window -> Preferences
2. Using the Tree, Navigate to Run/Debug -> Launching
3. Here you will find the group called Launch Operation
4. Choose the option that says Always launch the previously launched application

Enjoy and pay it forward!!!

Thursday, September 11, 2008

PuttyCS...You rock

Have you ever wished that there was a way to open many putty sessions, and be able to send the same command to all of the putty sessions at once? I have, and looks like I am not the only one.

PuttyCS is a tool that you can use to send the same command to many different telnet / ssh clients. This little tool has come in handy many times recently.

I had a situation where I had to simulate 200 instances of a command line application running on a server, in order to determine the performance of the application and the load that it would put on the server. Using this tool, I was able to open 200 putty sessions, start the application, and exercise it.

Another situation that I have is ongoing. Working within a cluster environment provides many benefits, but also, it can be a headache. In my circumstance, I have to tail the logs on each node from time to time. Using this tool, it becomes very easy to connect to different servers, and tail the logs on each node.

Once again, I highly recommend this tool, it is sure to save a lot of time and effort.

Monday, August 11, 2008

EasyMock Tip

EasyMock is a light weight Mock library used for mocking interfaces in Unit Tests. Here is a little tip on what I believe to be an undocumented best practice.

Let's say you have a Unit Test that is testing an object called CustomerController. CustomerController has a dependency on Interface CustomerDao Typically, you may see in the unit test, code that looks similar to the following:

protected void setUp() throws Exception {
    controller = new CustomerController();
    custDao = createMock(CustomerDao.class);
    controller.setCustDao(custDao);
}

public void testFindCustomers() throws Exception {
    expect(custDao.getCustomers()).andReturn(new ArrayList());
    replay(custDao);
    controller.findCustomers();
    verify(custDao);
}

As you will notice, I am specifying one mock custDao, and the system under test is the controller. This is pretty standard and straightforward use of EasyMock. Now let's see what happens when we have to add an additional dependency on the controller:

protected void setUp() throws Exception {
    controller = new CustomerController();
    custDao = createMock(CustomerDao.class);
    invoiceController = createMock(InvoiceController.class);
    controller.setCustDao(custDao);
    controller.setInvoiceController(invoiceController);
}

public void testFindCustomers() throws Exception {
    expect(custDao.getCustomers()).andReturn(new ArrayList());
    replay(custDao);
    controller.findCustomers();
    verify(custDao);
}

public void testApplyCustomerPayment() throws Exception {
    Customer customer = new Customer();
    BigDecimal paymentAmt = new BigDecimal(100);
    invoiceController.applyPayment(customer, paymentAmt);
    replay(invoiceController);
    controller.applyPayment(customer, paymentAmt);
    verify(invoiceController);
}

This also seems to be a very valid test. I would argue though that it is not complete. You are verifying that the appropriate methods are called on the objects / interfaces that you set expectations for. However, if a method is called on the custDao in the controller.applyPayment or a method is called on the invoiceController during the controller.findCustomers, without an expectation / verification the test will still pass. This may not be the desired behavior and therefore I would argue that the test is incomplete.

On my team, someone determined that a way to handle these types of situations is to create a private method on the test called replayAll and verifyAll. This included the maintaining of an array list of all of the mocks that is used within the test. While this is a valid solution, I wanted to see what other EasyMock users were doing. Here is what I found:

protected void setUp() throws Exception {
    controller = new CustomerController();
    mockControl = createControl();
    custDao = mockControl.createMock(CustomerDao.class);
    invoiceController= mockControl.createMock(InvoiceController.class);
    controller.setCustDao(custDao);
    controller.setInvoiceController(invoiceController);
}

public void testFindCustomers() throws Exception {
    expect(custDao.getCustomers()).andReturn(new ArrayList());
    mockControl.replay();
    controller.findCustomers();
    mockControl.verify();
}

public void testApplyCustomerPayment() throws Exception {
    Customer customer = new Customer();
    BigDecimal paymentAmt = new BigDecimal(100);
    invoiceController.applyPayment(customer, paymentAmt);
    mockControl.replay();
    controller.applyPayment(customer, paymentAmt);
    mockControl.verify();
}

The subtle difference to look at between these two scenarios is that the latest one uses what is known as a IMocksControl object. This IMocksControl object keeps track of what mocks were created on it so that all you would need to do in order to make sure that you are replaying and verifying all of the mocks is to call the replay and verify methods on the IMocksControl object. This ensures that all of the expectations, whether explicit or implied, are met. Thanks Tammo Freese for your help in finding this great golden nugget within the EasyMock framework.

Thursday, August 7, 2008

Barcamp Grand Rapids Episode 3 :- Revenge of the Unconference

Barcamp Grand Rapids 3 is almost here!!!

After attending the first two BarCampGR(s), I have to say that I am most excited about attending this one. Not only does each one seems to get better and better, but this year, we have had a huge amount of support from the local businesses and we have surpassed expectation of registered attendees. I am very excited to see friends that I have made from the past events, as well as making new ones.

If you are new to the BarCamp concept, please take the time to investigate it further. I will say that is unlike any other commercial conference that you have ever attended. First and foremost, the event is 100% free. Now don't get the wrong impression, since the event is going to last for two days and it is free, please don't expect that you will be forced to brown bag it for the weekend. Thanks to generous donations from local area businesses, this event will also be catered and we will be handing out schwag to boot.

This will be a great time, and I hope to see you there.....

Friday, July 11, 2008

Eclipse Tip: Static Imports

One of the great features of Java 1.5 is Static Imports. In order to configure Eclipse to search for static imports in a particular class, you have to perform the following steps:

  1. Navigate to Preferences by clicking on the Window -> Preferences Menu Item
  2. Navigate to Java -> Editor -> Content Assist -> Favorites using the menu tree (or search for Favorites using the search bar at the top)
  3. Click the New Type button
  4. Type in the name of the Class that has static methods that you would like to be used when using Eclipse's Content Assist / Code Completion (eg Assert)
  5. Click on the Browse button which will bring up the Open Type Dialog using what you entered previously as the search criteria
  6. Find the class that you would like to add, and then click Okay on the Open Type Dialog
  7. Then Click Okay on the New Type Favorite Dialog.
Now when you are editing Java code, instead of typing Assert.assertEquals, you only need to type assertEquals, with Ctrl-Space, and the Assert Type will be searched for in order to resolve the static import.

If this helps, pay it forward....

Friday, July 4, 2008

Goodbye P2, Welcome Back Update Manager

Well, I gave it a good go. I tried my best to wrap my arms and my head around P2. It wasn't until I read this blog, that I finally understood how to get P2 to work the way that I wanted it to work - complete with bundle pooling. However, I felt that there were just too many steps necessary in order to get it up and running in that fashion. Combine that with the limited documentation, I have reverted Ganymede to use the old Update Manager using these instructions.

I still do believe that P2 is a step in the right direction, however the learning curve is just too steep for me at this point. I hope to see improvement in P2's user experience in the future, at which time, I will give it another try.

Thursday, June 26, 2008

Fixing Eclipse Ganymede Startup Problems

If by chance, you are like me, and the first time that you ran Eclipse Ganymede, you were greeted with an exception screen like this one:



Here are the steps necessary to resolve that issue:

1.) Navigate to your $ECLIPSE_HOME directory (the directory where you installed Eclipse)
2.) Copy your eclipse.ini file, for backup purposes
3.) Open your eclipse.ini file and modify it to look similar to this:




4.) Try to start up Eclipse again, and hopefully your problem should be solved

The main problem seems to be that the initial settings does not allocate enough memory for Eclipse to run. By adjusting the memory settings, by using the example eclipse.ini file that I have shown, you are giving Eclipse enough memory to start up.

This interesting thing is that this issue does not seem to affect all installations. The "vanilla" Eclipse Ganymede that I obtained from Eclipse worked on one of my machines, but not on another.

If this helps, please pay it forward.

Thanks

Wednesday, June 25, 2008

Eclipse Ganymede Released Today

Well, the the Eclipse Community has done it again. Today is the day that they will be releasing their annual simultaneous release of the Eclipse Platform. Sticking with the moons of Jupiter, this year's simultaneous release, code named Ganymede, will include 23 seperate projects - not that you have to download and use all 23 projects, it is just that you can be assured that these projects are compatible with each other.

One of the biggest features / enhancements to the Eclipse Platform is the introduction of P2. P2 is the new provisioning platform that is used to manage OSGi bundles / Eclipse plugins, and the replacement for the Update Manager. There has been a lot of discussion with varying views on P2 - some in favor, others not.

I have not used it enough to form an official opinion, but at first glance I can say that the Update Manager was narrowly scoped in that it's purpose was really surrounding Eclipse Plugins. The idea of p2 is to not only be used to manage Eclipse Plugins, but also to manage OSGi bundles as a whole, which means that it can be used for any OSGi based application. I am also interested in trying their bundle pooling approach to managing Eclipse plugins versus the extension location approach, which I have been using up until now.

Here is information on how to Get Started with p2.

Here is where you will be able to download Eclipse Ganymede...as soon as the link is enabled.

Happy Coding!!!

Monday, June 16, 2008

Firefox 3 Download Day

The Mozilla Foundation has declared tomorrow, June 17th 2008, as Download Day. In an attempt to set a Guiness World Record for software downloads, they are encouraging everyone to participate by downloading Firefox 3 tomorrow.

I have been a fan of the Firefox Browser for a number of years now, however I will admit, that only recently have I started using their latest release candidate. This is because there are some add ons in Firefox, that I just cannot live without. Thankfully, the following add ons are now available and supported for the Firefox 3 platform:

Delicious BookMarks
If you are not familiar with the social networking site del.icio.us, it is a site that you can set and share bookmarks. By creating bookmarks and tagging them, those bookmarks become searchable by the social community. This is a great, but I really use it to keep my bookmarks in sync with the many computers that I work on. With this add on, you can log into your del.icio.us account, and use your bookmarks in Firefox.

FireGestures
In my view, Mouse Gestures is another one of those productivity boosters that is great to have in your arsenal. Instead of relying on clicking different buttons in the toolbar or using the Menu system in Firefox, you can simply use the mouse, and wave it across the screen while holding down the right click button, and Firefox will do your bidding. This is one of those features that I often take for granted on my machine, and expect to be available on others. I am disappointed often when I try to perform a Mouse Gesture, only to be greeted with the Context Menu that comes up when you right click on a web page.

Good luck Mozilla Foundation in breaking that Guiness World Record. You can count on my support.

Friday, May 2, 2008

Eclipse Hack :- Change the Splash Screen

Hey everyone,

Inspired by a recent posting on Planet Eclipse, I thought that I would share with you a hack that I have used to change the splash screen that is used when Eclipse starts. Enjoy.

Changing the Splash Screen:
1.) Navigate to the directory where you installed Eclipse, which I will now refer to as $ECLIPSE_HOME
2.) Move to the following directory $ECLIPSE_HOME/plugins/org.eclipse.platform_xxxxxx
3.) There you will find a file called splash.bmp
4.) Rename this file to splash.bmp.bak in order to keep the original file.
5.) Create your new bitmap file and place it in this directory as splash.bmp

And that is all there is to it.

Note: I am currently using eclipse-rcp-europa-winter-win32, so the directory on my machine that contains the splash.bmp file is org.eclipse.platform_3.3.3.r33x_r20080129. If you are using a different version of Eclipse, it may be something different, but it should at least start with the org.eclipse.platform.

Enjoy.

Thursday, February 14, 2008

Maven Archetype Upgraded

Finally, it is here. An upgraded of the Maven plugin, archetype, was just recently released. Well, according to JIRA, it was actually released last week, but just recently have I taken notice :).

One of the slams that Maven receives is related to the verbosity necessary in order to use the Archetype plugin to create new projects. In order to create a new simple java project using the older version of the Maven Archetype plugin, you had to do something similar to the following:

mvn archetype:create "-DgroupId=com.sagetech.example" "-DartifactId=dummyProject"

The above would create a simple project with the jar packaging type. If you wanted to do something a little more complex, like create a project of a different packaging type, (ejb, ear, web module), you had to specify even more properties to the archetype plugin, something similar to...

mvn archetype:create "-DgroupId=com.sagetech.example" "-DartifactId=dummyProjectWeb"
"-DarchetypeArtifactId="

Although the above approaches work without any issues, it still seemed to be a cold way for a developer to get started with an application. And it appears that I am not the only one who thought so.

With the recent release of the Archetype plugin, you don't have to specify any properties at all. Simply type the command:

mvn archetype:generate

...and during the course of the execution of the plugin, it will prompt you for the following information:

1.) Choose the archetype
2.) Choose the groupId
3.) Define the artifactId
4.) Define the version
5.) Define the package

A friend of mine has always given me a hard time about how verbose you have to be in order to create a java application using the Maven Archetype Plugin. I cannot wait until he reads this post...Dave Brondsema, this one is for you.... :)

Using Groovy...

In one of my earlier posts, I mentioned that I have really enjoyed my journey into Groovy, because I have been able to leverage it's power practically on the job. For this post, I wanted to give you a practical example of how I have been able to do just that.

I currently deal with large quantities of data. So much, in fact, that our project has stored procedures that will truncate tables because there is not enough tablespace in order to handle delete queries. This works fine if I want to delete all the records in a table, but what if I only want to delete a subset, let's say 50,000 of the multiple million records? With groovy, and it's built in SQL functionality, this becomes very easy to accomplish. Here is the complete program:


import groovy.sql.Sql

def sql = Sql.newInstance("${jdbc.url}", "${jdbc.userId}", "${jdbc.password}", "${jdbc.driver})
def query = "select customer_id from customer where customer_group_id = 11020"
sql.eachRow(query as String, {row -> sql.execute("delete from customer where customer_id = ?", [row.CUSTOMER_ID])})


The Sql object represents the connection to the database. The first thing we do is open the connection to the database using the newInstance method which takes in a valid jdbc url, db user id, db password, and jdbc driver.

Note: The JDBC Driver must exist on the classpath when executing this script.

The query variable holds the sql statement that will return me all of the id's of the customers that I would like to delete from the customer table.

Using the eachRow method on the Sql object, as each row is returned, I execute the anonymous in line closure...


{row -> sql.execute("delete from customer where customer_id = ?", [row.CUSTOMER_ID])}


Which will execute a delete statement for each record returned from the original query.

Running the application is just as simple as well...

groovy -cp .\lib\<> deleteCustomers.groovy


This is one of the many uses that I have found for this simple, yet powerful dynamic language. I hope to share more with you in the future.

Enjoy.

Tuesday, February 12, 2008

This year will be Groovy

One of the things that I wanted to accomplish this year, was to learn a new programming language. There are so many different languages that now are able to run on the JVM, that I didn't know exactly where I would focus my attention.

Originally, I started out with the idea that I wanted to learn Scala. One of the main reasons why I wanted to learn Scala is because it is a functional language. I have never worked with a functional language before. I was interested to see if solving problems in a functional manner, would inspire me to approach problems in Java differently. I downloaded it and started to go to the different tutorials online. To be honest, I was a little intimidated. The syntax is very different, and I don't think that I would be able to spend the amount of time necessary in order to get up to speed with this language.

The next language that I considered learning was JRuby. Of course, everyone has heard all of the hype around Ruby. However, I was more interested in learning more about JRuby because I have a ton of Java code / experience that I didn't want to lay waste. With JRuby, it is possible to use all of the fun Ruby syntax, but not have to give up your Java code / experience. Ultimately, I decided not to spend time looking into this language either. I think that I am still interested in picking this up in the future, but now I am even more interested in....

Groovy. I like Groovy. I like Groovy, a lot. One of the main reasons why I like Groovy so much, is because I have just started using it, and already I have been able to apply it to solve real customer requirements. We had a need to generate XML files based off of a Database query. Now, you can imagine how much Java code would be necessary in order to accomplish such a feat. By leveraging Groovy's integrated Sql features together with their Template approach, I was able to solve the problem very easily.

Another reason why I enjoy this language so much is because it was built with integration with Java in mind. If you cannot think of a Groovy way to get a task done, you can always import a Java class and use it. I didn't know the best way to format a Date in Groovy. However, in Java, I know that I can use the SimpleDateFormat. So that is what I did. I imported it at the top of my Groovy script, and used it. Simple.

What are you learning new this year?

Sunday, February 3, 2008

Groovy - Dynamic Language for Java Developers

Every Software Developer should be able to program in both a static and dynamically typed language. There are some situations that call for the flexibility of a dynamically typed language, and others where it makes sense to depend upon a statically typed one. I am more comfortable writing large scale enterprise applications in a statically typed language. However, when it comes to quick little scripts that I need to develop, and would probably never see production, it would make sense in those situations to write these programs in a more dynamically typed language. As a Java Developer, I am very interested in the Groovy language.

Groovy is a very interesting language because it was designed and developed to be compatible with Java. From many of the articles that I have read, you can change many of your *.java files and make it a *.groovy file and *presto* you have a groovy script. It also has the capability of leveraging your existing Java code base. Using Groovy, you can import any of your Java code, and leverage it in your Groovy script.

If you are interested in learning more, Guillaume Laforge has started a series of articles under the title "From Java to Groovy". Currently, there are two installments:

For something a little more advanced, please refer to John Carnell "Lightweight meets Heavyweight: Spring, Groovy and the Enterprise". In this article he demonstrates how to build a JMS message publisher and consumer using Groovy and Spring.

Enjoy.