/* * JBoss, Home of Professional Open Source. * See the COPYRIGHT.txt file distributed with this work for information * regarding copyright ownership. Some portions may be licensed * to Red Hat, Inc. under one or more contributor license agreements. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA. */ package org.teiid.example; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import org.teiid.adminapi.Admin; import org.teiid.adminapi.AdminException; import org.teiid.adminapi.VDB; import org.teiid.adminapi.VDB.Status; import org.teiid.adminapi.impl.ModelMetaData; import org.teiid.resource.adapter.mongodb.MongoDBManagedConnectionFactory; import org.teiid.runtime.EmbeddedConfiguration; import org.teiid.runtime.EmbeddedServer; import org.teiid.translator.mongodb.MongoDBExecutionFactory; @SuppressWarnings("nls") public class TeiidEmbeddedMongoDBDataSource { private static String SERVERLIST = "127.0.0.1:27017"; private static String DBNAME = "test"; public static void main(String[] args) throws Exception { String dbName = "test"; String dsName = "java:/mongoDS"; String vdbName = "mongovdb"; String modelName = "mongomodel"; String translator = "mongodb"; EmbeddedServer server = new EmbeddedServer(); MongoDBExecutionFactory factory = new MongoDBExecutionFactory(); factory.start(); server.addTranslator(translator, factory); MongoDBManagedConnectionFactory managedconnectionFactory = new MongoDBManagedConnectionFactory(); managedconnectionFactory.setRemoteServerList(SERVERLIST); managedconnectionFactory.setDatabase(DBNAME); server.addConnectionFactory("java:/mongoDS", managedconnectionFactory.createConnectionFactory()); server.start(new EmbeddedConfiguration()); Admin admin = server.getAdmin(); ModelMetaData mongoDBModelMetaData = createModelMetaData(vdbName, modelName, dsName, false, dbName, null, translator, null); server.deployVDB(vdbName, mongoDBModelMetaData); waitForVDBLoad(admin, vdbName, "1", 5); Connection c = server.getDriver().connect("jdbc:teiid:" + vdbName, null); String mongoSchema = admin.getSchema(vdbName, "1", modelName, null, null); System.out.println(mongoSchema); execute(c, "SELECT * FROM products"); c.close(); server.stop(); } private static ModelMetaData createModelMetaData(String vdbName, String modelName, String dsName, Boolean withView, String catalog, String schema, String translator, ArrayList declaredFunctions) { ModelMetaData modelMetaData = new ModelMetaData(); modelMetaData.setName(modelName); // if (withView) { modelMetaData.addProperty("importer.tableTypes", "TABLE,VIEW"); } else { modelMetaData.addProperty("importer.tableTypes", "TABLE"); } // if (catalog != null) { modelMetaData.addProperty("importer.catalog", catalog); } // if (schema != null) { modelMetaData.addProperty("importer.schemaPattern", schema); modelMetaData.addProperty("importer.useFullSchemaName", "true"); } // modelMetaData.addSourceMapping(vdbName, translator, dsName); modelMetaData.addSourceMetadata("NATIVE", ""); if (declaredFunctions != null && declaredFunctions.size() > 0) { StringBuilder sb = new StringBuilder(); for (String declaredFct : declaredFunctions) { declaredFct = "CREATE FOREIGN FUNCTION " + declaredFct; sb.append(declaredFct).append(";"); } modelMetaData.addSourceMetadata("DDL", sb.toString()); } return modelMetaData; } static boolean waitForVDBLoad(Admin admin, String vdbName, String vdbVersion, int timeoutInSecs) throws AdminException { long waitUntil = System.currentTimeMillis() + timeoutInSecs * 1000; if (timeoutInSecs < 0) { waitUntil = Long.MAX_VALUE; } boolean first = true; do { if (!first) { try { Thread.sleep(2000); } catch (InterruptedException e) { break; } } else { first = false; } VDB vdb = admin.getVDB(vdbName, vdbVersion); if (vdb != null && vdb.getStatus() != Status.LOADING) { return true; } } while (System.currentTimeMillis() < waitUntil); return false; } public static void execute(Connection connection, String sql) throws Exception { System.out.println("SQL: " + sql); //$NON-NLS-1$ Statement stmt = null; ResultSet rs = null; try { stmt = connection.createStatement(); boolean hasResults = stmt.execute(sql); if (hasResults) { rs = stmt.getResultSet(); ResultSetMetaData metadata = rs.getMetaData(); int columns = metadata.getColumnCount(); for (int row = 1; rs.next(); row++) { System.out.print(row + ": "); for (int i = 0; i < columns; i++) { if (i > 0) { System.out.print(", "); } System.out.print(rs.getObject(i + 1)); } System.out.println(); } rs.close(); } stmt.close(); } catch (SQLException e) { e.printStackTrace(); } System.out.println(); } }