Search in sources :

Example 1 with CacheException

use of org.apache.ibatis.cache.CacheException in project mybatis-3 by mybatis.

the class CacheBuilder method setStandardDecorators.

private Cache setStandardDecorators(Cache cache) {
    try {
        MetaObject metaCache = SystemMetaObject.forObject(cache);
        if (size != null && metaCache.hasSetter("size")) {
            metaCache.setValue("size", size);
        }
        if (clearInterval != null) {
            cache = new ScheduledCache(cache);
            ((ScheduledCache) cache).setClearInterval(clearInterval);
        }
        if (readWrite) {
            cache = new SerializedCache(cache);
        }
        cache = new LoggingCache(cache);
        cache = new SynchronizedCache(cache);
        if (blocking) {
            cache = new BlockingCache(cache);
        }
        return cache;
    } catch (Exception e) {
        throw new CacheException("Error building standard cache decorators.  Cause: " + e, e);
    }
}
Also used : BlockingCache(org.apache.ibatis.cache.decorators.BlockingCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheException(org.apache.ibatis.cache.CacheException) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache) CacheException(org.apache.ibatis.cache.CacheException)

Example 2 with CacheException

use of org.apache.ibatis.cache.CacheException 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)

Example 3 with CacheException

use of org.apache.ibatis.cache.CacheException in project mybatis-3 by mybatis.

the class SerializedCache method deserialize.

private Serializable deserialize(byte[] value) {
    Serializable result;
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(value);
        ObjectInputStream ois = new CustomObjectInputStream(bis);
        result = (Serializable) ois.readObject();
        ois.close();
    } catch (Exception e) {
        throw new CacheException("Error deserializing object.  Cause: " + e, e);
    }
    return result;
}
Also used : Serializable(java.io.Serializable) ByteArrayInputStream(java.io.ByteArrayInputStream) CacheException(org.apache.ibatis.cache.CacheException) IOException(java.io.IOException) CacheException(org.apache.ibatis.cache.CacheException) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with CacheException

use of org.apache.ibatis.cache.CacheException in project mybatis-3 by mybatis.

the class SerializedCache method serialize.

private byte[] serialize(Serializable value) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(value);
        oos.flush();
        oos.close();
        return bos.toByteArray();
    } catch (Exception e) {
        throw new CacheException("Error serializing object.  Cause: " + e, e);
    }
}
Also used : CacheException(org.apache.ibatis.cache.CacheException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) CacheException(org.apache.ibatis.cache.CacheException)

Aggregations

CacheException (org.apache.ibatis.cache.CacheException)4 IOException (java.io.IOException)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 Map (java.util.Map)1 InitializingObject (org.apache.ibatis.builder.InitializingObject)1 BlockingCache (org.apache.ibatis.cache.decorators.BlockingCache)1 LoggingCache (org.apache.ibatis.cache.decorators.LoggingCache)1 ScheduledCache (org.apache.ibatis.cache.decorators.ScheduledCache)1 SerializedCache (org.apache.ibatis.cache.decorators.SerializedCache)1 SynchronizedCache (org.apache.ibatis.cache.decorators.SynchronizedCache)1