Ejemplo n.º 1
0
  /** Returns the object that is stored for the given type (or null if none is stored). */
  @SuppressWarnings("unchecked")
  public <T extends Serializable> T getValue(Class<T> clazz) throws StorageException {

    byte[] bytes = store.getWithString(clazz.getName());
    if (bytes == null) {
      return null;
    }

    Object value;
    try {
      value =
          SerializationUtils.deserializeFromByteArray(
              bytes, Thread.currentThread().getContextClassLoader());
    } catch (ClassNotFoundException e) {
      throw new StorageException(e);
    } catch (IOException e) {
      throw new StorageException(e);
    }

    if (!clazz.isInstance(value)) {
      throw new StorageException(
          "Storage system returned object of unexpected type: " + value.getClass());
    }

    return (T) value;
  }
Ejemplo n.º 2
0
 /**
  * Sets a value using an explicit class. This is useful if the value used is a subclass, but the
  * more general type should be used for storing.
  */
 public <S extends Serializable, T extends S> void setValue(T value, Class<S> explicitClass)
     throws StorageException {
   try {
     store.putWithString(explicitClass.getName(), SerializationUtils.serializeToByteArray(value));
   } catch (IOException e) {
     throw new StorageException(e);
   }
 }