org.apache.ibatis.cache.CacheException Java Examples

The following examples show how to use org.apache.ibatis.cache.CacheException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeneralExceptionsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
  Class<?>[] exceptionTypes = {
      BindingException.class,
      CacheException.class,
      DataSourceException.class,
      ExecutorException.class,
      LogException.class,
      ParsingException.class,
      BuilderException.class,
      PluginException.class,
      ReflectionException.class,
      PersistenceException.class,
      SqlSessionException.class,
      TransactionException.class,
      TypeException.class, 
      ScriptingException.class
  };
  for (Class<?> exceptionType : exceptionTypes) {
    testExceptionConstructors(exceptionType);
  }

}
 
Example #2
Source File: GeneralExceptionsTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
  Class<?>[] exceptionTypes = {
      BindingException.class,
      CacheException.class,
      DataSourceException.class,
      ExecutorException.class,
      LogException.class,
      ParsingException.class,
      BuilderException.class,
      PluginException.class,
      ReflectionException.class,
      PersistenceException.class,
      SqlSessionException.class,
      TransactionException.class,
      TypeException.class, 
      ScriptingException.class
  };
  for (Class<?> exceptionType : exceptionTypes) {
    testExceptionConstructors(exceptionType);
  }

}
 
Example #3
Source File: PerpetualCache.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object o) {
  //只要id相等就认为两个cache相同
  if (getId() == null) {
    throw new CacheException("Cache instances require an ID.");
  }
  if (this == o) {
    return true;
  }
  if (!(o instanceof Cache)) {
    return false;
  }

  Cache otherCache = (Cache) o;
  return getId().equals(otherCache.getId());
}
 
Example #4
Source File: PerpetualCache.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object o) {
  //只要id相等就认为两个cache相同
  if (getId() == null) {
    throw new CacheException("Cache instances require an ID.");
  }
  if (this == o) {
    return true;
  }
  if (!(o instanceof Cache)) {
    return false;
  }

  Cache otherCache = (Cache) o;
  return getId().equals(otherCache.getId());
}
 
Example #5
Source File: CacheBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {
  Constructor<? extends Cache> cacheConstructor = getBaseCacheConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(id);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache implementation (" + cacheClass + "). Cause: " + e, e);
  }
}
 
Example #6
Source File: CacheBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void setCacheProperties(Cache cache) {
  if (properties != null) {
    MetaObject metaCache = SystemMetaObject.forObject(cache);
    //用反射设置额外的property属性
    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);
        }
      }
    }
  }
}
 
Example #7
Source File: PerpetualCache.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
  if (getId() == null) {
    throw new CacheException("Cache instances require an ID.");
  }
  return getId().hashCode();
}
 
Example #8
Source File: CacheBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {
  Constructor<? extends Cache> cacheConstructor = getBaseCacheConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(id);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache implementation (" + cacheClass + "). Cause: " + e, e);
  }
}
 
Example #9
Source File: BlockingCache.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void acquireLock(Object key) {
  Lock lock = getLockForKey(key);
  if (timeout > 0) {
    try {
      boolean acquired = lock.tryLock(timeout, TimeUnit.MILLISECONDS);
      if (!acquired) {
        throw new CacheException("Couldn't get a lock in " + timeout + " for the key " +  key + " at the cache " + delegate.getId());  
      }
    } catch (InterruptedException e) {
      throw new CacheException("Got interrupted while trying to acquire lock for key " + key, e);
    }
  } else {
    lock.lock();
  }
}
 
Example #10
Source File: SerializedCache.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private byte[] serialize(Serializable value) {
  try {
      //序列化核心就是ByteArrayOutputStream
    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);
  }
}
 
Example #11
Source File: SerializedCache.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public void putObject(Object key, Object object) {
  if (object == null || object instanceof Serializable) {
      //先序列化,再委托被包装者putObject
    delegate.putObject(key, serialize((Serializable) object));
  } else {
    throw new CacheException("SharedCache failed to make a copy of a non-serializable object: " + object);
  }
}
 
Example #12
Source File: CacheBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Constructor<? extends Cache> getCacheDecoratorConstructor(Class<? extends Cache> cacheClass) {
  try {
    return cacheClass.getConstructor(Cache.class);
  } catch (Exception e) {
    throw new CacheException("Invalid cache decorator (" + cacheClass + ").  " +
        "Cache decorators must have a constructor that takes a Cache instance as a parameter.  Cause: " + e, e);
  }
}
 
