@Test public void testWatch() throws IOException, IllegalAccessException ,InterruptedException{ System.setProperty("rocketmq.home.dir", "src/test/resources"); System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl-test.yml"); String fileName =System.getProperty("rocketmq.home.dir", "src/test/resources")+System.getProperty("rocketmq.acl.plain.file", "/conf/plain_acl.yml"); File transport = new File(fileName); transport.delete(); transport.createNewFile(); FileWriter writer = new FileWriter(transport); writer.write("accounts:\r\n"); writer.write("- accessKey: watchrocketmq\r\n"); writer.write(" secretKey: 12345678\r\n"); writer.write(" whiteRemoteAddress: 127.0.0.1\r\n"); writer.write(" admin: true\r\n"); writer.flush(); writer.close(); PlainPermissionManager plainPermissionManager = new PlainPermissionManager(); Assert.assertTrue(plainPermissionManager.isWatchStart()); { Map<String, PlainAccessResource> plainAccessResourceMap = (Map<String, PlainAccessResource>) FieldUtils.readDeclaredField(plainPermissionManager, "plainAccessResourceMap", true); PlainAccessResource accessResource = plainAccessResourceMap.get("watchrocketmq"); Assert.assertNotNull(accessResource); Assert.assertEquals(accessResource.getSecretKey(), "12345678"); Assert.assertTrue(accessResource.isAdmin()); } Map<String, Object> updatedMap = AclUtils.getYamlDataObject(fileName, Map.class); List<Map<String, Object>> accounts = (List<Map<String, Object>>) updatedMap.get("accounts"); accounts.get(0).remove("accessKey"); accounts.get(0).remove("secretKey"); accounts.get(0).put("accessKey", "watchrocketmq1"); accounts.get(0).put("secretKey", "88888888"); accounts.get(0).put("admin", "false"); AclUtils.writeDataObject(fileName, updatedMap); Thread.sleep(1000); { Map<String, PlainAccessResource> plainAccessResourceMap = (Map<String, PlainAccessResource>) FieldUtils.readDeclaredField(plainPermissionManager, "plainAccessResourceMap", true); PlainAccessResource accessResource = plainAccessResourceMap.get("watchrocketmq1"); Assert.assertNotNull(accessResource); Assert.assertEquals(accessResource.getSecretKey(), "88888888"); Assert.assertFalse(accessResource.isAdmin()); } transport.delete(); System.setProperty("rocketmq.home.dir", "src/test/resources"); System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl.yml"); }
private void watch() { try { String watchFilePath = fileHome + fileName; FileWatchService fileWatchService = new FileWatchService(new String[] {watchFilePath}, new FileWatchService.Listener() { @Override public void onChanged(String path) { log.info("The plain acl yml changed, reload the context"); load(); } }); fileWatchService.start(); log.info("Succeed to start AclWatcherService"); this.isWatchStart = true; } catch (Exception e) { log.error("Failed to start AclWatcherService", e); } }
PlainPermissionManager { private void watch() { try { String watchFilePath = fileHome + fileName; FileWatchService fileWatchService = new FileWatchService(new String[] {watchFilePath}, new FileWatchService.Listener() { @Override public void onChanged(String path) { log.info("The plain acl yml changed, reload the context"); load(); } }); fileWatchService.start(); log.info("Succeed to start AclWatcherService"); this.isWatchStart = true; } catch (Exception e) { log.error("Failed to start AclWatcherService", e); } } }
PlainPermissionManager { private void watch() { try { String watchFilePath = fileHome + fileName; FileWatchService fileWatchService = new FileWatchService(new String[] {watchFilePath}, new FileWatchService.Listener() { @Override public void onChanged(String path) { log.info("The plain acl yml changed, reload the context"); load(); } }); fileWatchService.start(); log.info("Succeed to start AclWatcherService"); this.isWatchStart = true; } catch (Exception e) { log.error("Failed to start AclWatcherService", e); } } PlainPermissionManager(); }
PlainPermissionManager { private void watch() { try { String watchFilePath = fileHome + fileName; FileWatchService fileWatchService = new FileWatchService(new String[] {watchFilePath}, new FileWatchService.Listener() { @Override public void onChanged(String path) { log.info("The plain acl yml changed, reload the context"); load(); } }); fileWatchService.start(); log.info("Succeed to start AclWatcherService"); this.isWatchStart = true; } catch (Exception e) { log.error("Failed to start AclWatcherService", e); } } PlainPermissionManager(); void load(); String getAclConfigDataVersion(); boolean updateAccessConfig(PlainAccessConfig plainAccessConfig); boolean deleteAccessConfig(String accesskey); boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList); AclConfig getAllAclConfig(); PlainAccessResource buildPlainAccessResource(PlainAccessConfig plainAccessConfig); void validate(PlainAccessResource plainAccessResource); boolean isWatchStart(); }
PlainPermissionManager { private void watch() { try { String watchFilePath = fileHome + fileName; FileWatchService fileWatchService = new FileWatchService(new String[] {watchFilePath}, new FileWatchService.Listener() { @Override public void onChanged(String path) { log.info("The plain acl yml changed, reload the context"); load(); } }); fileWatchService.start(); log.info("Succeed to start AclWatcherService"); this.isWatchStart = true; } catch (Exception e) { log.error("Failed to start AclWatcherService", e); } } PlainPermissionManager(); void load(); String getAclConfigDataVersion(); boolean updateAccessConfig(PlainAccessConfig plainAccessConfig); boolean deleteAccessConfig(String accesskey); boolean updateGlobalWhiteAddrsConfig(List<String> globalWhiteAddrsList); AclConfig getAllAclConfig(); PlainAccessResource buildPlainAccessResource(PlainAccessConfig plainAccessConfig); void validate(PlainAccessResource plainAccessResource); boolean isWatchStart();  }