Search in sources :

Example 16 with ExecutionFactory

use of org.teiid.translator.ExecutionFactory in project teiid by teiid.

the class VDBService method loadMetadata.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void loadMetadata(final VDBMetaData vdb, final ModelMetaData model, final ConnectorManagerRepository cmr, final MetadataRepository metadataRepo, final MetadataStore vdbMetadataStore, final AtomicInteger loadCount, final VDBResources vdbResources) {
    String msg = IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50029, vdb.getName(), vdb.getVersion(), model.getName(), SimpleDateFormat.getInstance().format(new Date()));
    model.setMetadataStatus(Model.MetadataStatus.LOADING);
    model.addRuntimeMessage(Severity.INFO, msg);
    LogManager.logInfo(LogConstants.CTX_RUNTIME, msg);
    final Runnable job = new Runnable() {

        @Override
        public void run() {
            boolean cached = false;
            Exception ex = null;
            TranslatorException te = null;
            // if this is not the first time trying to load metadata
            if (model.getMetadataStatus() != Model.MetadataStatus.LOADING) {
                model.setMetadataStatus(Model.MetadataStatus.RETRYING);
            }
            // designer based models define data types based on their built in data types, which are system vdb data types
            Map<String, Datatype> datatypes = vdbMetadataStore.getDatatypes();
            final File cachedFile = getSerializer().buildModelFile(vdb, model.getName());
            MetadataFactory factory = getSerializer().loadSafe(cachedFile, MetadataFactory.class);
            if (factory != null) {
                factory.correctDatatypes(datatypes);
                cached = true;
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                LogManager.logDetail(LogConstants.CTX_RUNTIME, "Model ", model.getName(), "in VDB ", vdb.getName(), " was loaded from cached metadata");
            } else {
                factory = createMetadataFactory(vdb, vdbMetadataStore, model, vdbResources.getEntriesPlusVisibilities());
                ExecutionFactory ef = null;
                Object cf = null;
                for (ConnectorManager cm : getConnectorManagers(model, cmr)) {
                    if (ex != null) {
                        // $NON-NLS-1$
                        LogManager.logDetail(LogConstants.CTX_RUNTIME, ex, "Failed to get metadata, trying next source.");
                        ex = null;
                        te = null;
                    }
                    try {
                        if (cm != null) {
                            ef = cm.getExecutionFactory();
                            cf = cm.getConnectionFactory();
                        }
                    } catch (TranslatorException e) {
                        // $NON-NLS-1$
                        LogManager.logDetail(LogConstants.CTX_RUNTIME, e, "Failed to get a connection factory for metadata load.");
                        te = e;
                    }
                    ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
                    try {
                        LogManager.logDetail(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50104, vdb.getName(), vdb.getVersion(), model.getName(), cm != null ? cm.getTranslatorName() : null, cm != null ? cm.getConnectionName() : null));
                        Thread.currentThread().setContextClassLoader(metadataRepo.getClass().getClassLoader());
                        metadataRepo.loadMetadata(factory, ef, cf);
                        LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50030, vdb.getName(), vdb.getVersion(), model.getName(), SimpleDateFormat.getInstance().format(new Date())));
                        break;
                    } catch (Exception e) {
                        factory = createMetadataFactory(vdb, vdbMetadataStore, model, vdbResources.getEntriesPlusVisibilities());
                        ex = e;
                    } finally {
                        Thread.currentThread().setContextClassLoader(originalCL);
                    }
                }
            }
            synchronized (vdb) {
                VDBStatusChecker marked = model.removeAttachment(VDBStatusChecker.class);
                if (ex == null) {
                    if (!cached) {
                        // cache the schema to disk
                        cacheMetadataStore(model, factory);
                    }
                    metadataLoaded(vdb, model, vdbMetadataStore, loadCount, factory, true, cmr, vdbResources);
                } else {
                    String errorMsg = ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage();
                    if (te != null) {
                        // $NON-NLS-1$
                        errorMsg += ": " + te.getMessage();
                    }
                    model.addRuntimeError(errorMsg);
                    model.setMetadataStatus(Model.MetadataStatus.FAILED);
                    LogManager.logWarning(LogConstants.CTX_RUNTIME, ex, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50036, vdb.getName(), vdb.getVersion(), model.getName(), errorMsg));
                    if (ex instanceof RuntimeException) {
                        metadataLoaded(vdb, model, vdbMetadataStore, loadCount, factory, false, cmr, vdbResources);
                    } else {
                        if (marked != null) {
                            getExecutor().execute(this);
                        } else {
                            // defer the load to the status checker if/when a source is available/redeployed
                            model.addAttchment(Runnable.class, this);
                        }
                    }
                }
            }
        }
    };
    Executor executor = getExecutor();
    // wrap the runnable to trap exceptions that may be caused by an asynch deployment issue
    executor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                job.run();
            } catch (IllegalStateException e) {
                if (vdb.getStatus() != Status.FAILED && vdb.getStatus() != Status.REMOVED) {
                    throw e;
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_RUNTIME, e, "Could not load metadata for a removed or failed deployment.");
            }
        }
    });
}
Also used : ExecutionFactory(org.teiid.translator.ExecutionFactory) VDBStatusChecker(org.teiid.deployers.VDBStatusChecker) ConnectorManager(org.teiid.dqp.internal.datamgr.ConnectorManager) Date(java.util.Date) ConnectorManagerException(org.teiid.dqp.internal.datamgr.ConnectorManagerRepository.ConnectorManagerException) XMLStreamException(javax.xml.stream.XMLStreamException) OperationFailedException(org.jboss.as.controller.OperationFailedException) TranslatorException(org.teiid.translator.TranslatorException) VirtualDatabaseException(org.teiid.deployers.VirtualDatabaseException) StartException(org.jboss.msc.service.StartException) IOException(java.io.IOException) AdminProcessingException(org.teiid.adminapi.AdminProcessingException) Datatype(org.teiid.metadata.Datatype) Executor(java.util.concurrent.Executor) MetadataFactory(org.teiid.metadata.MetadataFactory) TranslatorException(org.teiid.translator.TranslatorException) File(java.io.File)

