Unstable Terrain

Software development in the real world

Archive for December 2009

Property Editors Part 2

with one comment

My previous post showed how to use a custom PropertyEditor to adapt strings to any class (in this case, Class files) when using JavaBean properties.

Turns out Spring already has a ClassEditor, and has since 2003.

All you have to do is wire it up:

<pre><bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
        <map>
            <entry key="java.lang.Class">
                <bean class="org.springframework.beans.propertyeditors.ClassEditor"/>
            </entry>
        </map>
    </property>
</bean>

although it’s probably better to write a PropertyEditorRegistrar if you wanted to be forward-compatible.

Note to self: Don’t reinvent the wheel.

Advertisements

Written by Trent

December 27, 2009 at 11:48 am

Posted in Software Development

Tagged with ,

Converting Strings to Class files in Spring

with 2 comments

Today I integrated Spiros Tzavellas‘ neat implementation of the circuit breaker pattern into an app at work. I looked at other implementations but Spiros’ code is the cleanest (and actually threadsafe – a definite plus).

One neat thing about the CircuitBreaker class is that it can ignore certain exception classes. However, the code is written as

public void setIgnoredExceptions(Set<Class<? extends Throwable>> ignored) {
    if (ignored != null) {
        ignoredExceptions.clear();
        ignoredExceptions.addAll(ignored);
    }
}

Which is fine, except that Spring has some difficulty providing a Set of Class objects. How do you convert it?

PropertyEditor to the rescue

Turns out that hooks to do this conversion already exist. All we have to do is create the property editor, register that editor with Spring, and from then on, Spring will convert Strings to Class objects whenever it has to wire things up.

For reference, the code to do this is:

import java.beans.PropertyEditorSupport;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * A {@link BeanFactoryPostProcessor} that registers a property editor for {@link Class} objects.
 */
@Component
public class ClassProcessor implements BeanFactoryPostProcessor {

    private static final PropertyEditorRegistrar REGISTRAR = new PropertyEditorRegistrar() {

        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(Class.class, new Editor());
        }
    };

    public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {
        bf.addPropertyEditorRegistrar(REGISTRAR);
    }

    /** A {@code PropertyEditor} for {@code Class} objects. */
    public static class Editor extends PropertyEditorSupport {

        /** {@inheritDoc} */
        @Override
        public String getAsText() {
            return getValue().getClass().toString();
        }

        /** {@inheritDoc} */
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            try {
                setValue(Class.forName(text));
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Cannot set <" + text + ">", e);
            }
        }
    }
}

Update

Turns out Spiros has already blogged about this before, except for java.util.Date instead. Oh well, only three years late…

Update 2

See how to do it properly.

Written by Trent

December 24, 2009 at 5:14 pm

Silver Bullet Life Cycle

leave a comment »

I recently found an article from 2003 that describes the life cycle of the ‘silver bullet’ methodology du jour. It’s a timely wake-up call that the metamorphosis and eventual ossification of a new process by organisations that do not understand it will inevitably lead to its downfall.

Written by Trent

December 23, 2009 at 5:33 pm

Lifers and Losers

leave a comment »

Further to my previous post, I am reminded of one of the workplaces a friend works at; a government-owned corporation.

I asked her, once, what her coworkers were like, since I have little experience working in the public sector. She replied, “They’re either lifers or losers.”

“What’s that mean?” I prompted.

“Well,” she said, “Lifers are workers who lack ambition so they’re happy doing the same thing for the same company for the rest of their career. Losers are workers who are so incompetent that nobody else will hire them.”

“Surely there are people who don’t fit neatly into those categories,” I said.

“Oh, sure,” she replied. “Some people are lifers and losers.”

Written by Trent

December 15, 2009 at 9:00 am

Posted in Humour, IT Industry

The Dead Sea Effect

leave a comment »

In Australia, we’re coming out of what is colloquially called the GFC (or Global Financial Crisis). Due to an uncommonly-persistent bout of common sense by past Australian Governments, Australia has weathered the GFC relatively unscathed, although the signs a year ago were substantially more bleak.

This all means that the private sector (and by inference, each company in it) is moving from survival mode to growth mode. From past experience, this is a tricky time to be a manager, because the pendulum rapidly swings from “My employees are lucky to have a job” to “My employees are all leaving for better offers”.

An interesting opinion piece on this subject is The Dead Sea Effect. Check it out – I’m interested in what everyone thinks about it.

Written by Trent

December 12, 2009 at 11:43 pm