KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > clustering > wadi > WADISessionAdaptor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.clustering.wadi;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.apache.geronimo.clustering.Session;
24 import org.codehaus.wadi.web.WebSession;
25
26 /**
27  *
28  * @version $Rev$ $Date$
29  */

30 public class WADISessionAdaptor implements Session {
31     private final WebSession session;
32     private final Map JavaDoc state;
33     
34     public WADISessionAdaptor(WebSession session) {
35         this.session = session;
36         
37         state = new StateMap();
38     }
39
40     public String JavaDoc getSessionId() {
41         return session.getId();
42     }
43
44     public void release() {
45         try {
46             session.destroy();
47         } catch (Exception JavaDoc e) {
48             throw new IllegalStateException JavaDoc("Cannot release session " + session);
49         }
50     }
51
52     public Object JavaDoc addState(String JavaDoc key, Object JavaDoc value) {
53         return session.setAttribute(key, value);
54     }
55
56     public Object JavaDoc getState(String JavaDoc key) {
57         return session.getAttribute(key);
58      }
59
60     public Object JavaDoc removeState(String JavaDoc key) {
61         return session.removeAttribute(key);
62     }
63
64     public Map JavaDoc getState() {
65         return state;
66     }
67     
68     private class StateMap implements Map JavaDoc {
69
70         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
71             String JavaDoc wadiKey = ensureTypeAndCast(key);
72             return addState(wadiKey, value);
73         }
74
75         public Object JavaDoc remove(Object JavaDoc key) {
76             String JavaDoc wadiKey = ensureTypeAndCast(key);
77             return removeState(wadiKey);
78         }
79
80         public void clear() {
81             throw new UnsupportedOperationException JavaDoc();
82         }
83
84         public boolean containsKey(Object JavaDoc key) {
85             throw new UnsupportedOperationException JavaDoc();
86         }
87
88         public boolean containsValue(Object JavaDoc value) {
89             throw new UnsupportedOperationException JavaDoc();
90         }
91
92         public Set JavaDoc entrySet() {
93             throw new UnsupportedOperationException JavaDoc();
94         }
95
96         public Object JavaDoc get(Object JavaDoc key) {
97             String JavaDoc wadiKey = ensureTypeAndCast(key);
98             return getState(wadiKey);
99         }
100
101         public boolean isEmpty() {
102             throw new UnsupportedOperationException JavaDoc();
103         }
104
105         public Set JavaDoc keySet() {
106             return session.getAttributeNameSet();
107         }
108
109         public void putAll(Map JavaDoc t) {
110             throw new UnsupportedOperationException JavaDoc();
111         }
112
113         public int size() {
114             return session.getAttributeNameSet().size();
115         }
116
117         public Collection JavaDoc values() {
118             throw new UnsupportedOperationException JavaDoc();
119         }
120
121         private String JavaDoc ensureTypeAndCast(Object JavaDoc key) {
122             if (!(key instanceof String JavaDoc)) {
123                 throw new ClassCastException JavaDoc(String JavaDoc.class + " is expected.");
124             }
125             return (String JavaDoc) key;
126         }
127     }
128 }
129
Popular Tags