@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())); }
/** * 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); }
@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); } }
@AfterAll static void unregisterFactory() { BackendFactoryRegistry.getInstance().unregister(SCHEME); }
@BeforeAll static void registerFactory() { BackendFactoryRegistry.getInstance().register(SCHEME, new AbstractBackendFactory<BaseConfig<?>>("fake") {}); }
@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()); }
@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; }
/** * 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; }