Search in sources :

Example 1 with VirtualFileConnection

use of org.teiid.file.VirtualFileConnection in project teiid by teiid.

the class FileExecutionFactory method createProcedureExecution.

// @Override
public ProcedureExecution createProcedureExecution(final Call command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Connection conn) throws TranslatorException {
    if (conn instanceof VirtualFileConnection) {
        return new VirtualFileProcedureExecution(command, (VirtualFileConnection) conn);
    }
    final FileConnection fc = (FileConnection) conn;
    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) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Saving", filePath);
                InputStream is = null;
                try {
                    if (file instanceof SQLXML) {
                        is = ((SQLXML) file).getBinaryStream();
                    } else if (file instanceof Clob) {
                        is = new ReaderInputStream(((Clob) file).getCharacterStream(), encoding);
                    } else if (file instanceof Blob) {
                        is = ((Blob) file).getBinaryStream();
                    } else {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("unknown_type"));
                    }
                    ObjectConverterUtil.write(is, fc.getFile(filePath));
                } catch (IOException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (SQLException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                }
            }

            @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();
            }
        };
    } else if (command.getProcedureName().equalsIgnoreCase(DELETEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                if (filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Deleting", filePath);
                try {
                    File f = fc.getFile(filePath);
                    if (!f.exists()) {
                        if (exceptionIfFileNotFound) {
                            // $NON-NLS-1$
                            throw new TranslatorException(DataPlugin.Util.gs("file_not_found", filePath));
                        }
                    } else if (!f.delete()) {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("error_deleting"));
                    }
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_deleting"));
                }
            }

            @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);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SQLXML(java.sql.SQLXML) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ProcedureExecution(org.teiid.translator.ProcedureExecution) TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Clob(java.sql.Clob) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) VirtualFileConnection(org.teiid.file.VirtualFileConnection) VirtualFileConnection(org.teiid.file.VirtualFileConnection) FileConnection(org.teiid.translator.FileConnection)

Example 2 with VirtualFileConnection

use of org.teiid.file.VirtualFileConnection in project teiid by teiid.

the class TestFtpFileConnection method testRemove.

@Test
public void testRemove() throws ResourceException, IOException {
    VirtualFileConnection conn = sample();
    // $NON-NLS-1$
    VirtualFile pom = VFS.getChild(new File("pom.xml").getAbsolutePath());
    // $NON-NLS-1$
    VirtualFile file = conn.getFile("pom.xml");
    conn.add(pom.openStream(), pom);
    assertTrue(file.exists());
    // $NON-NLS-1$
    conn.remove("pom.xml");
    assertFalse(file.exists());
    conn.close();
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) VirtualFileConnection(org.teiid.file.VirtualFileConnection) Test(org.junit.Test)

Example 3 with VirtualFileConnection

use of org.teiid.file.VirtualFileConnection in project teiid by teiid.

the class TestFtpFileConnection method testAdd.

@Test
public void testAdd() throws IOException, ResourceException {
    VirtualFileConnection conn = sample();
    // $NON-NLS-1$
    VirtualFile file = conn.getFile("pom.xml");
    assertFalse(file.exists());
    // $NON-NLS-1$
    VirtualFile pom = VFS.getChild(new File("pom.xml").getAbsolutePath());
    conn.add(pom.openStream(), pom);
    assertTrue(file.exists());
    conn.close();
    conn = sample();
    assertTrue(file.isFile());
    assertNotNull(file.openStream());
    conn.close();
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) VirtualFileConnection(org.teiid.file.VirtualFileConnection) Test(org.junit.Test)

Example 4 with VirtualFileConnection

use of org.teiid.file.VirtualFileConnection in project teiid by teiid.

the class TestFtpFileConnection method testGetFile.

@Test
public void testGetFile() throws ResourceException, IOException {
    VirtualFileConnection conn = sample();
    // $NON-NLS-1$
    VirtualFile file = conn.getFile("marketdata-price.txt");
    assertNotNull(file.openStream());
    // $NON-NLS-1$
    file = conn.getFile("marketdata-price1.txt");
    assertNotNull(file.openStream());
    conn.close();
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) VirtualFileConnection(org.teiid.file.VirtualFileConnection) Test(org.junit.Test)

Example 5 with VirtualFileConnection

use of org.teiid.file.VirtualFileConnection in project teiid by teiid.

the class TestFtpFileConnection method testGetFiles.

@Test(expected = ResourceException.class)
public void testGetFiles() throws ResourceException, IOException {
    VirtualFileConnection conn = sample();
    VirtualFile[] files = conn.getFiles("*.txt");
    // $NON-NLS-1$
    assertEquals(2, files.length);
    conn.close();
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) VirtualFileConnection(org.teiid.file.VirtualFileConnection) Test(org.junit.Test)

Aggregations

VirtualFile (org.jboss.vfs.VirtualFile)5 VirtualFileConnection (org.teiid.file.VirtualFileConnection)5 Test (org.junit.Test)4 File (java.io.File)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 SQLXML (java.sql.SQLXML)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResourceException (javax.resource.ResourceException)1 ReaderInputStream (org.teiid.core.util.ReaderInputStream)1 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)1 FileConnection (org.teiid.translator.FileConnection)1 ProcedureExecution (org.teiid.translator.ProcedureExecution)1 TranslatorException (org.teiid.translator.TranslatorException)1