org.apache.calcite.avatica.jdbc.JdbcMeta Java Examples

The following examples show how to use org.apache.calcite.avatica.jdbc.JdbcMeta. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Override public Cache<String, Connection>
getRemoteConnectionMap(AvaticaConnection connection) throws Exception {
  Field metaF = AvaticaConnection.class.getDeclaredField("meta");
  metaF.setAccessible(true);
  Meta clientMeta = (Meta) metaF.get(connection);
  Field remoteMetaServiceF = clientMeta.getClass().getDeclaredField("service");
  remoteMetaServiceF.setAccessible(true);
  LocalJsonService remoteMetaService = (LocalJsonService) remoteMetaServiceF.get(clientMeta);
  // Get the field explicitly off the correct class to avoid LocalLoggingJsonService.class
  Field remoteMetaServiceServiceF = LocalJsonService.class.getDeclaredField("service");
  remoteMetaServiceServiceF.setAccessible(true);
  LocalService remoteMetaServiceService =
      (LocalService) remoteMetaServiceServiceF.get(remoteMetaService);
  Field remoteMetaServiceServiceMetaF =
      remoteMetaServiceService.getClass().getDeclaredField("meta");
  remoteMetaServiceServiceMetaF.setAccessible(true);
  JdbcMeta serverMeta = (JdbcMeta) remoteMetaServiceServiceMetaF.get(remoteMetaServiceService);
  Field jdbcMetaConnectionCacheF = JdbcMeta.class.getDeclaredField("connectionCache");
  jdbcMetaConnectionCacheF.setAccessible(true);
  //noinspection unchecked
  @SuppressWarnings("unchecked")
  Cache<String, Connection> cache =
      (Cache<String, Connection>) jdbcMetaConnectionCacheF.get(serverMeta);
  return cache;
}
 
