001: package com.jat.integration;
002:
003: import java.util.Enumeration;
004: import java.util.Hashtable;
005: import java.util.Properties;
006: import java.util.Vector;
007:
008: import com.jat.core.config.Config;
009: import com.jat.core.log.LogManager;
010: import com.jat.util.OrderVector;
011:
012: /**
013: * <p>Title: JAT</p>
014: * <p>Description:This class is an abstract class that implement Data Sources
015: * (see {@link com.jat.integration.DataSource}
016: * It is able to initialize Data Source loading its configured operations
017: * (see {@link com.jat.integration.GenericOperationDefinition})
018: * and relative configures fields
019: * (see {@link com.jat.integration.GenericFieldDefinition})</p>
020: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
021: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
022: * @author stf
023: * @version 1.0
024: * @since 1.2
025: */
026:
027: public abstract class GenericDataSource implements DataSource {
028:
029: /**
030: * Use this method to initilize your Data Source
031: * @throws IntegrationException, if any exeption occours
032: */
033: protected abstract void initDataSource()
034: throws IntegrationException;
035:
036: /**
037: * This method is used to get the {@link com.jat.integration.GenericOperationDefinition} implementaion
038: * of your GenericDataSource
039: * @return your {@link com.jat.integrationGenericOperationDefinition} implemnation instance
040: */
041: public abstract GenericOperationDefinition getEmptyOperationDefinition();
042:
043: /**
044: * Save the content of the Data Source
045: * @throws java.lang.Exception if any exception occour
046: */
047: public void persist() throws Exception {
048: Config config = this .getConfig();
049: Hashtable origSec = config.removeSection(this .getName());
050: config.addSection(this .getName());
051: Hashtable sec = config.getSection(this .getName());
052: sec = this .putInitProperties(sec);
053: int index = 1;
054: for (Enumeration e = this .operations.elements(); e
055: .hasMoreElements();) {
056: GenericOperationDefinition operation = (GenericOperationDefinition) e
057: .nextElement();
058: sec = this .getOperationConfig(sec, operation, index);
059: index++;
060: }
061: config.save();
062: }
063:
064: protected abstract Hashtable putInitProperties(Hashtable hash)
065: throws Exception;
066:
067: protected Hashtable getOperationConfig(Hashtable sec,
068: GenericOperationDefinition operation, int index) {
069: sec.put(operation.getConfigKey() + index
070: + operation.CONFIG_NAME, operation.getName());
071: sec.put(operation.getConfigKey() + index
072: + operation.CONFIG_VALUE, operation.getValue());
073: Properties properties = operation.getProperties();
074: for (Enumeration e = properties.propertyNames(); e
075: .hasMoreElements();) {
076: String prop = (String) e.nextElement();
077: String propVal = properties.getProperty(prop);
078: sec.put(operation.getConfigKey() + index + "." + prop,
079: propVal);
080: }
081: sec = this .getFieldConfig(sec, operation, index);
082:
083: return sec;
084: }
085:
086: protected Hashtable getFieldConfig(Hashtable sec,
087: GenericOperationDefinition operation, int index) {
088: int fieldIndex = 1;
089: for (Enumeration e = operation.fields(); e.hasMoreElements();) {
090: GenericFieldDefinition field = (GenericFieldDefinition) e
091: .nextElement();
092: sec.put(operation.getConfigKey() + index
093: + field.CONFIG_FIELD + fieldIndex
094: + field.CONFIG_NAME, field.getName());
095: Properties properties = field.getProperties();
096: for (Enumeration e2 = properties.propertyNames(); e2
097: .hasMoreElements();) {
098: String prop = (String) e2.nextElement();
099: String propVal = properties.getProperty(prop);
100: sec.put(operation.getConfigKey() + index
101: + field.CONFIG_FIELD + fieldIndex + "." + prop,
102: propVal);
103: }
104: fieldIndex++;
105: }
106: return sec;
107: }
108:
109: /**
110: * This method initializes Data Source properties and Operation Properties
111: * @param name the Data Source name
112: * @throws IntegrationException if any exception occours during initialization
113: */
114: public void init(String name) throws IntegrationException {
115: this .name = name;
116: this .initDataSource();
117: this .initOperations();
118: }
119:
120: /**
121: * Return the Data Source name
122: * @return the Data Source name
123: */
124: public String getName() {
125: return this .name;
126: }
127:
128: /**
129: * Get the Operation by name
130: * @param name the configured operation name
131: * @return the operation instance
132: * @throws IntegrationException if the configured operation name does not exist
133: */
134: public GenericOperationDefinition getOperation(String name)
135: throws IntegrationException {
136: GenericOperationDefinition god = (GenericOperationDefinition) this .operations
137: .get(name);
138: if (god == null)
139: throw new IntegrationException(this .getClass().getName()
140: + "::getQueryDefinition: operation '" + name
141: + "' does not exist");
142: return god;
143: }
144:
145: public Enumeration operationNames() {
146: return this .operations.keys();
147: }
148:
149: /**
150: * Get operations
151: * @return the iterator of operations
152: */
153: public Enumeration operations() {
154: return operations.elements();
155: }
156:
157: public int operationSize() {
158: return this .operations.size();
159: }
160:
161: public GenericOperationDefinition removeOperation(String operation) {
162: return (GenericOperationDefinition) this .operations
163: .remove(operation);
164: }
165:
166: public void addOperation(GenericOperationDefinition operation) {
167: this .operations.put(operation.getName(), operation);
168: }
169:
170: public String toString() {
171: String ret = "[NAME=" + this .getName() + "]" + " [OPERATIONS={";
172: if (this .operations != null) {
173: int i = 0;
174: for (Enumeration e = this .operations(); e.hasMoreElements();) {
175: ret += (e.nextElement()).toString();
176: if (i++ < this .operations.size() - 1)
177: ret += ";";
178: }
179: }
180: ret += "}]";
181: return ret;
182: }
183:
184: protected Config getConfig() throws Exception {
185: String confname = Config.getCurrent().getValue(
186: DataSourceFactory.CONFIG_SECTION, "config.name");
187: Config config = Config.getCurrent().getConfig(confname);
188: if (config == null) {
189: LogManager.sendError(this .getClass().getName()
190: + "::getConfig: exception: Config file " + confname
191: + " not found");
192: throw new Exception("Config file " + confname
193: + " not found");
194: }
195: return config;
196: }
197:
198: /**
199: * This method initailizes Data Source operations (see {@link com.jat.integration.GenericOperationDefinition})
200: * @throws IntegrationException if any exception occours during initialization
201: */
202: protected void initOperations() throws IntegrationException {
203: LogManager.sendDebug(this .getClass().getName()
204: + "::initOperations: start");
205: try {
206: Config config = this .getConfig();
207: this .operations = new Hashtable();
208: String configKey = this .getEmptyOperationDefinition()
209: .getConfigKey();
210: for (Enumeration e = config
211: .getSubKeys(this .name, configKey).elements(); e
212: .hasMoreElements();) {
213: GenericOperationDefinition god = getEmptyOperationDefinition();
214: god.init(this .name, (String) e.nextElement());
215: this .operations.put(god.getName(), god);
216: }
217: } catch (Exception ex) {
218: throw new IntegrationException(this .getClass().getName()
219: + "::initOperations:: exception: " + ex);
220: }
221: LogManager.sendDebug(this .getClass().getName()
222: + "::initOperations: end");
223: }
224:
225: private String name;
226: private Hashtable operations = new Hashtable();
227:
228: /** @link dependency
229: * @stereotype use*/
230: /*# GenericOperationDefinition lnkGenericOperationDefinition; */
231: }
|