Search in sources :

Example 1 with InitializingObject

use of org.apache.ibatis.builder.InitializingObject in project mybatis-3 by mybatis.

the class CacheBuilder method setCacheProperties.

private void setCacheProperties(Cache cache) {
    if (properties != null) {
        MetaObject metaCache = SystemMetaObject.forObject(cache);
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            String name = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (metaCache.hasSetter(name)) {
                Class<?> type = metaCache.getSetterType(name);
                if (String.class == type) {
                    metaCache.setValue(name, value);
                } else if (int.class == type || Integer.class == type) {
                    metaCache.setValue(name, Integer.valueOf(value));
                } else if (long.class == type || Long.class == type) {
                    metaCache.setValue(name, Long.valueOf(value));
                } else if (short.class == type || Short.class == type) {
                    metaCache.setValue(name, Short.valueOf(value));
                } else if (byte.class == type || Byte.class == type) {
                    metaCache.setValue(name, Byte.valueOf(value));
                } else if (float.class == type || Float.class == type) {
                    metaCache.setValue(name, Float.valueOf(value));
                } else if (boolean.class == type || Boolean.class == type) {
                    metaCache.setValue(name, Boolean.valueOf(value));
                } else if (double.class == type || Double.class == type) {
                    metaCache.setValue(name, Double.valueOf(value));
                } else {
                    throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
                }
            }
        }
    }
    if (InitializingObject.class.isAssignableFrom(cache.getClass())) {
        try {
            ((InitializingObject) cache).initialize();
        } catch (Exception e) {
            throw new CacheException("Failed cache initialization for '" + cache.getId() + "' on '" + cache.getClass().getName() + "'", e);
        }
    }
}
Also used : InitializingObject(org.apache.ibatis.builder.InitializingObject) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheException(org.apache.ibatis.cache.CacheException) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) InitializingObject(org.apache.ibatis.builder.InitializingObject) MetaObject(org.apache.ibatis.reflection.MetaObject) Map(java.util.Map) CacheException(org.apache.ibatis.cache.CacheException)

Aggregations

Map (java.util.Map)1 InitializingObject (org.apache.ibatis.builder.InitializingObject)1 CacheException (org.apache.ibatis.cache.CacheException)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)1