[teiid-commits] teiid SVN: r2203 - trunk/connectors/translator-file/src/main/java/org/teiid/translator/file.
teiid-commits at lists.jboss.org
teiid-commits at lists.jboss.org
Tue Jun 8 15:17:36 EDT 2010
Author: shawkins
Date: 2010-06-08 15:17:35 -0400 (Tue, 08 Jun 2010)
New Revision: 2203
Modified:
trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java
Log:
TEIID-962 exposing a write method via the file translator
Modified: trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java
===================================================================
--- trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java 2010-06-08 19:11:59 UTC (rev 2202)
+++ trunk/connectors/translator-file/src/main/java/org/teiid/translator/file/FileExecutionFactory.java 2010-06-08 19:17:35 UTC (rev 2203)
@@ -23,12 +23,17 @@
package org.teiid.translator.file;
import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
import java.nio.charset.Charset;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.SQLException;
+import java.sql.SQLXML;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import javax.resource.ResourceException;
import javax.resource.cci.ConnectionFactory;
import org.teiid.core.BundleUtil;
@@ -38,9 +43,9 @@
import org.teiid.core.types.ClobImpl;
import org.teiid.core.types.ClobType;
import org.teiid.core.types.InputStreamFactory.FileInputStreamFactory;
+import org.teiid.core.util.ObjectConverterUtil;
+import org.teiid.core.util.ReaderInputStream;
import org.teiid.language.Call;
-import org.teiid.logging.LogConstants;
-import org.teiid.logging.LogManager;
import org.teiid.metadata.MetadataFactory;
import org.teiid.metadata.Procedure;
import org.teiid.metadata.RuntimeMetadata;
@@ -49,7 +54,6 @@
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.ExecutionFactory;
import org.teiid.translator.FileConnection;
-import org.teiid.translator.MetadataProvider;
import org.teiid.translator.ProcedureExecution;
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
@@ -57,7 +61,7 @@
import org.teiid.translator.TypeFacility;
@Translator(name="file")
-public class FileExecutionFactory extends ExecutionFactory implements MetadataProvider {
+public class FileExecutionFactory extends ExecutionFactory<ConnectionFactory, FileConnection> {
private final class FileProcedureExecution implements ProcedureExecution {
private final Call command;
@@ -84,11 +88,7 @@
@Override
public void close() {
- try {
- fc.close();
- } catch (ResourceException e) {
- LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Exception closing"); //$NON-NLS-1$
- }
+
}
@Override
@@ -126,6 +126,7 @@
public static final String GETTEXTFILES = "getTextFiles"; //$NON-NLS-1$
public static final String GETFILES = "getFiles"; //$NON-NLS-1$
+ public static final String SAVEFILE = "saveFile"; //$NON-NLS-1$
private String encoding = Charset.defaultCharset().name();
@@ -141,27 +142,75 @@
//@Override
public ProcedureExecution createProcedureExecution(final Call command,
final ExecutionContext executionContext, final RuntimeMetadata metadata,
- Object connectionFactory) throws TranslatorException {
- final FileConnection fc;
- try {
- fc = (FileConnection)((ConnectionFactory)connectionFactory).getConnection();
- } catch (ResourceException e) {
- throw new TranslatorException(e);
+ final FileConnection fc) throws TranslatorException {
+ if (command.getProcedureName().equalsIgnoreCase(SAVEFILE)) {
+ return new ProcedureExecution() {
+
+ @Override
+ public void execute() throws TranslatorException {
+ String filePath = (String)command.getArguments().get(0).getArgumentValue().getValue();
+ Object file = command.getArguments().get(1).getArgumentValue().getValue();
+ if (file == null || filePath == null) {
+ throw new TranslatorException(UTIL.getString("non_null")); //$NON-NLS-1$
+ }
+ InputStream is = null;
+ try {
+ if (file instanceof SQLXML) {
+ is = ((SQLXML)file).getBinaryStream();
+ } else if (file instanceof Clob) {
+ is = new ReaderInputStream(((Clob)file).getCharacterStream(), Charset.forName(encoding));
+ } else if (file instanceof Blob) {
+ is = ((Blob)file).getBinaryStream();
+ } else {
+ throw new TranslatorException(UTIL.getString("unknown_type")); //$NON-NLS-1$
+ }
+
+ ObjectConverterUtil.write(is, fc.getFile(filePath));
+ } catch (IOException e) {
+ throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
+ } catch (SQLException e) {
+ throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
+ }
+ }
+
+ @Override
+ public void close() {
+ }
+
+ @Override
+ public void cancel() throws TranslatorException {
+ }
+
+ @Override
+ public List<?> next() throws TranslatorException, DataNotAvailableException {
+ return null;
+ }
+
+ @Override
+ public List<?> getOutputParameterValues() throws TranslatorException {
+ return Collections.emptyList();
+ }
+ };
}
return new FileProcedureExecution(command, fc);
}
@Override
- public void getConnectorMetadata(MetadataFactory metadataFactory, Object connectionFactory) throws TranslatorException {
+ public void getMetadata(MetadataFactory metadataFactory, FileConnection connection) throws TranslatorException {
Procedure p = metadataFactory.addProcedure(GETTEXTFILES);
- metadataFactory.addProcedureParameter("path", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
+ metadataFactory.addProcedureParameter("pathAndPattern", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.CLOB, p); //$NON-NLS-1$
- metadataFactory.addProcedureResultSetColumn("path", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$
+ metadataFactory.addProcedureResultSetColumn("filePath", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$
Procedure p1 = metadataFactory.addProcedure(GETFILES);
- metadataFactory.addProcedureParameter("path", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p1); //$NON-NLS-1$
+ metadataFactory.addProcedureParameter("pathAndPattern", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p1); //$NON-NLS-1$
metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.BLOB, p1); //$NON-NLS-1$
- metadataFactory.addProcedureResultSetColumn("path", TypeFacility.RUNTIME_NAMES.STRING, p1); //$NON-NLS-1$
+ metadataFactory.addProcedureResultSetColumn("filePath", TypeFacility.RUNTIME_NAMES.STRING, p1); //$NON-NLS-1$
+
+ Procedure p2 = metadataFactory.addProcedure(SAVEFILE);
+ metadataFactory.addProcedureParameter("filePath", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p2); //$NON-NLS-1$
+ //reporting as object, but can be one of xml, clob, blob
+ metadataFactory.addProcedureParameter("file", TypeFacility.RUNTIME_NAMES.OBJECT, Type.In, p2); //$NON-NLS-1$
}
}
More information about the teiid-commits
mailing list