@SuppressWarnings("unchecked") private final <T> Encoding<T, Value<T>> resolve(final Key key) { Encoding<T, Value<T>> enc; if ((enc = (Encoding<T, Value<T>>) byKey.get(key)) != null) return enc; else throw new NdbcException("Can't decode value " + key); }
@SuppressWarnings("unchecked") private final <T> T loadEncoding(final String cls) { try { return (T) Class.forName(cls).newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new NdbcException("Can't load encoding " + cls + ". Make sure to provide an empty constructor.", e); } }
@SuppressWarnings("unchecked") private final <T> Encoding<T, Value<T>> resolve(final Value<?> value) { Encoding<T, Value<T>> enc; if ((enc = (Encoding<T, Value<T>>) byValueClass.get(value.getClass())) != null) return enc; else throw new NdbcException("Can't encode value: " + value); }
@SuppressWarnings("unchecked") public final <T> void encodeBinary(final Value<T> value, final BufferWriter writer) { Encoding<T, Value<T>> enc; if ((enc = (Encoding<T, Value<T>>) byValueClass.get(value.getClass())) != null) enc.encodeBinary(value, writer); else throw new NdbcException("Can't encode value: " + value); }
public final Value<?> decode(final int oid, final Format format, final BufferReader reader) { Encoding<?, ?> enc; if ((enc = byOid.get(oid)) != null) return enc.decode(format, reader); else throw new NdbcException("Can't decode value of type " + oid); }
public final FieldType fieldType(final Value<?> value) { final Encoding<?, ?> encoding = byValueClass.get(value.getClass()); if (encoding == null) throw new NdbcException("Can't encode " + value); return encoding.key().fieldType; }
private static final <T> T getRequiredProperty(final String prefix, final Properties properties, final String name, final Function<String, T> parser) { final String value = getRequiredProperty(prefix, properties, name); try { return parser.apply(value); } catch (final Exception ex) { throw new NdbcException("Can't parse value `" + value + "` for config `" + prefix + "." + name + "`.", ex); } }
private static final String getRequiredProperty(final String prefix, final Properties properties, final String name) { return getProperty(prefix, properties, name).orElseGet(() -> { throw new NdbcException("Missing config `" + prefix + "." + name + "`."); }); }
private static final <T> Optional<T> getProperty(final String prefix, final Properties properties, final String name, final Function<String, T> parser) { return getProperty(prefix, properties, name).map(value -> { try { return parser.apply(value); } catch (final Exception ex) { throw new NdbcException( "Can't parse value `" + value + "` for config `" + prefix + "." + name + "`.", ex); } }); }
public static byte[] scramble411(final String password, final byte[] seed, final Charset charset) { try { final MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); final byte[] initialDigest = messageDigest.digest(password.getBytes(charset)); final byte[] finalDigest = messageDigest.digest(initialDigest); messageDigest.reset(); messageDigest.update(seed); messageDigest.update(finalDigest); final byte[] result = messageDigest.digest(); int counter = 0; while (counter < result.length) { result[counter] = (byte) (result[counter] ^ initialDigest[counter]); counter++; } return result; } catch (final NoSuchAlgorithmException e) { throw new NdbcException("NoSuchAlgorithmException []", e); } } }
static <R> Exchange<R> fail(final String error) { return fail(new NdbcException(error)); }
/** * Creates the data source based on a `Config` instance. * * @param config * the config * @return the data source instance */ @SuppressWarnings("unchecked") public static DataSource<PreparedStatement, Row> fromConfig(final Config config) { return config.embedded().map(embedded -> { try { final Supplier<DataSource<PreparedStatement, Row>> supplier = (Supplier<DataSource<PreparedStatement, Row>>) Class .forName(embedded.supplierClass) .getConstructor(Config.class, Optional.class).newInstance(config, embedded.version); return supplier.get(); } catch (final Exception e) { throw new NdbcException("Can't load DataSource supplier for: " + embedded, e); } }).orElseGet(() -> { try { final Supplier<DataSource<PreparedStatement, Row>> supplier = (Supplier<DataSource<PreparedStatement, Row>>) Class .forName(config.dataSourceSupplierClass()) .getConstructor(Config.class).newInstance(config); return supplier.get(); } catch (final Exception e) { throw new NdbcException("Can't load DataSource supplier: " + config.dataSourceSupplierClass(), e); } }); }
dsSupplier = "io.trane.ndbc.postgres.netty4.DataSourceSupplier"; else throw new NdbcException("Can't determine the data source supplier from the jdbc url"); prop.put(prefix + "dataSourceSupplierClass", dsSupplier); prop.put(prefix + key, value); } catch (final UnsupportedEncodingException e) { throw new NdbcException("Can't decode the url parameters", e);
public Exchange<Supplier<Exchange<List<io.trane.ndbc.Row>>>> rows() { return Exchange.receive(unmarshallers.terminator.orElse(unmarshallers.columnCount)).flatMap(msg -> { if (msg instanceof ColumnCount) { final int count = (int) ((ColumnCount) msg).count; return fields(count, new ArrayList<Field>(count)) .map(fields -> resultSet(fields)); } else if (msg instanceof OkPacket) return Exchange.value(() -> emptyRows); else throw new NdbcException(msg.toString()); }); }
private Optional<T> read(final byte tpe, final BufferReader b) { switch (tpe) { case 'E': final InfoResponse.ErrorResponse error = new InfoResponse.ErrorResponse( infoResponseFieldsUnmarshaller.apply(b)); throw new NdbcException(error.toString()); case 'N': final InfoResponse.NoticeResponse notice = new InfoResponse.NoticeResponse( infoResponseFieldsUnmarshaller.apply(b)); log.debug(notice.toString()); return Optional.empty(); case 'A': final NotificationResponse notification = new NotificationResponse(b.readInt(), b.readCString(charset), b.readCString(charset)); log.info(notification.toString()); return Optional.empty(); default: if (!acceptsType(tpe)) { String msg = "Unmarshaller " + getClass() + " doesn't accept message type " + (char) tpe; throw new IllegalStateException(msg); } return Optional.of(decode(tpe, b)); } }
@Override public void apply(final HandshakeResponseMessage message, final BufferWriter b) { final PacketBufferWriter packet = new PacketBufferWriter(b, message.sequence); long clientCapabilities = BASE_CAPABILITIES; if (message.database.isPresent()) clientCapabilities |= CLIENT_CONNECT_WITH_DB; packet.writeUnsignedInt(clientCapabilities); packet.writeUnsignedInt(MAX_3_BYTES); final int collationId = Collation.getCollationByEncoding(message.encoding).id; packet.writeByte((byte) collationId); packet.writeBytes(new byte[23]); packet.writeCString(message.username); if (message.password.isPresent()) { if (message.authenticationMethod != "mysql_native_password") throw new NdbcException("authenticationMethod not supported"); final byte[] bytes = scramble411(message.password.get(), message.seed, packet.getCharset()); packet.writeByte((byte) 0x14); packet.writeBytes(bytes); } else packet.writeByte((byte) 0); message.database.ifPresent(packet::writeCString); packet.writeCString(message.authenticationMethod); packet.flush(); }