All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tentackle.pdo.AbstractPdoFactory Maven / Gradle / Ivy

/*
 * Tentackle - https://tentackle.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.tentackle.pdo;

import org.tentackle.session.Session;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;

/**
 * Base implementation of a {@link PdoFactory}.
 *
 * @author harald
 */
public abstract class AbstractPdoFactory implements PdoFactory {

  private static final String CANNOT_CREATE_PDO_FOR = "cannot create PDO for ";

  /**
   * Parent constructor.
   */
  public AbstractPdoFactory() {
    // see -Xlint:missing-explicit-ctor since Java 16
  }

  /**
   * Creates a PDO invocation handler for given class.
* Override this method if you need an application-specific invocation handler. * * @param the PDO type * @param clazz the PDO interface * @return the invocation handler * @throws ClassNotFoundException if clazz not found * @see PdoInvocationHandler */ protected > PdoInvocationHandler createInvocationHandler(Class clazz) throws ClassNotFoundException { return new PdoInvocationHandler<>(getPersistenceMapper(), getDomainMapper(), clazz); } @SuppressWarnings("unchecked") private > T createPdo(Class clazz, DomainContext context) { try { PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); handler.setupDelegates(instance, context); return instance; } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + clazz + " in context '" + context + "'", e); } } @SuppressWarnings("unchecked") private > T createPdo(Class clazz, Session session) { try { PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); handler.setupDelegates(instance, session); return instance; } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + clazz + " for session " + session, e); } } @SuppressWarnings("unchecked") private > T createPdo(Class clazz, PersistentObject persistenceDelegate) { try { PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); handler.setupDelegates(instance, persistenceDelegate); return instance; } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + clazz + " for persistence delegate " + persistenceDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(Class clazz, DomainContext context, DomainObject domainDelegate) { try { PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); handler.setupDelegates(instance, context, domainDelegate); return instance; } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + clazz + " for domain delegate " + domainDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(Class clazz, Session session, DomainObject domainDelegate) { try { PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); handler.setupDelegates(instance, session, domainDelegate); return instance; } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + clazz + " for domain delegate " + domainDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(Class clazz, PersistentObject persistenceDelegate, DomainObject domainDelegate) { try { PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); handler.setupDelegates(instance, persistenceDelegate, domainDelegate); return instance; } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + clazz + " for delegates " + persistenceDelegate + " : " + domainDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(Class clazz) { try { PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); handler.setupDelegates(instance); return instance; } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + clazz, e); } } @SuppressWarnings("unchecked") private > T createPdo(T pdo, String contextName) { try { Class clazz = pdo.getEffectiveClass(); PdoInvocationHandler handler = createInvocationHandler(clazz); T instance = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler); DomainContext context = pdo.getPersistenceDelegate().getDomainContext(); if (contextName != null && !context.isWithinContext(contextName)) { context = context.cloneKeepRoot(contextName); } handler.setupDelegates(instance, context); return instance; } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { throw new PdoRuntimeException("cannot create PDO from template " + pdo, e); } } @SuppressWarnings("unchecked") private > T createPdo(String className, DomainContext context) { try { return createPdo((Class)Class.forName(className), context); } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + className + " in context " + context, e); } } @SuppressWarnings("unchecked") private > T createPdo(String className, Session session) { try { return createPdo((Class) Class.forName(className), session); } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + className + " for session " + session, e); } } @SuppressWarnings("unchecked") private > T createPdo(String className, PersistentObject persistenceDelegate) { try { return createPdo((Class) Class.forName(className), persistenceDelegate); } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + className + " for persistence delegate " + persistenceDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(String className, DomainContext context, DomainObject domainDelegate) { try { return createPdo((Class) Class.forName(className), context, domainDelegate); } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + className + " for domain delegate " + domainDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(String className, Session session, DomainObject domainDelegate) { try { return createPdo((Class) Class.forName(className), session, domainDelegate); } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + className + " for domain delegate " + domainDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(String className, PersistentObject persistenceDelegate, DomainObject domainDelegate) { try { return createPdo((Class) Class.forName(className), persistenceDelegate, domainDelegate); } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + className + " for domain delegate " + domainDelegate, e); } } @SuppressWarnings("unchecked") private > T createPdo(String className) { try { return createPdo((Class) Class.forName(className)); } catch (ClassNotFoundException e) { throw new PdoRuntimeException(CANNOT_CREATE_PDO_FOR + className, e); } } // ----------------------- implements PdoFactory -------------------------------- @Override public > T create(Class clazz, DomainContext context) { return createPdo(clazz, context); } @Override public > T create(String className, DomainContext context) { return createPdo(className, context); } @Override public > T create(T pdo) { return createPdo(pdo, null); } @Override public > T create(T pdo, String contextName) { return createPdo(pdo, contextName); } @Override public > T create(Class clazz, Session session) { return createPdo(clazz, session); } @Override public > T create(String className, Session session) { return createPdo(className, session); } @Override public > T create(Class clazz, PersistentObject persistenceDelegate) { return createPdo(clazz, persistenceDelegate); } @Override public > T create(String className, PersistentObject persistenceDelegate) { return createPdo(className, persistenceDelegate); } @Override public > T create(Class clazz) { return createPdo(clazz); } @Override public > T create(String className) { return createPdo(className); } @Override public > T create(Class clazz, DomainContext context, DomainObject domainDelegate) { return createPdo(clazz, context, domainDelegate); } @Override public > T create(String className, DomainContext context, DomainObject domainDelegate) { return createPdo(className, context, domainDelegate); } @Override public > T create(Class clazz, Session session, DomainObject domainDelegate) { return createPdo(clazz, session, domainDelegate); } @Override public > T create(String className, Session session, DomainObject domainDelegate) { return createPdo(className, session, domainDelegate); } @Override public > T create(Class clazz, PersistentObject persistenceDelegate, DomainObject domainDelegate) { return createPdo(clazz, persistenceDelegate, domainDelegate); } @Override public > T create(String className, PersistentObject persistenceDelegate, DomainObject domainDelegate) { return createPdo(className, persistenceDelegate, domainDelegate); } @Override @SuppressWarnings("unchecked") public > Class> getPersistenceClass(String className) { try { return (Class>) getPersistenceMapper().mapLenient(className); } catch (ClassNotFoundException ex) { throw new PdoRuntimeException("could not create persistence class for " + className, ex); } } @Override @SuppressWarnings("unchecked") public > Class> getDomainClass(String className) { try { return (Class>) getDomainMapper().mapLenient(className); } catch (ClassNotFoundException ex) { throw new PdoRuntimeException("could not create domain class for " + className, ex); } } @Override public > PersistentObject createPersistenceDelegate(String className) { Class> clazz = getPersistenceClass(className); try { return clazz.getConstructor().newInstance(); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex) { throw new PdoRuntimeException("could not create persistence delegate for " + clazz, ex); } } @Override public > DomainObject createDomainDelegate(String className) { Class> clazz = getDomainClass(className); try { return clazz.getConstructor().newInstance(); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex) { throw new PdoRuntimeException("could not create domain delegate for " + className, ex); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy