@Override public void swap(Set<Map.Entry<String, T>> entries) { lock.writeLock().lock(); try { clearCache(); immutables.clear(); matches.clear(); for (Map.Entry<String, T> entry : entries) { addMatch(entry.getKey(), entry.getValue(), true, false); } } finally { lock.writeLock().unlock(); } onChange(); }
/** * return the value held against the nearest match * * @param match the match to look for * @return the value */ @Override public T getMatch(final String match) { String modifiedMatch = matchModifier.modify(match); T cacheResult = cache.get(modifiedMatch); if (cacheResult != null) { return cacheResult; } lock.readLock().lock(); try { T actualMatch; Map<String, Match<T>> possibleMatches = getPossibleMatches(modifiedMatch); Collection<Match<T>> orderedMatches = sort(possibleMatches); actualMatch = merge(orderedMatches); T value = actualMatch != null ? actualMatch : defaultmatch; if (value != null) { cache.put(modifiedMatch, value); } return value; } finally { lock.readLock().unlock(); } }
/** * remove a match from the repository * * @param match the match to remove */ @Override public void removeMatch(final String match) { lock.writeLock().lock(); try { String modMatch = matchModifier.modify(match); boolean isImmutable = immutables.contains(modMatch); if (isImmutable) { logger.debug("Cannot remove match " + modMatch + " since it came from a main config"); } else { /** * clear the cache before removing the match. This will force any thread at * {@link #getMatch(String)} to get the lock to recompute. */ clearCache(); matches.remove(modMatch); onChange(); } } finally { lock.writeLock().unlock(); } }
private static void printPages(File pageDirectory, DescribeJournal describeJournal, PrintStream out, boolean safe) { try { ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1, ActiveMQThreadFactory.defaultThreadFactory()); final ExecutorService executor = Executors.newFixedThreadPool(10, ActiveMQThreadFactory.defaultThreadFactory()); ExecutorFactory execfactory = new ExecutorFactory() { @Override public ArtemisExecutor getExecutor() { return ArtemisExecutor.delegate(executor); } }; final StorageManager sm = new NullStorageManager(); PagingStoreFactory pageStoreFactory = new PagingStoreFactoryNIO(sm, pageDirectory, 1000L, scheduled, execfactory, false, null); HierarchicalRepository<AddressSettings> addressSettingsRepository = new HierarchicalObjectRepository<>(); addressSettingsRepository.setDefault(new AddressSettings()); PagingManager manager = new PagingManagerImpl(pageStoreFactory, addressSettingsRepository); printPages(describeJournal, sm, manager, out, safe); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testMatchingDocs() throws Throwable { HierarchicalObjectRepository<String> repo = new HierarchicalObjectRepository<>(); repo.addMatch("a.b.#", "ab#"); repo.addMatch("a.b.d.#", "abd#"); repo.addMatch("#", "root"); Assert.assertEquals("ab#", repo.getMatch("a.b")); Assert.assertEquals("ab#", repo.getMatch("a.b.c")); Assert.assertEquals("abd#", repo.getMatch("a.b.d.lll")); Assert.assertEquals("root", repo.getMatch("z.z.z.z.z")); Assert.assertEquals("root", repo.getMatch("a.babc")); Assert.assertEquals("ab#", repo.getMatch("a.b.dabc")); Assert.assertEquals("abd#", repo.getMatch("a.b.d")); }
/** * Add a new match to the repository * * @param match The regex to use to match against * @param value the value to hold against the match */ @Override public void addMatch(final String match, final T value, final boolean immutableMatch) { addMatch(match, value, immutableMatch, true); }
/** * set the default value to fallback to if none found * * @param defaultValue the value */ @Override public void setDefault(final T defaultValue) { clearCache(); defaultmatch = defaultValue; }
@Override public void enableListeners() { lock.writeLock().lock(); try { this.listenersEnabled = true; } finally { lock.writeLock().unlock(); } onChange(); }
@Override @Before public void setUp() throws Exception { super.setUp(); securityRepository = new HierarchicalObjectRepository<>(); }
@Test public void testMatchingDocsCustomUnderscorDelimiter() throws Throwable { WildcardConfiguration wildcardConfiguration = new WildcardConfiguration(); wildcardConfiguration.setDelimiter('_'); HierarchicalObjectRepository<String> repo = new HierarchicalObjectRepository<>(wildcardConfiguration); repo.addMatch("a_b_#", "ab#"); repo.addMatch("a_b_d_#", "abd#"); repo.addMatch("#", "root"); Assert.assertEquals("ab#", repo.getMatch("a_b")); Assert.assertEquals("ab#", repo.getMatch("a_b_c")); Assert.assertEquals("abd#", repo.getMatch("a_b_d_lll")); Assert.assertEquals("root", repo.getMatch("z_z_z_z_z")); Assert.assertEquals("root", repo.getMatch("a_babc")); Assert.assertEquals("ab#", repo.getMatch("a_b_dabc")); Assert.assertEquals("abd#", repo.getMatch("a_b_d")); }
@Override public void addMatch(final String match, final T value) { addMatch(match, value, false); }
private void addMatch(final String match, final T value, final boolean immutableMatch, boolean notifyListeners) { lock.writeLock().lock(); try { String modifiedMatch = matchModifier.modify(match); clearCache(); if (immutableMatch) { immutables.add(modifiedMatch); } Match.verify(modifiedMatch, wildcardConfiguration); Match<T> match1 = new Match<>(modifiedMatch, value, wildcardConfiguration); matches.put(modifiedMatch, match1); } finally { lock.writeLock().unlock(); } // Calling the onChange outside of the wrieLock as some listeners may be doing reads on the matches if (notifyListeners) { onChange(); } }
@Override public void clear() { lock.writeLock().lock(); try { clearCache(); listeners.clear(); matches.clear(); } finally { lock.writeLock().unlock(); } }
addressSettingsRepository = new HierarchicalObjectRepository<>(configuration.getWildcardConfiguration(), new HierarchicalObjectRepository.MatchModifier() { @Override public String modify(String input) { securityRepository = new HierarchicalObjectRepository<>(configuration.getWildcardConfiguration());
@Test public void testMatchingDocsCustomForwardSlashDelimiter() throws Throwable { WildcardConfiguration wildcardConfiguration = new WildcardConfiguration(); wildcardConfiguration.setDelimiter('/'); HierarchicalObjectRepository<String> repo = new HierarchicalObjectRepository<>(wildcardConfiguration); repo.addMatch("a/b/#", "ab#"); repo.addMatch("a/b/d/#", "abd#"); repo.addMatch("#", "root"); Assert.assertEquals("ab#", repo.getMatch("a/b")); Assert.assertEquals("ab#", repo.getMatch("a/b/c")); Assert.assertEquals("abd#", repo.getMatch("a/b/d/lll")); Assert.assertEquals("root", repo.getMatch("z/z/z/z/z")); Assert.assertEquals("root", repo.getMatch("a/babc")); Assert.assertEquals("ab#", repo.getMatch("a/b/dabc")); Assert.assertEquals("abd#", repo.getMatch("a/b/d")); }
private static void printPages(File pageDirectory, DescribeJournal describeJournal, PrintStream out, boolean safe) { try { ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1, ActiveMQThreadFactory.defaultThreadFactory()); final ExecutorService executor = Executors.newFixedThreadPool(10, ActiveMQThreadFactory.defaultThreadFactory()); ExecutorFactory execfactory = new ExecutorFactory() { @Override public ArtemisExecutor getExecutor() { return ArtemisExecutor.delegate(executor); } }; final StorageManager sm = new NullStorageManager(); PagingStoreFactory pageStoreFactory = new PagingStoreFactoryNIO(sm, pageDirectory, 1000L, scheduled, execfactory, false, null); HierarchicalRepository<AddressSettings> addressSettingsRepository = new HierarchicalObjectRepository<>(); addressSettingsRepository.setDefault(new AddressSettings()); PagingManager manager = new PagingManagerImpl(pageStoreFactory, addressSettingsRepository); printPages(describeJournal, sm, manager, out, safe); } catch (Exception e) { e.printStackTrace(); } }
protected void initializeJournal(Configuration configuration) throws Exception { this.config = configuration; executor = Executors.newFixedThreadPool(5, ActiveMQThreadFactory.defaultThreadFactory()); executorFactory = new OrderedExecutorFactory(executor); scheduledExecutorService = new ScheduledThreadPoolExecutor(configuration.getScheduledThreadPoolMaxSize(), new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r); } }); HierarchicalRepository<AddressSettings> addressSettingsRepository = new HierarchicalObjectRepository<>(config.getWildcardConfiguration()); addressSettingsRepository.setDefault(new AddressSettings()); if (configuration.isJDBC()) { storageManager = new JDBCJournalStorageManager(config, null, scheduledExecutorService, executorFactory, executorFactory, null); PagingStoreFactory pageStoreFactory = new PagingStoreFactoryDatabase((DatabaseStorageConfiguration) configuration.getStoreConfiguration(), storageManager, 1000L, scheduledExecutorService, executorFactory, false, null); pagingmanager = new PagingManagerImpl(pageStoreFactory, addressSettingsRepository, configuration.getManagementAddress()); } else { storageManager = new JournalStorageManager(config, EmptyCriticalAnalyzer.getInstance(), executorFactory, executorFactory); PagingStoreFactory pageStoreFactory = new PagingStoreFactoryNIO(storageManager, config.getPagingLocation(), 1000L, scheduledExecutorService, executorFactory, true, null); pagingmanager = new PagingManagerImpl(pageStoreFactory, addressSettingsRepository, configuration.getManagementAddress()); } }
protected void initializeJournal(Configuration configuration) throws Exception { this.config = configuration; executor = Executors.newFixedThreadPool(5, ActiveMQThreadFactory.defaultThreadFactory()); executorFactory = new OrderedExecutorFactory(executor); scheduledExecutorService = new ScheduledThreadPoolExecutor(configuration.getScheduledThreadPoolMaxSize(), new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r); } }); HierarchicalRepository<AddressSettings> addressSettingsRepository = new HierarchicalObjectRepository<>(config.getWildcardConfiguration()); addressSettingsRepository.setDefault(new AddressSettings()); if (configuration.isJDBC()) { storageManager = new JDBCJournalStorageManager(config, null, scheduledExecutorService, executorFactory, executorFactory, null); PagingStoreFactory pageStoreFactory = new PagingStoreFactoryDatabase((DatabaseStorageConfiguration) configuration.getStoreConfiguration(), storageManager, 1000L, scheduledExecutorService, executorFactory, false, null); pagingmanager = new PagingManagerImpl(pageStoreFactory, addressSettingsRepository, configuration.getManagementAddress()); } else { storageManager = new JournalStorageManager(config, EmptyCriticalAnalyzer.getInstance(), executorFactory, executorFactory); PagingStoreFactory pageStoreFactory = new PagingStoreFactoryNIO(storageManager, config.getPagingLocation(), 1000L, scheduledExecutorService, executorFactory, true, null); pagingmanager = new PagingManagerImpl(pageStoreFactory, addressSettingsRepository, configuration.getManagementAddress()); } }
@Test public void testIllegalMatches() { HierarchicalRepository<String> repository = new HierarchicalObjectRepository<>(); try { repository.addMatch("hjhjhjhjh.#.hhh", "test"); fail("expected exception"); } catch (IllegalArgumentException e) { // pass } try { repository.addMatch(null, "test"); fail("expected exception"); } catch (IllegalArgumentException e) { // pass } }
@Test public void testAddListener() { HierarchicalRepository<String> repository = new HierarchicalObjectRepository<>(); repository.addMatch("#", "1"); repository.addMatch("B", "2"); final AtomicInteger called = new AtomicInteger(0); repository.registerListener(new HierarchicalRepositoryChangeListener() { @Override public void onChange() { called.incrementAndGet(); } }); assertEquals(1, called.get()); repository.disableListeners(); repository.addMatch("C", "3"); assertEquals(1, called.get()); repository.enableListeners(); assertEquals(2, called.get()); repository.addMatch("D", "4"); assertEquals(3, called.get()); repository.removeMatch("D"); assertEquals(4, called.get()); repository.disableListeners(); repository.removeMatch("C"); assertEquals(4, called.get()); }