001     package railo.runtime.type;
002    
003    import railo.runtime.exp.ExpressionException;
004    import railo.runtime.exp.PageException;
005    import railo.runtime.exp.PageRuntimeException;
006    
007    /**
008     * a read only Struct if flag is set to readonly 
009     */
010    public class ReadOnlyStruct extends StructImpl {
011            
012    
013            private boolean isReadOnly=false;
014            
015            /**
016             * sets if scope is readonly or not
017             * @param isReadOnly
018             */
019            public void setReadOnly(boolean isReadOnly) {
020                    this.isReadOnly=isReadOnly;
021            }
022            
023            @Override
024            public Object remove(Collection.Key key) throws PageException {
025                    if(isReadOnly)throw new ExpressionException("can't remove key ["+key.getString()+"] from struct, struct is readonly");
026                    return super.remove (key);
027            }
028            
029            @Override
030            public Object removeEL(Collection.Key key) {
031                    if(isReadOnly)return null;
032                    return super.removeEL (key);
033            }
034            
035            public void removeAll() {
036                    if(!isReadOnly)super.clear();
037            }
038    
039            @Override
040            public Object set(Collection.Key key, Object value) throws PageException {
041                    if(isReadOnly)throw new ExpressionException("can't set key ["+key.getString()+"] to struct, struct is readonly");
042                    return super.set (key,value);
043            }
044            
045            @Override
046            public Object setEL(Collection.Key key, Object value) {
047                    if(!isReadOnly)super.setEL (key,value);
048                    return value;
049            }
050            
051    
052            @Override
053            public Collection duplicate(boolean deepCopy) {
054                    ReadOnlyStruct trg=new ReadOnlyStruct();
055                    trg.isReadOnly=isReadOnly;
056                    copy(this, trg, deepCopy);
057                    return trg;
058            }
059            
060    
061            @Override
062            public void clear() {
063                    if(isReadOnly)throw new PageRuntimeException(new ExpressionException("can't clear struct, struct is readonly"));
064                    super.clear();
065            }
066    }