Search in sources :

Example 41 with Clock

use of java.time.Clock in project torodb by torodb.

the class Main method main.

public static void main(String[] args) throws Exception {
    try {
        Console console = JCommander.getConsole();
        ResourceBundle cliBundle = PropertyResourceBundle.getBundle("CliMessages");
        final CliConfig cliConfig = new CliConfig();
        JCommander jCommander = new JCommander(cliConfig, cliBundle, args);
        jCommander.setColumnSize(Integer.MAX_VALUE);
        if (cliConfig.isVersion()) {
            BuildProperties buildProperties = new DefaultBuildProperties();
            console.println(buildProperties.getFullVersion());
            System.exit(0);
        }
        if (cliConfig.isHelp()) {
            jCommander.usage();
            System.exit(0);
        }
        if (cliConfig.isHelpParam()) {
            console.println(cliBundle.getString("cli.help-param-header"));
            ConfigUtils.printParamDescriptionFromConfigSchema(Config.class, cliBundle, console, 0);
            System.exit(0);
        }
        cliConfig.addParams();
        final Config config = CliConfigUtils.readConfig(cliConfig);
        if (cliConfig.isPrintConfig()) {
            ConfigUtils.printYamlConfig(config, console);
            System.exit(0);
        }
        if (cliConfig.isPrintXmlConfig()) {
            ConfigUtils.printXmlConfig(config, console);
            System.exit(0);
        }
        if (cliConfig.isPrintParam()) {
            JsonNode jsonNode = ConfigUtils.getParam(config, cliConfig.getPrintParamPath());
            if (jsonNode != null) {
                console.print(jsonNode.asText());
            }
            System.exit(0);
        }
        configureLogger(cliConfig, config);
        config.getBackend().getBackendImplementation().accept(new BackendImplementationVisitor() {

            @Override
            public void visit(AbstractDerby value) {
                parseToropassFile(value);
            }

            @Override
            public void visit(AbstractPostgres value) {
                parseToropassFile(value);
            }

            public void parseToropassFile(BackendPasswordConfig value) {
                try {
                    ConfigUtils.parseToropassFile(value);
                } catch (Exception ex) {
                    throw new SystemException(ex);
                }
            }
        });
        AbstractReplication replication = config.getReplication();
        if (replication.getAuth().getUser() != null) {
            HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource()).withDefaultPort(27017);
            ConfigUtils.parseMongopassFile(new MongoPasswordConfig() {

                @Override
                public void setPassword(String password) {
                    replication.getAuth().setPassword(password);
                }

                @Override
                public String getUser() {
                    return replication.getAuth().getUser();
                }

                @Override
                public Integer getPort() {
                    return syncSource.getPort();
                }

                @Override
                public String getPassword() {
                    return replication.getAuth().getPassword();
                }

                @Override
                public String getMongopassFile() {
                    return config.getReplication().getMongopassFile();
                }

                @Override
                public String getHost() {
                    return syncSource.getHostText();
                }

                @Override
                public String getDatabase() {
                    return replication.getAuth().getSource();
                }
            });
        }
        if (config.getBackend().isLike(AbstractPostgres.class)) {
            AbstractPostgres postgres = config.getBackend().as(AbstractPostgres.class);
            if (cliConfig.isAskForPassword()) {
                console.print("Type database user " + postgres.getUser() + "'s password:");
                postgres.setPassword(readPwd());
            }
            if (postgres.getPassword() == null) {
                throw new SystemException("No password provided for database user " + postgres.getUser() + ".\n\n" + "Please add following line to file " + postgres.getToropassFile() + ":\n" + postgres.getHost() + ":" + postgres.getPort() + ":" + postgres.getDatabase() + ":" + postgres.getUser() + ":<password>\n" + "Replace <password> for database user " + postgres.getUser() + "'s password");
            }
        }
        try {
            Clock clock = Clock.systemDefaultZone();
            Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

                @Override
                @SuppressFBWarnings(value = "DM_EXIT", justification = "Since is really hard to stop cleanly all threads when an OOME is thrown we must " + "exit to avoid no more action is performed that could lead to an unespected " + "state")
                public void uncaughtException(Thread t, Throwable e) {
                    if (e instanceof OutOfMemoryError) {
                        try {
                            LOGGER.error("Fatal out of memory: " + e.getLocalizedMessage(), e);
                        } finally {
                            System.exit(1);
                        }
                    }
                }
            });
            Service stampedeService = StampedeBootstrap.createStampedeService(config, clock);
            stampedeService.startAsync();
            stampedeService.awaitTerminated();
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                stampedeService.stopAsync();
                stampedeService.awaitTerminated();
            }));
        } catch (CreationException ex) {
            ex.getErrorMessages().stream().forEach(m -> {
                if (m.getCause() != null) {
                    LOGGER.error(m.getCause().getMessage());
                } else {
                    LOGGER.error(m.getMessage());
                }
            });
            LogManager.shutdown();
            System.exit(1);
        }
    } catch (Throwable ex) {
        LOGGER.debug("Fatal error on initialization", ex);
        Throwable rootCause = Throwables.getRootCause(ex);
        String causeMessage = rootCause.getMessage();
        LogManager.shutdown();
        JCommander.getConsole().println("Fatal error while ToroDB was starting: " + causeMessage);
        System.exit(1);
    }
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Config(com.torodb.stampede.config.model.Config) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) DefaultBuildProperties(com.torodb.packaging.DefaultBuildProperties) ConfigUtils(com.torodb.packaging.config.util.ConfigUtils) ResourceBundle(java.util.ResourceBundle) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) JsonNode(com.fasterxml.jackson.databind.JsonNode) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) Charsets(com.google.common.base.Charsets) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) JCommander(com.beust.jcommander.JCommander) Throwables(com.google.common.base.Throwables) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) Console(com.beust.jcommander.internal.Console) HostAndPort(com.google.common.net.HostAndPort) Service(com.google.common.util.concurrent.Service) SystemException(com.torodb.core.exceptions.SystemException) CreationException(com.google.inject.CreationException) Logger(org.apache.logging.log4j.Logger) BuildProperties(com.torodb.core.BuildProperties) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) Clock(java.time.Clock) Log4jUtils(com.torodb.packaging.util.Log4jUtils) LogManager(org.apache.logging.log4j.LogManager) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) InputStream(java.io.InputStream) Config(com.torodb.stampede.config.model.Config) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DefaultBuildProperties(com.torodb.packaging.DefaultBuildProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode) Clock(java.time.Clock) HostAndPort(com.google.common.net.HostAndPort) SystemException(com.torodb.core.exceptions.SystemException) JCommander(com.beust.jcommander.JCommander) Console(com.beust.jcommander.internal.Console) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Service(com.google.common.util.concurrent.Service) CreationException(com.google.inject.CreationException) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) IOException(java.io.IOException) SystemException(com.torodb.core.exceptions.SystemException) CreationException(com.google.inject.CreationException) DefaultBuildProperties(com.torodb.packaging.DefaultBuildProperties) BuildProperties(com.torodb.core.BuildProperties) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig)

