Skip to content

Commit

Permalink
Merge pull request #364 from twicksell/removeHibernateValidator
Browse files Browse the repository at this point in the history
remove old hibernate-validator dependency by removing rarely used
  • Loading branch information
twicksell authored May 2, 2017
2 parents b21d4a3 + 43dc61c commit 375dc4e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 108 deletions.
1 change: 0 additions & 1 deletion governator-legacy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ apply plugin: 'java'
dependencies {
compile project(':governator-api')
compile project(':governator-core')
compile 'org.hibernate:hibernate-validator:4.1.0.Final'
compile 'org.ow2.asm:asm:5.0.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.3'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@

package com.netflix.governator.lifecycle;

import static com.netflix.governator.internal.BinaryConstant.I10_1024;
import static com.netflix.governator.internal.BinaryConstant.I15_32768;
import static com.netflix.governator.internal.BinaryConstant.I16_65536;

import java.io.Closeable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.PostConstruct;
import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.MapMaker;
import com.google.inject.Binding;
import com.google.inject.Inject;
Expand All @@ -58,7 +53,6 @@
import com.netflix.governator.internal.JSR250LifecycleAction.ValidationMode;
import com.netflix.governator.internal.PreDestroyLifecycleFeature;
import com.netflix.governator.internal.PreDestroyMonitor;
import static com.netflix.governator.internal.BinaryConstant.*;

/**
* Main instance management container
Expand All @@ -85,7 +79,6 @@ private enum State
private final ConfigurationMapper configurationMapper;
private final ResourceMapper resourceMapper;
final LifecycleListener[] listeners;
private final Validator validator;
private final PreDestroyMonitor preDestroyMonitor;
private com.netflix.governator.LifecycleManager newLifecycleManager;

Expand All @@ -112,7 +105,6 @@ public LifecycleManager(LifecycleManagerArguments arguments, Injector injector)
newLifecycleManager = arguments.getLifecycleManager();
listeners = arguments.getLifecycleListeners().toArray(new LifecycleListener[0]);
resourceMapper = new ResourceMapper(injector, ImmutableSet.copyOf(arguments.getResourceLocators()));
validator = Validation.buildDefaultValidatorFactory().getValidator();
configurationDocumentation = arguments.getConfigurationDocumentation();
configurationProvider = arguments.getConfigurationProvider();
}
Expand Down Expand Up @@ -232,8 +224,6 @@ public void start() throws Exception
{
Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTING), "Already started");

validate();

new ConfigurationColumnWriter(configurationDocumentation).output(log);
if (newLifecycleManager != null) {
newLifecycleManager.notifyStarted();
Expand Down Expand Up @@ -364,85 +354,11 @@ public synchronized void close()
}
}

private void initializeObjectPostStart(Object obj) throws ValidationException
private void initializeObjectPostStart(Object obj)
{
validate(obj);
}

/**
* Run the validations on the managed objects. This is done automatically when {@link #start()} is called.
* But you can call this at any time you need.
*
* @throws ValidationException
*/
public void validate() throws ValidationException
{
ValidationException exception = null;
for ( Object managedInstance : objectStates.keySet() )
{
if (managedInstance != null) {
exception = internalValidateObject(exception, managedInstance, validator);
}
}

if ( exception != null )
{
throw exception;
}
}

/**
* Run validations on the given object
*
* @param obj the object to validate
* @throws ValidationException
*/
public void validate(Object obj) throws ValidationException
{
ValidationException exception = internalValidateObject(null, obj, validator);
if ( exception != null )
{
throw exception;
}
}

private ValidationException internalValidateObject(ValidationException exception, Object obj, Validator validator)
{
Set<ConstraintViolation<Object>> violations = validator.validate(obj, Default.class);
if (!violations.isEmpty()) {
for ( ConstraintViolation<Object> violation : violations )
{
String path = getPath(violation);
String message = String.format("%s - %s.%s = %s", violation.getMessage(), obj.getClass().getName(), path, String.valueOf(violation.getInvalidValue()));
if ( exception == null )
{
exception = new ValidationException(message);
}
else
{
exception = new ValidationException(message, exception);
}
}
}
return exception;
}

private String getPath(ConstraintViolation<Object> violation)
{
Iterable<String> transformed = Iterables.transform
(
violation.getPropertyPath(),
new Function<Path.Node, String>()
{
@Override
public String apply(Path.Node node)
{
return node.getName();
}
}
);
return Joiner.on(".").join(transformed);
}


@Override
public void call(Injector injector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.netflix.governator.lifecycle;

import static com.netflix.governator.internal.BinaryConstant.*;
import static com.netflix.governator.internal.BinaryConstant.I15_32768;
import static com.netflix.governator.internal.BinaryConstant.I2_4;
import static com.netflix.governator.internal.BinaryConstant.I3_8;
import static com.netflix.governator.internal.BinaryConstant.I4_16;
import static com.netflix.governator.internal.BinaryConstant.I5_32;
import static com.netflix.governator.internal.BinaryConstant.I9_512;

import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
Expand All @@ -33,7 +33,6 @@
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -42,7 +41,6 @@
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.validation.Constraint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -133,10 +131,6 @@ void addLifeCycleMethods(Class<?> clazz, Multimap<Class<? extends Annotation>, S
continue;
}

if (!hasValidations) {
checkForValidations(field);
}

for (Class<? extends Annotation> annotationClass : fieldAnnotations) {
processField(field, annotationClass, usedNames);
}
Expand Down Expand Up @@ -192,10 +186,6 @@ private void handleReflectionError(Class<?> clazz, Throwable e) {
}
}

private void checkForValidations(Field field) {
this.hasValidations =field.getAnnotationsByType(Constraint.class).length > 0;
}

private void processField(Field field, Class<? extends Annotation> annotationClass,
Multimap<Class<? extends Annotation>, String> usedNames) {
if (field.isAnnotationPresent(annotationClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.junit.Assert;
import org.junit.Test;
import javax.validation.constraints.Min;

@SuppressWarnings("UnusedDeclaration")
public class TestLifecycleManager
Expand All @@ -28,13 +27,11 @@ public void testValidation() throws Exception
{
Object goodObj = new Object()
{
@Min(1)
private int a = 10;
};

Object badObj = new Object()
{
@Min(1)
private int a = 0;
};

Expand All @@ -47,11 +44,11 @@ public void testValidation() throws Exception
try
{
manager.start();
Assert.fail();

}
catch ( ValidationException e )
{
// correct
Assert.fail(); //should no longer throw a validation exception as this feature was removed in 1.17.0
}
}
}

0 comments on commit 375dc4e

Please sign in to comment.