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);
}
Aggregations