KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > orm > config > AutomagicDbOrmConfig


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

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 JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
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     /**
30      * Return elapsed number of milliseconds for configuration.
31      */

32     public long getElapsed() {
33         return elapsed;
34     }
35
36     /**
37      * Configures {@link jodd.db.orm.DbOrm} with specified class path.
38      * @see #configure(jodd.db.orm.DbOrm)
39      */

40     public void configure(DbOrm dbOrm, URL JavaDoc[] classpath) {
41         this.dbOrm = dbOrm;
42         elapsed = System.currentTimeMillis();
43         dbOrm.clearAllMappings();
44         for (URL JavaDoc path : classpath) {
45             try {
46                 scanUrl(path);
47             } catch (IOException JavaDoc 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     /**
56      * Configures {@link jodd.db.orm.DbOrm} with default class path.
57      * @see #configure(jodd.db.orm.DbOrm, java.net.URL[])
58      */

59     public void configure(DbOrm dbOrm) {
60         configure(dbOrm, ClassLoaderUtil.getFullClassPath(AutomagicDbOrmConfig.class));
61     }
62
63     /**
64      * Scans all classes and registers only those annotated with {@link jodd.db.orm.meta.DbTable}.
65      * Because of performance purposes, classes are not dynamically loaded; instead, their
66      * file content is examined.
67      */

68     @SuppressWarnings JavaDoc({"unchecked"})
69     protected void onClassName(String JavaDoc className, InputStream JavaDoc inputStream) {
70         byte[] data;
71         try {
72             data = StreamUtil.readBytes(inputStream);
73         } catch (IOException JavaDoc 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 JavaDoc dbTableClass;
81         try {
82             dbTableClass = ClassLoaderUtil.loadClass(className, this.getClass());
83         } catch (ClassNotFoundException JavaDoc 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