How to order a pizza

July 28th, 2010 Patrick No comments

I have had the experience the past couple of weeks of being invited to lunches where pizza was ordered for large groups. I really only enjoy meat on my pizza, and its us meat eaters that get shafted when someone else does the ordering. People who like other pizza such as supremes and Hawaiians typically also like just the meat pizza. When a order is placed I will typically say that I would like a pepperoni and they get one and the same will be done for the supremes and other specialty pizzas. The problem with this is that the specialty eater will take at least one slice on average from the pepperoni and the meat eater will not take any slices from the specialty pizza. When you are talking an average of 3 slices per person and 8 slices a pizza that means at most that single pepperoni can feed one meat eater and five specialty eaters now if you have two meat eaters then you will need to order a second pepperoni. This equation can start to get complicated so I am introducing a new project of mine; it will be a site where all this math will be done for you. I just purchased the domain pizzaequation.com.

Categories: Uncategorized Tags:

Blogging at 78 MPH

July 3rd, 2010 Patrick 1 comment

Just before I left for my trip to Colorado I updated my nexus one to the Android 2.2 Froyo. One of the biggest features I was looking forward to was tethering. And what google was just take the existing WiFi radio and made it broadcast the  mobile data connection. So as I sit in the passenger seat doing 78 MPH on highway 70 I simply enabled the MiFi tethering feature set the phone down, picked up my laptop and started blogging. I find using the WiFi to tether to a mobile device a simple but creative solution that requires no software and drivers.

My Maven Pledge

June 14th, 2010 Patrick No comments

When we first started our JEE project a developer put together a quick ANT script that would checkout our code, build a couple of JAR’s, and bundle them into a EAR. We started to do some refactoring on our project structures, which included adding new projects, we quickly realized that we needed to modify our build process. The outcome of all refactoring was a common build project that was not included in our EAR. What this project contained was common build script, properties, and jar files for our application server.

Although this solution was a massive improvement over the previous build script, we are still having problems with project structures and build scripts. The idea was that we wanted anyone who had ANT can check out the projects and be able to build the entire project. This requires that all jars that are need to build the code to be checked into svn but not necessarily in the same project with the dependency. We are also running into problems with project structure, I generally think its a bad idea to check in IDE specific files. This became more true when most of our developers moved to Mac machines and upgraded to eclipse 3.5. We still had developers working on Windows XP using eclipse 3.3 and the IDE files were not compatible. This means that all developers have to maintain a document on how to properly build the complete project in their IDE. I would say the most frustrating problem is what works in the IDE might not work on the build script. This has caused many failed build in our continues integration environment due to either different versions of jars or using a jar that is in eclipse’s classpath but not in the build script.

Being well versed in ANT, I typically just default to ANT any time I need to build Java code. I am currently looking into Maven in a effort to find solutions to our current problems. I have attempted before to look at Maven but I feel as if the documentation is lacking the clarity that I was needing. I am going to start dedicating time to learning maven and documenting my progress. This may lead to some incorrect assumptions but I am more curious to see the evolution of my knowledge in Maven.

Using Java’s Proxy Class

June 1st, 2010 Patrick No comments

Java 1.3 introduced some handy new classes java.lang.reflect.Proxy and the java.lang.reflect.InvocationHandler; basically this allows you to dynamically create a proxy class that implements given set of interfaces and wrap this proxy class around a concret implementation of the class. It works by implementing the InvocationHandler interface which is called by the proxy prior to calling the wrapped class. Your implementation of the InvocationHandler can then choose to invoke the wrapped class, throw an exception, call another class, or anything else you may want it to do. At first it does not seem apparent why this is needed but they real power comes in building generic frameworks such as Hibernate and EJB3 application servers where offen times you are working with proxies instead of concreate implementations.

Recently I was asked to create a functional test to call a set of three web services for every member in our system. This was driven by the fact that we are integrating to a new system and there is a large data migration effort that is currently underway; the steak holders found it to be crucial that every member was migrated properly. The first thing that came to mind was how to deal with the large dataset, just under two million members, but the problems with dealing with datasets of this size have mostly been solved. So with no real problems facing me I was simple going to build a few very large loops and let the test run; but then I started to think about reusability.

Today we are calling three methods for all members, what about running one method on all accounts, or we can run all the plan methods against all our promocodes that have a free trial period. Since these are all plausible tests in not only production but within dev as well I wanted to make this as reusable and helpful as possible. Currently we have all our functional test calling web services clients that were generated by using JAX-WS. We have a ServiceFactory class that is used to create all web service clients for our functional test. So if you can wrap a proxy around these web services you can then monitor and record any method call you like in detail.

To start out I first I created a Recorder interface that would provide the contract on how record a service call.

