001: /*
002: * $Id: ExternalTableFactory.java,v 1.7 2005/06/29 21:11:10 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040: package org.axiondb.engine.tables;
041:
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Properties;
046:
047: import org.axiondb.AxionException;
048: import org.axiondb.Column;
049: import org.axiondb.Database;
050: import org.axiondb.ExternalTable;
051: import org.axiondb.ExternalTableLoader;
052: import org.axiondb.Table;
053: import org.axiondb.TableFactory;
054:
055: /**
056: * Implementation of ExternalTableFactory, to generate instances of concrete
057: * implementations of ExternalTable, such as flatfile and remote tables.
058: *
059: * @author Jonathan Giron
060: * @author Ahimanikya Satapathy
061: * @version $Revision: 1.7 $ $Date: 2005/06/29 21:11:10 $
062: */
063: public class ExternalTableFactory implements TableFactory {
064:
065: public static final String TYPE_DELIMITED = "DELIMITED"; // NOI18N
066: public static final String TYPE_FIXEDWIDTH = "FIXEDWIDTH"; // NOI18N
067: public static final String TYPE_TAGGEDEBCDIC = "TAGGEDEBCDIC"; // NOI18N
068: public static final String TYPE_REMOTE = "REMOTE"; // NOI18N
069: public static final String TYPE_REMOTE_AXION = "REMOTE_AXION"; // NOI18N
070:
071: public static final HashMap EXTERNAL_LOADERS = new HashMap(4);
072:
073: static {
074: EXTERNAL_LOADERS.put(TYPE_DELIMITED,
075: new DelimitedFlatfileTableLoader());
076: EXTERNAL_LOADERS.put(TYPE_FIXEDWIDTH,
077: new FixedWidthFlatfileTableLoader());
078: EXTERNAL_LOADERS.put(TYPE_TAGGEDEBCDIC,
079: new TaggedEBCDICTableLoader());
080: EXTERNAL_LOADERS.put(TYPE_REMOTE,
081: new ExternalDatabaseTableLoader());
082: EXTERNAL_LOADERS.put(TYPE_REMOTE_AXION,
083: new ExternalAxionDBTableLoader());
084: }
085:
086: public Table createTable(Database database, String name)
087: throws AxionException {
088: throw new AxionException(
089: "Unsupported method - use overloaded method to "
090: + "supply external properties and column metadata.");
091: }
092:
093: public ExternalTable createTable(final Database database,
094: final String name, Properties props, List columns)
095: throws AxionException {
096: assertValidProperty(props);
097:
098: String type = props.getProperty(ExternalTable.PROP_LOADTYPE);
099: String vendor = props.getProperty(ExternalTable.PROP_VENDOR);
100: if (vendor != null && vendor.trim().length() != 0) {
101: type = type + "_" + vendor;
102: }
103:
104: ExternalTableLoader loader = (ExternalTableLoader) EXTERNAL_LOADERS
105: .get(type.toUpperCase());
106:
107: if (loader != null) {
108: ExternalTable table = loader.createExternalTable(database,
109: name);
110:
111: if (columns == null || columns.isEmpty()) {
112: throw new AxionException(
113: "Must supply List of Column instances to populate column metadata");
114: }
115:
116: // Must add column information before invoking setTableProperties(), since we
117: // build our remote create and/or select statements from Axion column metadata
118: Iterator iter = columns.iterator();
119: while (iter.hasNext()) {
120: table.addColumn((Column) iter.next());
121: }
122:
123: table.loadExternalTable(props);
124: return table;
125: }
126: throw new AxionException("Unknown external table type: " + type);
127: }
128:
129: public void assertValidProperty(Properties props)
130: throws AxionException {
131: if (props == null || props.isEmpty()
132: || props.get(ExternalTable.PROP_LOADTYPE) == null) {
133: throw new AxionException(
134: "LOADTYPE property required in organization clause.");
135: }
136: }
137: }
|