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.components.persistency.meta.impl;
010:
011: import com.completex.objective.components.log.Log;
012: import com.completex.objective.components.log.adapter.StdErrorLogAdapter;
013: import com.completex.objective.components.persistency.UserDefinedTypeMetaModel;
014: import com.completex.objective.components.persistency.meta.ModelStorerPlugin;
015: import com.completex.objective.components.sdl.writer.SdlWriter;
016: import com.completex.objective.components.sdl.writer.impl.SdlPrinterImpl;
017: import com.completex.objective.util.PropertyMap;
018:
019: import java.io.IOException;
020: import java.io.Writer;
021: import java.util.Map;
022:
023: /**
024: * @author Gennady Krizhevsky
025: */
026: public class UdtModelStorerPlugin implements ModelStorerPlugin {
027:
028: private Log logger = StdErrorLogAdapter.newLogInstance();
029: public static final String PLUGIN_KEY = "udt";
030: private static final SdlWriter sdlWriter = new SdlPrinterImpl();
031:
032: private ExternalFileModelStorerAdapter externalFileModelStorerAdapter;
033: private InternalFileModelStorerAdapter internalFileModelStorerAdapter;
034:
035: public UdtModelStorerPlugin() {
036: }
037:
038: public UdtModelStorerPlugin(String externalFileName,
039: String internalFileName, String outDir) {
040: setUp(outDir, internalFileName, externalFileName);
041: }
042:
043: public void configure(PropertyMap udtProperties) throws IOException {
044: String outDir = udtProperties.getProperty("out_dir", true);
045: String externalFileName = udtProperties.getProperty(
046: "external_desc_file", true);
047: String internalFileName = udtProperties.getProperty(
048: "internal_desc_file", true);
049: setUp(outDir, internalFileName, externalFileName);
050: }
051:
052: private void setUp(String outDir, String internalFileName,
053: String externalFileName) {
054: internalFileModelStorerAdapter = new InternalFileModelStorerAdapter(
055: outDir, internalFileName);
056: if (externalFileName != null) {
057: externalFileModelStorerAdapter = new ExternalFileModelStorerAdapter(
058: outDir, externalFileName);
059: }
060: }
061:
062: public String getPluginKey() {
063: return PLUGIN_KEY;
064: }
065:
066: public void store(UserDefinedTypeMetaModel model)
067: throws IOException {
068: internalFileModelStorerAdapter.store(model);
069: if (externalFileModelStorerAdapter != null) {
070: externalFileModelStorerAdapter.store(model);
071: }
072: }
073:
074: public ExternalFileModelStorerAdapter getExternalFileModelStorerAdapter() {
075: return externalFileModelStorerAdapter;
076: }
077:
078: public InternalFileModelStorerAdapter getInternalFileModelStorerAdapter() {
079: return internalFileModelStorerAdapter;
080: }
081:
082: //
083: //
084: // Auxilliary adapters
085: //
086: //
087: class ExternalFileModelStorerAdapter extends
088: FileModelStorerPluginImpl {
089:
090: public ExternalFileModelStorerAdapter(String outputDir,
091: String fileName) {
092: super (outputDir, fileName);
093: }
094:
095: public void configure(PropertyMap pluginConfig)
096: throws IOException {
097: }
098:
099: public String getPluginKey() {
100: return PLUGIN_KEY;
101: }
102:
103: protected Map toMap(UserDefinedTypeMetaModel model) {
104: return model.toExternalMap();
105: }
106:
107: protected void writeModel(Map modelMap, Writer writer)
108: throws IOException {
109: try {
110: sdlWriter.write(writer, modelMap);
111: } finally {
112: try {
113: writer.flush();
114: } catch (IOException e) {
115: error("Cannot flush", e);
116: }
117: try {
118: writer.close();
119: } catch (IOException e) {
120: error("Cannot close", e);
121: }
122: }
123: }
124: }
125:
126: private void error(String s, Exception e) {
127: logger.error(s, e);
128: }
129:
130: class InternalFileModelStorerAdapter extends
131: FileModelStorerPluginImpl {
132:
133: public InternalFileModelStorerAdapter(String outputDir,
134: String fileName) {
135: super (outputDir, fileName);
136: }
137:
138: public void configure(PropertyMap pluginConfig)
139: throws IOException {
140: }
141:
142: public String getPluginKey() {
143: return PLUGIN_KEY;
144: }
145:
146: protected Map toMap(UserDefinedTypeMetaModel model) {
147: return model.toInternalMap();
148: }
149:
150: protected void writeModel(Map modelMap, Writer writer)
151: throws IOException {
152: try {
153: sdlWriter.write(writer, modelMap);
154: } finally {
155: try {
156: writer.flush();
157: } catch (IOException e) {
158: error("Cannot flush", e);
159: }
160: try {
161: writer.close();
162: } catch (IOException e) {
163: error("Cannot close", e);
164: }
165: }
166: }
167: }
168:
169: }
|