This project has retired. For details please refer to its Attic page.
RepositoryInfoCache 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  
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.data.RepositoryInfo;
30  
31  /**
32   * A cache for repository info objects.
33   */
34  public class RepositoryInfoCache implements Serializable {
35  
36      private static final long serialVersionUID = 1L;
37  
38      private static final int CACHE_SIZE_REPOSITORIES = 10;
39  
40      private final Cache cache;
41  
42      /**
43       * Constructor.
44       * 
45       * @param session
46       *            the session object
47       */
48      public RepositoryInfoCache(BindingSession session) {
49          int repCount = session.get(SessionParameter.CACHE_SIZE_REPOSITORIES, CACHE_SIZE_REPOSITORIES);
50          if (repCount < 1) {
51              repCount = CACHE_SIZE_REPOSITORIES;
52          }
53  
54          cache = new CacheImpl("Repository Info Cache");
55          cache.initialize(new String[] { MapCacheLevelImpl.class.getName() + " " + MapCacheLevelImpl.CAPACITY + "="
56                  + repCount });
57      }
58  
59      /**
60       * Adds a repository info object to the cache.
61       * 
62       * @param repositoryInfo
63       *            the repository info object
64       */
65      public void put(RepositoryInfo repositoryInfo) {
66          if ((repositoryInfo == null) || (repositoryInfo.getId() == null)) {
67              return;
68          }
69  
70          cache.put(repositoryInfo, repositoryInfo.getId());
71      }
72  
73      /**
74       * Retrieves a repository info object from the cache.
75       * 
76       * @param repositoryId
77       *            the repository id
78       * @return the repository info object or <code>null</code> if the object is
79       *         not in the cache
80       */
81      public RepositoryInfo get(String repositoryId) {
82          return (RepositoryInfo) cache.get(repositoryId);
83      }
84  
85      /**
86       * Removes a repository info object from the cache.
87       * 
88       * @param repositoryId
89       *            the repository id
90       */
91      public void remove(String repositoryId) {
92          cache.remove(repositoryId);
93      }
94  
95      @Override
96      public String toString() {
97          return cache.toString();
98      }
99  }