View Javadoc
1   package org.synchronoss.cpo.core.cache;
2   
3   /*-
4    * [[
5    * core
6    * ==
7    * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8    * ==
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ]]
23   */
24  
25  import java.util.Collection;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  import org.synchronoss.cpo.core.meta.CpoMetaDescriptor;
29  
30  /**
31   * Process-wide cache of {@link CpoMetaDescriptor} instances keyed by descriptor name.
32   *
33   * <p>The backing map is a {@link ConcurrentHashMap} because descriptors are read during query
34   * processing while hot-deploy reloads can replace or remove descriptors concurrently.
35   *
36   * @author dberry
37   */
38  public class CpoMetaDescriptorCache {
39  
40    // ConcurrentHashMap: read during query processing while hot-deploy reloads descriptors
41    private static final Map<String, CpoMetaDescriptor> metaDescriptorMap = new ConcurrentHashMap<>();
42  
43    /** Constructs an instance; the cache itself is static and shared by all instances. */
44    protected CpoMetaDescriptorCache() {}
45  
46    /**
47     * Looks up a previously cached meta descriptor by key.
48     *
49     * @param adapterKey the descriptor's cache key; {@code null} always yields a miss
50     * @return the cached {@link CpoMetaDescriptor}, or {@code null} if none is registered under that
51     *     key
52     */
53    protected static CpoMetaDescriptor findCpoMetaDescriptor(String adapterKey) {
54      CpoMetaDescriptor metaDescriptor = null;
55      if (adapterKey != null) {
56        metaDescriptor = metaDescriptorMap.get(adapterKey);
57      }
58  
59      return metaDescriptor;
60    }
61  
62    /**
63     * Registers a meta descriptor under its own {@link CpoMetaDescriptor#getName()}, replacing any
64     * descriptor previously registered under that name.
65     *
66     * @param metaDescriptor the descriptor to cache; if {@code null} or its name is {@code null} the
67     *     call is a no-op
68     * @return the descriptor previously registered under the same name, or {@code null} if there was
69     *     none
70     */
71    protected static CpoMetaDescriptor addCpoMetaDescriptor(CpoMetaDescriptor metaDescriptor) {
72      CpoMetaDescriptor oldMetaDescriptor = null;
73      if (metaDescriptor != null && metaDescriptor.getName() != null) {
74        oldMetaDescriptor = metaDescriptorMap.put(metaDescriptor.getName(), metaDescriptor);
75      }
76      return oldMetaDescriptor;
77    }
78  
79    /**
80     * @return A collection of names of all meta descriptors currently loaded
81     */
82    protected static Collection<String> getCpoMetaDescriptorNames() {
83      return metaDescriptorMap.keySet();
84    }
85  
86    /**
87     * Removes a single cached meta descriptor.
88     *
89     * @param adapterKey the cache key of the descriptor to remove
90     */
91    protected static void removeCpoMetaDescriptor(String adapterKey) {
92      metaDescriptorMap.remove(adapterKey);
93    }
94  
95    /** Removes all cached meta descriptors. */
96    protected static void clearCpoMetaDescriptorCache() {
97      metaDescriptorMap.clear();
98    }
99  }