001: package net.sourceforge.jaxor.generator;
002:
003: import net.sourceforge.jaxor.parser.CodeTemplate;
004: import net.sourceforge.jaxor.parser.Jaxor;
005: import net.sourceforge.jaxor.util.SystemException;
006: import org.apache.tools.ant.BuildException;
007: import org.apache.tools.ant.Task;
008: import org.apache.velocity.VelocityContext;
009: import org.apache.velocity.runtime.RuntimeServices;
010: import org.apache.velocity.runtime.log.LogSystem;
011: import org.apache.regexp.RESyntaxException;
012:
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.IOException;
016: import java.sql.Connection;
017: import java.sql.DriverManager;
018: import java.sql.ResultSet;
019: import java.sql.SQLException;
020: import java.text.MessageFormat;
021: import java.util.Date;
022: import java.util.Enumeration;
023: import java.util.Properties;
024:
025: /**
026: * Created By: fede
027: * Date: 2-feb-2004
028: * Time: 14.40.51
029: *
030: * Last Checkin: $Author: fspinazzi $
031: * Date: $Date: 2004/02/11 13:50:54 $
032: * Revision: $Revision: 1.7 $
033: */
034: public class MappingGeneratorTask extends Task implements LogSystem {
035: private Sql2JavaMapping mapping = new Sql2JavaMapping();
036:
037: private String driver;
038: private String url;
039: private String user;
040: private String password;
041: private String schema;
042: private String destDir;
043: private String catalog;
044: private String packageName;
045: private String mappingProperties;
046: private String tableRegExp;
047: private boolean verbose;
048: private int _logLevel = 2;
049: public static final String ALL_TABLE_REGEXP = ".+";
050:
051: public String getDriver() {
052: return driver;
053: }
054:
055: public void setDriver(String driver) {
056: this .driver = driver;
057: }
058:
059: public String getUrl() {
060: return url;
061: }
062:
063: public void setUrl(String url) {
064: this .url = url;
065: }
066:
067: public String getUser() {
068: return user;
069: }
070:
071: public void setUser(String user) {
072: this .user = user;
073: }
074:
075: public String getPassword() {
076: return password;
077: }
078:
079: public void setPassword(String password) {
080: this .password = password;
081: }
082:
083: public String getSchema() {
084: return schema;
085: }
086:
087: public void setSchema(String schema) {
088: this .schema = schema;
089: }
090:
091: public boolean isVerbose() {
092: return verbose;
093: }
094:
095: public void setVerbose(boolean verbose) {
096: this .verbose = verbose;
097: }
098:
099: public String getCatalog() {
100: return catalog;
101: }
102:
103: public void setCatalog(String catalog) {
104: this .catalog = catalog;
105: }
106:
107: public String getDestDir() {
108: return destDir;
109: }
110:
111: public void setDestDir(String destDir) {
112: this .destDir = destDir;
113: }
114:
115: public String getPackageName() {
116: return packageName;
117: }
118:
119: public void setPackageName(String packageName) {
120: this .packageName = packageName;
121: }
122:
123: public String getMappingProperties() {
124: return mappingProperties;
125: }
126:
127: public void setMappingProperties(String mappingProperties) {
128: this .mappingProperties = mappingProperties;
129: }
130:
131: public void execute() throws BuildException {
132:
133: Connection connection = null;
134: ResultSet tables = null;
135: try {
136: connection = estabilishConnection();
137: log(MessageFormat.format(
138: "reversing schema {0} from jdbc url {1}",
139: new Object[] { getSchema(), getUrl() }));
140: registerMappings();
141: if (getTableRegExp() != ALL_TABLE_REGEXP)
142: log("only tables matching '" + getTableRegExp()
143: + "' will be processed");
144: MappingMetaData mappingMeta = null;
145: try {
146: mappingMeta = new MappingMetaData(connection,
147: getCatalog(), getSchema(), getMapping(),
148: getTableRegExp()) {
149: protected void logUnknownDataType(int dataType,
150: String dataName, String name) {
151: log("warning: unknown sql data type "
152: + dataType + "(" + dataName
153: + ") column: " + name);
154: }
155:
156: protected void log(String msg) {
157: MappingGeneratorTask.this .log(msg);
158: }
159: };
160: } catch (RESyntaxException e) {
161: throw new BuildException(tableRegExp
162: + " is not a valid regular expression: "
163: + e.getMessage());
164: }
165: Jaxor[] jaxors = mappingMeta.getMapped();
166: for (int i = 0; i < jaxors.length; i++) {
167: Jaxor jaxor = jaxors[i];
168: File destFile = new File(getDestDir() + File.separator
169: + jaxor.getEntity().getJavaName() + ".jaxor");
170: log("Generating mapping file " + destFile);
171: jaxor.setPackage(getPackageName());
172: final VelocityContext context = new VelocityContext();
173: context.put("jaxor", jaxor);
174: context.put("now", new Date());
175: CodeTemplate template = new CodeTemplate(this , context);
176: template.write("jaxor.vm", destFile);
177: }
178: } catch (SystemException e) {
179: throw new BuildException(e.getMessage());
180: } finally {
181: try {
182: if (tables != null)
183: tables.close();
184: if (connection != null)
185: connection.close();
186: } catch (SQLException ignored) {
187: }
188: }
189: }
190:
191: private Sql2JavaMapping getMapping() {
192: return mapping;
193: }
194:
195: private void dafaultTableRegexp() {
196: tableRegExp = ALL_TABLE_REGEXP;
197: }
198:
199: private void registerMappings() throws SystemException {
200: if (getMappingProperties() == null)
201: return;
202: Properties properties = new Properties();
203: try {
204: final FileInputStream inStream = new FileInputStream(
205: getMappingProperties());
206: properties.load(inStream);
207: inStream.close();
208: } catch (IOException e) {
209: throw new SystemException(
210: "error opening/reading from file "
211: + getMappingProperties());
212: }
213: if (properties.size() > 0)
214: log("read additional mappings from file '"
215: + getMappingProperties() + "'");
216: final Enumeration enumeration = properties.propertyNames();
217: while (enumeration.hasMoreElements()) {
218: final String typeToMap = (String) enumeration.nextElement();
219: ;
220: final String typeMappedTo = properties
221: .getProperty(typeToMap);
222: String msg = MessageFormat.format(
223: "registering mapping from <{0}> to <{1}>",
224: new Object[] { typeToMap, typeMappedTo });
225: log(msg);
226: mapping.register(typeToMap, typeMappedTo);
227: }
228: }
229:
230: private Connection estabilishConnection() throws SystemException {
231: try {
232: Class.forName(getDriver()).newInstance();
233: Connection connection = DriverManager.getConnection(
234: getUrl(), getUser(), getPassword());
235: return connection;
236: } catch (InstantiationException e) {
237: throw new SystemException();
238: } catch (IllegalAccessException e) {
239: throw new SystemException();
240: } catch (ClassNotFoundException e) {
241: throw new SystemException(
242: "cannot create instance of driver '" + getDriver()
243: + "'");
244: } catch (SQLException e) {
245: throw new SystemException("cannot get a connection to '"
246: + getUrl() + "'");
247: }
248: }
249:
250: public void init(RuntimeServices runtimeServices) throws Exception {
251: }
252:
253: public void logVelocityMessage(int i, String s) {
254: if (i >= _logLevel)
255: log(s, i);
256: }
257:
258: public String getTableRegExp() {
259: if (tableRegExp == null)
260: dafaultTableRegexp();
261: return tableRegExp;
262: }
263:
264: public void setTableRegExp(String tableRegExp) {
265: this.tableRegExp = tableRegExp;
266: }
267: }
|