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 * http://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 public final class ClassResolvers {
24
25 /**
26 * cache disabled
27 * @param classLoader - specific classLoader to use, or null if you want to revert to default
28 * @return new instance of class resolver
29 */
30 public static ClassResolver cacheDisabled(ClassLoader classLoader) {
31 return new ClassLoaderClassResolver(defaultClassLoader(classLoader));
32 }
33
34 /**
35 * non-agressive non-concurrent cache
36 * good for non-shared default cache
37 *
38 * @param classLoader - specific classLoader to use, or null if you want to revert to default
39 * @return new instance of class resolver
40 */
41 public static ClassResolver weakCachingResolver(ClassLoader classLoader) {
42 return new CachingClassResolver(
43 new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
44 new WeakReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
45 }
46
47 /**
48 * agressive non-concurrent cache
49 * good for non-shared cache, when we're not worried about class unloading
50 *
51 * @param classLoader - specific classLoader to use, or null if you want to revert to default
52 * @return new instance of class resolver
53 */
54 public static ClassResolver softCachingResolver(ClassLoader classLoader) {
55 return new CachingClassResolver(
56 new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
57 new SoftReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
58 }
59
60 /**
61 * non-agressive concurrent cache
62 * good for shared cache, when we're worried about class unloading
63 *
64 * @param classLoader - specific classLoader to use, or null if you want to revert to default
65 * @return new instance of class resolver
66 */
67 public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) {
68 return new CachingClassResolver(
69 new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
70 new WeakReferenceMap<String, Class<?>>(
71 PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
72 }
73
74 /**
75 * agressive concurrent cache
76 * good for shared cache, when we're not 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 softCachingConcurrentResolver(ClassLoader classLoader) {
82 return new CachingClassResolver(
83 new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
84 new SoftReferenceMap<String, Class<?>>(
85 PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
86 }
87
88 static ClassLoader defaultClassLoader(ClassLoader classLoader) {
89 if (classLoader != null) {
90 return classLoader;
91 }
92
93 final ClassLoader contextClassLoader = PlatformDependent.getContextClassLoader();
94 if (contextClassLoader != null) {
95 return contextClassLoader;
96 }
97
98 return PlatformDependent.getClassLoader(ClassResolvers.class);
99 }
100
101 private ClassResolvers() {
102 // Unused
103 }
104 }