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.persistency.JavaToMetaType;
012: import com.completex.objective.components.persistency.meta.ModelLoader;
013: import com.completex.objective.components.persistency.meta.ModelLoaderPlugin;
014: import com.completex.objective.components.persistency.meta.ModelStorerPlugin;
015: import com.completex.objective.components.persistency.meta.adapter.ModelLoaderAdapter;
016: import com.completex.objective.components.persistency.meta.impl.UdtBaseModelLoaderPlugin;
017: import com.completex.objective.util.PropertyMap;
018:
019: import java.io.FileReader;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.sql.SQLException;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: /**
028: * Args[0]: persistent-descriptor-config.properties
029: *
030: * @author Gennady Krizhevsky
031: */
032: public class PersistentDescriptorGenerator extends
033: AbstractPersistentDescriptorGenerator {
034: public static final String DEFAULT_INTERNAL_DESC_FILE_NAME = "internal_desc_file.sdl";
035: public static final String DEFAULT_EXTERNAL_DESC_FILE_NAME = "external_desc_file.sdl";
036: public static final String TAG_TRANSFORMERS = "transformers";
037:
038: public PersistentDescriptorGenerator() {
039: }
040:
041: public PersistentDescriptorGenerator(String propertiesPath)
042: throws IOException, IllegalAccessException,
043: ClassNotFoundException, InstantiationException,
044: SQLException {
045: this (propertiesPath, null);
046: }
047:
048: public PersistentDescriptorGenerator(String propertiesPath,
049: String envPath) throws IOException, IllegalAccessException,
050: ClassNotFoundException, InstantiationException,
051: SQLException {
052: setup(propertiesPath, envPath);
053: }
054:
055: /*
056: {
057: plugins = {
058: PLUGIN_01 = {
059: loader = {
060: class = <className> # Required, must be of ModelLoaderPlugin type
061: # config = {} # Optional
062: }
063:
064: storer = {
065: class = <className> # Required, must be of ModelStorerPlugin type
066: # config = {} # Optional
067: }
068: # ...
069: }
070: }
071:
072: transform = {
073: transformers = [
074: {
075: class = <className> # Required, must be of ModelTransformer type
076: # config = {} # Optional
077: }
078: ]
079: }
080: }
081: */
082: public PersistentDescriptorGenerator(PropertyMap propertyMap)
083: throws IOException, IllegalAccessException,
084: ClassNotFoundException, InstantiationException,
085: SQLException {
086: setup(propertyMap);
087: }
088:
089: public PersistentDescriptorGenerator(InputStream inputStream)
090: throws IOException, IllegalAccessException,
091: InstantiationException, ClassNotFoundException,
092: SQLException {
093: setup(inputStream);
094: }
095:
096: protected void beforeInitModelLoader(PropertyMap propertyMap) {
097: }
098:
099: protected void extractExternalFileName(PropertyMap propertyMap) {
100: externalFileName = propertyMap
101: .getProperty(TAG_EXTERNAL_DESC_FILE,
102: DEFAULT_EXTERNAL_DESC_FILE_NAME);
103: }
104:
105: protected void extractInternalFileName(PropertyMap propertyMap) {
106: internalFileName = propertyMap
107: .getProperty(TAG_INTERNAL_DESC_FILE,
108: DEFAULT_INTERNAL_DESC_FILE_NAME);
109: }
110:
111: protected ModelLoader getDatabaseModelLoaderAdapter(
112: String filterPattern, JavaToMetaType javaToMetaType,
113: String schema, String catalog) throws SQLException {
114: return new ModelLoaderAdapter.DatabaseModelLoaderAdapter(
115: schema, catalog, filterPattern, getConnection(), logger);
116: }
117:
118: protected void setupTransformers(Map map)
119: throws ClassNotFoundException, IllegalAccessException,
120: InstantiationException {
121: ModelTransformer[] transformers = null;
122: PropertyMap configPropertyMap = PropertyMap.toPropertyMap(map);
123: if (configPropertyMap != null) {
124: PropertyMap config = configPropertyMap
125: .getPropertyMap(ModelTransformer.KEY);
126: if (config != null) {
127: List transformerMaps = config.getList(TAG_TRANSFORMERS);
128: if (transformerMaps != null
129: && transformerMaps.size() > 0) {
130: transformers = new ModelTransformer[transformerMaps
131: .size()];
132: for (int i = 0; i < transformerMaps.size(); i++) {
133: PropertyMap transformerConfig = PropertyMap
134: .toPropertyMap(((Map) transformerMaps
135: .get(i)));
136: String className = transformerConfig
137: .getProperty("class", true);
138: Class clazz = Class.forName(className);
139: transformers[i] = (ModelTransformer) clazz
140: .newInstance();
141: transformers[i].configure(transformerConfig
142: .getPropertyMap("config"));
143: }
144: }
145: }
146: }
147: this .transformers = transformers;
148: }
149:
150: protected void setupUdtTransformers(Map udtMap)
151: throws ClassNotFoundException, IllegalAccessException,
152: InstantiationException {
153: UserDefinedTypeModelTransformer[] transformers = null;
154: PropertyMap configPropertyMap = PropertyMap
155: .toPropertyMap(udtMap);
156: if (configPropertyMap != null) {
157: PropertyMap config = configPropertyMap
158: .getPropertyMap(UdtBaseModelLoaderPlugin.PLUGIN_KEY);
159: if (config != null) {
160: List transformerMaps = config.getList(TAG_TRANSFORMERS);
161: if (transformerMaps != null
162: && transformerMaps.size() > 0) {
163: transformers = new UserDefinedTypeModelTransformer[transformerMaps
164: .size()];
165: for (int i = 0; i < transformerMaps.size(); i++) {
166: PropertyMap transformerConfig = PropertyMap
167: .toPropertyMap(((Map) transformerMaps
168: .get(i)));
169: String className = transformerConfig
170: .getProperty("class", true);
171: Class clazz = Class.forName(className);
172: transformers[i] = (UserDefinedTypeModelTransformer) clazz
173: .newInstance();
174: transformers[i].configure(transformerConfig
175: .getPropertyMap("config"));
176: }
177: }
178: }
179: }
180: udtTransformers = transformers;
181: }
182:
183: protected Map setupPlugins(PropertyMap propertyMap)
184: throws IOException, ClassNotFoundException,
185: InstantiationException, IllegalAccessException {
186: Map map = null;
187: String pluginsConfigPath = propertyMap
188: .getProperty(TAG_PLUGINS_CONFIG_PATH);
189: if (pluginsConfigPath != null) {
190: map = (Map) sdlReader
191: .read(new FileReader(pluginsConfigPath));
192: setupPlugins0(map);
193: }
194: return map;
195: }
196:
197: private void setupPlugins0(Map map) throws ClassNotFoundException,
198: InstantiationException, IllegalAccessException, IOException {
199: PropertyMap configPropertyMap = PropertyMap.toPropertyMap(map);
200: if (configPropertyMap != null) {
201: PropertyMap pluginsConfig = configPropertyMap
202: .getPropertyMap("plugins");
203: if (pluginsConfig != null) {
204: //
205: // Register plugins:
206: //
207: registerPlugins(pluginsConfig);
208: }
209: }
210: }
211:
212: private void registerPlugins(PropertyMap configPropertyMap)
213: throws ClassNotFoundException, InstantiationException,
214: IllegalAccessException, IOException {
215: for (Iterator it = configPropertyMap.keySet().iterator(); it
216: .hasNext();) {
217: String key = (String) it.next();
218: PropertyMap config = configPropertyMap.getPropertyMap(key,
219: true);
220: //
221: // Loader:
222: //
223: PropertyMap loaderConfig = config.getPropertyMap("loader");
224: if (loaderConfig != null) {
225: String classString = loaderConfig.getProperty("class",
226: true);
227: Class loaderClass = Class.forName(classString);
228: ModelLoaderPlugin loaderPlugin = (ModelLoaderPlugin) loaderClass
229: .newInstance();
230: modelLoaderAdapter.registerPlugin(loaderPlugin);
231: PropertyMap pluginConfig = loaderConfig
232: .getPropertyMap("config");
233: loaderPlugin.configure(pluginConfig);
234: if (loaderPlugin.needsConnection()) {
235: loaderPlugin.setConnection(transaction
236: .getConnection());
237: }
238: }
239: //
240: // Storer:
241: //
242: PropertyMap storerConfig = config.getPropertyMap("storer");
243: if (storerConfig != null) {
244: String classString = storerConfig.getProperty("class",
245: true);
246: Class storerClass = Class.forName(classString);
247: ModelStorerPlugin storerPlugin = (ModelStorerPlugin) storerClass
248: .newInstance();
249: modelStorerAdapter.registerPlugin(storerPlugin);
250: PropertyMap pluginConfig = storerConfig
251: .getPropertyMap("config");
252: storerPlugin.configure(pluginConfig);
253: }
254: }
255: }
256:
257: public static void main(String[] args) throws Exception {
258: if (args.length < 1) {
259: System.out
260: .println("Usage: [persisent-descriptor-generator] <persistent-descriptor-config.properties> <env.properties>\n"
261: + " where <persistent-descriptor-config.properties> - required configuration file path;\n"
262: + " <env.properties> - optional token resolution file path.");
263: System.exit(1);
264: }
265: String envPath = args.length > 1 ? args[1] : null;
266:
267: PersistentDescriptorGenerator generator = new PersistentDescriptorGenerator(
268: args[0], envPath);
269: generator.generate();
270: generator.dispose();
271: }
272: }
|