Example 17 with ExecutionFactory

use of org.teiid.translator.ExecutionFactory in project teiid by teiid.

the class TestVDBService method testMissingDelegate.

@Test(expected = ConnectorManagerException.class)
public void testMissingDelegate() throws ConnectorManagerException {
    TranslatorRepository repo = new TranslatorRepository();
    VDBTranslatorMetaData tmd = new VDBTranslatorMetaData();
    Properties props = new Properties();
    props.put("delegateName", "y");
    tmd.setProperties(props);
    tmd.setExecutionFactoryClass(SampleExecutionFactory.class);
    repo.addTranslatorMetadata("x", tmd);
    TranslatorUtil.getExecutionFactory("x", repo, repo, new VDBMetaData(), new IdentityHashMap<Translator, ExecutionFactory<Object, Object>>(), new HashSet<String>());
}
Also used : TranslatorRepository(org.teiid.dqp.internal.datamgr.TranslatorRepository) Translator(org.teiid.adminapi.Translator) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) BaseDelegatingExecutionFactory(org.teiid.translator.BaseDelegatingExecutionFactory) ExecutionFactory(org.teiid.translator.ExecutionFactory) Properties(java.util.Properties) VDBTranslatorMetaData(org.teiid.adminapi.impl.VDBTranslatorMetaData) Test(org.junit.Test)

Example 18 with ExecutionFactory

use of org.teiid.translator.ExecutionFactory in project teiid by teiid.

the class TranslatorUtil method buildExecutionFactory.

public static ExecutionFactory buildExecutionFactory(VDBTranslatorMetaData data) throws TeiidException {
    ExecutionFactory executionFactory;
    try {
        Class<?> executionClass = data.getExecutionFactoryClass();
        Object o = executionClass.newInstance();
        if (!(o instanceof ExecutionFactory)) {
            throw new TeiidException(RuntimePlugin.Event.TEIID40024, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40024, executionClass));
        }
        executionFactory = (ExecutionFactory) o;
        synchronized (executionFactory) {
            injectProperties(executionFactory, data);
            ClassLoader orginalCL = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(executionFactory.getClass().getClassLoader());
                executionFactory.start();
            } finally {
                Thread.currentThread().setContextClassLoader(orginalCL);
            }
        }
        return executionFactory;
    } catch (InvocationTargetException e) {
        throw new TeiidException(RuntimePlugin.Event.TEIID40025, e);
    } catch (IllegalAccessException e) {
        throw new TeiidException(RuntimePlugin.Event.TEIID40026, e);
    } catch (InstantiationException e) {
        throw new TeiidException(CorePlugin.Event.TEIID10036, e);
    }
}
Also used : DelegatingExecutionFactory(org.teiid.translator.DelegatingExecutionFactory) ExecutionFactory(org.teiid.translator.ExecutionFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) TeiidException(org.teiid.core.TeiidException)

