org.apache.calcite.avatica.remote.LocalService Java Examples

The following examples show how to use org.apache.calcite.avatica.remote.LocalService. 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);
  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 #2
Source File: HttpServerCustomizerTest.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
@Test public void serverCustomizersInvoked() {
  ServerCustomizer<Server> mockCustomizer1 =
      (ServerCustomizer<Server>) mock(ServerCustomizer.class);
  ServerCustomizer<Server> mockCustomizer2 =
      (ServerCustomizer<Server>) mock(ServerCustomizer.class);
  Service service = new LocalService(mockMeta);
  HttpServer server =
      HttpServer.Builder.<Server>newBuilder().withHandler(service, Driver.Serialization.PROTOBUF)
          .withServerCustomizers(Arrays.asList(mockCustomizer1, mockCustomizer2), Server.class)
          .withPort(0).build();
  try {
    server.start();
    verify(mockCustomizer2).customize(any(Server.class));
    verify(mockCustomizer1).customize(any(Server.class));
  } finally {
    server.stop();
  }
}
 
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: CalciteRemoteDriverTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Service create(AvaticaConnection connection) {
  try {
    Connection conn = JdbcFrontLinqBackTest.makeConnection();
    final CalciteMetaImpl meta =
        new CalciteMetaImpl(
            conn.unwrap(CalciteConnectionImpl.class));
    return new LocalService(meta);
  } catch (Exception e) {
    throw TestUtil.rethrow(e);
  }
}
 
Example #11
Source File: CalciteRemoteDriverTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Service create(AvaticaConnection connection) {
  try {
    Connection localConnection = CalciteAssert.hr().connect();
    final Meta meta = CalciteConnectionImpl.TROJAN
        .getMeta((CalciteConnectionImpl) localConnection);
    return new LocalJsonService(new LocalService(meta));
  } catch (Exception e) {
    throw TestUtil.rethrow(e);
  }
}
 
Example #12
Source File: CalciteRemoteDriverTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Service create(AvaticaConnection connection) {
  try {
    Connection conn = makeConnection();
    final CalciteMetaImpl meta =
        new CalciteMetaImpl(conn.unwrap(CalciteConnectionImpl.class));
    return new LocalService(meta);
  } catch (Exception e) {
    throw TestUtil.rethrow(e);
  }
}
 
Example #13
Source File: Main.java    From quark with Apache License 2.0 5 votes vote down vote up
public void run(String[] args) throws Exception {
  logJVMInfo();
  try {

    Class<? extends Meta.Factory> factoryClass = QuarkMetaFactoryImpl.class;

    Meta.Factory factory =
        factoryClass.getDeclaredConstructor().newInstance();
    Meta meta = factory.create(Arrays.asList(args));

    int port = 8765;
    if (QuarkMetaFactoryImpl.serverConfig.port != 0) {
      port = QuarkMetaFactoryImpl.serverConfig.port;
    }
    LOG.debug("Listening on port " + port);

    final HandlerFactory handlerFactory = new HandlerFactory();
    Service service = new LocalService(meta);
    server = new HttpServer(port, getHandler(service, handlerFactory));
    Class.forName("com.qubole.quark.fatjdbc.QuarkDriver");
    server.start();
    runningLatch.countDown();
    server.join();
  } catch (Throwable t) {
    LOG.fatal("Unrecoverable service error. Shutting down.", t);
    this.t = t;
  }
}
 
Example #14
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 #15
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 #16
Source File: KarelDbMain.java    From kareldb with Apache License 2.0 5 votes vote down vote up
public static HttpServer start(KarelDbConfig config, KarelDbLeaderElector elector) throws SQLException {
    Meta meta = create(config);
    Service service = new LocalService(meta);
    AvaticaHandler localHandler = new AvaticaJsonHandler(service);
    AvaticaHandler handler = new DynamicAvaticaJsonHandler(config, localHandler, elector);
    HttpServer server = new HttpServerExtension(elector.getIdentity(), handler, config);
    server.start();
    return server;
}
 
