001: package net.sourceforge.jaxor.parser;
002:
003: import net.sourceforge.jaxor.MetaRow;
004: import net.sourceforge.jaxor.util.FileUtils;
005: import net.sourceforge.jaxor.util.SystemException;
006: import net.sourceforge.jaxor.util.Validation;
007: import org.apache.tools.ant.BuildException;
008: import org.apache.tools.ant.taskdefs.MatchingTask;
009:
010: import java.io.File;
011: import java.sql.Connection;
012: import java.sql.SQLException;
013:
014: public class ColumnChecker extends MatchingTask {
015:
016: private File _srcdir;
017: private String _driver;
018: private String _url;
019: private String _user;
020: private String _password;
021: private boolean _verbose;
022: //used for looking up metadata
023: private boolean _capitalizeTable = true;
024:
025: public boolean isCapitalizeTable() {
026: return _capitalizeTable;
027: }
028:
029: public void setCapitalizeTable(boolean capitalizeTable) {
030: _capitalizeTable = capitalizeTable;
031: }
032:
033: public boolean isVerbose() {
034: return _verbose;
035: }
036:
037: public void setVerbose(boolean verbose) {
038: _verbose = verbose;
039: }
040:
041: public String getDriver() {
042: return _driver;
043: }
044:
045: public void setDriver(String driver) {
046: _driver = driver;
047: }
048:
049: public String getUrl() {
050: return _url;
051: }
052:
053: public void setUrl(String url) {
054: _url = url;
055: }
056:
057: public String getUser() {
058: return _user;
059: }
060:
061: public void setUser(String user) {
062: _user = user;
063: }
064:
065: public String getPassword() {
066: return _password;
067: }
068:
069: public void setPassword(String password) {
070: _password = password;
071: }
072:
073: public File getSrcdir() {
074: return _srcdir;
075: }
076:
077: public void setSrcdir(File srcdir) {
078: _srcdir = srcdir;
079: }
080:
081: private Connection createConnection() {
082: return net.sourceforge.jaxor.db.ConnectionRegistry
083: .getConnection(getDriverClass()).create(getUrl(),
084: getUser(), getPassword());
085: }
086:
087: private Class getDriverClass() {
088: try {
089: return Class.forName(getDriver());
090: } catch (ClassNotFoundException e) {
091: throw new SystemException(e);
092: }
093: }
094:
095: public void execute() {
096: String[] files = getDirectoryScanner(_srcdir)
097: .getIncludedFiles();
098: log("Validating: " + files.length + " entities");
099: Connection conn = createConnection();
100: Validation val = new Validation();
101: for (int i = 0; i < files.length; i++) {
102: MetaRow row = getMetaRowForClass(files[i]);
103: val.add(row.validateColumns(conn, _capitalizeTable));
104: }
105: try {
106: conn.close();
107: } catch (SQLException e) {
108: e.printStackTrace();
109: }
110: if (_verbose)
111: log(val.toString());
112:
113: if (val.hasErrors())
114: throw new BuildException(val.toString());
115: }
116:
117: public MetaRow getMetaRowForClass(String src) {
118: Class clzz = loadClass(src);
119: try {
120: return (MetaRow) clzz.newInstance();
121: } catch (Exception e) {
122: throw new SystemException("Failed to invoke method on "
123: + clzz.getName(), e);
124: }
125: }
126:
127: private Class loadClass(String src) {
128: ClassLoader loader = getClass().getClassLoader();
129: Class clzz = null;
130: try {
131: clzz = loader.loadClass(FileUtils.toClassFileName(src));
132: } catch (ClassNotFoundException e) {
133: throw new SystemException(e);
134: }
135: return clzz;
136: }
137:
138: }
|