Example 19 with ExecutionFactory

use of org.teiid.translator.ExecutionFactory in project teiid by teiid.

the class TestEmbeddedServer method testQueryTimeout.

@Test
public void testQueryTimeout() throws Exception {
    es.start(new EmbeddedConfiguration());
    es.addTranslator("foo", new ExecutionFactory() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
            return super.createResultSetExecution(command, executionContext, metadata, connection);
        }
    });
    es.deployVDB(new ByteArrayInputStream("<vdb name=\"test\" version=\"1\"><model name=\"test\"><source name=\"foo\" translator-name=\"foo\"/><metadata type=\"DDL\"><![CDATA[CREATE foreign table x (y xml);]]> </metadata></model></vdb>".getBytes()));
    Connection c = es.getDriver().connect("jdbc:teiid:test", null);
    Statement s = c.createStatement();
    s.setQueryTimeout(1);
    try {
        s.execute("select * from x");
        fail();
    } catch (SQLException e) {
        assertEquals(SQLStates.QUERY_CANCELED, e.getSQLState());
    }
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) ExecutionFactory(org.teiid.translator.ExecutionFactory) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Example 20 with ExecutionFactory

use of org.teiid.translator.ExecutionFactory in project teiid by teiid.

the class TestEmbeddedServer method testSourceLobUnderTxn.

@Test
public void testSourceLobUnderTxn() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    ec.setMaxResultSetCacheStaleness(0);
    MockTransactionManager tm = new MockTransactionManager();
    ec.setTransactionManager(tm);
    ec.setUseDisk(false);
    es.start(ec);
    final AtomicBoolean closed = new AtomicBoolean();
    es.addTranslator("foo", new ExecutionFactory() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return new ResultSetExecution() {

                private boolean returned;

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                    closed.set(true);
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (returned) {
                        return null;
                    }
                    returned = true;
                    ArrayList<Object> result = new ArrayList<Object>(1);
                    result.add(new SQLXMLImpl(new InputStreamFactory() {

                        @Override
                        public InputStream getInputStream() throws IOException {
                            // need to make it of a sufficient size to not be inlined
                            return new ByteArrayInputStream(new byte[DataTypeManager.MAX_LOB_MEMORY_BYTES + 1]);
                        }
                    }));
                    return result;
                }
            };
        }
    });
    es.deployVDB(new ByteArrayInputStream("<vdb name=\"test\" version=\"1\"><model name=\"test\"><source name=\"foo\" translator-name=\"foo\"/><metadata type=\"DDL\"><![CDATA[CREATE foreign table x (y xml);]]> </metadata></model></vdb>".getBytes()));
    Connection c = es.getDriver().connect("jdbc:teiid:test", null);
    c.setAutoCommit(false);
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("select * from x");
    rs.next();
    assertFalse(closed.get());
    s.close();
    assertTrue(closed.get());
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ExecutionFactory(org.teiid.translator.ExecutionFactory) IOException(java.io.IOException) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) InputStreamFactory(org.teiid.core.types.InputStreamFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslatorException(org.teiid.translator.TranslatorException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Aggregations

ExecutionFactory (org.teiid.translator.ExecutionFactory)29 Test (org.junit.Test)18 TranslatorException (org.teiid.translator.TranslatorException)11 ExecutionContext (org.teiid.translator.ExecutionContext)9 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)8 ResultSetExecution (org.teiid.translator.ResultSetExecution)8 List (java.util.List)7 QueryExpression (org.teiid.language.QueryExpression)7 ArrayList (java.util.ArrayList)6 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)6 ConnectorManager (org.teiid.dqp.internal.datamgr.ConnectorManager)6 MetadataFactory (org.teiid.metadata.MetadataFactory)6 Connection (java.sql.Connection)5 VDBTranslatorMetaData (org.teiid.adminapi.impl.VDBTranslatorMetaData)5 ConnectorManagerRepository (org.teiid.dqp.internal.datamgr.ConnectorManagerRepository)5 ConnectorManagerException (org.teiid.dqp.internal.datamgr.ConnectorManagerRepository.ConnectorManagerException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OracleExecutionFactory (org.teiid.translator.jdbc.oracle.OracleExecutionFactory)4 TeiidExecutionFactory (org.teiid.translator.jdbc.teiid.TeiidExecutionFactory)4 FileInputStream (java.io.FileInputStream)3