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.");
}
}
});
}
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>());
}
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);
}
}
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());
}
}
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());
}
Aggregations