View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.impl;
20  
21  import java.io.Serializable;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.UUID;
26  import java.util.concurrent.locks.ReentrantReadWriteLock;
27  
28  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
29  
30  /**
31   * CMIS binding session implementation.
32   */
33  public class SessionImpl implements BindingSession {
34  
35      private static final long serialVersionUID = 1L;
36  
37      private final String id;
38      private final Map<String, Object> data;
39  
40      private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
41  
42      /**
43       * Constructor.
44       */
45      public SessionImpl() {
46          id = UUID.randomUUID().toString();
47          data = new HashMap<String, Object>();
48      }
49  
50      @Override
51      public String getSessionId() {
52          return id;
53      }
54  
55      @Override
56      public Collection<String> getKeys() {
57          return data.keySet();
58      }
59  
60      @Override
61      public Object get(String key) {
62          Object value = null;
63  
64          lock.readLock().lock();
65          try {
66              value = data.get(key);
67          } finally {
68              lock.readLock().unlock();
69          }
70  
71          if (value instanceof TransientWrapper) {
72              return ((TransientWrapper) value).getObject();
73          }
74  
75          return value;
76      }
77  
78      @Override
79      public Object get(String key, Object defValue) {
80          Object value = get(key);
81          return value == null ? defValue : value;
82      }
83  
84      @Override
85      public int get(String key, int defValue) {
86          Object value = get(key);
87          int intValue = defValue;
88  
89          if (value instanceof Integer) {
90              intValue = ((Integer) value).intValue();
91          } else if (value instanceof String) {
92              try {
93                  intValue = Integer.valueOf((String) value);
94              } catch (NumberFormatException e) {
95                  // invalid number -> return default value
96              }
97          }
98  
99          return intValue;
100     }
101 
102     @Override
103     public boolean get(String key, boolean defValue) {
104         Object value = get(key);
105 
106         if (value instanceof Boolean) {
107             return ((Boolean) value).booleanValue();
108         }
109 
110         if (value instanceof String) {
111             return Boolean.parseBoolean((String) value);
112         }
113 
114         return defValue;
115     }
116 
117     @Override
118     public void put(String key, Serializable obj) {
119         lock.writeLock().lock();
120         try {
121             data.put(key, obj);
122         } finally {
123             lock.writeLock().unlock();
124         }
125     }
126 
127     @Override
128     public void put(String key, Object obj, boolean isTransient) {
129         Object value = isTransient ? new TransientWrapper(obj) : obj;
130         if (!(value instanceof Serializable)) {
131             throw new IllegalArgumentException("Object must be serializable!");
132         }
133 
134         lock.writeLock().lock();
135         try {
136             data.put(key, value);
137         } finally {
138             lock.writeLock().unlock();
139         }
140     }
141 
142     @Override
143     public void remove(String key) {
144         lock.writeLock().lock();
145         try {
146             data.remove(key);
147         } finally {
148             lock.writeLock().unlock();
149         }
150     }
151 
152     @Override
153     public void readLock() {
154         lock.readLock().lock();
155     }
156 
157     @Override
158     public void readUnlock() {
159         lock.readLock().unlock();
160     }
161 
162     @Override
163     public void writeLock() {
164         lock.writeLock().lock();
165     }
166 
167     @Override
168     public void writeUnlock() {
169         lock.writeLock().unlock();
170     }
171 
172     @Override
173     public String toString() {
174         return "Session " + id + ": " + data.toString();
175     }
176 }