1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.util.privilegedactions;
8
9 import java.security.PrivilegedAction;
10
11 import org.hibernate.validator.internal.util.Contracts;
12
13 import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
14
15 /**
16  * @author Emmanuel Bernard
17  */

18 public final class GetClassLoader implements PrivilegedAction<ClassLoader> {
19
20     private static final GetClassLoader CONTEXT = new GetClassLoader( null );
21
22     private final Class<?> clazz;
23
24     public static GetClassLoader fromContext() {
25         return CONTEXT;
26     }
27
28     public static GetClassLoader fromClass(Class<?> clazz) {
29         Contracts.assertNotNull( clazz, MESSAGES.classIsNull() );
30         return new GetClassLoader( clazz );
31     }
32
33     private GetClassLoader(Class<?> clazz) {
34         this.clazz = clazz;
35     }
36
37     @Override
38     public ClassLoader run() {
39         if ( clazz != null ) {
40             return clazz.getClassLoader();
41         }
42         else {
43             return Thread.currentThread().getContextClassLoader();
44         }
45     }
46 }
47