001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.tools.generators;
010:
011: import com.completex.objective.components.log.Log;
012: import com.completex.objective.components.log.impl.PrimitiveLogImpl;
013: import com.completex.objective.components.log.adapter.StdOutputLogAdapter;
014: import com.completex.objective.components.persistency.JavaToMetaType;
015: import com.completex.objective.components.persistency.JavaToMetaTypeImpl;
016: import com.completex.objective.components.persistency.MetaColumn;
017: import com.completex.objective.components.persistency.Persistency2;
018: import com.completex.objective.components.persistency.core.DatabasePolicy;
019: import com.completex.objective.components.persistency.core.adapter.DefaultPersistencyAdapter;
020: import com.completex.objective.components.persistency.core.impl.DatabasePolicyFactory;
021: import com.completex.objective.components.persistency.meta.MetaModel;
022: import com.completex.objective.components.persistency.meta.ModelLoader;
023: import com.completex.objective.components.persistency.meta.adapter.ModelLoaderAdapter;
024: import com.completex.objective.components.persistency.meta.adapter.ModelStorerAdapter;
025: import com.completex.objective.components.persistency.transact.Transaction;
026: import com.completex.objective.components.sdl.reader.SdlReader;
027: import com.completex.objective.components.sdl.reader.impl.SdlReaderImpl;
028: import com.completex.objective.util.PropertyMap;
029:
030: import java.io.BufferedReader;
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.FileNotFoundException;
034: import java.io.FileReader;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.Reader;
038: import java.sql.Connection;
039: import java.sql.SQLException;
040: import java.util.Arrays;
041: import java.util.LinkedHashSet;
042: import java.util.Map;
043: import java.util.Properties;
044:
045: /**
046: * @author Gennady Krizhevsky
047: */
048: public abstract class AbstractPersistentDescriptorGenerator {
049: protected static SdlReader sdlReader = new SdlReaderImpl();
050: protected static final Log logger = StdOutputLogAdapter
051: .newInfoLogInstance();
052: protected Transaction transaction;
053: protected Persistency2 persistency;
054: protected ModelLoaderAdapter modelLoaderAdapter;
055: protected ModelStorerAdapter modelStorerAdapter;
056: protected ModelTransformer[] transformers;
057: protected UserDefinedTypeModelTransformer[] udtTransformers;
058: protected MetaColumn.GeneratorStruct creationDateStruct;
059: protected MetaColumn.GeneratorStruct lastUpdatedStruct;
060: public static final String TAG_OUT_DIR = "out_dir"; // Top level directory for generated classes
061: public static final String TAG_USER = "user";
062: public static final String TAG_URL = "url";
063: public static final String TAG_PASSWORD = "password";
064: public static final String TAG_DRIVER = "driver";
065: public static final String TAG_SCHEMA = "schema";
066: public static final String TAG_CATALOG = "catalog";
067: public static final String TAG_FILTER_PATTERN = "filter_pattern";
068: public static final String TAG_REQUIRED_PASSWORD = "required_password";
069: public static final String TAG_USE_PRIMITIVES = "use_primitives";
070: public static final String TAG_USE_PRIMITIVES_FOR_NOT_NULL_NUMBERS = "use_primitives_for_required";
071: public static final String TAG_CHAR1_BOOLEAN = "char1boolean";
072: public static final String TAG_LAST_UPDATED_NAMES = "last_updated_names";
073: public static final String TAG_CREATION_DATE_NAMES = "creation_date_names";
074: public static final String TAG_FORCE_INTERNAL_FROM_FILE = "force_internal_from_file";
075: public static final String TAG_PLUGINS_CONFIG_PATH = "plugins_config_path";
076: public static final String TAG_INTERNAL_DESC_FILE = "internal_desc_file";
077: public static final String TAG_EXTERNAL_DESC_FILE = "external_desc_file";
078: public static final String TAG_DEBUG = "debug";
079: protected String outDir;
080: protected String internalFileName;
081: protected String externalFileName;
082: protected String schema;
083: protected String catalog;
084: protected String filterPattern;
085: protected boolean usePrimitives;
086: protected boolean usePrimitivesForRequired;
087: protected boolean char1boolean;
088: protected boolean forceInternalFromFile;
089: protected boolean debug;
090:
091: PropertyMap propertyMap = new PropertyMap();
092:
093: public void dispose() {
094: try {
095: if (persistency != null) {
096: persistency.getTransactionManager().rollback(
097: transaction);
098: persistency.getTransactionManager().shutdown();
099: }
100: } catch (SQLException e) {
101: // Do nothing
102: }
103: }
104:
105: protected ModelStorerAdapter getModelStorerAdapter(String outDir,
106: String internalFileName, String externalFileName)
107: throws IOException {
108: return new ModelStorerAdapter(outDir, internalFileName,
109: externalFileName);
110: }
111:
112: protected ModelLoader getExternalFileModelLoader(String outDir,
113: String externalFileName, String filterPattern)
114: throws FileNotFoundException {
115: File externalFile = new File(outDir + "/" + externalFileName);
116: ModelLoader externalFileModelLoader = null;
117: if (externalFile.exists()) {
118: Reader externalFileReader = new BufferedReader(
119: new FileReader(externalFile));
120: externalFileModelLoader = new ModelLoaderAdapter.ExternalFileModelLoaderAdapter(
121: externalFileReader, filterPattern);
122: }
123: return externalFileModelLoader;
124: }
125:
126: protected abstract ModelLoader getDatabaseModelLoaderAdapter(
127: String filterPattern, JavaToMetaType javaToMetaType,
128: String schema, String catalog) throws SQLException;
129:
130: protected ModelLoaderAdapter getModelLoaderAdapter(
131: ModelLoader internalLoader,
132: ModelLoader externalFileModelLoader,
133: JavaToMetaType javaToMetaType) {
134: return new ModelLoaderAdapter(internalLoader,
135: externalFileModelLoader, javaToMetaType,
136: creationDateStruct, lastUpdatedStruct, logger);
137: }
138:
139: protected ModelLoader resolveInternalLoader(
140: ModelLoader databaseModelLoaderAdapter,
141: ModelLoader internalFileModelLoader) {
142: ModelLoader internalLoader = databaseModelLoaderAdapter;
143: if (internalFileModelLoader != null) {
144: internalLoader = internalFileModelLoader;
145: }
146: return internalLoader;
147: }
148:
149: protected ModelLoader getInternalFileModelLoader(String outDir,
150: String internalFileName, boolean forceInternalFromFile,
151: String filterPattern) throws FileNotFoundException {
152: ModelLoader internalFileModelLoader = null;
153: File internalFile = new File(outDir + "/" + internalFileName);
154: if (forceInternalFromFile) {
155: if (internalFile.exists()) {
156: Reader internalFileReader = new BufferedReader(
157: new FileReader(internalFile));
158: internalFileModelLoader = new ModelLoaderAdapter.InternalFileModelLoaderAdapter(
159: internalFileReader, filterPattern);
160: } else {
161: throw new FileNotFoundException("File not found : "
162: + internalFile.getAbsolutePath());
163: }
164: }
165: return internalFileModelLoader;
166: }
167:
168: protected MetaColumn.GeneratorStruct toGeneratoStruct(
169: final String dateNames) {
170: MetaColumn.GeneratorStruct datesStruct = null;
171: if (dateNames != null && dateNames.trim().length() > 0) {
172: String[] names = dateNames.trim().split(",");
173: LinkedHashSet set = new LinkedHashSet(Arrays.asList(names));
174: datesStruct = new MetaColumn.GeneratorStruct(set);
175: }
176: return datesStruct;
177: }
178:
179: public void modifyProperties(Properties properties) {
180: }
181:
182: public Connection getConnection() throws SQLException {
183: return getTransaction().getConnection();
184: }
185:
186: public Transaction getTransaction() throws SQLException {
187: boolean closed = true;
188: try {
189: if (transaction != null
190: && !transaction.getConnection().isClosed()) {
191: closed = false;
192: }
193: } catch (SQLException e) {
194: // Do nothing
195: }
196: return closed ? persistency.getTransactionManager().begin()
197: : transaction;
198: }
199:
200: public MetaModel loadModel() throws Exception {
201: logger.info("Load Model started");
202: MetaModel model = null;
203: try {
204: model = modelLoaderAdapter.load(null);
205: } finally {
206: logger.info("Load Model ended, model size = "
207: + (model == null ? "0" : model.size() + ""));
208: }
209: return model;
210: }
211:
212: public void storeModel(MetaModel model) throws Exception {
213: logger.info("Store Model started");
214: try {
215: modelStorerAdapter.store(model);
216: } finally {
217: logger.info("Store Model ended");
218: }
219: }
220:
221: protected void transformModel(MetaModel model) {
222: getLogger().info("Transform model started");
223: try {
224: if (transformers != null) {
225: for (int i = 0; i < transformers.length; i++) {
226: transformers[i].transformModel(model);
227: }
228: }
229:
230: if (udtTransformers != null) {
231: for (int i = 0; i < udtTransformers.length; i++) {
232: udtTransformers[i].transformModel(model
233: .getUserDefinedTypeMetaModel());
234: }
235: }
236: } finally {
237: getLogger().info("Transform model ended");
238: }
239:
240: }
241:
242: public static Log getLogger() {
243: return logger;
244: }
245:
246: public MetaModel generate() throws Exception {
247: MetaModel model = loadModel();
248: transformModel(model);
249: // logger.info(model);
250: storeModel(model);
251: return model;
252: }
253:
254: public void setup(String propertiesPath) throws IOException,
255: IllegalAccessException, InstantiationException,
256: ClassNotFoundException, SQLException {
257: setup(propertiesPath, null);
258: }
259:
260: public void setup(String propertiesPath, String envPath)
261: throws IOException, IllegalAccessException,
262: InstantiationException, ClassNotFoundException,
263: SQLException {
264: if (propertiesPath == null) {
265: throw new IllegalArgumentException("propertiesPath is null");
266: }
267: InputStream inputStream = new FileInputStream(propertiesPath);
268: setup(inputStream, env(envPath));
269: }
270:
271: public void setup(final InputStream inputStream)
272: throws IOException, ClassNotFoundException,
273: IllegalAccessException, InstantiationException,
274: SQLException {
275: setup(inputStream, null);
276: }
277:
278: public void setup(final InputStream inputStream, Properties env)
279: throws IOException, ClassNotFoundException,
280: IllegalAccessException, InstantiationException,
281: SQLException {
282: propertyMap.load(inputStream);
283: PropertyMap.resolveParameters(propertyMap, env);
284: setup(propertyMap);
285: }
286:
287: protected void openTransaction(PropertyMap propertyMap)
288: throws SQLException {
289: String user = propertyMap.getPropertyTrimmed(TAG_USER, true);
290: boolean requiredPassword = propertyMap
291: .getBoolean(TAG_REQUIRED_PASSWORD);
292: final String driver = propertyMap.getPropertyTrimmed(
293: TAG_DRIVER, true);
294: String url = propertyMap.getPropertyTrimmed(TAG_URL, true);
295: final String password = propertyMap.getPropertyTrimmed(
296: TAG_PASSWORD, requiredPassword);
297: DatabasePolicy policy = DatabasePolicyFactory.getInstance()
298: .getDatabasePolicy(url);
299:
300: persistency = new DefaultPersistencyAdapter(policy, driver,
301: url, user, password, 0, 3, 0, logger);
302: transaction = getTransaction();
303: }
304:
305: protected JavaToMetaType getJavaToMetaType() {
306: return new JavaToMetaTypeImpl(usePrimitives,
307: usePrimitivesForRequired, char1boolean);
308: }
309:
310: protected void initModelLoader(JavaToMetaType javaToMetaType)
311: throws IOException, SQLException {
312: ModelLoader externalFileModelLoader = getExternalFileModelLoader(
313: outDir, externalFileName, filterPattern);
314: ModelLoader databaseModelLoaderAdapter = getDatabaseModelLoaderAdapter(
315: filterPattern, javaToMetaType, schema, catalog);
316: ModelLoader internalFileModelLoader = getInternalFileModelLoader(
317: outDir, internalFileName, forceInternalFromFile,
318: filterPattern);
319: ModelLoader internalLoader = resolveInternalLoader(
320: databaseModelLoaderAdapter, internalFileModelLoader);
321: modelLoaderAdapter = getModelLoaderAdapter(internalLoader,
322: externalFileModelLoader, javaToMetaType);
323: modelStorerAdapter = getModelStorerAdapter(outDir,
324: internalFileName, externalFileName);
325: }
326:
327: protected abstract void extractExternalFileName(
328: PropertyMap propertyMap);
329:
330: protected abstract void extractInternalFileName(
331: PropertyMap propertyMap);
332:
333: protected void initCoreVariables(PropertyMap propertyMap) {
334: creationDateStruct = toGeneratoStruct(propertyMap
335: .getProperty(TAG_CREATION_DATE_NAMES));
336: lastUpdatedStruct = toGeneratoStruct(propertyMap
337: .getProperty(TAG_LAST_UPDATED_NAMES));
338: outDir = propertyMap.getProperty(TAG_OUT_DIR, true);
339: extractInternalFileName(propertyMap);
340: extractExternalFileName(propertyMap);
341: schema = propertyMap.getProperty(TAG_SCHEMA);
342: catalog = propertyMap.getProperty(TAG_CATALOG);
343: filterPattern = propertyMap.getProperty(TAG_FILTER_PATTERN);
344: usePrimitives = propertyMap.getBoolean(TAG_USE_PRIMITIVES);
345: usePrimitivesForRequired = propertyMap
346: .getBoolean(TAG_USE_PRIMITIVES_FOR_NOT_NULL_NUMBERS);
347: char1boolean = propertyMap.getBoolean(TAG_CHAR1_BOOLEAN,
348: Boolean.FALSE, true);
349: forceInternalFromFile = propertyMap
350: .getBoolean(TAG_FORCE_INTERNAL_FROM_FILE);
351: debug = propertyMap.getBoolean(TAG_DEBUG);
352: if (logger instanceof PrimitiveLogImpl) {
353: ((PrimitiveLogImpl) logger).setDebugEnabled(debug);
354: }
355: }
356:
357: public void setup(PropertyMap propertyMap) throws IOException,
358: ClassNotFoundException, InstantiationException,
359: IllegalAccessException, SQLException {
360: modifyProperties(propertyMap);
361: //
362: // 1st extract
363: //
364: initCoreVariables(propertyMap);
365: //
366: // Open transaction has to happen before Initialize ModelLoader
367: //
368: openTransaction(propertyMap);
369: //
370: // Factories have to be created before Initializing ModelLoader
371: //
372: beforeInitModelLoader(propertyMap);
373: //
374: // Initialize ModelLoader
375: //
376: JavaToMetaType javaToMetaType = getJavaToMetaType();
377: initModelLoader(javaToMetaType);
378: //
379: // Plugins setup:
380: //
381: Map pluginsConfig = setupPlugins(propertyMap);
382: //
383: //
384: //
385: setupTransformers(pluginsConfig);
386: //
387: // Transformers setup:
388: //
389: setupUdtTransformers(pluginsConfig);
390:
391: System.out.println("GENERATED FILES IN " + outDir);
392: }
393:
394: protected void setupUdtTransformers(Map pluginsConfig)
395: throws ClassNotFoundException, IllegalAccessException,
396: InstantiationException {
397: }
398:
399: protected void setupTransformers(Map pluginsConfig)
400: throws ClassNotFoundException, IllegalAccessException,
401: InstantiationException {
402: }
403:
404: protected abstract void beforeInitModelLoader(
405: PropertyMap propertyMap) throws IOException;
406:
407: protected Map setupPlugins(PropertyMap propertyMap)
408: throws IOException, ClassNotFoundException,
409: InstantiationException, IllegalAccessException {
410: return null;
411: }
412:
413: public PropertyMap getPropertyMap() {
414: return propertyMap;
415: }
416:
417: protected PropertyMap env(String envPath) throws IOException {
418: return envPath == null ? null : PropertyMap
419: .createAndLoad(envPath);
420: }
421: }
|