@Override public void initWithNiwsConfig(IClientConfig clientConfig) { clientName = clientConfig.getClientName(); vipAddresses = clientConfig.resolveDeploymentContextbasedVipAddresses(); if (vipAddresses == null && ConfigurationManager.getConfigInstance() .getBoolean("DiscoveryEnabledNIWSServerList.failFastOnNullVip", true)) { throw new NullPointerException("VIP address for client " + clientName + " is null"); } isSecure = Boolean.parseBoolean( "" + clientConfig.getProperty(CommonClientConfigKey.IsSecure, "false")); prioritizeVipAddressBasedServers = Boolean.parseBoolean( "" + clientConfig.getProperty( CommonClientConfigKey.PrioritizeVipAddressBasedServers, prioritizeVipAddressBasedServers)); datacenter = ConfigurationManager.getDeploymentContext().getDeploymentDatacenter(); targetRegion = (String) clientConfig.getProperty(CommonClientConfigKey.TargetRegion); // override client configuration and use client-defined port if (clientConfig.getPropertyAsBoolean( CommonClientConfigKey.ForceClientPortConfiguration, false)) { if (isSecure) { if (clientConfig.containsProperty(CommonClientConfigKey.SecurePort)) { overridePort = clientConfig.getPropertyAsInteger( CommonClientConfigKey.SecurePort, DefaultClientConfigImpl.DEFAULT_PORT); shouldUseOverridePort = true; } else { logger.warn( clientName + " set to force client port but no secure port is set, so ignoring"); } } else { if (clientConfig.containsProperty(CommonClientConfigKey.Port)) { overridePort = clientConfig.getPropertyAsInteger( CommonClientConfigKey.Port, DefaultClientConfigImpl.DEFAULT_PORT); shouldUseOverridePort = true; } else { logger.warn(clientName + " set to force client port but no port is set, so ignoring"); } } } }
@Override public void contextInitialized(ServletContextEvent servletContextEvent) { super.contextInitialized(servletContextEvent); /* * -------------------------------------------------------------------- * Archaius Configuration Settings * -------------------------------------------------------------------- */ Env env = Env.getEnvironment(); if (env == Env.ALL) { ConfigurationManager.getDeploymentContext().setDeploymentEnvironment("CHOP"); LOG.info("Setting environment to: CHOP"); } else if (env == Env.UNIT) { LOG.info("Operating in UNIT environment"); } try { ConfigurationManager.loadCascadedPropertiesFromResources("project"); } catch (IOException e) { LOG.error("Failed to load project properties!", e); throw new RuntimeException("Cannot do much without properly loading our configuration.", e); } /* * -------------------------------------------------------------------- * Environment Based Configuration Property Adjustments * -------------------------------------------------------------------- */ servletFig = injector.getInstance(ServletFig.class); runner = injector.getInstance(Runner.class); project = injector.getInstance(Project.class); ServletContext context = servletContextEvent.getServletContext(); /* * -------------------------------------------------------------------- * Adjust Runner Settings to Environment * -------------------------------------------------------------------- */ if (env == Env.UNIT || env == Env.INTEG || env == Env.ALL) { runner.bypass(Runner.HOSTNAME_KEY, "localhost"); runner.bypass(Runner.IPV4_KEY, "127.0.0.1"); } else if (env == Env.CHOP) { Ec2Metadata.applyBypass(runner); } StringBuilder sb = new StringBuilder(); sb.append("https://") .append(runner.getHostname()) .append(':') .append(runner.getServerPort()) .append(context.getContextPath()); String baseUrl = sb.toString(); runner.bypass(Runner.URL_KEY, baseUrl); LOG.info("Setting url key {} to base url {}", Runner.URL_KEY, baseUrl); File tempDir = new File(System.getProperties().getProperty("java.io.tmpdir")); runner.bypass(Runner.RUNNER_TEMP_DIR_KEY, tempDir.getAbsolutePath()); LOG.info( "Setting runner temp directory key {} to context temp directory {}", Runner.RUNNER_TEMP_DIR_KEY, tempDir.getAbsolutePath()); /* * -------------------------------------------------------------------- * Adjust ServletFig Settings to Environment * -------------------------------------------------------------------- */ servletFig.bypass(ServletFig.SERVER_INFO_KEY, context.getServerInfo()); LOG.info( "Setting server info key {} to {}", ServletFig.SERVER_INFO_KEY, context.getServerInfo()); servletFig.bypass(ServletFig.CONTEXT_PATH, context.getContextPath()); LOG.info( "Setting server context path key {} to {}", ServletFig.CONTEXT_PATH, context.getContextPath()); // @todo Is this necessary? servletFig.bypass(ServletFig.CONTEXT_TEMPDIR_KEY, tempDir.getAbsolutePath()); LOG.info( "Setting runner context temp directory key {} to context temp directory {}", ServletFig.CONTEXT_TEMPDIR_KEY, tempDir.getAbsolutePath()); /* * -------------------------------------------------------------------- * Start Up The RunnerRegistry and Register * -------------------------------------------------------------------- */ if (isTestMode()) { runner.bypass(Runner.HOSTNAME_KEY, "localhost"); runner.bypass(Runner.IPV4_KEY, "127.0.0.1"); project.bypass(Project.LOAD_KEY, "bogus-load-key"); project.bypass(Project.ARTIFACT_ID_KEY, "bogus-artifact-id"); project.bypass(Project.GROUP_ID_KEY, "org.apache.usergrid.chop"); project.bypass(Project.CHOP_VERSION_KEY, "bogus-chop-version"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); project.bypass(Project.CREATE_TIMESTAMP_KEY, dateFormat.format(new Date())); project.bypass( Project.GIT_URL_KEY, "http://stash.safehaus.org/projects/CHOP/repos/main/browse"); project.bypass(Project.GIT_UUID_KEY, "d637a8ce"); project.bypass(Project.LOAD_TIME_KEY, dateFormat.format(new Date())); project.bypass(Project.PROJECT_VERSION_KEY, "1.0.0-SNAPSHOT"); } if (runner.getHostname() != null && project.getLoadKey() != null) { final RunnerRegistry registry = getInjector().getInstance(RunnerRegistry.class); if (env != Env.TEST && env != Env.UNIT) { registry.register(runner); registered = true; Runtime.getRuntime() .addShutdownHook( new Thread( new Runnable() { @Override public void run() { if (registered) { System.err.println( "Premature shutdown, attempting to unregister this runner."); registry.unregister(runner); LOG.info("Unregistering runner on shutdownx: {}", runner.getHostname()); registered = false; } } })); LOG.info("Registered runner information in coordinator registry."); } else { LOG.warn("Env = {} so we are not registering this runner.", env); } } else { LOG.warn( "Runner registry not started, and not registered: insufficient configuration parameters."); } }
@BeforeClass public static void init() { ConfigurationManager.getDeploymentContext().setValue(ContextKey.zone, "us-eAst-1C"); }