Example 42 with Clock

use of java.time.Clock in project torodb by torodb.

the class Main method main.

public static void main(String[] args) throws Exception {
    Console console = JCommander.getConsole();
    ResourceBundle cliBundle = PropertyResourceBundle.getBundle("CliMessages");
    final CliConfig cliConfig = new CliConfig();
    JCommander jCommander = new JCommander(cliConfig, cliBundle, args);
    jCommander.setColumnSize(Integer.MAX_VALUE);
    if (cliConfig.isHelp()) {
        jCommander.usage();
        System.exit(0);
    }
    if (cliConfig.isHelpParam()) {
        console.println(cliBundle.getString("help-param-header"));
        ResourceBundle configBundle = PropertyResourceBundle.getBundle("ConfigMessages");
        ConfigUtils.printParamDescriptionFromConfigSchema(Config.class, configBundle, console, 0);
        System.exit(0);
    }
    final Config config = CliConfigUtils.readConfig(cliConfig);
    if (cliConfig.isPrintConfig()) {
        ConfigUtils.printYamlConfig(config, console);
        System.exit(0);
    }
    if (cliConfig.isPrintXmlConfig()) {
        ConfigUtils.printXmlConfig(config, console);
        System.exit(0);
    }
    configureLogger(cliConfig, config);
    config.getBackend().getBackendImplementation().accept(new BackendImplementationVisitor() {

        @Override
        public void visit(AbstractDerby value) {
            parseToropassFile(value);
        }

        @Override
        public void visit(AbstractPostgres value) {
            parseToropassFile(value);
        }

        public void parseToropassFile(BackendPasswordConfig value) {
            try {
                ConfigUtils.parseToropassFile(value);
            } catch (Exception ex) {
                throw new SystemException(ex);
            }
        }
    });
    if (config.getProtocol().getMongo().getReplication() != null) {
        for (AbstractReplication replication : config.getProtocol().getMongo().getReplication()) {
            if (replication.getAuth().getUser() != null) {
                HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource()).withDefaultPort(27017);
                ConfigUtils.parseMongopassFile(new MongoPasswordConfig() {

                    @Override
                    public void setPassword(String password) {
                        replication.getAuth().setPassword(password);
                    }

                    @Override
                    public String getUser() {
                        return replication.getAuth().getUser();
                    }

                    @Override
                    public Integer getPort() {
                        return syncSource.getPort();
                    }

                    @Override
                    public String getPassword() {
                        return replication.getAuth().getPassword();
                    }

                    @Override
                    public String getMongopassFile() {
                        return config.getProtocol().getMongo().getMongopassFile();
                    }

                    @Override
                    public String getHost() {
                        return syncSource.getHostText();
                    }

                    @Override
                    public String getDatabase() {
                        return replication.getAuth().getSource();
                    }
                });
            }
        }
    }
    if (config.getBackend().isLike(AbstractPostgres.class)) {
        AbstractPostgres postgres = config.getBackend().as(AbstractPostgres.class);
        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + postgres.getUser() + " password:");
            postgres.setPassword(readPwd());
        }
    } else if (config.getBackend().isLike(AbstractDerby.class)) {
        AbstractDerby derby = config.getBackend().as(AbstractDerby.class);
        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + derby.getUser() + " password:");
            derby.setPassword(readPwd());
        }
    }
    try {
        Clock clock = Clock.systemDefaultZone();
        Service server;
        if (config.getProtocol().getMongo().getReplication() == null || config.getProtocol().getMongo().getReplication().isEmpty()) {
            Service toroDbServer = ToroDbBootstrap.createStandaloneService(config, clock);
            toroDbServer.startAsync();
            toroDbServer.awaitRunning();
            server = toroDbServer;
        } else {
            throw new UnsupportedOperationException("Replication not supported yet!");
        }
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            server.stopAsync();
            server.awaitTerminated();
        }));
    } catch (CreationException ex) {
        ex.getErrorMessages().stream().forEach(m -> {
            if (m.getCause() != null) {
                LOGGER.error(m.getCause().getMessage());
            } else {
                LOGGER.error(m.getMessage());
            }
        });
        System.exit(1);
    } catch (Throwable ex) {
        LOGGER.error("Fatal error on initialization", ex);
        Throwable rootCause = Throwables.getRootCause(ex);
        String causeMessage = rootCause.getMessage();
        JCommander.getConsole().println("Fatal error while ToroDB was starting: " + causeMessage);
        System.exit(1);
    }
}
Also used : Charsets(com.google.common.base.Charsets) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) Config(com.torodb.standalone.config.model.Config) JCommander(com.beust.jcommander.JCommander) Throwables(com.google.common.base.Throwables) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) Console(com.beust.jcommander.internal.Console) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) HostAndPort(com.google.common.net.HostAndPort) Service(com.google.common.util.concurrent.Service) SystemException(com.torodb.core.exceptions.SystemException) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) ConfigUtils(com.torodb.packaging.config.util.ConfigUtils) CreationException(com.google.inject.CreationException) Logger(org.apache.logging.log4j.Logger) ResourceBundle(java.util.ResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) Clock(java.time.Clock) Log4jUtils(com.torodb.packaging.util.Log4jUtils) LogManager(org.apache.logging.log4j.LogManager) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) InputStream(java.io.InputStream) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) Config(com.torodb.standalone.config.model.Config) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) Service(com.google.common.util.concurrent.Service) CreationException(com.google.inject.CreationException) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) Clock(java.time.Clock) IOException(java.io.IOException) SystemException(com.torodb.core.exceptions.SystemException) CreationException(com.google.inject.CreationException) HostAndPort(com.google.common.net.HostAndPort) SystemException(com.torodb.core.exceptions.SystemException) JCommander(com.beust.jcommander.JCommander) Console(com.beust.jcommander.internal.Console) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig)

