This project has retired. For details please refer to its Attic page.
TypeDefinitionCache 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  
23  import org.apache.chemistry.opencmis.client.bindings.cache.Cache;
24  import org.apache.chemistry.opencmis.client.bindings.cache.impl.CacheImpl;
25  import org.apache.chemistry.opencmis.client.bindings.cache.impl.LruCacheLevelImpl;
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.definitions.TypeDefinition;
30  
31  /**
32   * A cache for type definition objects.
33   */
34  public class TypeDefinitionCache implements Serializable {
35  
36      private static final long serialVersionUID = 1L;
37  
38      private static final int CACHE_SIZE_REPOSITORIES = 10;
39      private static final int CACHE_SIZE_TYPES = 100;
40  
41      private final Cache cache;
42  
43      /**
44       * Constructor.
45       * 
46       * @param session
47       *            the session object
48       */
49      public TypeDefinitionCache(BindingSession session) {
50          int repCount = session.get(SessionParameter.CACHE_SIZE_REPOSITORIES, CACHE_SIZE_REPOSITORIES);
51          if (repCount < 1) {
52              repCount = CACHE_SIZE_REPOSITORIES;
53          }
54  
55          int typeCount = session.get(SessionParameter.CACHE_SIZE_TYPES, CACHE_SIZE_TYPES);
56          if (typeCount < 1) {
57              typeCount = CACHE_SIZE_TYPES;
58          }
59  
60          cache = new CacheImpl("Type Definition Cache");
61          cache.initialize(new String[] {
62                  MapCacheLevelImpl.class.getName() + " " + MapCacheLevelImpl.CAPACITY + "=" + repCount, // repository
63                  LruCacheLevelImpl.class.getName() + " " + LruCacheLevelImpl.MAX_ENTRIES + "=" + typeCount // type
64          });
65      }
66  
67      /**
68       * Adds a type definition object to the cache.
69       * 
70       * @param repositoryId
71       *            the repository id
72       * @param typeDefinition
73       *            the type definition object
74       */
75      public void put(String repositoryId, TypeDefinition typeDefinition) {
76          if ((typeDefinition == null) || (typeDefinition.getId() == null)) {
77              return;
78          }
79  
80          cache.put(typeDefinition, repositoryId, typeDefinition.getId());
81      }
82  
83      /**
84       * Retrieves a type definition object from the cache.
85       * 
86       * @param repositoryId
87       *            the repository id
88       * @param typeId
89       *            the type id
90       * @return the type definition object or <code>null</code> if the object is
91       *         not in the cache
92       */
93      public TypeDefinition get(String repositoryId, String typeId) {
94          return (TypeDefinition) cache.get(repositoryId, typeId);
95      }
96  
97      /**
98       * Removes a type definition object from the cache.
99       * 
100      * @param repositoryId
101      *            the repository id
102      * @param typeId
103      *            the type id
104      */
105     public void remove(String repositoryId, String typeId) {
106         cache.remove(repositoryId, typeId);
107     }
108 
109     /**
110      * Removes all type definition objects of a repository from the cache.
111      * 
112      * @param repositoryId
113      *            the repository id
114      */
115     public void remove(String repositoryId) {
116         cache.remove(repositoryId);
117     }
118 
119     @Override
120     public String toString() {
121         return cache.toString();
122     }
123 }