You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2017/06/21 08:52:44 UTC
tomee git commit: correct serialization impl for LazyValidator
Repository: tomee
Updated Branches:
refs/heads/master 6098bfacc -> 4a39c0cc2
correct serialization impl for LazyValidator
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/4a39c0cc
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/4a39c0cc
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/4a39c0cc
Branch: refs/heads/master
Commit: 4a39c0cc2d845660f42b0e04b1bd91c6fb42892d
Parents: 6098bfa
Author: rmannibucau <rm...@apache.org>
Authored: Wed Jun 21 10:52:37 2017 +0200
Committer: rmannibucau <rm...@apache.org>
Committed: Wed Jun 21 10:52:37 2017 +0200
----------------------------------------------------------------------
.../assembler/classic/LazyValidator.java | 34 ++++++++++-------
.../assembler/classic/LazyValidatorFactory.java | 2 +-
.../assembler/classic/LazyValidatorTest.java | 39 ++++++++++++++++++++
3 files changed, 60 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/4a39c0cc/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
index 03fd304..407000c 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
@@ -17,18 +17,20 @@
package org.apache.openejb.assembler.classic;
+import org.apache.openejb.bval.ValidatorUtil;
+
+import javax.naming.NamingException;
+import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
+import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.util.concurrent.locks.ReentrantLock;
-
-public class LazyValidator implements InvocationHandler {
- private final ReentrantLock lock = new ReentrantLock();
- private final ValidatorFactory factory;
- private Validator validator;
+public class LazyValidator implements InvocationHandler, Serializable {
+ private transient ValidatorFactory factory;
+ private transient volatile Validator validator;
public LazyValidator(final ValidatorFactory factory) {
this.factory = factory;
@@ -46,20 +48,24 @@ public class LazyValidator implements InvocationHandler {
private void ensureDelegate() {
if (validator == null) {
-
- final ReentrantLock l = lock;
- l.lock();
-
- try {
+ synchronized (this) {
if (validator == null) {
- validator = factory.usingContext().getValidator();
+ if (validator == null) {
+ validator = (factory == null ? findFactory() : factory).usingContext().getValidator();
+ }
}
- } finally {
- l.unlock();
}
}
}
+ private ValidatorFactory findFactory() {
+ try {
+ return ValidatorUtil.lookupFactory();
+ } catch (final NamingException ne) {
+ return Validation.buildDefaultValidatorFactory();
+ }
+ }
+
public Validator getValidator() {
ensureDelegate();
return validator;
http://git-wip-us.apache.org/repos/asf/tomee/blob/4a39c0cc/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidatorFactory.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidatorFactory.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidatorFactory.java
index 6265804..57b2c42 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidatorFactory.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidatorFactory.java
@@ -24,7 +24,7 @@ import java.lang.reflect.Method;
import java.util.concurrent.locks.ReentrantLock;
// TODO: make it generic (LazyDelegate + Factory + refactor LazyValidator)
-public class LazyValidatorFactory implements InvocationHandler {
+public class LazyValidatorFactory implements InvocationHandler, Serializable {
private final ReentrantLock lock = new ReentrantLock();
private final ClassLoader loader;
private final ValidationInfo info;
http://git-wip-us.apache.org/repos/asf/tomee/blob/4a39c0cc/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyValidatorTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyValidatorTest.java b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyValidatorTest.java
new file mode 100644
index 0000000..2f65fdb
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/LazyValidatorTest.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.assembler.classic;
+
+import org.apache.commons.lang3.SerializationUtils;
+import org.junit.Test;
+
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import java.io.Serializable;
+import java.lang.reflect.Proxy;
+
+import static org.junit.Assert.assertNotNull;
+
+public class LazyValidatorTest {
+ @Test
+ public void serialize() {
+ final Serializable obj = Serializable.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class<?>[]{ValidatorFactory.class}, new LazyValidator(Validation.buildDefaultValidatorFactory())));
+ final LazyValidator deserialized = LazyValidator.class.cast(Proxy.getInvocationHandler(SerializationUtils.deserialize(SerializationUtils.serialize(obj))));
+ final Validator validator = deserialized.getValidator();
+ assertNotNull(validator);
+ }
+}