Thursday, October 22, 2009

VarArgs in Java 5

I know this has been around for a while.  I’ve seen it, but didn’t really know what it was.  Turns out its a short hand for using arrays as an argument to a method.

If you are creating a method that takes variable arguments, it is common to supply the arguments in some sort of array.  Take for instance the MessageFormat type classes:

MessageFormat.format(String format, Object[] args);

In order to use this method, the caller would have to make the call something like:

MessageFormat.format(“Hello {0}, isn’t life {1}”, new Object[]{“world”,”swell”});

VarArgs allows you to shorthand this by declaring your method like:

MessageFormat.format(String format, Object… args);

Which can then be called like:

MessageFormat.format(“Hello {0}, isn’t life {1}”, “world”, “swell”);

The only stipulation is that your vararg argument is the last in the parameter list.  Java then autoboxes your arguments into an Object array for you.


Thursday, August 20, 2009

Why Test Driven Bug Fixing is Better #tdd #struts

Its often difficult to convince developers that aren’t doing Test Driven Development (TDD) that its worth the little bit of extra effort to write failing tests first before touching the code.  Most times, the dissenters complain about tight timelines and lack of end user benefit to ‘be bothered’ with it.

Although I am relatively new to TDD, I’ve adopted the method in every aspect of code development I can think of…especially bug fixing.

A Case Study

This is an actual example where I used TDD to fix a bug (the names have been changed to protect the innocent).

QA reports a bug in newly developed software.  In this case, the application is a struts web application and there is a radio button on a form that doesn’t have a default set; the analyst believes that it should be set to value ‘x’ by default.

The radio button is backed by a form bean class with the property ‘theRadioOption’ in the class MyFormBean which is scoped to the request for the struts action in question.