Example #17
Source File: HttpServerCustomizerTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
@Test public void onlyJettyCustomizersAllowed() {
  Service service = new LocalService(mockMeta);
  List<ServerCustomizer<UnsupportedServer>> unsupportedCustomizers = new ArrayList<>();
  unsupportedCustomizers.add(new ServerCustomizer<UnsupportedServer>() {
    @Override public void customize(UnsupportedServer server) {
    }
  });
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("Only Jetty Server customizers are supported");
  HttpServer.Builder.<UnsupportedServer>newBuilder()
      .withHandler(service, Driver.Serialization.PROTOBUF)
      .withServerCustomizers(unsupportedCustomizers, UnsupportedServer.class).withPort(0).build();
}
 
Example #18
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());
  }
}
 
Example #19
Source File: CalciteRemoteDriverTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Override public Service create(AvaticaConnection connection) {
  try {
    Connection conn = JdbcFrontLinqBackTest.makeConnection();
    final CalciteMetaImpl meta =
        new CalciteMetaImpl(
            conn.unwrap(CalciteConnectionImpl.class));
    return new LocalService(meta);
  } catch (Exception e) {
    throw TestUtil.rethrow(e);
  }
}
 
Example #20
Source File: CalciteRemoteDriverTest.java    From Quicksql with MIT License 5 votes vote down vote up
public Service create(AvaticaConnection connection) {
  try {
    Connection localConnection = CalciteAssert.hr().connect();
    final Meta meta = CalciteConnectionImpl.TROJAN
        .getMeta((CalciteConnectionImpl) localConnection);
    return new LocalJsonService(new LocalService(meta));
  } catch (Exception e) {
    throw TestUtil.rethrow(e);
  }
}
 
Example #21
Source File: CalciteRemoteDriverTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Override public Service create(AvaticaConnection connection) {
  try {
    Connection conn = makeConnection();
    final CalciteMetaImpl meta =
        new CalciteMetaImpl(conn.unwrap(CalciteConnectionImpl.class));
    return new LocalService(meta);
  } catch (Exception e) {
    throw TestUtil.rethrow(e);
  }
}
 
Example #22
Source File: CustomAuthHttpServerTest.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
BasicAuthHandlerCustomizer(AvaticaServerConfiguration configuration, LocalService service,
    boolean isBasicAuth) {
  this.configuration = configuration;
  this.service = service;
  this.isBasicAuth = isBasicAuth;
}
 
Example #23
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private LoggingLocalJsonService(LocalService localService) {
  super(localService);
}
 
Example #24
Source File: Main.java    From calcite-avatica with Apache License 2.0 3 votes vote down vote up
/**
 * Creates and starts an {@link HttpServer} using the given factory to create the Handler.
 *
 * <p>Arguments are as follows:
 * <ul>
 *   <li>args[0]: the {@link org.apache.calcite.avatica.Meta.Factory} class
 *       name
 *   <li>args[1+]: arguments passed along to
 *   {@link org.apache.calcite.avatica.Meta.Factory#create(java.util.List)}
 * </ul>
 *
 * @param args Command-line arguments
 * @param port Server port to bind
 * @param handlerFactory Factory to create the handler used by the server
 */
public static HttpServer start(String[] args, int port, HandlerFactory handlerFactory)
    throws ClassNotFoundException, InstantiationException, IllegalAccessException,
    NoSuchMethodException, InvocationTargetException {
  String factoryClassName = args[0];
  @SuppressWarnings("unchecked") Class<Meta.Factory> factoryClass =
      (Class<Meta.Factory>) Class.forName(factoryClassName);
  Meta.Factory factory = factoryClass.getConstructor().newInstance();
  Meta meta = factory.create(Arrays.asList(args).subList(1, args.length));
  Service service = new LocalService(meta);
  HttpServer server = new HttpServer(port,
      HttpServer.wrapJettyHandler(handlerFactory.createHandler(service)));
  server.start();
  return server;
}