@Override public void putAll(Map<String, String> props) { m_configProps.putAll(props); fireConfigChangeEventAsynchronously(); // causes the config to be written eventually... scheduleStore(); }
@Override protected void onStart() throws Exception { // Notify all interested listeners about this, but do this the first time synchronously, see ACE-431. fireConfigChangeEventSynchronously(); }
private File getConfigDir() throws IOException { File dir = new File(getAgentContext().getWorkDir(), CONFIG_STORAGE_SUBDIR); if (!dir.exists() && !dir.mkdir()) { throw new IOException("Unable to access configuration directory: " + dir.getAbsolutePath()); } return dir; }
@Override protected void onInit() throws Exception { loadConfig(); m_timer = new ResettableTimer(getExecutorService(), this, 5, TimeUnit.SECONDS); }
private void fireConfigChangeEventSynchronously() { getEventsHandler().sendEvent(EVENT_AGENT_CONFIG_CHANGED, getConfigurationSnapshot()); }
private void resetConfigurationHandler() throws Exception { resetConfigurationHandler(new ConfigurationHandlerImpl(m_context)); }
@Override public String get(String key, String defaultValue) { Object value = getProperty(key); if (value == null) { value = defaultValue; } return String.valueOf(value); }
private void loadConfig() throws IOException { InputStream input = null; try { input = new FileInputStream(getConfigFile()); Properties props = new Properties(); props.load(input); m_configProps.putAll(props); } finally { if (input != null) { input.close(); } } }
private File getConfigFile() throws IOException { File file = new File(getConfigDir(), CONFIG_STORAGE_FILENAME); if (!file.exists() && !file.createNewFile()) { throw new IOException("Unable to access configuration file: " + file.getAbsolutePath()); } return file; }
@Test public void testConfigClean() throws Exception { ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); assertEquals(configurationHandler.get("key1", "default1"), "default1"); // should be persisted configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); assertEquals(configurationHandler.get("key1", "default1"), "default1"); }
private void fireConfigChangeEventAsynchronously() { getEventsHandler().postEvent(EVENT_AGENT_CONFIG_CHANGED, getConfigurationSnapshot()); }
@Override public boolean getBoolean(String key, boolean defaultValue) { Object value = getProperty(key); if (value == null) { return defaultValue; } return (value instanceof Boolean) ? ((Boolean) value).booleanValue() : Boolean.parseBoolean(String.valueOf(value)); }
private void storeConfig() throws IOException { OutputStream output = null; try { output = new FileOutputStream(getConfigFile()); Properties props = new Properties(); props.putAll(m_configProps); props.store(output, "ACE Agent configuration"); } finally { if (output != null) { output.close(); } } } }
@Test public void testConfigLongProps() throws Exception { ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); configureAgent(configurationHandler, "long1", "42", "long2", "4"); assertEquals(configurationHandler.getLong("long1", 0l), 42); assertEquals(configurationHandler.getLong("long2", 0l), 4l); assertEquals(configurationHandler.getLong("longX", 42l), 42l); }
@Override public long getLong(String key, long defaultValue) { Object value = getProperty(key); if (value == null) { return defaultValue; } try { if (value instanceof Number) { return ((Number) value).longValue(); } else if (value instanceof String) { return Long.decode((String) value); } } catch (NumberFormatException exception) { // Ignore; return default... } return defaultValue; }
@Test public void testConfigBooleanProps() throws Exception { ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); configureAgent(configurationHandler, "boolean1", "true", "boolean2", "false"); assertEquals(configurationHandler.getBoolean("boolean1", false), true); assertEquals(configurationHandler.getBoolean("boolean2", true), false); assertEquals(configurationHandler.getBoolean("booleanX", true), true); assertEquals(configurationHandler.getBoolean("booleanY", false), false); }
@Override public int getInt(String key, int defaultValue) { Object value = getProperty(key); if (value == null) { return defaultValue; } try { if (value instanceof Number) { return ((Number) value).intValue(); } else if (value instanceof String) { return Integer.decode((String) value); } } catch (NumberFormatException exception) { // Ignore; return default... } return defaultValue; }
@BeforeClass public void setUpOnceAgain() throws Exception { m_webServer = new TestWebServer(PORT, "/", "generated"); m_webServer.start(); m_availableURL1 = new URL("http://localhost:" + PORT); m_availableURL2 = new URL("http://127.0.0.1:" + PORT); m_unavailableURL1 = new URL("http://localhost:19999"); m_unavailableURL2 = new URL("http://127.0.0.1:19999"); BundleContext bc = mockBundleContext(); m_agentContextImpl = mockAgentContext(); m_agentContext = m_agentContextImpl; // Make sure the default server URL is not reachable, as used for this test... m_agentContextImpl.setHandler(DiscoveryHandler.class, new DiscoveryHandlerImpl(m_unavailableURL1.toExternalForm(), true)); m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(bc)); m_agentContextImpl.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(bc)); m_agentContextImpl.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl()); replayTestMocks(); m_agentContextImpl.start(); }
@BeforeClass(alwaysRun = true) public void setUpOnceAgain() throws Exception { m_agentContext = mockAgentContext(); BundleContext bc = mockBundleContext(); m_eventsHandler = new EventsHandlerImpl(bc); m_agentContext.setHandler(EventsHandler.class, m_eventsHandler); m_agentContext.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(bc)); m_agentContext.setHandler(FeedbackHandler.class, new TestFeedbackHandler()); replayTestMocks(); m_agentContext.start(); }
@BeforeClass public void setUpAgain() throws Exception { m_agentContextImpl = mockAgentContext(); replayTestMocks(); BundleContext bc = mockBundleContext(); m_agentContextImpl.setHandler(FeedbackHandler.class, new FeedbackHandlerImpl()); m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(bc)); m_agentContextImpl.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(bc)); m_agentContextImpl.start(); }