Search in sources :

Example 1 with javassist

use of javassist in project Portofino by ManyDesigns.

the class SessionFactoryBuilder method buildSessionFactory.

public SessionFactoryAndCodeBase buildSessionFactory(FileObject root) throws Exception {
    List<Table> mappableTables = database.getAllTables();
    mappableTables.removeIf(this::checkInvalidPrimaryKey);
    List<Table> externallyMappedTables = mappableTables.stream().filter(t -> {
        boolean externallyMapped = t.getActualJavaClass() != null;
        if (externallyMapped) {
            logger.debug("Skipping table explicitly mapped with {}", t.getActualJavaClass());
        }
        return externallyMapped;
    }).collect(Collectors.toList());
    mappableTables.removeAll(externallyMappedTables);
    // Use a new classloader as scratch space for Javassist
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    URLClassLoader scratchClassLoader = new URLClassLoader(new URL[0], contextClassLoader);
    Thread.currentThread().setContextClassLoader(scratchClassLoader);
    try {
        CtClass baseClass = generateBaseClass();
        FileObject databaseDir = root.resolveFile(database.getDatabaseName());
        databaseDir.deleteAll();
        databaseDir.createFolder();
        FileObject baseClassFile = databaseDir.resolveFile("BaseEntity.class");
        try (OutputStream outputStream = baseClassFile.getContent().getOutputStream()) {
            outputStream.write(baseClass.toBytecode());
        }
        for (Table table : mappableTables) {
            generateClass(table);
        }
        for (Table table : mappableTables) {
            mapRelationships(table);
        }
        for (Table table : mappableTables) {
            byte[] classFile = getClassFile(table);
            FileObject location = getEntityLocation(root, table);
            try (OutputStream outputStream = location.getContent().getOutputStream()) {
                outputStream.write(classFile);
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
    return buildSessionFactory(new JavaCodeBase(root), mappableTables, externallyMappedTables);
}
Also used : ConstPool(javassist.bytecode.ConstPool) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) SequenceGenerator(com.manydesigns.portofino.model.database.SequenceGenerator) TableGenerator(com.manydesigns.portofino.model.database.TableGenerator) URLClassLoader(java.net.URLClassLoader) PersistentClass(org.hibernate.mapping.PersistentClass) Column(com.manydesigns.portofino.model.database.Column) MultiTenancyImplementation(com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementation) Method(java.lang.reflect.Method) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) javassist(javassist) Immutable(org.hibernate.annotations.Immutable) PersistentDateTime(org.jadira.usertype.dateandtime.joda.PersistentDateTime) FileSystemException(org.apache.commons.vfs2.FileSystemException) Collectors(java.util.stream.Collectors) JavaCodeBase(com.manydesigns.portofino.code.JavaCodeBase) MetadataSources(org.hibernate.boot.MetadataSources) Serializable(java.io.Serializable) Nullable(org.jetbrains.annotations.Nullable) ForeignKey(com.manydesigns.portofino.model.database.ForeignKey) javassist.bytecode.annotation(javassist.bytecode.annotation) FileName(org.apache.commons.vfs2.FileName) Modifier(java.lang.reflect.Modifier) NotNull(org.jetbrains.annotations.NotNull) TypeDefs(org.hibernate.annotations.TypeDefs) Types(java.sql.Types) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) java.util(java.util) javax.persistence(javax.persistence) AvailableSettings(org.hibernate.cfg.AvailableSettings) CodeBase(com.manydesigns.portofino.code.CodeBase) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) VFS(org.apache.commons.vfs2.VFS) Table(com.manydesigns.portofino.model.database.Table) OutputStream(java.io.OutputStream) Updatable(com.manydesigns.elements.annotations.Updatable) Logger(org.slf4j.Logger) ClassFile(javassist.bytecode.ClassFile) EntityMode(org.hibernate.EntityMode) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl) MultiTenancyStrategy(org.hibernate.MultiTenancyStrategy) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) DateTime(org.joda.time.DateTime) DatabasePlatform(com.manydesigns.portofino.model.database.platforms.DatabasePlatform) IOException(java.io.IOException) com.manydesigns.portofino.model.database(com.manydesigns.portofino.model.database) FileObject(org.apache.commons.vfs2.FileObject) GenericGenerator(org.hibernate.annotations.GenericGenerator) ServiceRegistry(org.hibernate.service.ServiceRegistry) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Configuration(org.apache.commons.configuration2.Configuration) MetadataBuilder(org.hibernate.boot.MetadataBuilder) Component(org.hibernate.mapping.Component) TypeDef(org.hibernate.annotations.TypeDef) Table(com.manydesigns.portofino.model.database.Table) URLClassLoader(java.net.URLClassLoader) OutputStream(java.io.OutputStream) URLClassLoader(java.net.URLClassLoader) FileObject(org.apache.commons.vfs2.FileObject) JavaCodeBase(com.manydesigns.portofino.code.JavaCodeBase)

Aggregations

Updatable (com.manydesigns.elements.annotations.Updatable)1 CodeBase (com.manydesigns.portofino.code.CodeBase)1 JavaCodeBase (com.manydesigns.portofino.code.JavaCodeBase)1 com.manydesigns.portofino.model.database (com.manydesigns.portofino.model.database)1 Column (com.manydesigns.portofino.model.database.Column)1 ForeignKey (com.manydesigns.portofino.model.database.ForeignKey)1 SequenceGenerator (com.manydesigns.portofino.model.database.SequenceGenerator)1 Table (com.manydesigns.portofino.model.database.Table)1 TableGenerator (com.manydesigns.portofino.model.database.TableGenerator)1 DatabasePlatform (com.manydesigns.portofino.model.database.platforms.DatabasePlatform)1 MultiTenancyImplementation (com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementation)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Serializable (java.io.Serializable)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Types (java.sql.Types)1 java.util (java.util)1