Example #13
Source File: CacheBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Cache newCacheDecoratorInstance(Class<? extends Cache> cacheClass, Cache base) {
  Constructor<? extends Cache> cacheConstructor = getCacheDecoratorConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(base);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache decorator (" + cacheClass + "). Cause: " + e, e);
  }
}
 
Example #14
Source File: CacheBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Constructor<? extends Cache> getBaseCacheConstructor(Class<? extends Cache> cacheClass) {
  try {
    return cacheClass.getConstructor(String.class);
  } catch (Exception e) {
    throw new CacheException("Invalid base cache implementation (" + cacheClass + ").  " +
        "Base cache implementations must have a constructor that takes a String id as a parameter.  Cause: " + e, e);
  }
}
 
Example #15
Source File: CacheBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private Cache newCacheDecoratorInstance(Class<? extends Cache> cacheClass, Cache base) {
  Constructor<? extends Cache> cacheConstructor = getCacheDecoratorConstructor(cacheClass);
  try {
    return cacheConstructor.newInstance(base);
  } catch (Exception e) {
    throw new CacheException("Could not instantiate cache decorator (" + cacheClass + "). Cause: " + e, e);
  }
}
 
Example #16
Source File: CacheBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void setCacheProperties(Cache cache) {
  if (properties != null) {
    MetaObject metaCache = SystemMetaObject.forObject(cache);
    //用反射设置额外的property属性
    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);
        }
      }
    }
  }
}
 
Example #17
Source File: PerpetualCache.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
  if (getId() == null) {
    throw new CacheException("Cache instances require an ID.");
  }
  return getId().hashCode();
}
 
Example #18
Source File: CacheBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private Constructor<? extends Cache> getBaseCacheConstructor(Class<? extends Cache> cacheClass) {
  try {
    return cacheClass.getConstructor(String.class);
  } catch (Exception e) {
    throw new CacheException("Invalid base cache implementation (" + cacheClass + ").  " +
        "Base cache implementations must have a constructor that takes a String id as a parameter.  Cause: " + e, e);
  }
}
 
Example #19
Source File: BlockingCache.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void acquireLock(Object key) {
  Lock lock = getLockForKey(key);
  if (timeout > 0) {
    try {
      boolean acquired = lock.tryLock(timeout, TimeUnit.MILLISECONDS);
      if (!acquired) {
        throw new CacheException("Couldn't get a lock in " + timeout + " for the key " +  key + " at the cache " + delegate.getId());  
      }
    } catch (InterruptedException e) {
      throw new CacheException("Got interrupted while trying to acquire lock for key " + key, e);
    }
  } else {
    lock.lock();
  }
}
 
Example #20
Source File: SerializedCache.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private byte[] serialize(Serializable value) {
  try {
      //序列化核心就是ByteArrayOutputStream
    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);
  }
}
 
Example #21
Source File: SerializedCache.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public void putObject(Object key, Object object) {
  if (object == null || object instanceof Serializable) {
      //先序列化,再委托被包装者putObject
    delegate.putObject(key, serialize((Serializable) object));
  } else {
    throw new CacheException("SharedCache failed to make a copy of a non-serializable object: " + object);
  }
}
 
Example #22
Source File: CacheBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private Constructor<? extends Cache> getCacheDecoratorConstructor(Class<? extends Cache> cacheClass) {
  try {
    return cacheClass.getConstructor(Cache.class);
  } catch (Exception e) {
    throw new CacheException("Invalid cache decorator (" + cacheClass + ").  " +
        "Cache decorators must have a constructor that takes a Cache instance as a parameter.  Cause: " + e, e);
  }
}
 
Example #23
Source File: MyBatisCache.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Utility method.
 *
 * @param object      object to cast to Serializable.
 * @param description the object description.
 * @return a serializable object.
 * @throws CacheException if the object is not serializable
 */
private static Serializable castToSerializable(final Object object, final String description) throws CacheException {

   if (object == null) {

      return null;
   }

   if (object instanceof Serializable) {

      return (Serializable) object;
   } else {

      throw new CacheException(description + " must implement java.io.Serializable or java.io.Externalizable: " + object);
   }
}