Author: rmannibucau
Date: Thu Nov  8 12:23:30 2012
New Revision: 1407030

URL: http://svn.apache.org/viewvc?rev=1407030&view=rev
Log:
just try to be lazy to initialize the validator if it can't be created at 
startup time (because of a stupid tck test)

Added:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java?rev=1407030&r1=1407029&r2=1407030&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 Thu Nov  8 12:23:30 2012
@@ -144,6 +144,7 @@ import javax.sql.DataSource;
 import javax.transaction.TransactionManager;
 import javax.transaction.TransactionSynchronizationRegistry;
 import javax.validation.ValidationException;
+import javax.validation.Validator;
 import javax.validation.ValidatorFactory;
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -152,6 +153,7 @@ import java.io.InputStream;
 import java.lang.instrument.ClassFileTransformer;
 import java.lang.instrument.Instrumentation;
 import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -638,7 +640,15 @@ public class Assembler extends Assembler
                     ValidatorFactory factory = validatorFactory.getValue();
                     try {
                         
containerSystemContext.bind(VALIDATOR_FACTORY_NAMING_CONTEXT + id, factory);
-                        containerSystemContext.bind(VALIDATOR_NAMING_CONTEXT + 
id, factory.usingContext().getValidator());
+
+                        Validator validator = null;
+                        try {
+                            validator = factory.usingContext().getValidator();
+                        } catch (Exception e) {
+                            validator = (Validator) 
Proxy.newProxyInstance(appContext.getClassLoader(), new Class<?>[]{ 
Validator.class }, new LazyValidator(factory));
+                        }
+
+                        containerSystemContext.bind(VALIDATOR_NAMING_CONTEXT + 
id, validator);
                     } catch (NameAlreadyBoundException e) {
                         throw new OpenEJBException("ValidatorFactory already 
exists for module " + id, e);
                     } catch (Exception e) {

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java?rev=1407030&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/LazyValidator.java
 Thu Nov  8 12:23:30 2012
@@ -0,0 +1,43 @@
+/*
+ * 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 javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+public class LazyValidator implements InvocationHandler {
+    private final ValidatorFactory factory;
+    private Validator validator = null;
+
+    public LazyValidator(final ValidatorFactory factory) {
+        this.factory = factory;
+    }
+
+    @Override
+    public Object invoke(final Object proxy, final Method method, final 
Object[] args) throws Throwable {
+        if (validator == null) {
+            synchronized (this) {
+                if (validator == null) {
+                    validator = factory.usingContext().getValidator();
+                }
+            }
+        }
+        return method.invoke(validator, args);
+    }
+}


Reply via email to