1 3 package jodd.db.orm.config; 4 5 import jodd.db.orm.DbOrm; 6 import jodd.db.orm.DbOrmException; 7 import jodd.db.orm.meta.DbTable; 8 import jodd.io.StreamUtil; 9 import jodd.io.findfile.FindClass; 10 import jodd.util.ArraysUtil; 11 import jodd.util.ClassLoaderUtil; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 17 public class AutomagicDbOrmConfig extends FindClass implements DbOrmConfig { 18 19 protected byte[] dbTableAnnotationBytes = "Ljodd/db/orm/meta/DbTable;".getBytes(); 20 21 public AutomagicDbOrmConfig() { 22 this.createInputStream = true; 23 } 24 25 protected DbOrm dbOrm; 26 27 protected long elapsed; 28 29 32 public long getElapsed() { 33 return elapsed; 34 } 35 36 40 public void configure(DbOrm dbOrm, URL [] classpath) { 41 this.dbOrm = dbOrm; 42 elapsed = System.currentTimeMillis(); 43 dbOrm.clearAllMappings(); 44 for (URL path : classpath) { 45 try { 46 scanUrl(path); 47 } catch (IOException ioex) { 48 throw new DbOrmException("Unable to scan classpath: '" + path + "'.", ioex); 49 } 50 } 51 elapsed = System.currentTimeMillis() - elapsed; 52 System.out.println("DbOrm configured in " + elapsed + "ms"); 53 } 54 55 59 public void configure(DbOrm dbOrm) { 60 configure(dbOrm, ClassLoaderUtil.getFullClassPath(AutomagicDbOrmConfig.class)); 61 } 62 63 68 @SuppressWarnings ({"unchecked"}) 69 protected void onClassName(String className, InputStream inputStream) { 70 byte[] data; 71 try { 72 data = StreamUtil.readBytes(inputStream); 73 } catch (IOException ioex) { 74 throw new DbOrmException("Unable to read class file '" + className + "'.", ioex); 75 } 76 int index = ArraysUtil.indexOf(data, dbTableAnnotationBytes); 77 if (index == -1) { 78 return; 79 } 80 Class dbTableClass; 81 try { 82 dbTableClass = ClassLoaderUtil.loadClass(className, this.getClass()); 83 } catch (ClassNotFoundException cnfex) { 84 throw new DbOrmException("Unable to load class '" + className + "'.", cnfex); 85 } 86 87 88 DbTable dbTable = (DbTable) dbTableClass.getAnnotation(DbTable.class); 89 if (dbTable == null) { 90 return; 91 } 92 dbOrm.mapTable(dbTableClass); 93 } 94 95 } 96 | Popular Tags |