Tuesday, May 29, 2007

Creating a Spring Bean from a Java 5 Enum

Instances of Java enums, introduced in Java 5, can be a bit tricky to instantiate as Spring beans because the Java compiler hides enum constructors. However, we can create a bean from an enum by using the enum valueOf static method as a factory method.

For example, let's say we have defined an enum class as follows:


package robertmaldon.cafe;

public enum Coffee {
MOCHA,
ESPRESSO,
LATTE,
FRAPPUCCINO;
}


The Spring XML config for a bean instance then looks something like this:


<bean id="latte" class="robertmaldon.cafe.Coffee" factory-method="valueOf">
<constructor-arg>
<value>LATTE</value>
</constructor-arg>
</bean>


NOTE: Declaring the bean scope as prototype or singleton makes no difference. For the same class loader a singleton bean is always returned.

If you don't want to declare a bean for each value of an enum you can simply use the enum value name at the point where it is injected into a dependent class. For example, let's say you have a class that is injected with an instance of our enum above:


package robertmaldon.cafe;

public class CoffeeMachine {
private Coffee coffee;

public void setCoffee(Coffee coffee) {
this.coffee = coffee;
}
}


In the Spring XML config simply use the enum value as the property value (apparently works with Spring 1.2.2 and beyond):


<bean id="machine" class="robertmaldon.cafe.CoffeeMachine">
<property name="coffee" value="MOCHA"/>
</bean>

Wednesday, May 23, 2007

Only 7 Band Names Remaining

In the back of my mind I suspected this might happen some day...

According to data released Monday by the International Registry of Rock Band Names, only seven of the estimated 518 million potential names for musical acts remain available.

"Following the selection of 'The Stripped Amygdaloids,' 'A Purple Spray Of Cloth Violets,' and 'Guestowel' this past weekend, it is essential that new bands pick a name as soon as possible," read a statement on the organization's website...

Sunday, May 13, 2007

Live Ink: A Revolutionary Way To Read Text Online

Scientists been examining the way human brains are wired to read blocks of text and found that the brain really struggles to read traditional blocks/paragraphs of text.

Scientific research conducted by Walker Reading Technologies, a small Minnesota startup that has been studying our ability to read for the last ten years, has concluded that the natural field of focus for our eyes is circular, so our eyes view the printed page as if we’re peering through a straw.

And a very bad-behaving straw at that, because not only do our eyes feed our brain the words we’re reading, they’re also uploading characters and words from the two sentences above and below the line we’re reading.

Every time we read block text, we’re forcing our brain to a wage a constant subconscious battle with itself to filter and discard the superfluous inputs. This mental tug of war slows reading speed and diminishes comprehension.


From this research a startup has come up with a new method formatting text for the online world which, having tried it, seems to me to be much easier and quicker to read - but (graphical artists beware) takes up more space.

Links:

Tuesday, May 08, 2007

Lotus Notes: still intuitive and easy to use

It's great to be using Notes again...

Tuesday, April 24, 2007

Making a local copy of a YouTube video

I've had a few people ask me this so it is worth putting down in a blog post.

Video on the web has really exploded over the last year thanks to sites like YouTube and MySpace. Many of these sites stream videos in FLV format. FLV has the advantage of playing on any browser and platform that runs Flash - which is most - and you don't have the hassle of having to install lots of different codecs to get the thing to work in Windows Media Player.

So why would you want to make a local copy of an FLV video instead of viewing the video online in your browser?
  • You cannot view the video if you are not connected to the internet
  • If you don't have enough bandwidth the video can be jerky while continually buffering
  • The "full screen" button on most of the sites doesn't work, forcing you to squint at the tiny little video box
So how can you download an FLV video to your PC? A few sites like YouTubia offer services where you can type in a YouTube URL and get back a download link (see this article for an extensive list of downloading service sites), allowing you to save the video locally with a ".flv" file extension. To view a local FLV file you can use a free FLV player like BitComet FLV Player.

If your computer is more than a couple years old, however, I haven't found a standalone FLV player that smoothly plays full screen. The next step, then, is converting an FLV file to another video format that other players can handle efficiently. Again some people have been kind enough to offer free conversion services such as convert.viloader.net and vixy.net.

So what video format works smoothly and keeps the audio and video tracks in sync? wmv is reasonably smooth on newer computers, but the format that works best on all types of hardware that I've tried is avi using the DivX codecs - download the free player codec from here.

After all that effort you can enjoy watching uncopyrighted videos like Steve Ballmer - Developers, Instructions For Clustering or the Chad Vader - Day Shift Manager series in full-screen glory.

Enjoy.

You know you've been web surfing too long when...

http://www.ziff.net/404/404.htm

Monday, April 23, 2007

Using Java instrumentation to remove logging

Bytecode instrumentation is a common technique used to mainpulate an existing compiled class at either compile time or runtime. I'm used to seeing instrumentation used to add functionality to a class - e.g. add event logging, add statistics counts to method calls, add persistence capability to a POJO - but I came across someone who has used instrumentation to remove functionality.

The idea? Remove a functio call to log4j at runtime if that call is below the default logging level. This way the performance penalty to even the isDebugEnabled()/isInfoEnabled()/etc calls are completely removed. This is really, really clever.

The only downside to this approach that I can think of is if you dynamically change your application log levels downwards then you won't see any additional logging because the logging calls have already been removed.