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

com.jpattern.orm.generator.AutoGeneratorStrategy Maven / Gradle / Ivy

There is a newer version: 3.5.1
Show newest version
package com.jpattern.orm.generator;

import com.jpattern.javassist.ClassPool;
import com.jpattern.orm.classmapper.IClassMapper;
import com.jpattern.orm.generator.javassist.JavassistPersistorGenerator;
import com.jpattern.orm.generator.reflection.ReflectionPersistorGenerator;
import com.jpattern.orm.logger.OrmLogger;

/**
 * This is the default generator strategy. If it found the jporm-javassist jar is in the classpath it will create
 * an instance of the JavassistPersistorGenerator, otherwise an instance of the ReflectionPersistorGenerator will be created.
 * The JavassistPersistorGenerator creates bytecode at runtime and it is faster then the ReflectionPersistorGenerator
 * which uses the standard java reflection.
 * 
 * @author Francesco Cina'
 *
 * Mar 24, 2012
 */
public class AutoGeneratorStrategy implements IGeneratorStrategy {

	private boolean checked = false;
	private boolean javassistFound = false;

	@Override
	public  IPersistorGenerator getGenerator(IClassMapper classMapper, String classNameSuffix) {
		try {
			checkJavassistClasspath();
		}
		catch (final Throwable e) {
			OrmLogger.getOrmLogger(getClass()).warn("getGenerator", "jporm-javassist jar not found in classpath. Reflection mechanism will be used istead of bytecode generation.\nInclude jporm-javassit in the classpath to enable bytecode generation and to have better ORM performance!");
		}
		if (javassistFound) {
			return new JavassistPersistorGenerator(classMapper, classNameSuffix);
		}
		return new ReflectionPersistorGenerator(classMapper);
	}

	private void checkJavassistClasspath() throws ClassNotFoundException {
		if (!checked) {
			checked = true;
			Class.forName( ClassPool.class.getCanonicalName() );
			javassistFound = true;
			OrmLogger.getOrmLogger(getClass()).info("getGenerator", "jporm-javassist jar found in classpath. Javassist will be used to do runtime bytecode generation.");
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy