查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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 org.jboss.netty.handler.codec.serialization;
17  
18  import java.lang.ref.Reference;
19  import java.util.HashMap;
20  import java.util.concurrent.ConcurrentHashMap;
21  
22  public final class ClassResolvers {
23  
24      /**
25       * cache disabled
26       * @param classLoader - specific classLoader to use, or null if you want to revert to default
27       * @return new instance of class resolver
28       */
29      public static ClassResolver cacheDisabled(ClassLoader classLoader) {
30          return new ClassLoaderClassResolver(defaultClassLoader(classLoader));
31      }
32  
33      /**
34       * non-agressive non-concurrent cache
35       * good for non-shared default cache
36       *
37       * @param classLoader - specific classLoader to use, or null if you want to revert to default
38       * @return new instance of class resolver
39       */
40      public static ClassResolver weakCachingResolver(ClassLoader classLoader) {
41          return new CachingClassResolver(
42                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
43                  new WeakReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
44      }
45  
46      /**
47       * agressive non-concurrent cache
48       * good for non-shared cache, when we're not worried about class unloading
49       *
50       * @param classLoader - specific classLoader to use, or null if you want to revert to default
51       * @return new instance of class resolver
52       */
53      public static ClassResolver softCachingResolver(ClassLoader classLoader) {
54          return new CachingClassResolver(
55                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
56                  new SoftReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>()));
57      }
58  
59      /**
60       * non-agressive concurrent cache
61       * good for shared cache, when we're worried about class unloading
62       *
63       * @param classLoader - specific classLoader to use, or null if you want to revert to default
64       * @return new instance of class resolver
65       */
66      public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) {
67          return new CachingClassResolver(
68                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
69                  new WeakReferenceMap<String, Class<?>>(new ConcurrentHashMap<String, Reference<Class<?>>>()));
70      }
71  
72      /**
73       * agressive concurrent cache
74       * good for shared cache, when we're not worried about class unloading
75       *
76       * @param classLoader - specific classLoader to use, or null if you want to revert to default
77       * @return new instance of class resolver
78       */
79      public static ClassResolver softCachingConcurrentResolver(ClassLoader classLoader) {
80          return new CachingClassResolver(
81                  new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
82                  new SoftReferenceMap<String, Class<?>>(new ConcurrentHashMap<String, Reference<Class<?>>>()));
83      }
84  
85      static ClassLoader defaultClassLoader(ClassLoader classLoader) {
86          if (classLoader != null) {
87              return classLoader;
88          }
89  
90          final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
91          if (contextClassLoader != null) {
92              return contextClassLoader;
93          }
94  
95          return ClassResolvers.class.getClassLoader();
96      }
97  
98      private ClassResolvers() {
99          // Unused
100     }
101 }