Package org.datanucleus.api.jpa.beanvalidation

Source Code of org.datanucleus.api.jpa.beanvalidation.BeanValidatorHandlerFactory

/**********************************************************************
Copyright (c) 2009 Erik Bengtson and others. All rights reserved.
Licensed 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.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.api.jpa.beanvalidation;

import java.lang.reflect.Constructor;

import javax.persistence.PersistenceException;

import org.datanucleus.PersistenceConfiguration;
import org.datanucleus.state.CallbackHandler;
import org.datanucleus.store.ExecutionContext;

/**
* Handles the integration to javax.validation.
* Implements only the methods preDelete, preStore, prePersist
*/
public class BeanValidatorHandlerFactory
{
    /**
     * Creates an instance of {@link BeanValidatorHandler}
     * @param ec ExecutionContext
     * @return null if validation mode is none or validation mode is auto and an error occurs.
     * @throws PersistenceException if an error occurred and validation mode is callback
     */
    public static CallbackHandler newInstance(ExecutionContext ec)
    {
        PersistenceConfiguration conf = ec.getNucleusContext().getPersistenceConfiguration();
        if (conf.hasPropertyNotNull("javax.persistence.validation.mode"))
        {
            if (conf.getStringProperty("javax.persistence.validation.mode").equalsIgnoreCase("none"))
            {
                return null;
            }
        }
        try
        {
            Class cls = ec.getClassLoaderResolver().classForName("org.datanucleus.api.jpa.beanvalidation.BeanValidatorHandler");
            Constructor constructor = cls.getConstructor(new Class[]{ExecutionContext.class});
            return (CallbackHandler) constructor.newInstance(new Object[]{ec});
        }
        catch (Throwable ex) //throwable used to catch linkage errors
        {
            if (conf.hasPropertyNotNull("javax.persistence.validation.mode"))
            {
                if (conf.getStringProperty("javax.persistence.validation.mode").equalsIgnoreCase("callback"))
                {
                    throw new PersistenceException(ex.getMessage(), ex);
                }
            }
            // TODO log
        }
        return null;
    }
}
TOP

Related Classes of org.datanucleus.api.jpa.beanvalidation.BeanValidatorHandlerFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.