public class MyFormBean extends ActionForm
	private string theRadioOption;
	public String getTheRadioOption(){
		return theRadioOption;
	public void setTheRadioOption(String value){
		theRadioOption = value;

Given this snapshot of information, many experienced struts developers would say “OK, so I’ll go and set the default value in the bean…its one line of code, no need for a test.”

Before touching the code however, I created a test by extending MockStrutsTestCase and coded a very simple test:

public class FormActionTest extends MockStrutsTestCase
	public void testFormDefaults()
		final MyFormBean form = (MyFormBean) getActionForm();
		assertEquals("Wrong Default","x", form.getTheRadioOption());

Running the test, of course, it fails…perfect…now to fix the code.

private string theRadioOption = "x";

There, that was easy, now before we build the application and deploy the thing, I run the test again to see if it indeed passes.  Surprisingly, it fails…so much for my “one line of code.”

Upon further examination, I looked in the action class that forwards to the web page.  What I found was that the developer that made the form was explicitly clearing the values in the form by setting the property to null.  My little ‘fix’ wasn’t seeing the light of day because the action was ‘undoing’ my default.

public ActionForward execute(ActionMapping mapping, ActionForm form, ...etc){
	final MyActionform myForm = (MyActionForm) form;
	return mapping.findForward("SHOWFORM");

The real fix is to get rid of the explicit set to null in the action class or set it to the desired default instead of null.  After setting the default in the action and running my test again I can then see that the test passes.

This test becomes part of the project now and is run during the build process every night…the issue will never return undetected.  How much time did I save here?  Developer builds and deploys only once instead of two or three times, QA didn’t have to re-open the bug and therefore test the feature three times instead of two, the next developer didn’t have to ask “what should this be set to by default anyway” since it is well documented in the test.

My question is how can you justify NOT putting in the little extra effort to do Test Driven Bug Fixing?

Friday, August 7, 2009

The Puf Principle: Online burndown chart generator

Simple but handy… this little script creates a burndown chart for your sprint.

The Puf Principle: Online burndown chart generator

Thursday, August 6, 2009

Tiles Enabling your Struts Project

Tiles is a tag library framework that allows you to create templates for your JSP pages and greatly simplifies the creation of Web front ends and overall maintainability of the view layer in a JEE project.

Tiles is an Apache project, not a struts project, so it can be used with other frameworks as well.  Our project is based on Struts so I’ll limit my post to how we integrate tiles with struts.  If you want more information, I encourage you to check out the project website at the apache foundation where you can download the library, see examples and read documentation to your hearts content.

There are a number of ways to use tiles, we typically use a separate tiles definition xml file to define our screens and use the Tiles Plug-in in the struts-config file.

Struts comes bundled with Tiles, so if you are using struts, you already have what you need to get started.

The Request Processor

Since tiles basically intercepts forward processing in struts, you have to use an extension of the struts request processor class that handles this.  This is easy to do in your struts configuration file:

<?xml version="1.0"?>

This request processor class deals with parsing the tiles configuration and putting all the pieces together, it intercepts the ‘findForward’ method to see if the forward is a tile definition or if it is just a regular forward.  You can mix tiles with simple forwards in the same implementation of struts, for instance <forward name=”success” path=”/WEB-INF/jsp/SuccessPage.jsp” /> and <forward name=”failure” path=”MyFailureTileDef” /> works just fine.

The Plug-in

The other part to configure in struts-config.xml is the Tiles plug-in which basically allows you to configure tiles for each struts module and initializes the factory for the module.

<?xml version="1.0"?>
  <plug-in className="org.apache.struts.tiles.TilesPlugin">
    <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>

This is where you can set some of the properties of the tiles request processor.  Once you have this in place, you’re all set to begin creating tiles.  In this example, I set the location of the tiles definition file that I want to use in this module.  Since this is the default location, it isn’t required, but in case you want to use a different file, or more likely, multiple definition files, this is how you do it. (comma separate multiple file names).

The Template

Tiles is based on JSP files that are put together at run time.  Note that I said JSP files and not JSPF files.  I had some trouble initially when I tried using tiles, thinking it would be good to call the template files JSP files and the pieces would be JSPF…this didn’t work and switched to using JSP for everything and just organized using folders.

Every tile has a template file, the template then uses the tiles tag library to insert page components using the configuration file which we’ll discuss shortly.  Here is a simple template:

<%@ page %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
  <title>Simple Tile</title>
  Page Header
  <hr />
  <tiles:insert attribute="body" />
  <hr />
  Page Footer

as you can see, the template just defines a nice little placeholder for an attribute called ‘body’ which is referenced from the configuration file.

The Configuration File

A simple tiles definition looks something like this:

<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" "">
  <definition name="simpletile" page="/WEB-INF/jsp/layout.jsp">
    <put name="body" value="/WEB-INF/jsp/Simple.jsp" />

This simple definition takes your layout.jsp file and inserts a small Simple.jsp in the body marker.  This file isn’t a complete HTML page, in fact it is likely just a table or a paragraph or something that gets inserted in.  It makes life a whole lot easier when you want to change the navigation menu, or the header or something, you don’t have to change every file…just the layout file.

Where the definitions get interesting though is if you have a hierarchy of page ‘types’ that you want to configure, consider this definition file:

<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" "">
  <definition name="BaseDef" path="/WEB-INF/jsp/layout.jsp">
    <put name="header" value="/WEB-INF/jsp/PageHeader.jsp" />
    <put name="footer" value="/WEB-INF/jsp/PageFooter.jsp" />
    <put name="menu" value="/WEB-INF/jsp/Blank.jsp" />
    <put name="body" value="/WEB-INF/jsp/Blank.jsp" />
  <definition name="ClientPage" extends="BaseDef">
    <put name="menu" value="/WEB-INF/jsp/ClientMenu.jsp" />
  <definition name="SupplierPage" extends="BaseDef">
    <put name="menu" value="/WEB-INF/jsp/SupplierMenu.jsp" />
  <definition name="ClientOrderForm" extends="ClientPage">
    <put name="body" value="/WEB-INF/jsp/OrderForm.jsp" />

This definition takes the basic page layout that is common to all pages in the “BaseDef” definition and puts in the header and footer components, presumably, the layout.jsp in this definition has an insert for “header”, “footer”, “menu” and “body” such as:

<%@ page %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<title>My WebSite</title>
  <tiles:insert attribute="header" />
  <tiles:insert attribute="menu" />
  <tiles:insert attribute="body" />
  <tiles:insert attribute="footer" />

Once I have all the high level parts such as the menu, header and footer coded, if I want to add an order form, I simply create a definition that extends the high level page type and then insert my body using the <put> element.


There are lots of ways to use tiles, this is just they way that I have always used it.  It provides me with a mechanism to quickly generate the view layer and easily maintain the look and feel of my entire application by reducing all of the html overhead to one layout page and simple component pages that I can easily change when needed.

Newer versions of tiles are available and all work basically the same way, but I know that some of the architecture has changed over several iterations.  If you know of newer, better ways to use tiles, I invite you to comment and share.

Thursday, July 16, 2009

Getting Started with MockEJB

I spent a little time recently trying to get a lightweight framework for some basic EJB unit testing with the goal in mind to reduce the number of times that I had to deploy to the application server to test new code.  I had been using MockRunner for a while to test tag classes, so I thought I would try to use the EJB component which uses MockEJB as a base for testing.

The hardest part of the whole thing was really figuring out how to get the finders to work, but as usual, I was likely making it a bit harder than I had to, this is how I did it.


MockRunner provides a base class for doing an EJB Test Case called EJBTestCaseAdapter, the setUp() method does pretty much everything to do with setting up your context and creating the basic environment, all you have to do is deploy your EJB classes.  The base class has methods for deploying both entity and session interfaces, but I found that the deploy(BasicEjbDescriptor) method worked best for me, giving me the flexibility to deploy local and remote interfaces from multiple packages.  The deploySession and deployEntity methods the the MockRunner class just provide a wrapper for the deploy method to reduce some of the complexity.  So as long as you don’t have a complicated project, they should work fine for you.

I extended this test case adapter class to make my life a little easier and implemented my own deploy methods since my entity and session classes are in different packages.  This just makes life a little easier because now I just have to use deployMyEntity(“Tblwhatever”):

  1: protected void deployMyEntity(String name) throws Exception
  2: {
  3:     final String interfaceName = "my.entity.package." + name;
  4:     ejbModule.deploy( new EntityBeanDescriptor(
  5:         "java:comp/env/ejb/local/" + name,
  6:         Class.forName(interfaceName + "LocalHome"),
  7:         Class.forName(interfaceName + "Local"),
  8:         Class.forName(interfaceName + "Bean")));
  9: }

Sessions are a little bit more complicated because our naming convention for Remote and Local Interfaces are a bit different:

  1: protected void deployMySession(String name, boolean local) throws Exception
  2: {
  3:     final String myPackage = "my.session.package.";
  4:     final String interfaceName = myPackage + name;
  5:     final String jndiName = name;
  7:     if ( local )
  8:     {
  9:         jndiName = "java:comp/env/ejb/local/" + name;
 10:         interfaceName = interfaceName + "Local";
 11:     }
 13:     ejbModule.deploy( new SessionBeanDescriptor(
 14:         jndiName,
 15:         Class.forName(interfaceName + "Home"),
 16:         Class.forName(interfaceName),
 17:         Class.forName(myPackage + name + "Bean")), TransactionPolicy.REQUIRED);
 18: }

Both of these classes make reference to an ejbModule which are provided by a method in the EJBTestCase Adapter.  I set this up in my setUp() method:

  1: protected final EJBTestModule ejbModule;
  2: protected final JDBCTestModule jdbcModule;
  4: @Override
  5: public void setUp() throws Exception
  6: {
  7:     super.setUp();
  9:     jdbcModule = createJDBCTestModule();
 10:     ejbModule = createEJBTestModule();
 11:     ejbModule.bindToContext("jdbc/MyDS", 
 12:         getJDBCMockObjectFactory().getMockDataSource());
 13: }

Creating a Test

My TestCase simply extends this class where I deploy the sessions and entities that I will need during my test.  For simplicity, I have a simple entity that has an id field that I want to update

  1: public class MyTest extends MyEJBTestCaseAdapter
  2: {
  3:   @Override
  4:   public void setUp() throws Exception
  5:   {
  6:     super.setUp();
  8:     deployMySession("MySessionEJB", false);
  9:     deployMyEntity("Tblsomething");
 10:   }
 12:   public void testSomething() throws Exception
 13:   {
 14:     final MySessionEJB session = EjbHomeFactory.lookup("MySessionEJB");
 15:     session.updateSomething(1L);
 17:     final Tblsomething t = findByPrimaryKey(1L);
 18:     assertEquals(1L, t.getId());
 19:   }
 20: }

Dealing with Aspects

Assuming that your session just creates the Tblsomething entity, you likely will not have a problem running this test.  But if you are looking up an entity, you will have to implement an Aspect that will intercept your finder and return something for you to play with.  Here is a simple Aspect that intercepts a findByPrimaryKey method and returns a new entity that is probably suitable for a simple test:

  1: class FinderHandler implements Aspect
  2: {
  3:     FinderHandler(String entity, String finderMethod, Object[] createParams, Object pk)
  4:     {
  5: 	targetEntity = entity;
  6: 	targetFinder = finderMethod;
  7: 	entityToReturn = 
  8:           createEntityBean("java:comp/env/ejb/local/" + entity, 
  9:              createParams, pk);
 10:     }
 12:     private final String targetEntity;
 13:     private final String targetFinder;
 14:     private final Object entityToReturn;
 16:     public Pointcut getPointcut()
 17:     {
 18: 	return new MethodPatternPointcut(
 19:                targetEntity + ".*" + targetFinder);
 20:     }
 22:     public void intercept(InvocationContext invocationContext)
 23:     {
 24: 	invocationContext.setReturnObject(entityToReturn);
 25:     }
 26: }

This aspect class will basically create an entity that you define using the parameters of your create method as an Object[].  The EJBTestCaseAdapter class provides this handy utility for creating an entity by looking up the class and using Reflection to find the create method and invoking it and then persisting the entity in an in memory data source that can then be retrieved using the findByPrimaryKey(Object) method in your test case.


Obviously, this kind of test does nothing to attempt to test the actual EJB platform with all of its intricate dependencies and configurations, but it does allow a developer to execute EJB code on their development workstations and apply TDD principles when developing EJB code.

Thursday, July 9, 2009

Creating Testable Web UI Components

Until recently, I always thought that the only way to test the UI components in an application was to create an end-to-end test using some kind of robot user.  The reason being is that much of the UI is created using JSP pages or custom tags…these are sometimes hard to run separately outside the application, so we’ve created tests for them before but running front end tests and hoping for good results.

Since this is painful to do any detailed testing on, we generally resort to good old human testing in our QA department to make sure that the UI comes out right…obviously a bad practice.

This article goes through a case study on how to create a testable UI component using a custom tag and shows the various tests I use to validate the code.

The Tag Class

If you look at some of my old code for tag classes, you end up with some very complicated presentation logic to try and follow.  My thought on this at the time was to put the complicated logic in a tag class instead of using struts logic tags and scriptlets in my JSP page.  Although using a tag class is preferred to this, I now view my tag class as simply being the ‘glue’ between the data that I am trying to present and how it is presented.

What I mean by this is that, the tag class should really be used for getting a reference to the data that you want to display and passing it to the components which render the output and then passing that output to the browser.  The actual tag reference is just a place holder to get that output in the right spot on the page.  This model makes for cleaner, easily testable code.  When you want to test the functionality of a tag class, you simply need to write a test that validates that when you pass a reference to the tag, it can find the data it needs and render an output, you don’t have to try and test all of the possible output issues involved.

The Renderer

Our application uses Struts, so a renderer is not technically part of the framework at this point as it is in say JSF.  The concept is that of a component that can convert a data structure into some form of visual representation, as in say HTML.  I use the term ‘renderer’ to refer to any software component that does this type of translation.

I usually start with a renderer interface since it is likely that I may need to render this component in one of many ways.   I pass off the responsibility of selecting the appropriate renderer to the tag class this way I can make configurable outputs.  The renderer is specific enough to be able to perform simple presentation logic, this makes the testing of a renderer component fairly straight forward.

Consider this interface:

  1: public interface ClientRenderer
  2: {
  3:     String output(Client client);
  4: }

I love using interfaces in this case because I don’t have to worry about what my output will look like to get it all wired together.  I can make a very simple implementation that will just output the clients name for starters

  1: public class SimpleClientRenderer
  2: 	implements ClientRenderer
  3: {
  4: 	public String output(Client client)
  5: 	{
  6: 		return client.getOrganizationName();
  7: 	}
  8: }

A test for this class is very simple to do and doesn’t require that you use a whole bunch of mock framework classes:

  1: public class SimpleClientRendererTest
  2: 	extends TestCase
  3: {
  4: 	public void testOutput() throws Exception
  5: 	{
  6: 		final Client cl = new Client();
  7: 		cl.setOraganizationName("testme");
  9: 		final ClientRenderer r = new SimpleClientRenderer();
 10: 		final String out = r.output(cl);
 12: 		assertEquals("Unexpected Output", "testme", out);
 13: 	}
 14: }

Obviously, this is a very simple example that would likely be handled with a bean:write tag, but the idea is that this renderer class doesn’t have any framework dependencies to test.  It is simply a class that outputs in a predictable (and therefore testable) manner.

Testing the Tag Class

JSP Tag classes run within a framework and require references to a pageContext, a request and a response.  I use MockRunner for testing tag classes since they can run within the context of my build server and don’t have to be deployed anywhere.

Since I’ve already tested the fact that my client will render the correct way, all I need to test now is that I can get the correct reference to my renderer.

  1: public class ClientOutputTag
  2: 	extends TagSupport
  3: {
  4: 	private Client clientRef;
  5: 	protected String attribute;
  7: 	public void doStartTag() throws JspException
  8: 	{
  9: 		... // get client ref from whereever
 10: 	}
 12: 	public void doEndTag() throws JspException
 13: 	{
 14: 		final ClientRenderer r = new SimpleClientRenderer();
 15: 		final String out = r.output(clientRef);
 17: 		try
 18: 		{
 19: 			pageContext.getOut().write(out);
 20: 		}
 21: 		catch(IOException e)
 22: 		{
 23: 			// error
 24: 		}
 25: 	}
 27: 	... // req'd accessors
 28: }

As you can see by this implementation, the tag class is responsible only for getting the reference and calling the renderer, you test would look something like:

  1: public class ClientOutputTagTest
  2: 	extends BasicTagTestCaseAdapter
  3: {
  4: 	public void testTag() throws Exception
  5: 	{
  6: 		final Client cl = new Client();
  7: 		cl.setOrganization("testme");
  8: 		final ClientOutputTag tag = 
  9: 			(ClientOutputTag) createTag(ClientOutputTag.class);
 11: 		tag.setAttribute("client");
 12: 		setRequestAttribute("client", cl);
 14: 		tag.doStartTag();
 15: 		tag.doEndTag();
 17: 		final String out = getOutput();
 19: 		assertEquals("Unexpected Output", "testme", out);
 20: 	}
 21: }

This is a simple example, so there isn’t much to test here.  I may want to test to make sure that if I’m outputting html, that it will be valid or something like that; I would use a sax parser for that.

Wednesday, July 8, 2009

Why are We Embarrassed to Admit That We Don’t Know How to Write Tests? | Javalobby

I found this excellent article today on writing testable code.  I find one of the big problems in our organization is that we know we need to test code, and it sounds like a simple thing to do, but it is difficult thing to get your head around.

This article talks about making your code testable.  It is important to differentiate between writing unit tests and writing end-to-end tests.  An end to end test is a test that acts like the user for your application, and it is good for finding wiring bugs.  Unit tests on the other hand, handle much simpler form of bug and a more common form of bug.

I recommend this article for all developers were not currently writing unit tests for all of their code.

Why are We Embarrassed to Admit That We Don’t Know How to Write Tests? | Javalobby

Tuesday, June 9, 2009

Updating the User Interface

As a java developer, I’ve found the best way to actually create software is to create functionality from the software point of view.  Too often, I see software that has been built from the User Interface perspective which creates a dependency on the UI for the software.  Dependency on the UI is generally a bad thing because it makes it hard to test, hard to change and generally hard to understand.

I’ve made it a rule of mine to build software that does not depend on whether it is a web application, swing application, javafx application or any other environment specific application.  I also try to even limit my software’s dependency on where the model is stored.  I do this generally by creating an interface and more than likely a factory class to hide the implementation.  This way, I can code my software to utilize the interface through the controller, rather than starting with a specific implementation that will manipulate my software component and inevitably lock me into using the implementation forevermore.

Lets say for example that I have a software component that has a counter in it.  I know that I want the counter displayed to the user, but I’m not sure how yet.  I also want to stick to my rule about not depending on any specific display implementation.  If I follow the herd on this one, its likely that I would get a handle on my HttpServletRequest object, get the count and set it as an attribute and then create a tag to show it as an HTML element.  Personally, I’ve done this hundreds of times…the struts action get the count from the counter and puts it in the request or session.

But then what happens if I want to run my software in JavaFX?  I have to change the way my software works, which means that if I want to stay compatible with my current solution, I need to add a bunch of logic that says “If we are running in JavaFX do this…otherwise, do that.”  This idea is really fragile and likely to be riddled with defects.  Worse than that, I may re-implement the logic to run on another platform…this creates serious maintenance issues.

In another approach, lets say that I want my software to just update an appropriate display.  My software doesn’t need to know about what kind of display it is, so I just add a bit of abstraction to my code that allows me to decide later how I want my display updated and then carry on writing my counter code.  To do that, I create an interface:

  1: public interface CounterDisplay
  2: {
  3:    void updateDisplay(int value);
  4: }

OK.  That was easy, now I may use a factory to give this Little piece to my counter software, or I may just hard code it depending on how flexible I need to be.  The main thing is, that my software just knows that there is a counter display to deal with…it might be a JOptionPane message box, a JSF page or a JavaFX Scene.  How we update the display isn’t important to the software component.

  1: public void incrementCounter(CounterDisplay display)
  2: {
  3:   counter++;
  4:   display.updateDisplay(counter);
  5: }

In a struts application, I may have my CounterDisplay as an action form.  In this case, I could simply implement my CounterDisplay interface on the action form and then allow struts to do its thing:

  1: public class MyForm extends ActionForm implements CounterDisplay
  2: {
  3:    private int counter;
  4:    ...
  5:    public int getCounter()
  6:    {
  7:       return counter;
  8:    }
 10:    public void updateDisplay(int value)
 11:    {
 12:       counter = value;
 13:    }
 14: }

This way, we separate the UI from the software and we can implement it anywhere.

Thursday, May 28, 2009

Creating a Custom Client Server Application in JAVA

I recently had a need to do something a little different from my usual J2EE thing that required me to do a little local networking directly between two clients.  I needed to split a task between two users without creating a bunch of infrastructure.

Since the task was a data entry task, we were using an applet to get some data from a web service, copy that data to the users clipboard, and then call a javascript function to open a web page where the user could paste in the data.  I wanted to split the task so that one user could get the data from the web service and then pass it to another user to do the javascript and pasting part.

If you read this, you will notice right off hand that there are a few things to get past here before you even get to do the client server part.  Number one is that if you are running an applet and want access to the clipboard, you first have to either modify the client policy to allow this, or sign the jar file…I opted to sign the jar file.  Secondly, if you want an applet to run javascript, you have to add the ‘mayscript’ parameter to your applet tag in your html page.  I’m not planning on covering these topics here, but I may blog about them later.

Java Networking

The Client

A client just has to create a new instance of a Socket which has a handle to an input and output stream.  All your client does is write to an InputStream and read the server response from the OutputStream.  This is really quite trivial to do by using a Reader and Writer class.  I used a PrintWriter to write to the server and a BufferedReader to read the response.  I use another BufferedReader to get an input from the command line:

  1: public class SimpleClient
  2: {
  3:     private final Socket clientSocket;
  4:     private enum ServerState{ READY, DONE };
  6:     public SimpleTransferClient(String server, int port) throws UnknownHostException, IOException
  7:     {
  8:         clientSocket = new Socket(server, port);
  9:     }
 11:     public void start()
 12:     {
 13:         PrintWriter out;
 14:         BufferedReader response;
 15:         BufferedReader commandLine;
 16:         ServerState state = ServerState.READY;
 18:         try
 19:         {
 20:             out = new PrintWriter(clientSocket.getOutputStream(), true);
 21:             response = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 22:             commandLine = new BufferedReader(new InputStreamReader(;
 24:             String message;
 26:             while( ( message = response.readLine() ) != null )
 27:             {
 28:                 state = TransferState.valueOf(message);
 30:                 switch(state)
 31:                 {
 32:                     case READY:
 33:                         out.println(commandLine.readLine());
 34:                         break;
 35:                     default:
 36:                         out.close();
 37:                         response.close();
 38:                         break;
 39:                 }
 40:             }
 42:             clientSocket.close();
 43:         }
 44:         catch (IOException e)
 45:         {
 46:             // noop
 47:         }
 48:     }
 49: }

Making this work involves creating an instance of the SimpleClient using the server ip address and port in the constructor. 

The Listener

Although a listener is not necessarily required, I thought that it would be better to launch a thread to handle the networking stuff and hand off the request to a server instance.  This way, I could allow multiple clients to connect simultaneously instead of in a queue.

The listener simply creates a ServerSocket and just accepts client requests.  Once it gets a client socket request, it just passes the socket off to a server thread and then goes back to listening for another client.  I took a simplistic approach to this, you may want to limit the number of processes, etc to ensure that you don’t get DDOS’d to death, but in my case, this wasn’t really an issue.

  1: final ServerSocket listener = new ServerSocket(4567);
  2: while(true)
  3: {
  4:     new ServerThread(listener.accept()).start();
  5: }

As I said, this isn’t fancy, but it sets up the service, loops forever and accepts clients as quickly as it can spin off a thread.  Since I was running this inside an applet and wanted some user interaction, I also put this in its own thread.  Obviously, depending on what you are trying to do, this may not be necessary, but this will work in any case.

  1: public final class Listener
  2: {
  3:     private static Thread listenerThread;
  5:     /**
  6:      * Static method to start the thread
  7:      */
  8:     public static void start()
  9:     {
 10:         listenerThread = new ListenerThread();
 11:         listenerThread.start();
 12:     }
 14:     /**
 15:      * Static method to stop the thread
 16:      */
 17:     public static void stop()
 18:     {
 19:         if ( listenerThread != null )
 20:             listenerThread.interrupt();
 21:     }
 23:     /**
 24:      * The actual thread class
 25:      */
 26:     private static class ListenerThread extends Thread
 27:     {
 28:         private boolean listening = false;
 29:         private ServerSocket listener = null;
 31:         public void run()
 32:         {
 33:             try
 34:             {
 35:                 listener = new ServerSocket(4567);
 36:                 listening = true;
 37:                 while(listening)
 38:                 {
 39:                     new ServerThread(listener.accept()).start();
 40:                     try
 41:                     {
 42:                         // be nice and sleep once in a while
 43:                         Thread.sleep(1000);
 44:                     }
 45:                     catch (InterruptedException e)
 46:                     {
 47:                         listening = false;
 48:                     }
 49:                 }
 50:                 listener.close();
 51:             }
 52:             catch (IOException e)
 53:             {
 54:                 listening = false;
 55:             }
 57:         }
 59:         public void interrupt()
 60:         {
 61:             try
 62:             {
 63:                 if ( listener != null )
 64:                     listener.close();
 65:             }
 66:             catch (IOException e)
 67:             {
 68:                 // noop
 69:             }
 71:             listening = false;
 72:         }
 73:     }
 74: }

So then firing up the listener would just be a static call to Listener.start();

The Server

In order to make sure that client requests don’t get queued up, we implement the actual server as a thread as well.  This way, when a client connects, the listener creates a new ServerThread and then goes back to listening.

The server is almost identical to the client in terms of code.  Both sides use a plain old Socket and read and write from an output stream.  I used a PrintWriter for my output and a BufferedReader for my input:

  1: public class ServerThread
  2: {
  3:     private final Socket clientSocket;
  5:     private enum ServerState{ READY, DONE };
  7:     public ServerThread(Socket socket)
  8:     {
  9:         super("ServerThread");
 10:         clientSocket = socket;
 11:     }
 13:     public void run()
 14:     {
 15:         PrintWriter output;
 16:         BufferedReader response;
 18:         try
 19:         {
 20:             output = new PrintWriter(clientSocket.getOutputStream(), true);
 21:             response = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 23:             ServerState state = ServerState.READY;
 24:             String message;
 25:             output.println(state.toString());
 27:             while(! state.equals(ServerState.DONE) && (message = response.readLine()) != null )
 28:             {
 29:                 if ( "quit".equalsIgnoreCase(message) )
 30:                     state = ServerState.DONE;
 32:                 output.println("You Said: " + message);
 33:                 output.println(state.toString());
 34:             }
 35:             output.println("Closing Connection...");
 36:             output.close();
 37:             response.close();
 38:             clientSocket.close();
 39:         }
 40:         catch (IOException e)
 41:         {
 42:             // noop
 43:         }
 44:     }
 45: }