Archive for the ‘Computers’ Category

Maven2, my first impressions

May 6, 2008
 by 

Maven has been a far departure from the usual Ant builds that I have become accustomed to. Now although Ant doesnt deal with dependency management like Maven does I have used Savant in conjunction which worked quite well. The constructs werent complicated, it was getting accustomed to Maven which required me to forget I know everything about Ant; oh yea, and a few days of pounding my head.


After the initial mental exercise I really got to liking it. It allowed me to easily handle multiple-module projects and all their dependencies. It also has great plugin support for anything you can think of, one of my favorite being Jetty (developing locally with Tomcat is for suckers!). I also really like the pom.xml settings as well as profile setting to handle different environments. Anyway, next time I start another project from scratch I will probably take another run with Maven.

PipedInputStream and PipedOutputStream with Java

April 10, 2008
 by 

Today I came across an interesting concurrency problem while deleting objects from the Social Graph (Semantic Web remember?). I have been tasked with mass deletes throughout our system, including exporting the objects in case they ever need to be reassembled again. Since our graph is so large, and we could potentially be deleting 10′s of thousands of triples at a time, the serialized XML would be about 10 times that many lines per triple represented in a file. In order to write the output to a file as fast as can be there was no need to store the serialized XML in memory. The best thing to do was to pipe the output stream to our binary store.


Now in order to do this, I need two threads, one to write and one to read. If you were to do this with one thread you would most likely run into a nasty deadlock situation. Anyway, here’s what I came up with:


public InputStream openStream() throws IOException {
    final PipedOutputStream out = new PipedOutputStream();

    Runnable exporter = new Runnable() {
        public void run() {
            tupleTransformer.asXML( tuples, out );
            IOUtils.closeQuietly( out );
        }
    };

    executor.submit( exporter );

    return new PipedInputStream( out );
}


Can anyone see the problem? Well unfortunately I couldnt either for over an hour. My unit tests would pass sometimes and fail others which led me to believe I was dealing with a timing issue. Turns out, sometimes the PipedOutputStream was completed before the PipedInputStream was even instantiated, completely missing the stream of the out.close(). The trick was to instantiate the two streams, in and out, at the same time then start the output with another thread. Problem solved. Here is what the finished product looks like:


public InputStream openStream() throws IOException {
    final PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream( out );

    Runnable exporter = new Runnable() {
        public void run() {
            tupleTransformer.asXML( tuples, out );
            IOUtils.closeQuietly( out );
        }
    };

    executor.submit( exporter );

    return in;
}

Ajax and IE 7: cache not invalidating

March 21, 2008
 by 

For the past day or two I had been struggling at work to figure out why Internet Explore 7 would not pay attention to the response headers stating not to cache the response. First, I tried setting the date header to expire instantly, with a value of -1. QA confirmed that this solved the problem in some revisions of IE 7 but not all. After digging around the web it turns out that you have to set a few more headers, one of which I had never even heard of. Here’s what solved the problem for me. This snippet is in Java but could apply to any language:

response.setDateHeader( "Expires", -1 );
response.setHeader( "Cache-Control", "private" );
response.setHeader( "Last-Modified", new Date().toString() );
response.setHeader( "Pragma", "no-cache" );
response.setHeader( "Cache-Control", "no-store, 
                                 no-cache, must-revalidate" );


IE, ImageIO, and Microsoft’s ‘image/pjpeg’ prank

February 28, 2008
 by 

Today I came across an issue at work regarding the made up MIME type of ‘image/pjpeg’ while uploading images from IE 7. Java’s ImageIO was choking while trying to upload this type of file because it is not in the list of supported MIME types. The supported MIME type list can be viewed by invoking ImageIO.getReaderMIMETypes(). After reading many blogs of others pounding their head, making fun of Microsoft, and using other solutions like ImageMagick, I found a solution. Add this to your list of supported types and move on. This format is made up and will not cause any problems. Pass the input stream onto your File IO and be done with it. Microsoft owes me half an hour of my life back.

Letter to the editor published in the Roundel

July 23, 2007
 by 

I recently wrote a letter to the editor of the Roundel Magazine, published by the BMW Car Club Of America, about my friend JP Cadoux at A1 Imports Autoworks in an effort to get the word out about what he does. I am kinda surprised they actually published considering I wrote in a serious manner but frustrated after reading silly articles about carbon fiber, xenon headlights, and other useless do-dads. Nevertheless, here it is.


article-about-jp-in-the-rou.gif

Dynamic Proxies in Java

July 13, 2007
 by 

Up until recently I never had to write any code that dealt with reflection. Before now I worked for CNET which is basically a series of differnent CMS’ with one request, one response. My first week at the new job required me to build a JMS system that would accept method invocations across the wire seamlessly for the producer. Java.lang.reflect was the answer.


Once you can wrap your head around the idea its pretty simple to write. Essentially what we are attempting to do is shield the caller from having to know anything about the other method that is going to invoked behind the scenes.


public Class MyConcreteProxyClass implements InvocationHandler {

   Object obj;
   public MyDynamicProxyClass(Object obj)
     { this.obj = obj; }

  public Object invoke(Object proxy, Method m, Object[] args)
         throws Throwable {
     try {
        // do stuff
     } catch (Exception e) {
        throw e;
     }
        // return something
     }
}

Now we have to create the proxy interface which the caller will implement. Any of the methods of this class that he implements and fire will invoke method on the MyConcreteProxyClass object.


public interface ProxyInterface{
    public Object myMethod();
}


Now its time to create wire everything up. There are several ways to do this which help hide this proxying like a static factory method but this simply demonstrates how this works.


ProxyInterface proxy = (ProxyInterface)
Proxy.newProxyInstance(obj.getClass().getClassLoader(),
         Class[] { ProxyInterface.class },
          new MyConcreteProxyClass(obj));

More press for A1 Imports

July 10, 2007
 by 

In an effort to the use social media to gain more publicity for the fantastic work that JP does we created this video and posted it on YouTube. Within the first day it recieved a few thousand views and was honored for two weeks on the site. It generated a lot of buzz on the Internet and was picked up by Jalopnik.com due to an anonymous tipster (myself). Here is a link to the post.

Once a URL goes live plan to maintain it forever

June 5, 2007
 by 

Yes it may sound crazy but once a URL goes live you must maintain it… forever! (if SEO is your thing that is). Now that isn’t to say that you shouldn’t change or remove some URL’s, just make sure you create an Apache rewrite rule utilizing a 301 redirect. I deal with this on a monthly basis at work and its not so bad as long as you track your URL movements. Rewrites are an SEO’s best friend. Here is a good way to check to see if a Google has any records of a given string in your URLs:


http://www.google.com/search?q=site:johnclarkemills.com+inurl:seo
http://www.google.com/search?q=site:johnclarkemills.com+inurl:2002

Fizz Buzz: Software engineering interview question

May 31, 2007
 by 

Having never heard of this before, it came as a shock. Its so simple that seems almost like a trick but its really not. I actually do like the question for that reason. Sometimes in the real world solutions are that simple as its a good trait to be able to recognize it. I answered the question well (or so I think), but I didnt pick up the simple refactor. If I was coding in front a computer I would have seen it in about 5 minutes but its different when you’re on a whiteboard in front of two senior architects. So heres the question:


Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.


So here’s what I came up with (in pseduo code):


for ( int i = 1 ; i <= 100; i ++ ) {
   if (i % 15 == 0 ) print "FizzBuzz";
   elseif (i % 3 == 0 ) print "Fizz";
   elseif (i % 5 == 0 ) print "Buzz";
   else print i;
}


So? Do you see it? Do you see the 'refactor' if it were (ignoring the ugly switch, design patterns, or thread safety for that matter)? Although the change is small change it does make a difference. Keep in mind this is pseudo code but they wanted me to see that if I cant print "Fizz" and "Buzz" in two separate 'if' blocks.


for ( int i = 1 ; i <= 100; i ++ ) {
   if (i % 3 == 0 ) print "Fizz";
   if (i % 5 == 0 ) print "Buzz";

   otherwise print i;
}

Java Concurrency in Practice

May 25, 2007
 by 

java-concurrency.gifThis is now one of my favorite books on Java which I am probably going to read again just to be sure I have soaked up as much information as I can. This is a practical book written by a practical person who understands his audience, engineers. He is straight to the point with his code examples and doesnt bore you to death explaining every little detail or referencing lines of code and functions 8 pages back. I wish more technical writers would be this concise and straight to the point. There is something to be said for any technical writer who can get their message across with fewer words. No where else can I think of a better example of the old adage “less is more”. I would highly recommend this book to any engineer, especially associates.


Copyright © 2005-2011 John Clarke Mills

Wordpress theme is open source and available on github.