View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.serialization;
17  
18  import io.netty.util.internal.PlatformDependent;
19  
20  import java.lang.ref.Reference;
21  import java.util.HashMap;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  /**
25   * Factory methods for creating {@link ClassResolver} instances.
26   * <p>
27   * <strong>Security:</strong> serialization can be a security liability,
28   * and should not be used without defining a list of classes that are
29   * allowed to be desirialized. Such a list can be specified with the
30   * <tt>jdk.serialFilter</tt> system property, for instance.
31   * See the <a href="https://docs.oracle.com/en/java/javase/17/core/serialization-filtering1.html">
32   * serialization filtering</a> article for more information.
33   *
34   * @deprecated This class has been deprecated with no replacement,
35   * because serialization can be a security liability
36   */
37  @Deprecated
38  public final class ClassResolvers {
39  
40      /**
41       * cache disabled
42       * @param classLoader - specific classLoader to use, or null if you want to revert to default
43       * @return new instance of class resolver
44       */
45      public static ClassResolver cacheDisabled(ClassLoader classLoader) {
46          return new ClassLoaderClassResolver(defaultClassLoader(classLoader));
47      }
48  
49      /**
50       * non-aggressive non-concurrent cache
51       * good for non-shared default cache
52       *
53       * @param classLoader - specific classLoader to use, or null if you want to revert to default
54       * @return new instance of class resolver
55       */
56      public static ClassResolver weakCachingResolver(ClassLoader classLoader) {
57          return new CachingClassResolver(
58                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
59                  new WeakReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
60      }
61  
62      /**
63       * aggressive non-concurrent cache
64       * good for non-shared cache, when we're not worried about class unloading
65       *
66       * @param classLoader - specific classLoader to use, or null if you want to revert to default
67       * @return new instance of class resolver
68       */
69      public static ClassResolver softCachingResolver(ClassLoader classLoader) {
70          return new CachingClassResolver(
71                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
72                  new SoftReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
73      }
74  
75      /**
76       * non-aggressive concurrent cache
77       * good for shared cache, when we're worried about class unloading
78       *
79       * @param classLoader - specific classLoader to use, or null if you want to revert to default
80       * @return new instance of class resolver
81       */
82      public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) {
83          return new CachingClassResolver(
84                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
85                  new WeakReferenceMap<String, Class<?>>(new ConcurrentHashMap<>()));
86      }
87  
88      /**
89       * aggressive concurrent cache
90       * good for shared cache, when we're not worried about class unloading
91       *
92       * @param classLoader - specific classLoader to use, or null if you want to revert to default
93       * @return new instance of class resolver
94       */
95      public static ClassResolver softCachingConcurrentResolver(ClassLoader classLoader) {
96          return new CachingClassResolver(
97                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
98                  new SoftReferenceMap<String, Class<?>>(new ConcurrentHashMap<>()));
99      }
100 
101     static ClassLoader defaultClassLoader(ClassLoader classLoader) {
102         if (classLoader != null) {
103             return classLoader;
104         }
105 
106         final ClassLoader contextClassLoader = PlatformDependent.getContextClassLoader();
107         if (contextClassLoader != null) {
108             return contextClassLoader;
109         }
110 
111         return PlatformDependent.getClassLoader(ClassResolvers.class);
112     }
113 
114     private ClassResolvers() {
115         // Unused
116     }
117 }