This project has retired. For details please refer to its Attic page.
SessionImpl xref

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.concurrent.locks.ReentrantReadWriteLock;
26  
27  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
28  
29  /**
30   * CMIS binding session implementation.
31   */
32  public class SessionImpl implements BindingSession {
33  
34      private static final long serialVersionUID = 1L;
35  
36      private final Map<String, Object> data;
37  
38      private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
39  
40      /**
41       * Constructor.
42       */
43      public SessionImpl() {
44          data = new HashMap<String, Object>();
45      }
46  
47      public Collection<String> getKeys() {
48          return data.keySet();
49      }
50  
51      public Object get(String key) {
52          Object value = null;
53  
54          lock.readLock().lock();
55          try {
56              value = data.get(key);
57          } finally {
58              lock.readLock().unlock();
59          }
60  
61          if (value instanceof TransientWrapper) {
62              return ((TransientWrapper) value).getObject();
63          }
64  
65          return value;
66      }
67  
68      public Object get(String key, Object defValue) {
69          Object value = get(key);
70          return (value == null ? defValue : value);
71      }
72  
73      public int get(String key, int defValue) {
74          Object value = get(key);
75          int intValue = defValue;
76  
77          if (value instanceof Integer) {
78              intValue = ((Integer) value).intValue();
79          } else if (value instanceof String) {
80              try {
81                  intValue = Integer.valueOf((String) value);
82              } catch (NumberFormatException e) {
83              }
84          }
85  
86          return intValue;
87      }
88  
89      public void put(String key, Serializable obj) {
90          lock.writeLock().lock();
91          try {
92              data.put(key, obj);
93          } finally {
94              lock.writeLock().unlock();
95          }
96      }
97  
98      public void put(String key, Object obj, boolean isTransient) {
99          Object value = (isTransient ? new TransientWrapper(obj) : obj);
100         if (!(value instanceof Serializable)) {
101             throw new IllegalArgumentException("Object must be serializable!");
102         }
103 
104         lock.writeLock().lock();
105         try {
106             data.put(key, value);
107         } finally {
108             lock.writeLock().unlock();
109         }
110     }
111 
112     public void remove(String key) {
113         lock.writeLock().lock();
114         try {
115             data.remove(key);
116         } finally {
117             lock.writeLock().unlock();
118         }
119     }
120 
121     public void readLock() {
122         lock.readLock().lock();
123     }
124 
125     public void readUnlock() {
126         lock.readLock().unlock();
127     }
128 
129     public void writeLock() {
130         lock.writeLock().lock();
131     }
132 
133     public void writeUnlock() {
134         lock.writeLock().unlock();
135     }
136 
137     @Override
138     public String toString() {
139         return data.toString();
140     }
141 }