View Javadoc
1   package org.apache.fulcrum.json.jackson;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  import java.util.concurrent.ConcurrentHashMap;
24  
25  import org.apache.avalon.framework.logger.LogEnabled;
26  import org.apache.avalon.framework.logger.Logger;
27  
28  import com.fasterxml.jackson.databind.AnnotationIntrospector;
29  import com.fasterxml.jackson.databind.ObjectMapper;
30  import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
31  import com.fasterxml.jackson.databind.ser.FilterProvider;
32  import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
33  
34  public class CacheService implements LogEnabled {
35  
36      AnnotationIntrospector primary;
37      Map<String, FilterProvider> filters =  new ConcurrentHashMap<>();
38      
39      private static Logger logger;
40      
41      public CacheService(AnnotationIntrospector primary) {
42          this.primary = primary;
43      }
44  
45      <T> void removeFilter(Class<T> filterClass, Boolean excludeType) {
46          if (filterClass == null)
47              return;
48          if (filters.containsKey(filterClass.getName())) {
49              logger.debug("removing filter: " + filterClass.getName());
50              removeCustomIntrospectorWithExternalFilterId(filterClass, excludeType);
51              SimpleFilterProvider smpfilter = (SimpleFilterProvider) filters
52                      .get(filterClass.getName());
53              smpfilter.removeFilter(filterClass.getName());
54              filters.remove(filterClass.getName());
55          }
56      }
57      
58      <T> void removeCustomIntrospectorWithExternalFilterId(
59              Class<T> externalFilterId, Boolean excludeType) {
60          if (primary instanceof SimpleNameIntrospector && externalFilterId != null) {
61                  ((SimpleNameIntrospector) primary)
62                          .removeFilteredClass(externalFilterId);
63                  if (excludeType) {
64                      ((SimpleNameIntrospector) primary)
65                      .removeExternalFilterExcludeClass(externalFilterId);
66                  }
67          }
68      }
69  
70      void cleanSerializerCache(ObjectMapper mapper) {
71          if (mapper.getSerializerProvider() instanceof DefaultSerializerProvider) {
72              int cachedSerProvs = ((DefaultSerializerProvider) mapper
73                      .getSerializerProvider()).cachedSerializersCount();
74              if (cachedSerProvs > 0) {
75  //                getLogger()
76  //                        .debug("flushing cachedSerializersCount:"
77  //                                + cachedSerProvs);
78                  ((DefaultSerializerProvider) mapper.getSerializerProvider())
79                          .flushCachedSerializers();
80              }
81          }
82      }
83  
84      public Map<String, FilterProvider> getFilters() {
85          return filters;
86      }
87  
88      public void setFilters(Map<String, FilterProvider> filters) {
89          this.filters = filters;
90      }
91  
92      @Override
93      public void enableLogging(Logger logger) {
94          CacheService.logger = logger;        
95      }
96  
97  }