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.engine.constraintvalidation;
8
9 import java.security.AccessController;
10 import java.security.PrivilegedAction;
11
12 import javax.validation.ConstraintValidator;
13 import javax.validation.ConstraintValidatorFactory;
14
15 import org.hibernate.validator.internal.util.privilegedactions.NewInstance;
16
17 /**
18  * Default {@code ConstraintValidatorFactory} using a no-arg constructor.
19  *
20  * @author Emmanuel Bernard
21  * @author Hardy Ferentschik
22  */

23 //TODO Can we make the constructor non-public?
24 public class ConstraintValidatorFactoryImpl implements ConstraintValidatorFactory {
25
26     @Override
27     public final <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
28         return run( NewInstance.action( key, "ConstraintValidator" ) );
29     }
30
31     @Override
32     public void releaseInstance(ConstraintValidator<?, ?> instance) {
33         // noop
34     }
35
36     /**
37      * Runs the given privileged action, using a privileged block if required.
38      * <p>
39      * <b>NOTE:</b> This must never be changed into a publicly available method to avoid execution of arbitrary
40      * privileged actions within HV's protection domain.
41      */

42     private <T> T run(PrivilegedAction<T> action) {
43         return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
44     }
45 }
46