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  
20  package org.apache.chemistry.opencmis.client.bindings.impl;
21  
22  import java.io.Serializable;
23  
24  import org.apache.chemistry.opencmis.client.bindings.cache.Cache;
25  import org.apache.chemistry.opencmis.client.bindings.cache.impl.CacheImpl;
26  import org.apache.chemistry.opencmis.client.bindings.cache.impl.MapCacheLevelImpl;
27  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
28  import org.apache.chemistry.opencmis.commons.SessionParameter;
29  import org.apache.chemistry.opencmis.commons.SessionParameterDefaults;
30  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
31  
32  /**
33   * A cache for repository info objects.
34   */
35  public class RepositoryInfoCache implements Serializable {
36  
37      private static final long serialVersionUID = 1L;
38  
39      private final Cache cache;
40  
41      /**
42       * Constructor.
43       * 
44       * @param session
45       *            the session object
46       */
47      public RepositoryInfoCache(BindingSession session) {
48          assert session != null;
49  
50          int repCount = session.get(SessionParameter.CACHE_SIZE_REPOSITORIES,
51                  SessionParameterDefaults.CACHE_SIZE_REPOSITORIES);
52          if (repCount < 1) {
53              repCount = SessionParameterDefaults.CACHE_SIZE_REPOSITORIES;
54          }
55  
56          cache = new CacheImpl("Repository Info Cache");
57          cache.initialize(new String[] { MapCacheLevelImpl.class.getName() + " " + MapCacheLevelImpl.CAPACITY + "="
58                  + repCount });
59      }
60  
61      /**
62       * Adds a repository info object to the cache.
63       * 
64       * @param repositoryInfo
65       *            the repository info object
66       */
67      public void put(RepositoryInfo repositoryInfo) {
68          if ((repositoryInfo == null) || (repositoryInfo.getId() == null)) {
69              return;
70          }
71  
72          cache.put(repositoryInfo, repositoryInfo.getId());
73      }
74  
75      /**
76       * Retrieves a repository info object from the cache.
77       * 
78       * @param repositoryId
79       *            the repository id
80       * @return the repository info object or <code>null</code> if the object is
81       *         not in the cache
82       */
83      public RepositoryInfo get(String repositoryId) {
84          return (RepositoryInfo) cache.get(repositoryId);
85      }
86  
87      /**
88       * Removes a repository info object from the cache.
89       * 
90       * @param repositoryId
91       *            the repository id
92       */
93      public void remove(String repositoryId) {
94          cache.remove(repositoryId);
95      }
96  
97      @Override
98      public String toString() {
99          return cache.toString();
100     }
101 }