nstream.persist.store.snapshot.SnapshotServiceLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-store-snapshot Show documentation
Show all versions of nstream-store-snapshot Show documentation
API for snapshot support to be used by stores
The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nstream.persist.store.snapshot;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import swim.uri.Uri;
public final class SnapshotServiceLoader {
private SnapshotServiceLoader() { }
/**
* Enumerate all of the {@link SnapshotService}s available in the module/class path and return an
* instance of the first to support the specified URI.
* @param uri The URI of snapshot repository.
* @return A snapshot service that supports the provided URI.
* @throws SnapshotException No available service supports the URI.
*/
public static SnapshotService findSnapshotService(Uri uri) throws SnapshotException {
if (uri == null) {
throw new IllegalArgumentException("The URI must be non-null.");
}
try {
final var services = ServiceLoader.load(SnapshotService.class);
for (var service : services) {
if (service.canHandle(uri)) {
return service;
}
}
throw new SnapshotException(String.format("No snapshot service was available that can handle %s.", uri));
} catch (ServiceConfigurationError ex) {
throw new SnapshotException("Could not load snapshot services.", ex);
}
}
}