@BeforeAll static void registerFactory() { BackendFactoryRegistry.getInstance().register(SCHEME, new AbstractBackendFactory<BaseConfig<?>>("fake") {}); }
/** * Constructs a new {@code DefaultPersistentResource} with the given {@code uri}. * * @param uri the URI of the resource */ public DefaultPersistentResource(URI uri) { super(uri); factory = BackendFactoryRegistry.getInstance().getFactoryFor(uri.scheme()); // Creates a transient backend until a call to `save()`/`load()` eStore = createTransientStore(); logState("created"); }
/** * {@inheritDoc} * <p> * The {@code uri} must be registered in the {@link BackendFactoryRegistry}. */ @Nonnull @Override public PersistentResource createResource(URI uri) { checkNotNull(uri, "uri"); checkArgument(BackendFactoryRegistry.getInstance().isRegistered(uri.scheme()), "Unregistered scheme (%s)", uri.scheme()); return new DefaultPersistentResource(uri); }
@AfterAll static void unregisterFactory() { BackendFactoryRegistry.getInstance().unregister(SCHEME); }
/** * Checks the registration of several {@link BackendFactory}s. */ @Test void testMultipleAdd() { REGISTRY.register(MOCK_1, factory1); REGISTRY.register(MOCK_2, factory2); assertThat(REGISTRY.getFactories()).hasSize(2); BackendFactory registeredFactory1 = REGISTRY.getFactoryFor(MOCK_1); assertThat(registeredFactory1).isNotNull().isSameAs(factory1); BackendFactory registeredFactory2 = REGISTRY.getFactoryFor(MOCK_2); assertThat(registeredFactory2).isNotNull().isSameAs(factory2); } }
@Test void createResourceRegistered() { PersistentResourceFactory resourceFactory = PersistentResourceFactory.getInstance(); URI uri = mock(URI.class); when(uri.scheme()).thenReturn("mock0"); Backend backend = mock(InMemoryBackend.class); BackendFactory backendFactory = mock(BackendFactory.class); when(backendFactory.createBackend(any(URI.class), any(ImmutableConfig.class))).thenReturn(backend); BackendFactoryRegistry.getInstance().register(uri.scheme(), backendFactory); PersistentResource resource = resourceFactory.createResource(uri); assertThat(resource).isNotNull(); assertThat(resource.getURI()).isSameAs(uri); assertThat(resource.eStore()).isNotNull(); BackendFactoryRegistry.getInstance().unregister(uri.scheme()); }
/** * Dynamically registers all {@link BackendFactory} instances with their URI scheme. */ private void registerAll() { ServiceProvider.getInstance() .load(BackendFactory.class) .map(ServiceDefinition::get) .forEach(b -> register(Bindings.schemeOf(b), b)); }
/** * Checks that a {@link BackendFactory} is registered for the given URI {@code scheme}. * * @param scheme the URI scheme identifying the factory * * @return {@code true} if a factory is registered for the {@code scheme} */ public boolean isRegistered(@Nullable String scheme) { if (isNull(scheme)) { return false; } if (!factories.containsKey(scheme)) { registerAll(); } return factories.containsKey(scheme); }
/** * Returns the {@link BackendFactory} identified by the given URI {@code scheme}. * * @param scheme the URI scheme identifying the factory * * @return the factory * * @throws IllegalArgumentException if {@code scheme} is {@code null}, or if no factory is registered for the {@code * scheme} */ @Nonnull public BackendFactory getFactoryFor(String scheme) { checkNotNull(scheme, "scheme"); if (isRegistered(scheme)) { return factories.get(scheme); } throw new IllegalArgumentException(String.format("No factory is registered to process the URI scheme '%s'", scheme)); }
/** * Unregisters all factories in the {@link BackendFactoryRegistry}. */ @BeforeEach void unregisterFactories() { REGISTRY.getFactories().keySet().forEach(REGISTRY::unregister); }
/** * Checks the registration of one {@link BackendFactory}. */ @Test void testSingleAdd() { REGISTRY.register(MOCK_1, factory1); assertThat(REGISTRY.getFactories()).hasSize(1); BackendFactory registeredFactory = REGISTRY.getFactoryFor(MOCK_1); assertThat(registeredFactory).isNotNull().isSameAs(factory1); }
@Test void createResourceNotRegistered() { PersistentResourceFactory factory = PersistentResourceFactory.getInstance(); URI uri = mock(URI.class); when(uri.scheme()).thenReturn("mock0"); BackendFactoryRegistry.getInstance().unregister(uri.scheme()); assertThat( catchThrowable(() -> factory.createResource(uri)) ).isExactlyInstanceOf(IllegalArgumentException.class); } }
/** * Creates a {@link PersistentResource} on the given {@code backend}. * * @param file the file path of the resource * @param backend the backend of the resource * * @return a new {@link PersistentResource} */ @Nonnull private static PersistentResource createMockResource(File file, Backend backend) throws IOException { BackendFactoryRegistry.getInstance().register(Bindings.schemeOf(MockBackendFactory.MockUriFactory.class), new MockBackendFactory(backend)); PersistentResource resource = PersistentResourceFactory.getInstance().createResource(new MockBackendFactory.MockUriFactory().createLocalUri(file)); resource.save(new MockBackendFactory.MockConfig()); return resource; }
@Nonnull @Override public URI createLocalUri(URI uri) { if (!supportsLocal) { throw new UnsupportedOperationException(String.format("%s does not support file-based URI", getClass().getSimpleName())); } checkNotNull(uri, "uri"); if (Objects.equals(scheme(), uri.scheme())) { checkArgument(BackendFactoryRegistry.getInstance().isRegistered(uri.scheme()), "Unregistered scheme (%s)", uri.scheme()); return new FileBasedUri(uri); } if (Objects.equals(FileBasedUri.SCHEME, uri.scheme())) { return createLocalUri(new File(uri.toFileString())); } throw new IllegalArgumentException(String.format("Cannot create URI with the scheme %s", uri.scheme())); }
/** * Creates a {@link PersistentResource} on the given {@code backend}. * * @param uri the URI of the resource * @param backend the backend of the resource * * @return a new {@link PersistentResource} */ @Nonnull private static PersistentResource createMockResource(URI uri, Backend backend) throws IOException { BackendFactoryRegistry.getInstance().register(Bindings.schemeOf(MockBackendFactory.MockUriFactory.class), new MockBackendFactory(backend)); PersistentResource resource = PersistentResourceFactory.getInstance().createResource(new MockBackendFactory.MockUriFactory().createLocalUri(uri)); resource.save(new MockBackendFactory.MockConfig()); return resource; }
@Nonnull @Override public URI createRemoteUri(InetAddress host, int port, String model) { if (!supportsRemoteUris()) { throw new UnsupportedOperationException(String.format("%s does not support server-based URIs", getClass().getSimpleName())); } checkNotNull(host, "host"); checkGreaterThanOrEqualTo(port, 0, "port (%d) must be between 0-65535", port); checkLessThanOrEqualTo(port, 65535, "port (%d) must be between 0-65535", port); checkNotNull(model, "model"); checkArgument(BackendFactoryRegistry.getInstance().isRegistered(scheme()), "Unregistered scheme (%s)", scheme()); return URI.createHierarchicalURI(scheme(), host.getHostAddress() + ':' + port, null, new String[]{model}, null, null); } }