001package lucee.runtime.type.scope.storage; 002 003import java.io.ByteArrayInputStream; 004import java.io.ByteArrayOutputStream; 005import java.io.ObjectInputStream; 006import java.io.ObjectOutputStream; 007import java.io.Serializable; 008 009import lucee.commons.io.IOUtil; 010import lucee.runtime.exp.PageException; 011import lucee.runtime.op.Caster; 012import lucee.runtime.type.Struct; 013 014public class StorageValue implements Serializable { 015 016 private static final long serialVersionUID = 2728185742217909233L; 017 private static final byte[] EMPTY = new byte[0]; 018 019 private transient Struct value; 020 private long lastModified; 021 private final byte[] barr; 022 023 public StorageValue(Struct value) throws PageException { 024 this.value=value; 025 this.barr=serialize(value); 026 this.lastModified=System.currentTimeMillis(); 027 } 028 029 public long lastModified() { 030 return lastModified; 031 } 032 public Struct getValue() throws PageException { 033 if(value==null) { 034 if(barr.length==0) return null; 035 value=deserialize(barr); 036 } 037 return value; 038 } 039 040 private static Struct deserialize(byte[] barr) throws PageException { 041 if(barr==null || barr.length==0) return null; 042 043 ObjectInputStream ois=null; 044 Struct sct=null; 045 try { 046 ois = new ObjectInputStream(new ByteArrayInputStream(barr)); 047 sct=(Struct)ois.readObject(); 048 } 049 catch(Exception e) { 050 throw Caster.toPageException(e); 051 } 052 finally { 053 IOUtil.closeEL(ois); 054 } 055 return sct; 056 } 057 058 private static byte[] serialize(Struct sct) throws PageException { 059 if(sct==null) return EMPTY; 060 061 ByteArrayOutputStream os = new ByteArrayOutputStream(); 062 ObjectOutputStream oos=null; 063 try { 064 oos = new ObjectOutputStream(os); 065 oos.writeObject(sct); 066 } 067 catch(Exception e){ 068 throw Caster.toPageException(e); 069 } 070 finally { 071 IOUtil.closeEL(oos); 072 } 073 return os.toByteArray(); 074 } 075}