Example #2
Source File: QuarkMetaFactoryImpl.java    From quark with Apache License 2.0 6 votes vote down vote up
@Override
public Meta create(List<String> args) {
  String url = "jdbc:quark:fat:db:";

  try {
    if (args.size() == 1) {
      String filePath = args.get(0);
      ObjectMapper objectMapper = new ObjectMapper();
      serverConfig = objectMapper.readValue(new File(filePath), ServerConfig.class);

      url = url + filePath;
    } else {
      throw new RuntimeException(
          "1 argument expected. Received " + Arrays.toString(args.toArray()));
    }
    return new JdbcMeta(url, new Properties());
  } catch (SQLException | IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #3
Source File: AvaticaServersForTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
static JdbcMeta getInstance(Properties properties) {
  try {
    if (properties == null) {
      return new JdbcMeta(CONNECTION_SPEC.url, CONNECTION_SPEC.username,
                CONNECTION_SPEC.password);
    }
    if (instances.get(properties) == null) {
      properties.put("user", CONNECTION_SPEC.username);
      properties.put("password", CONNECTION_SPEC.password);
      JdbcMeta instance = new JdbcMeta(CONNECTION_SPEC.url, properties);
      instances.put(properties, instance);
    }
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
  return instances.get(properties);
}
 
Example #4
Source File: RemoteMetaTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Test public void testLocalStackTraceHasServerStackTrace() {
  ConnectionSpec.getDatabaseLock().lock();
  try {
    Statement statement = DriverManager.getConnection(url).createStatement();
    statement.executeQuery("SELECT * FROM BOGUS_TABLE_DEF_DOESNT_EXIST");
  } catch (SQLException e) {
    // Verify that we got the expected exception
    assertThat(e, instanceOf(AvaticaSqlException.class));

    // Attempt to verify that we got a "server-side" class in the stack.
    assertThat(Throwables.getStackTraceAsString(e),
        containsString(JdbcMeta.class.getName()));
  } finally {
    ConnectionSpec.getDatabaseLock().unlock();
  }
}
 
Example #5
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Override public Cache<String, Connection>
getRemoteConnectionMap(AvaticaConnection connection) throws Exception {
  Field metaF = AvaticaConnection.class.getDeclaredField("meta");
  metaF.setAccessible(true);
  Meta clientMeta = (Meta) metaF.get(connection);
  Field remoteMetaServiceF = clientMeta.getClass().getDeclaredField("service");
  remoteMetaServiceF.setAccessible(true);
  LocalProtobufService remoteMetaService =
      (LocalProtobufService) remoteMetaServiceF.get(clientMeta);
  // Get the field explicitly off the correct class to avoid LocalLoggingJsonService.class
  Field remoteMetaServiceServiceF = LocalProtobufService.class.getDeclaredField("service");
  remoteMetaServiceServiceF.setAccessible(true);
  LocalService remoteMetaServiceService =
      (LocalService) remoteMetaServiceServiceF.get(remoteMetaService);
  Field remoteMetaServiceServiceMetaF =
      remoteMetaServiceService.getClass().getDeclaredField("meta");
  remoteMetaServiceServiceMetaF.setAccessible(true);
  JdbcMeta serverMeta = (JdbcMeta) remoteMetaServiceServiceMetaF.get(remoteMetaServiceService);
  Field jdbcMetaConnectionCacheF = JdbcMeta.class.getDeclaredField("connectionCache");
  jdbcMetaConnectionCacheF.setAccessible(true);
  //noinspection unchecked
  @SuppressWarnings("unchecked")
  Cache<String, Connection> cache =
      (Cache<String, Connection>) jdbcMetaConnectionCacheF.get(serverMeta);
  return cache;
}
 
Example #6
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Override public Cache<Integer, Object>
getRemoteStatementMap(AvaticaConnection connection) throws Exception {
  Field metaF = AvaticaConnection.class.getDeclaredField("meta");
  metaF.setAccessible(true);
  Meta clientMeta = (Meta) metaF.get(connection);
  Field remoteMetaServiceF = clientMeta.getClass().getDeclaredField("service");
  remoteMetaServiceF.setAccessible(true);
  LocalProtobufService remoteMetaService =
      (LocalProtobufService) remoteMetaServiceF.get(clientMeta);
  // Use the explicitly class to avoid issues with LoggingLocalJsonService
  Field remoteMetaServiceServiceF = LocalProtobufService.class.getDeclaredField("service");
  remoteMetaServiceServiceF.setAccessible(true);
  LocalService remoteMetaServiceService =
      (LocalService) remoteMetaServiceServiceF.get(remoteMetaService);
  Field remoteMetaServiceServiceMetaF =
      remoteMetaServiceService.getClass().getDeclaredField("meta");
  remoteMetaServiceServiceMetaF.setAccessible(true);
  JdbcMeta serverMeta = (JdbcMeta) remoteMetaServiceServiceMetaF.get(remoteMetaServiceService);
  Field jdbcMetaStatementMapF = JdbcMeta.class.getDeclaredField("statementCache");
  jdbcMetaStatementMapF.setAccessible(true);
  //noinspection unchecked
  @SuppressWarnings("unchecked")
  Cache<Integer, Object> cache = (Cache<Integer, Object>) jdbcMetaStatementMapF.get(serverMeta);
  return cache;
}
 
Example #7
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Override public Cache<Integer, Object>
getRemoteStatementMap(AvaticaConnection connection) throws Exception {
  Field metaF = AvaticaConnection.class.getDeclaredField("meta");
  metaF.setAccessible(true);
  Meta clientMeta = (Meta) metaF.get(connection);
  Field remoteMetaServiceF = clientMeta.getClass().getDeclaredField("service");
  remoteMetaServiceF.setAccessible(true);
  LocalJsonService remoteMetaService = (LocalJsonService) remoteMetaServiceF.get(clientMeta);
  // Use the explicitly class to avoid issues with LoggingLocalJsonService
  Field remoteMetaServiceServiceF = LocalJsonService.class.getDeclaredField("service");
  remoteMetaServiceServiceF.setAccessible(true);
  LocalService remoteMetaServiceService =
      (LocalService) remoteMetaServiceServiceF.get(remoteMetaService);
  Field remoteMetaServiceServiceMetaF =
      remoteMetaServiceService.getClass().getDeclaredField("meta");
  remoteMetaServiceServiceMetaF.setAccessible(true);
  JdbcMeta serverMeta = (JdbcMeta) remoteMetaServiceServiceMetaF.get(remoteMetaServiceService);
  Field jdbcMetaStatementMapF = JdbcMeta.class.getDeclaredField("statementCache");
  jdbcMetaStatementMapF.setAccessible(true);
  //noinspection unchecked
  @SuppressWarnings("unchecked")
  Cache<Integer, Object> cache = (Cache<Integer, Object>) jdbcMetaStatementMapF.get(serverMeta);
  return cache;
}
 
Example #8
Source File: BasicAuthHttpServerTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@BeforeClass public static void startServer() throws Exception {
  final String userPropertiesFile = URLDecoder.decode(BasicAuthHttpServerTest.class
      .getResource("/auth-users.properties").getFile(), "UTF-8");
  assertNotNull("Could not find properties file for basic auth users", userPropertiesFile);

  // Create a LocalService around HSQLDB
  final JdbcMeta jdbcMeta = new JdbcMeta(CONNECTION_SPEC.url,
      CONNECTION_SPEC.username, CONNECTION_SPEC.password);
  LocalService service = new LocalService(jdbcMeta);

  server = new HttpServer.Builder()
      .withBasicAuthentication(userPropertiesFile, new String[] { "users" })
      .withHandler(service, Driver.Serialization.PROTOBUF)
      .withPort(0)
      .build();
  server.start();

  url = "jdbc:avatica:remote:url=http://localhost:" + server.getPort()
      + ";authentication=BASIC;serialization=PROTOBUF";

  // Create and grant permissions to our users
  createHsqldbUsers();
}
 
Example #9
Source File: CustomAuthHttpServerTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked") // needed for the mocked customizers, not the builder
protected void createServer(AvaticaServerConfiguration config, boolean isBasicAuth)
    throws SQLException {
  final JdbcMeta jdbcMeta = new JdbcMeta(CONNECTION_SPEC.url,
      CONNECTION_SPEC.username, CONNECTION_SPEC.password);
  LocalService service = new LocalService(jdbcMeta);

  ConnectorCustomizer connectorCustomizer = new ConnectorCustomizer();
  BasicAuthHandlerCustomizer basicAuthCustomizer =
          new BasicAuthHandlerCustomizer(config, service, isBasicAuth);

  server = new HttpServer.Builder()
          .withCustomAuthentication(config)
          .withPort(0)
          .withServerCustomizers(
                  Arrays.asList(connectorCustomizer, basicAuthCustomizer), Server.class)
          .build();
  server.start();

  // Create and grant permissions to our users
  createHsqldbUsers();
  url = "jdbc:avatica:remote:url=http://localhost:" + connectorCustomizer.getLocalPort()
          + ";authentication=BASIC;serialization=PROTOBUF";
}
 
Example #10
Source File: DigestAuthHttpServerTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@BeforeClass public static void startServer() throws Exception {
  final String userPropertiesFile = URLDecoder.decode(BasicAuthHttpServerTest.class
      .getResource("/auth-users.properties").getFile(), "UTF-8");
  assertNotNull("Could not find properties file for digest auth users", userPropertiesFile);

  // Create a LocalService around HSQLDB
  final JdbcMeta jdbcMeta = new JdbcMeta(CONNECTION_SPEC.url,
      CONNECTION_SPEC.username, CONNECTION_SPEC.password);
  LocalService service = new LocalService(jdbcMeta);

  server = new HttpServer.Builder()
      .withDigestAuthentication(userPropertiesFile, new String[] { "users" })
      .withHandler(service, Driver.Serialization.PROTOBUF)
      .withPort(0)
      .build();
  server.start();

  url = "jdbc:avatica:remote:url=http://localhost:" + server.getPort()
      + ";authentication=DIGEST;serialization=PROTOBUF";

  // Create and grant permissions to our users
  createHsqldbUsers();
}
 
Example #11
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
static void initService() {
  try {
    final JdbcMeta jdbcMeta = new JdbcMeta(CONNECTION_SPEC.url,
        CONNECTION_SPEC.username, CONNECTION_SPEC.password);
    final LocalService localService = new LocalService(jdbcMeta);
    service = new LoggingLocalJsonService(localService);
    requestInspection = (RequestInspection) service;
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #12
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
static void initService() {
  try {
    final JdbcMeta jdbcMeta = new JdbcMeta(CONNECTION_SPEC.url,
        CONNECTION_SPEC.username, CONNECTION_SPEC.password);
    final LocalService localService = new LocalService(jdbcMeta);
    service = new LoggingLocalProtobufService(localService, new ProtobufTranslationImpl());
    requestInspection = (RequestInspection) service;
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #13
Source File: KarelDbMain.java    From kareldb with Apache License 2.0 5 votes vote down vote up
public static Meta create(KarelDbConfig config) {
    try {
        Map<String, String> configs = config.originalsStrings();
        Properties properties = new Properties();
        properties.put(CalciteConnectionProperty.SCHEMA_FACTORY.camelName(), SchemaFactory.class.getName());
        properties.put(CalciteConnectionProperty.SCHEMA.camelName(), "default");
        properties.put(CalciteConnectionProperty.PARSER_FACTORY.camelName(),
            "org.apache.calcite.sql.parser.parserextension.ExtensionSqlParserImpl#FACTORY");
        properties.put("schema.kind", "io.kareldb.kafka.KafkaSchema");
        for (Map.Entry<String, String> entry : configs.entrySet()) {
            properties.put("schema." + entry.getKey(), entry.getValue());
        }

        boolean testMode = false;
        if (testMode) {
            // Single connection for testing
            final Connection connection = DriverManager.getConnection(Driver.CONNECT_STRING_PREFIX, properties);
            return new MetaImpl((AvaticaConnection) connection);
        } else {
            // Multi-connection support
            return new JdbcMeta(Driver.CONNECT_STRING_PREFIX, properties);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example #14
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
@Override public Service create(AvaticaConnection connection) {
  try {
    return new LocalService(
        new JdbcMeta(CONNECTION_SPEC.url, CONNECTION_SPEC.username,
            CONNECTION_SPEC.password));
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #15
Source File: RemoteMetaTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
private static Connection getConnection(JdbcMeta m, String id) throws Exception {
  Field f = JdbcMeta.class.getDeclaredField("connectionCache");
  f.setAccessible(true);
  //noinspection unchecked
  Cache<String, Connection> connectionCache = (Cache<String, Connection>) f.get(m);
  return connectionCache.getIfPresent(id);
}
 
Example #16
Source File: ConnectionPropertiesTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> parameters() throws Exception {
  PROPERTIES.put(JdbcMeta.ConnectionCacheSettings.EXPIRY_DURATION.key(), "1");
  PROPERTIES.put(JdbcMeta.ConnectionCacheSettings.EXPIRY_UNIT.key(), TimeUnit.SECONDS.name());
  SERVERS.startServers(PROPERTIES);
  return SERVERS.getJUnitParameters();
}
 
Example #17
Source File: ConnectionPropertiesTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
private static Connection getConnection(JdbcMeta m, String id) throws Exception {
  Field f = JdbcMeta.class.getDeclaredField("connectionCache");
  f.setAccessible(true);
  //noinspection unchecked
  Cache<String, Connection> connectionCache = (Cache<String, Connection>) f.get(m);
  return connectionCache.getIfPresent(id);
}
 
Example #18
Source File: AlternatingRemoteMetaTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
private static Connection getConnection(JdbcMeta m, String id) throws Exception {
  Field f = JdbcMeta.class.getDeclaredField("connectionCache");
  f.setAccessible(true);
  //noinspection unchecked
  Cache<String, Connection> connectionCache = (Cache<String, Connection>) f.get(m);
  return connectionCache.getIfPresent(id);
}
 
Example #19
Source File: StandaloneServer.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
public void start() {
  if (null != server) {
    LOG.error("The server was already started");
    Unsafe.systemExit(ExitCodes.ALREADY_STARTED.ordinal());
    return;
  }

  try {
    JdbcMeta meta = new JdbcMeta(url);
    LocalService service = new LocalService(meta);

    // Construct the server
    this.server = new HttpServer.Builder()
        .withHandler(service, serialization)
        .withPort(port)
        .build();

    // Then start it
    server.start();

    LOG.info("Started Avatica server on port {} with serialization {}", server.getPort(),
        serialization);
  } catch (Exception e) {
    LOG.error("Failed to start Avatica server", e);
    Unsafe.systemExit(ExitCodes.START_FAILED.ordinal());
  }
}