1 /*
2  * Copyright 2016 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
17 package io.netty.util;
18
19 import io.netty.util.internal.ObjectUtil;
20 import io.netty.util.internal.PlatformDependent;
21 import io.netty.util.internal.SystemPropertyUtil;
22 import io.netty.util.internal.logging.InternalLogger;
23 import io.netty.util.internal.logging.InternalLoggerFactory;
24
25 import java.lang.reflect.Constructor;
26
27 /**
28  * This static factory should be used to load {@link ResourceLeakDetector}s as needed
29  */

30 public abstract class ResourceLeakDetectorFactory {
31     private static final InternalLogger logger = InternalLoggerFactory.getInstance(ResourceLeakDetectorFactory.class);
32
33     private static volatile ResourceLeakDetectorFactory factoryInstance = new DefaultResourceLeakDetectorFactory();
34
35     /**
36      * Get the singleton instance of this factory class.
37      *
38      * @return the current {@link ResourceLeakDetectorFactory}
39      */

40     public static ResourceLeakDetectorFactory instance() {
41         return factoryInstance;
42     }
43
44     /**
45      * Set the factory's singleton instance. This has to be called before the static initializer of the
46      * {@link ResourceLeakDetector} is called by all the callers of this factory. That is, before initializing a
47      * Netty Bootstrap.
48      *
49      * @param factory the instance that will become the current {@link ResourceLeakDetectorFactory}'s singleton
50      */

51     public static void setResourceLeakDetectorFactory(ResourceLeakDetectorFactory factory) {
52         factoryInstance = ObjectUtil.checkNotNull(factory, "factory");
53     }
54
55     /**
56      * Returns a new instance of a {@link ResourceLeakDetector} with the given resource class.
57      *
58      * @param resource the resource class used to initialize the {@link ResourceLeakDetector}
59      * @param <T> the type of the resource class
60      * @return a new instance of {@link ResourceLeakDetector}
61      */

62     public final <T> ResourceLeakDetector<T> newResourceLeakDetector(Class<T> resource) {
63         return newResourceLeakDetector(resource, ResourceLeakDetector.SAMPLING_INTERVAL);
64     }
65
66     /**
67      * @deprecated Use {@link #newResourceLeakDetector(Class, int)} instead.
68      * <p>
69      * Returns a new instance of a {@link ResourceLeakDetector} with the given resource class.
70      *
71      * @param resource the resource class used to initialize the {@link ResourceLeakDetector}
72      * @param samplingInterval the interval on which sampling takes place
73      * @param maxActive This is deprecated and will be ignored.
74      * @param <T> the type of the resource class
75      * @return a new instance of {@link ResourceLeakDetector}
76      */

77     @Deprecated
78     public abstract <T> ResourceLeakDetector<T> newResourceLeakDetector(
79             Class<T> resource, int samplingInterval, long maxActive);
80
81     /**
82      * Returns a new instance of a {@link ResourceLeakDetector} with the given resource class.
83      *
84      * @param resource the resource class used to initialize the {@link ResourceLeakDetector}
85      * @param samplingInterval the interval on which sampling takes place
86      * @param <T> the type of the resource class
87      * @return a new instance of {@link ResourceLeakDetector}
88      */

89     @SuppressWarnings("deprecation")
90     public <T> ResourceLeakDetector<T> newResourceLeakDetector(Class<T> resource, int samplingInterval) {
91         ObjectUtil.checkPositive(samplingInterval, "samplingInterval");
92         return newResourceLeakDetector(resource, samplingInterval, Long.MAX_VALUE);
93     }
94
95     /**
96      * Default implementation that loads custom leak detector via system property
97      */

98     private static final class DefaultResourceLeakDetectorFactory extends ResourceLeakDetectorFactory {
99         private final Constructor<?> obsoleteCustomClassConstructor;
100         private final Constructor<?> customClassConstructor;
101
102         DefaultResourceLeakDetectorFactory() {
103             String customLeakDetector;
104             try {
105                 customLeakDetector = SystemPropertyUtil.get("io.netty.customResourceLeakDetector");
106             } catch (Throwable cause) {
107                 logger.error("Could not access System property: io.netty.customResourceLeakDetector", cause);
108                 customLeakDetector = null;
109             }
110             if (customLeakDetector == null) {
111                 obsoleteCustomClassConstructor = customClassConstructor = null;
112             } else {
113                 obsoleteCustomClassConstructor = obsoleteCustomClassConstructor(customLeakDetector);
114                 customClassConstructor = customClassConstructor(customLeakDetector);
115             }
116         }
117
118         private static Constructor<?> obsoleteCustomClassConstructor(String customLeakDetector) {
119             try {
120                 final Class<?> detectorClass = Class.forName(customLeakDetector, true,
121                         PlatformDependent.getSystemClassLoader());
122
123                 if (ResourceLeakDetector.class.isAssignableFrom(detectorClass)) {
124                     return detectorClass.getConstructor(Class.classint.classlong.class);
125                 } else {
126                     logger.error("Class {} does not inherit from ResourceLeakDetector.", customLeakDetector);
127                 }
128             } catch (Throwable t) {
129                 logger.error("Could not load custom resource leak detector class provided: {}",
130                         customLeakDetector, t);
131             }
132             return null;
133         }
134
135         private static Constructor<?> customClassConstructor(String customLeakDetector) {
136             try {
137                 final Class<?> detectorClass = Class.forName(customLeakDetector, true,
138                         PlatformDependent.getSystemClassLoader());
139
140                 if (ResourceLeakDetector.class.isAssignableFrom(detectorClass)) {
141                     return detectorClass.getConstructor(Class.classint.class);
142                 } else {
143                     logger.error("Class {} does not inherit from ResourceLeakDetector.", customLeakDetector);
144                 }
145             } catch (Throwable t) {
146                 logger.error("Could not load custom resource leak detector class provided: {}",
147                         customLeakDetector, t);
148             }
149             return null;
150         }
151
152         @SuppressWarnings("deprecation")
153         @Override
154         public <T> ResourceLeakDetector<T> newResourceLeakDetector(Class<T> resource, int samplingInterval,
155                                                                    long maxActive) {
156             if (obsoleteCustomClassConstructor != null) {
157                 try {
158                     @SuppressWarnings("unchecked")
159                     ResourceLeakDetector<T> leakDetector =
160                             (ResourceLeakDetector<T>) obsoleteCustomClassConstructor.newInstance(
161                                     resource, samplingInterval, maxActive);
162                     logger.debug("Loaded custom ResourceLeakDetector: {}",
163                             obsoleteCustomClassConstructor.getDeclaringClass().getName());
164                     return leakDetector;
165                 } catch (Throwable t) {
166                     logger.error(
167                             "Could not load custom resource leak detector provided: {} with the given resource: {}",
168                             obsoleteCustomClassConstructor.getDeclaringClass().getName(), resource, t);
169                 }
170             }
171
172             ResourceLeakDetector<T> resourceLeakDetector = new ResourceLeakDetector<T>(resource, samplingInterval,
173                                                                                        maxActive);
174             logger.debug("Loaded default ResourceLeakDetector: {}", resourceLeakDetector);
175             return resourceLeakDetector;
176         }
177
178         @Override
179         public <T> ResourceLeakDetector<T> newResourceLeakDetector(Class<T> resource, int samplingInterval) {
180             if (customClassConstructor != null) {
181                 try {
182                     @SuppressWarnings("unchecked")
183                     ResourceLeakDetector<T> leakDetector =
184                             (ResourceLeakDetector<T>) customClassConstructor.newInstance(resource, samplingInterval);
185                     logger.debug("Loaded custom ResourceLeakDetector: {}",
186                             customClassConstructor.getDeclaringClass().getName());
187                     return leakDetector;
188                 } catch (Throwable t) {
189                     logger.error(
190                             "Could not load custom resource leak detector provided: {} with the given resource: {}",
191                             customClassConstructor.getDeclaringClass().getName(), resource, t);
192                 }
193             }
194
195             ResourceLeakDetector<T> resourceLeakDetector = new ResourceLeakDetector<T>(resource, samplingInterval);
196             logger.debug("Loaded default ResourceLeakDetector: {}", resourceLeakDetector);
197             return resourceLeakDetector;
198         }
199     }
200 }
201