public interface Recorder {
  public void record(Method method, Object[] params, Object returned);
  public void record(Method method, Object[] params, Throwable thrown);
}

Since we are only wanting to track a few service calls and not all the methods the service provides I created an abstract class for connivence.

abstract public class AbstractRecorder implements Recorder {

  List<Method> recordingMethods = new ArrayList<Method>();


  public void add(Method method) {
    recordingMethods.add(method);
  }

  public void add(Class clazz, String method, Class... params)
      throws SecurityException, NoSuchMethodException {
    add(clazz.getMethod(method, params));
  }

  public void add(Class clazz) {
    for (Method method : clazz.getMethods()) {
      recordingMethods.add(method);
    }
  }

  public boolean isRecording(Method method) {
    return recordingMethods.contains(method);
  }
}

Now that we have a contract for recording methods we can now implement the InvocationHandler interface and fulfill the contact for the proxy.

public class ServiceInvocationHandler<I> implements InvocationHandler {
  private final Recorder recorder;
  private final I service;

  private ServiceInvocationHandler(I service, Recorder recorder) {
    this.service = service;
    this.recorder = recorder;
  }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    Object returned;
    try {
      returned = method.invoke(service, args);
    } catch (Throwable t) {
      recorder.record(method, args, t);
      throw t;
    }
    recorder.record(method, args, returned);
    return returned;
  }

  public static <I> I createInvocationHandler(I service, Recorder recorder) {
    InvocationHandler hanlder = new ServiceInvocationHandler<I>(service, recorder);
    return (I) Proxy.newProxyInstance(service.getClass().getClassLoader(),
        new Class[] { service.getClass() }, hanlder);
  }
}

The object stores an instance of the concert class that is being wrapped and an instance of the recorder that will be used to record the service call. We will use the createInvocationHandler method to create proxy that wraps the service and also to create the InvocationHandler. When creating the proxy using newProxyInstance the first parameter is the classloader for the proxy. The second parameter is and array of interfaces you want the proxy to intercept, this means you can only intercept and wrap public methods since interfaces can only have public methods. The third paramater is the concreate class you would like to wrap.

For the most part I am ready to use my proxy. I can simply create a new service using my ServiceFactory and call createInvocationHandler which will wrap the service. But I wanted to place another layer of abstraction on creating the recordable service so any developer that needs to record service calls need to simple create a class that implements Recorder.

public class ServiceRecorderFactory {
  private final Recorder recorder;

  public ServiceRecorderFactory(Recorder recorder) {
    this.recorder = recorder;
  }

  public <I> I get(I serviceInterface) {
    I service = ServiceFactory.get(serviceInterface);
    return ServiceInvocationHandler.createInvocationHandler(service, recorder);
  }
}

When creating a ServiceRecorderFactory they would simply provide the Recorder you implemented and then they can request services just like before. It will simply call the normal ServiceFactory to create the service, create a proxy that wraps the service, then returns the proxy. Here is an example of it in use.

public class TestMember {
  public static void main(String[] args) {
    JdbcRecorder recorder = new JdbcRecorder();
    recorder.add(MemberService.class, "getMember", Long.class);
    ServiceRecorderFactory serviceFactory = new ServiceRecorderFactory(recorder);
   
    MemberService memberService = serviceFactory.get(MemberService.class);
   
    Long[] members = new Long[]{1L, 2L, 4L, 5L, 6L};
    for(Long memberId : members){
      memberService.getMember(memberId);  //will record
      memberService.getAddress(memberId); //will not record
    }
  }
}
Categories: Java Tags: , ,

Getting what I paid for from 1&1

May 27th, 2010 Patrick No comments

I do all my own web hosting on a dedicated server and control all my own DNS setting so for the most part I never cared to much on who I purchased my domains from. When I first started purchasing domains, way back when I added one image to a website and I thought I was the best designer ever, it was from Network Solutions. Since I have always provided my own hosting paying that much for a domain did not make much sense. So I started purchasing domains through godaddy.com for $10.00 a domain. Then a friend had me do some work on his site and he was getting hosting through 1&1. Since they were 10% cheaper, yeah its only a buck, I thought it would be a good idea to move all my domains to them. When I moved this domain [monekyoncode.com] to 1&1 to start this blog, I thought what can go wrong because I do my domain lookups and hosting. Well they still set up the DNS to forward lookups to your DNS server and apparently they screwed that up. The best thing about this is after only having my blog up for 3 days it was down for close to a month as they continually  just reset the domain and it would fail time and time again. So what leverage do I have over them, do you tell them “Fix it or I am taking my $9.00 someplace else.” Well as you can see the blog is back up but I am still unable to do my own DNS lookups.

Categories: Tech Rants Tags: