Apr 10 14

Two recent “incidents” triggered my real interested in protecting whales and dolphins:

  • I watched The Cove, 2010 Academy Award winner for Best Documentary, a movie about the annual killing of dolphins in a National Park at Taji, Wakayama, in Japan
  • I stumbled upon online petitions against killing whales and dolphins on the Faroe Islands as a ritual to prove adult hood

Both left me somewhat shocked. Learning about how so called human beings slaughter animals without real need for food makes me want to puke! I hardly find words to describe my rage.

Some links concerning the Faroe Island killings:

Apr 10 05

When you need to support self-signed SSL certificates in your Apache HttpClient based application you can use the contributed EasySSLProtocolSocketFactory as described in the HttpClient docs.

Instead of using HttpClient’s HostConfiguration object directly you’d modify its protocol socket factory in your code like so:

...
if (config.isAllowSelfSignedCertificates()) {
  ProtocolSocketFactory factory = new EasySSLProtocolSocketFactory();
  try {
    URI uri = new URI(config.getBaseUrl());
    int port = uri.getPort();
    if (port == -1) {
      port = 443;
    }
    Protocol easyHttps = new Protocol(uri.getScheme(), factory, port);
    hostConfiguration.setHost(uri.getHost(), port, easyHttps);
  } catch (URISyntaxException e) {
    throw new IOException("could not parse URI " + config.getBaseUrl(), e);
  }
}
...

Somewhere you’d instantiate a HttpClient object. Then you get its host config through HttpClient#getHostConfiguration() – I stored this in the hostConfiguration variable. The if, the config variable, checking for port, etc. is all my own code and has nothing to do with HttpClient directly.

There is one caveat, though! Never use absolute URIs against the HttpClient 3.x with the EasySSLProtocolSocketFactory in place! If you did you’d get the dreaded

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

I found that the HttpClient 3.x has the following code in its executeMethod() method:

...
if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
  // make a deep copy of the host defaults
  hostconfig = (HostConfiguration) hostconfig.clone();
  if (uri.isAbsoluteURI()) {
      hostconfig.setHost(uri);
  }
}
...

So, in my case (absolute URI) the modified host config (uses EasySSLProtocolSocketFactory) is cloned.

Problem: since the so called “deep copy” isn’t a proper deep copy the copy’s protocol’s socket factory is no longer EasySSLProtocolSocketFactory but the standard SSLProtocolSocketFactory instead!

Update

Apparently, this behavior is “well known and documented”. Since I couldn’t find anything in the HttpClient 3.x documentation I tried Google again and made note of the following JIRA issues and one particular mailing list entry:

Please note that HttpClient 4.x does not have this limitation!

Mar 10 07
A list of Japanese restaurants in Zurich (“Japanische Restaurants in Zürich” for our German-speaking friends).
  • Samurai
    If you judge a Japanese restaurants by the number of Japanese who frequent the place this was the clear favorite a few years ago. Samurai was considered an in-sider tip back then. Located next to a striptease club in one of the red-light areas it wasn’t mainstream yet. Since then the number of Japanese guests in the restaurant has declined since the word has spread that you get reasonably price truly authentic sushi and sashimi – most guests are Europeans nowadays.
    My wife and I think that the quality of the food and the care with which the components are arranged on the plates have slightly decreased over the years. However, we can still highly recommend it.
    Reservation almost required for dinner, the place is always full.
  • Sala of Tokyo
    The food is good and if you manage to get a table in the quite back room (ask for it if you make a reservation) the atmosphere is nice. It’s a little on the pricy side but it’s justified for many dishes. We wouldn’t recommend paying the extra surcharge to have wagyū meat instead of the regular beef. It’s not worth it.
    We didn’t like many of the rich snobbish other guests. The Swiss owner lacked Japanese hospitality.
  • Fujiya
    Next on our list.
  • Yooji’s
    Certainly not the most authentic place in Zurich but the sushi is ok. If you go with a group of four we’d recommend to get the big sushi plate for all to share. Friendly staff.
  • ISHI
    Too stylish and modern to be authentic. Since they don’t even have Japanese staff we never bothered trying. Heard some horrible stories from friends.
  • oKara
  • isakaYa
  • Ginger
  • Takano
    Open-air sukiyaki on the  terrace apparently -> we have to try in summer.
  • Ooki
    Ramen shop close to Stauffacher. Tiny restaurant – have dinner early or make a reservation. The place has its own charm, definitely worth a try. The menu is small but the food is really delicious. A few types of ramen and gyoza (must have), that’s all.
  • Blue Note
    No website (domain parking)
    Stockerstrasse 45
    8002 Zürich
    Tel: +41 44 202 17 17
  • Satori Sushi House
Feb 10 28

Looking for a way to modify the default printer presets i.e. the presets called “Standard” in OS X I came across a Mac OS X Hints article that talks about the ~/Library/Preferences/com.apple.print.custompresets.plist file.

However, what I found way more interesting is that apparently OS X provides a web interface for CUPS at http://localhost:631/ for the locally installed printers.

Through that interface you can nicely modify the default printer presets. Just go to http://localhost:631/printers/ -> choose your printer -> select Set Default Options in the second drop-down. You’ll be asked for an administrator’s username/password to save the changes. No surprise there.

Feb 10 09

In a recent stackoverflow.com question I asked for input on how to implemented animated GIFs in SWT table/tree viewer cells. Below is some code of the final solution.

Animation thread

The animation thread can be asked for the “current frame index” (CFI) by interested parties. The CFI thusly denotes the frame of an animated GIF which should be rendered. LabelProviders are the interested parties in this context because they actually render the images.

The thread increments the CFI in its run method. Also, Display#asyncExec is triggered in the run method. During the async execution the table/tree is ask to redraw and update – see lines 69ff.

/**
 * This animation thread doesn't actually animate anything itself. However, it acts as a pace maker
 * for other components that are able to render animated images. In a loop this thread marks
 * designated columns of a tree viewer (see constructor) for redrawing and calls
 * update() on the tree itself. Label providers on the other hand ask this thread for
 * the current frame index whenever they're triggered provide an image for the designated column.
 *
 * The viewer's content provider is ideally suited to control the life cycle of this thread. It can
 * create/destroy a thread instance whenever the viewer's content is updated.
 *
 */
public class BuildColorAnimationThread extends Thread {

  private final TreeViewer viewer;
  private final int animationColumnIndex;
  private final ImageLoader sampleImage;
  private int currentFrameIndex;
  private boolean cancel;
  private static BuildColorAnimationThread runningInstance;

  /**
   * C'tor which sets this instance to be the one running instance.
   *
   * @param viewer the viewer whose content should be animated
   * @param animationColumnIndex the index of the column to be animated
   * @param sampleImage a sample image which resembles the animation images in the viewer, the
   *        thread uses it to extract repeat-counts and frame delays
   * @see BuildColorAnimationThread#getRunningInstance()
   */
  public BuildColorAnimationThread(final TreeViewer viewer, final int animationColumnIndex,
      final ImageLoader sampleImage) {
    this.viewer = viewer;
    this.animationColumnIndex = animationColumnIndex;
    this.sampleImage = sampleImage;
    this.currentFrameIndex = 0;
    BuildColorAnimationThread.setRunningInstance(this);
  }

  /**
   * Returns the running instance of the animation thread or null if there's none.
   *
   * @return the running instance of the animation thread or null if there's none
   */
  public static BuildColorAnimationThread getRunningInstance() {
    return runningInstance;
  }

  private static void setRunningInstance(BuildColorAnimationThread runningInstance) {
    BuildColorAnimationThread.runningInstance = runningInstance;
  }

  @Override
  public void run() {
    int repeatCount = this.sampleImage.repeatCount;
    /*
     * The repeat count takes into consideration that an animated image might not be animated
     * infinitely but only for a given number of loops.
     */
    while (!this.cancel && !this.viewer.getTree().isDisposed()
        && (this.sampleImage.repeatCount == 0 || repeatCount > 0)) {
      this.viewer.getTree().getDisplay().asyncExec(new Runnable() {

        public void run() {
          final BuildColorAnimationThread thread = BuildColorAnimationThread.this;
          final Tree tree = thread.viewer.getTree();
          if (!tree.isDisposed()) {
            final Rectangle clientArea = tree.getClientArea();
            // Marks ONLY the animation column to be redrawn and not the entire table!
            tree.redraw(clientArea.x, clientArea.y, tree.getColumn(thread.animationColumnIndex)
                .getWidth(), clientArea.height, false);
            tree.update();
            // During the first loop currentFrameIndex=0...then currentFrameIndex=1,etc.
            thread.currentFrameIndex =
                (thread.currentFrameIndex + 1) % thread.sampleImage.data.length;
          }
        }
      });

      try {
        int ms = this.sampleImage.data[this.currentFrameIndex].delayTime * 10;
        if (ms < 20) {
          ms += 30;
        }
        if (ms < 30) {
          ms += 10;
        }
        Thread.sleep(ms);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
      /*
       * If the last frame was signaled as being the current frame, decrement the repeat count and
       * start again.
       */
      if (this.currentFrameIndex == this.sampleImage.data.length - 1) {
        repeatCount--;
      }
    }
  }

  /**
   * Returns the index of the current frame. Animation providers (i.e. renderers) call this method
   * in order to find out which frame of an animated image they should render at any given point in
   * time.
   *
   * @return frame index starting at 0
   */
  public int getCurrentFrameIndex() {
    return this.currentFrameIndex;
  }

  /**
   * Signals the thread to stop. It will finish the current execution in its run method and will
   * then stop.
   */
  public void cancel() {
    this.cancel = true;
  }
}

LabelProvider

I use my own implementation of an OwnerDrawLableProvider which paints a static image if the icon doesn’t have to be animated. If it needs to be animated (based on some attribute of the row’s input) it paints the frame of an animated GIF. The index of the frame to draw is determined by the animation thread shown above. Hint: the getImage() method is called by paint().

  /*
   * For each (animated) job color the provider keeps a map of frame_index-to-image entries. This
   * speeds up the process of delivering images for rendering because they don't have to be created
   * all the time from ImageData objects.
   */
  private final Map> imagesMap =
      new HashMap>();

  @Override
  protected Image getImage(Event event, Object element) {
    final IJob job = (IJob) element;
    Image image = BuildColorInfo.getIconImage(job.getColor());
    if (job.isRunning()) {
      final BuildColorAnimationThread animationThread =
          BuildColorAnimationThread.getRunningInstance();
      /*
       * Should no animation thread be running, the static image fetched a few lines above will be
       * returned.
       */
      if (animationThread != null) {
        final int currentFrameIndex = animationThread.getCurrentFrameIndex();
        Map images = this.imagesMap.get(job.getColor());
        if (images == null) {
          images = new HashMap();
          this.imagesMap.put(job.getColor(), images);
        }
        image = images.get(Integer.valueOf(currentFrameIndex));
        if (image == null) {
          image = createNewImage(event, job.getColor(), currentFrameIndex);
          images.put(Integer.valueOf(currentFrameIndex), image);
        }
      }
    }
    return image;
  }

  private Image createNewImage(Event event, JobColor color, int currentFrameIndex) {
    final ImageLoader imageLoader = BuildColorInfo.getImageLoader(color);
    return new Image(event.gc.getDevice(), imageLoader.data[currentFrameIndex]);
  }

“Controller”

The controller is responsible to start/stop the animation thread. Basically, whenever the viewer’s input changes the running animation thread must be canceled and a new thread must be started. The best hook for that is the viewer’s content provider – an implementation of I<Tree|Table>ContentProvider as it declares an inputChanged() method.

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
      if (this.animationThread != null) {
        this.animationThread.cancel();
      }
      // Animates the first column.
      this.animationThread =
          new BuildColorAnimationThread((TreeViewer) viewer, 0, BuildColorInfo
              .getImageLoader(JobColor.BLUE_ANIME));
      this.animationThread.start();
    }

The code presented here was implemented for H2Eclipse, the Hudson plugin for Eclipse.

Feb 10 08

Use the JavaScript console in Firebug and run the following script snippet:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("HEAD", "the_url",true); // Async HEAD request (relative path to avoid cross-domain restrictions)
xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) { // make sure the request is complete
    alert(xmlhttp.getAllResponseHeaders()) // display the headers
  }
}
xmlhttp.send(null); // send request
Feb 10 08

…A temporary issue with the Google Talk client requires that the Gmail service be added to any sign in account. If you don’t want to have an active Gmail account but want to use the Google Talk client, please sign up for Gmail once and delete the service permanently…

http://www.google.com/support/talk/bin/answer.py?answer=141360

Feb 10 06

The quote below was taken from a story about the serious issues Toyota seems to have with its jamming accelerators.

Some owners of recalled Toyotas are now saying they are afraid to drive them. “I live only a half mile from the office and I drive there,” said Elaine Byrnes, a Camry owner in Los Angeles. “If I had to go farther, I wouldn’t consider it.”

http://mobile.nytimes.com/article?a=544543&f=19

I lived in California for a year. So, I know what sort of attitude Americans have towards their cars but this was too much – definitely too much.

The story unfolded like this in my mind:

Some owners of recalled Toyotas are now saying they are afraid to drive them.

“Yes, that’s understandable. Toyota really has a problem. But what do you do? You depend on your cars, don’t you.”

“I live only a half mile from the office…”

“Ohh, well, then there’s no problem, is there? You could walk or take the bike.”
I even did the math quickly: an average adult walks about 5km (3.1 miles) in an hour. Hence, a half mile takes you a little over 10 minutes.

“…and I drive there,” said Elaine Byrnes, a Camry owner in Los Angeles.

“Seriously? What a waste!”

“If I had to go farther, I wouldn’t consider it.”

“What? That reasoning makes no sense at all. That most certainly qualifies for being one of the dumbest justifications for someones actions I’ve heard in a long while.”

As much as I love America, stories like this make me wonder if this planet has a future.

Jan 10 29

As I’m a very liberal mind my heart jumped when I came across the following quote today:

“Government big enough to supply everything you need is big enough to take everything you have.”

A quick Internet investigation lead me to a page that claims that the above quote isn’t by Thomas Jefferson but from Gerald Ford.

Jan 10 28

cache-control, pragma, no-cache, expires header, and tons more. I learned a lot from the below referenced articles.

Caching tutorial for web authors: http://www.mnot.net/cache_docs/

jGuru forum question: http://www.jguru.com/faq/view.jsp?EID=377