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.persistency.JavaToMetaType;
012: import com.completex.objective.components.persistency.UserDefinedTypeMetaModel;
013: import com.completex.objective.components.persistency.meta.ModelLoaderPlugin;
014:
015: import java.io.File;
016: import java.io.FileReader;
017: import java.io.IOException;
018: import java.io.Reader;
019: import java.sql.Connection;
020: import java.util.Map;
021:
022: /**
023: * @author Gennady Krizhevsky
024: */
025: public abstract class FileModelLoaderPluginImpl implements
026: ModelLoaderPlugin {
027:
028: private String modeDescriptorFilePath;
029: private Reader reader;
030: private String filterPattern;
031: private boolean debug;
032:
033: protected FileModelLoaderPluginImpl(String descriptorPath,
034: String filterPattern) {
035: this .modeDescriptorFilePath = descriptorPath;
036: this .filterPattern = filterPattern;
037: }
038:
039: public FileModelLoaderPluginImpl(Reader reader, String filterPattern) {
040: this .reader = reader;
041: this .filterPattern = filterPattern;
042: }
043:
044: public String getFilterPattern() {
045: return filterPattern;
046: }
047:
048: public UserDefinedTypeMetaModel load() throws Exception {
049: return this .load(new UserDefinedTypeMetaModel());
050: }
051:
052: public UserDefinedTypeMetaModel load(UserDefinedTypeMetaModel model) {
053: if (model == null) {
054: model = new UserDefinedTypeMetaModel();
055: }
056: try {
057: if (modeDescriptorFilePath != null) {
058: // 1st check if file exists - if not - exit:
059: String fileName = modeDescriptorFilePath;
060: File file = new File(fileName);
061: if (!file.exists() || file.isDirectory()) {
062: info("FileModelLoaderPluginImpl::load(): plugin descriptor file ["
063: + fileName
064: + "] does not exist - exiting load()");
065: return null;
066: } else {
067: info("FileModelLoaderPluginImpl::load(): loading plugin descriptor from file ["
068: + fileName + "]");
069: }
070: reader = new FileReader(file);
071: } else if (reader == null) {
072: throw new RuntimeException(
073: "Both modeDescriptorFilePath & reader are not set");
074: }
075:
076: Map modelMap = readModelMap(reader);
077: info("modelMap = " + modelMap);
078:
079: model = toModel(model, modelMap);
080: info("model = " + model);
081:
082: } catch (Exception e) {
083: throw new RuntimeException("Cannot load file "
084: + modeDescriptorFilePath, e);
085: }
086: return model;
087: }
088:
089: abstract protected UserDefinedTypeMetaModel toModel(
090: UserDefinedTypeMetaModel model, Map modelMap);
091:
092: abstract protected Map readModelMap(Reader reader)
093: throws IOException;
094:
095: private void info(String s) {
096: if (debug) {
097: System.out.println(s);
098: }
099: }
100:
101: public void setJavaToMetaType(JavaToMetaType javaToMetaType) {
102: }
103:
104: public boolean needsConnection() {
105: return false;
106: }
107:
108: public void setConnection(Connection connection) {
109: }
110:
111: }
|