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 org.apache.chemistry.opencmis.client.bindings.cache.Cache;
22  import org.apache.chemistry.opencmis.client.bindings.cache.TypeDefinitionCache;
23  import org.apache.chemistry.opencmis.client.bindings.cache.impl.CacheImpl;
24  import org.apache.chemistry.opencmis.client.bindings.cache.impl.LruCacheLevelImpl;
25  import org.apache.chemistry.opencmis.client.bindings.cache.impl.MapCacheLevelImpl;
26  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
27  import org.apache.chemistry.opencmis.commons.SessionParameter;
28  import org.apache.chemistry.opencmis.commons.SessionParameterDefaults;
29  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
30  
31  /**
32   * A cache for type definition objects.
33   */
34  public class TypeDefinitionCacheImpl implements TypeDefinitionCache {
35  
36      private static final long serialVersionUID = 1L;
37  
38      private Cache cache;
39  
40      /**
41       * Constructor.
42       */
43      public TypeDefinitionCacheImpl() {
44  
45      }
46  
47      @Override
48      public void initialize(BindingSession session) {
49          assert session != null;
50  
51          int repCount = session.get(SessionParameter.CACHE_SIZE_REPOSITORIES,
52                  SessionParameterDefaults.CACHE_SIZE_REPOSITORIES);
53          if (repCount < 1) {
54              repCount = SessionParameterDefaults.CACHE_SIZE_REPOSITORIES;
55          }
56  
57          int typeCount = session.get(SessionParameter.CACHE_SIZE_TYPES, SessionParameterDefaults.CACHE_SIZE_TYPES);
58          if (typeCount < 1) {
59              typeCount = SessionParameterDefaults.CACHE_SIZE_TYPES;
60          }
61  
62          cache = new CacheImpl("Type Definition Cache");
63          cache.initialize(new String[] {
64                  MapCacheLevelImpl.class.getName() + " " + MapCacheLevelImpl.CAPACITY + "=" + repCount, // repository
65                  LruCacheLevelImpl.class.getName() + " " + LruCacheLevelImpl.MAX_ENTRIES + "=" + typeCount // type
66          });
67      }
68  
69      @Override
70      public void put(String repositoryId, TypeDefinition typeDefinition) {
71          if (repositoryId == null || typeDefinition == null || typeDefinition.getId() == null) {
72              return;
73          }
74  
75          cache.put(typeDefinition, repositoryId, typeDefinition.getId());
76      }
77  
78      @Override
79      public TypeDefinition get(String repositoryId, String typeId) {
80          if(repositoryId == null || typeId == null) {
81              throw new IllegalArgumentException("Invalid repository ot type ID!");
82          }
83          
84          return (TypeDefinition) cache.get(repositoryId, typeId);
85      }
86  
87      @Override
88      public void remove(String repositoryId, String typeId) {
89          cache.remove(repositoryId, typeId);
90      }
91  
92      @Override
93      public void remove(String repositoryId) {
94          cache.remove(repositoryId);
95      }
96  
97      @Override
98      public void removeAll() {
99          cache.removeAll();
100     }
101 
102     @Override
103     public String toString() {
104         return cache.toString();
105     }
106 
107 }