class PolicyUpdater {

    public static void main(String[] args) throws IOException, InterruptedException {

        PolicyUpdaterConfiguration configuration = null;
        try {
            configuration = new PolicyUpdaterConfiguration();
        } catch (Exception ex) {
            LOG.error("Unable to create configuration object: " + ex.getMessage());
            System.exit(ZPUExitCode.CONFIG_CREATE_FAILURE.getCode());
        }

        Random randomGenerator = new Random();
        int randmonSleepInterval = 0;

        if (configuration.getStartupDelayIntervalInSecs() > 0) {
            randmonSleepInterval = randomGenerator.nextInt(configuration.getStartupDelayIntervalInSecs());
            LOG.info("Launching zpe_policy_updater in " + randmonSleepInterval + " seconds...");
            for (int i = 0; i < randmonSleepInterval; i++) {
                Thread.sleep(1000);
            }
        } else {
            LOG.info("Launching zpe_policy_updater with no delay...");
        }
        
        ZPUExitCode exitCode = ZPUExitCode.SUCCESS;
        try {
            try {
                configuration.init(null, null);
            } catch (Exception ex) {
                LOG.error("Unable to initialize configuration object: " + ex.getMessage());
                exitCode = ZPUExitCode.CONFIG_INIT_FAILURE;
                throw ex;
            }

            try {
                PolicyUpdater.policyUpdater(configuration, new ZTSClientFactoryImpl());
            } catch (Exception ex) {
                LOG.error("PolicyUpdater: Unable to update policy data: " + ex.getMessage());
                exitCode = ZPUExitCode.POLICY_UPDATE_FAILURE;
                throw ex;
            }
        } catch (Exception exc) {
            LOG.error("PolicyUpdater: Exiting upon error: " + exc.getMessage());
        } finally {
            System.exit(exitCode.getCode());
        }
    }

}