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