001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.clustering.wadi;
018    
019    import java.util.Collection;
020    import java.util.Map;
021    import java.util.Set;
022    
023    import org.apache.geronimo.clustering.Session;
024    import org.codehaus.wadi.web.WebSession;
025    
026    /**
027     *
028     * @version $Rev$ $Date$
029     */
030    public class WADISessionAdaptor implements Session {
031        private final WebSession session;
032        private final Map state;
033        
034        public WADISessionAdaptor(WebSession session) {
035            this.session = session;
036            
037            state = new StateMap();
038        }
039    
040        public String getSessionId() {
041            return session.getId();
042        }
043    
044        public void release() {
045            try {
046                session.destroy();
047            } catch (Exception e) {
048                throw new IllegalStateException("Cannot release session " + session);
049            }
050        }
051    
052        public Object addState(String key, Object value) {
053            return session.setAttribute(key, value);
054        }
055    
056        public Object getState(String key) {
057            return session.getAttribute(key);
058         }
059    
060        public Object removeState(String key) {
061            return session.removeAttribute(key);
062        }
063    
064        public Map getState() {
065            return state;
066        }
067        
068        private class StateMap implements Map {
069    
070            public Object put(Object key, Object value) {
071                String wadiKey = ensureTypeAndCast(key);
072                return addState(wadiKey, value);
073            }
074    
075            public Object remove(Object key) {
076                String wadiKey = ensureTypeAndCast(key);
077                return removeState(wadiKey);
078            }
079    
080            public void clear() {
081                throw new UnsupportedOperationException();
082            }
083    
084            public boolean containsKey(Object key) {
085                throw new UnsupportedOperationException();
086            }
087    
088            public boolean containsValue(Object value) {
089                throw new UnsupportedOperationException();
090            }
091    
092            public Set entrySet() {
093                throw new UnsupportedOperationException();
094            }
095    
096            public Object get(Object key) {
097                String wadiKey = ensureTypeAndCast(key);
098                return getState(wadiKey);
099            }
100    
101            public boolean isEmpty() {
102                throw new UnsupportedOperationException();
103            }
104    
105            public Set keySet() {
106                return session.getAttributeNameSet();
107            }
108    
109            public void putAll(Map t) {
110                throw new UnsupportedOperationException();
111            }
112    
113            public int size() {
114                return session.getAttributeNameSet().size();
115            }
116    
117            public Collection values() {
118                throw new UnsupportedOperationException();
119            }
120    
121            private String ensureTypeAndCast(Object key) {
122                if (!(key instanceof String)) {
123                    throw new ClassCastException(String.class + " is expected.");
124                }
125                return (String) key;
126            }
127        }
128    }