View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  package org.apache.geronimo.clustering.wadi;
19  
20  import java.util.Collection;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.geronimo.clustering.Session;
25  import org.codehaus.wadi.web.WebSession;
26  
27  /**
28   *
29   * @version $Rev$ $Date$
30   */
31  public class WADISessionAdaptor implements Session {
32      private final WebSession session;
33      private final Map state;
34      
35      public WADISessionAdaptor(WebSession session) {
36          this.session = session;
37          
38          state = new StateMap();
39      }
40  
41      public String getSessionId() {
42          return session.getId();
43      }
44  
45      public void release() {
46          try {
47              session.destroy();
48          } catch (Exception e) {
49              throw new IllegalStateException("Cannot release session " + session);
50          }
51      }
52  
53      public Object addState(String key, Object value) {
54          return session.setAttribute(key, value);
55      }
56  
57      public Object getState(String key) {
58          return session.getAttribute(key);
59       }
60  
61      public Object removeState(String key) {
62          return session.removeAttribute(key);
63      }
64  
65      public Map getState() {
66          return state;
67      }
68      
69      private class StateMap implements Map {
70  
71          public Object put(Object key, Object value) {
72              String wadiKey = ensureTypeAndCast(key);
73              return addState(wadiKey, value);
74          }
75  
76          public Object remove(Object key) {
77              String wadiKey = ensureTypeAndCast(key);
78              return removeState(wadiKey);
79          }
80  
81          public void clear() {
82              throw new UnsupportedOperationException();
83          }
84  
85          public boolean containsKey(Object key) {
86              throw new UnsupportedOperationException();
87          }
88  
89          public boolean containsValue(Object value) {
90              throw new UnsupportedOperationException();
91          }
92  
93          public Set entrySet() {
94              throw new UnsupportedOperationException();
95          }
96  
97          public Object get(Object key) {
98              String wadiKey = ensureTypeAndCast(key);
99              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 }