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