Example 43 with Clock

use of java.time.Clock in project jdk8u_jdk by JetBrains.

the class TCKYear method now_Clock.

//-----------------------------------------------------------------------
// now(Clock)
//-----------------------------------------------------------------------
@Test
public void now_Clock() {
    Instant instant = OffsetDateTime.of(LocalDate.of(2010, 12, 31), LocalTime.of(0, 0), ZoneOffset.UTC).toInstant();
    Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
    Year test = Year.now(clock);
    assertEquals(test.getValue(), 2010);
}
Also used : Year(java.time.Year) Instant(java.time.Instant) Clock(java.time.Clock) Test(org.testng.annotations.Test)

Example 44 with Clock

use of java.time.Clock in project jdk8u_jdk by JetBrains.

the class TCKLocalDate method now_Clock_allSecsInDay_offset.

@Test
public void now_Clock_allSecsInDay_offset() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE);
        LocalDate test = LocalDate.now(clock);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2);
    }
}
Also used : Instant(java.time.Instant) Clock(java.time.Clock) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 45 with Clock

use of java.time.Clock in project jdk8u_jdk by JetBrains.

the class TCKLocalDate method now_Clock_tooBig.

@Test(expectedExceptions = DateTimeException.class)
public void now_Clock_tooBig() {
    Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC);
    LocalDate.now(clock);
}
Also used : Clock(java.time.Clock) Test(org.testng.annotations.Test)

Aggregations

Clock (java.time.Clock)209 Test (org.junit.Test)62 Instant (java.time.Instant)51 Test (org.testng.annotations.Test)43 LocalTime (java.time.LocalTime)15 ZonedDateTime (java.time.ZonedDateTime)12 LocalDateTime (java.time.LocalDateTime)11 ZoneId (java.time.ZoneId)11 LocalDate (java.time.LocalDate)10 OffsetDateTime (java.time.OffsetDateTime)8 Test (org.junit.jupiter.api.Test)7 HandlerTest (com.yahoo.vespa.config.server.http.HandlerTest)6 SessionHandlerTest (com.yahoo.vespa.config.server.http.SessionHandlerTest)6 IOException (java.io.IOException)6 SessionTest (com.yahoo.vespa.config.server.session.SessionTest)5 OffsetTime (java.time.OffsetTime)5 ZoneOffset (java.time.ZoneOffset)5 Chronology (java.time.chrono.Chronology)5 IsoChronology (java.time.chrono.IsoChronology)5 Date (java.util.Date)4