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  
23  /**
24   * Factory methods for creating {@link ClassResolver} instances.
25   * <p>
26   * <strong>Security:</strong> serialization can be a security liability,
27   * and should not be used without defining a list of classes that are
28   * allowed to be desirialized. Such a list can be specified with the
29   * <tt>jdk.serialFilter</tt> system property, for instance.
30   * See the <a href="https://docs.oracle.com/en/java/javase/17/core/serialization-filtering1.html">
31   * serialization filtering</a> article for more information.
32   *
33   * @deprecated This class has been deprecated with no replacement,
34   * because serialization can be a security liability
35   */
36  @Deprecated
37  public final class ClassResolvers {
38  
39      /**
40       * cache disabled
41       * @param classLoader - specific classLoader to use, or null if you want to revert to default
42       * @return new instance of class resolver
43       */
44      public static ClassResolver cacheDisabled(ClassLoader classLoader) {
45          return new ClassLoaderClassResolver(defaultClassLoader(classLoader));
46      }
47  
48      /**
49       * non-aggressive non-concurrent cache
50       * good for non-shared default cache
51       *
52       * @param classLoader - specific classLoader to use, or null if you want to revert to default
53       * @return new instance of class resolver
54       */
55      public static ClassResolver weakCachingResolver(ClassLoader classLoader) {
56          return new CachingClassResolver(
57                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
58                  new WeakReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
59      }
60  
61      /**
62       * aggressive non-concurrent cache
63       * good for non-shared cache, when we're not worried about class unloading
64       *
65       * @param classLoader - specific classLoader to use, or null if you want to revert to default
66       * @return new instance of class resolver
67       */
68      public static ClassResolver softCachingResolver(ClassLoader classLoader) {
69          return new CachingClassResolver(
70                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
71                  new SoftReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
72      }
73  
74      /**
75       * non-aggressive concurrent cache
76       * good for shared cache, when we're worried about class unloading
77       *
78       * @param classLoader - specific classLoader to use, or null if you want to revert to default
79       * @return new instance of class resolver
80       */
81      public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) {
82          return new CachingClassResolver(
83                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
84                  new WeakReferenceMap<String, Class<?>>(
85                          PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
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<?>>(
99                          PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
100     }
101 
102     static ClassLoader defaultClassLoader(ClassLoader classLoader) {
103         if (classLoader != null) {
104             return classLoader;
105         }
106 
107         final ClassLoader contextClassLoader = PlatformDependent.getContextClassLoader();
108         if (contextClassLoader != null) {
109             return contextClassLoader;
110         }
111 
112         return PlatformDependent.getClassLoader(ClassResolvers.class);
113     }
114 
115     private ClassResolvers() {
116         // Unused
117     }
118 }