Thursday, April 16, 2015

EhCache config with BeanUtils

BeanUtils allows you to set Bean properties.
If you have configuration stored in a Map it's tempting to use BeanUtils to automagically setup EhCache configuration.
Sadly this class has mixed types in setters and getter and thus BeanUtils that use Introspector behind won't get getter and setter pairs properly. It will get only getters and thus inform you that these properties are read only: "Skipping read-only property".

My fast solution is to use BeanUtils and have a fallback to Reflection.

public static void setProperty(Object obj, String propertyName, Object propertyValue, boolean silently) {
        try {
            PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(obj, propertyName);
            Method writeMethod = desc.getWriteMethod(); 
                 
            if (writeMethod == null) {
                writeMethod = getAlternativeWriteMethod(obj, propertyName, propertyValue.getClass());
            }
            
            if (writeMethod == null) {
                if (silently) {
                    return;
                }
                throw new IllegalArgumentException("Can't find writerMethod for " + propertyName);
            }

            if (LOG.isTraceEnabled()) {
                LOG.trace(String.format("Setting %s property of %s", propertyName, obj.getClass().getSimpleName()));
            }
            
            writeMethod.invoke(obj, propertyValue);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new IllegalArgumentException("Error when setting object property.", e);
        }
    }

    private static Method getAlternativeWriteMethod(Object obj, String propertyName, Class paramClass) throws NoSuchMethodException {
        String setterMethod = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        Method m; 
        if ((m = getMethod(obj, paramClass, setterMethod)) != null) {
            return m;
        }
        Class altClass = paramClass.isPrimitive() ? ClassUtils.primitiveToWrapper(paramClass) : ClassUtils.wrapperToPrimitive(paramClass);
        if ((m = getMethod(obj, altClass, setterMethod)) != null) {
            return m;
        }
        
        return null;
    }

    private static Method getMethod(Object obj, Class paramClass, String setterMethod) {

        try {
            return obj.getClass().getMethod(setterMethod, paramClass);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }




I will think about PR to Configuration class but it's complicated as EhCache 2.x is not present on GitHub.

Thursday, September 5, 2013

Dostawcy

TouK zaangażował się w produkcje filmową.
Polecam stronę www.dostawcyfilm.pl

Friday, June 28, 2013

Grails on Ubuntu 13.04 Raring Ringtail

If you add grails ppa to your sources you still won't install grails. There's no packages ready yet.

Instead of crying you could (temporarily) edit your apt sources and install packages for 12.10 Quantal Quetzal. So do this:

sudo add-apt-repository ppa:groovy-dev/grails

sudo vim /etc/apt/sources.list.d/groovy-dev-grails-raring.list
and change path to
deb http://ppa.launchpad.net/groovy-dev/grails/ubuntu quantal main

sudo apt-get update
sudo apt-get install grails-VERSION

If after typing grails if you press the tab key then it will show all available grails versions from 1.2.5 to 2.2.0 and beyond.

Remember that you can install several versions of grails and switch between them with

 sudo update-alternatives --config grails

Tuesday, June 11, 2013

Prezentacja jQuery z 4developers

Prezentacja z konferencji 4developers jest całkiem interaktywna. Nie ma dema (gry Jeżyk), ale może kiedyś uda mi się je wrzucić.
http://zdanek.github.io/jquery.html

Monday, April 29, 2013

Java encoding problem (in Tomcat and other servers)


The problem with encoding of served files appears when there's something wrong with java configuration on system level. Even providing proper headers inside HTTP responses can't help because all files are read improperly.


If you have problems with tomcat or other server and your files are served with broken encoding, you should edit your start script and add to JAVA_OPTS

 -Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8

I assume that you use utf8. If not, correct statement above with your encoding but please consider moving to utf8. 


Thursday, April 11, 2013

Jutro 4developers


Jutro na Bobrowieckiej (tam gdzie była wielokrotnie Javarsovia i Confitura), jutro, 12.04.2013, odbędzie się 4developers. Ja też tam będę z moim wykładem jak zacząć developować przy użyciu jQuery ("jQuery kickstart"). Zapraszam na ścieżkę "Javascript & modern web".


Sunday, March 3, 2013

BitBucket push/pull keeps asking me for password

It does it even if you've added your ssh key?! Really?

So edit .git/config and change repo url from https to ssh.

It should look like this

url = git@bitbucket.org:your_login/your_project.git

If you don't know the address then go to your bitbucket repo page and check SSH address on the project's Overvier tab.

Don't forget to set up your name (bitbucket login) in [user] section. Refer git manual or just type

$ git config user.name your_login
$ git config user.email your_email