Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load protocols using service loader. #16566

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
import ch.cyberduck.core.LocaleFactory;
import ch.cyberduck.core.LoginOptions;
import ch.cyberduck.core.PathContainerService;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.text.DefaultLexicographicOrderComparator;

import java.util.Comparator;

import com.microsoft.azure.storage.core.Base64;
import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class AzureProtocol extends AbstractProtocol {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

import ch.cyberduck.core.AbstractProtocol;
import ch.cyberduck.core.PathContainerService;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.synchronization.ComparisonService;
import ch.cyberduck.core.synchronization.DefaultComparisonService;
import ch.cyberduck.core.synchronization.VersionIdComparisonService;
import ch.cyberduck.core.text.DefaultLexicographicOrderComparator;

import java.util.Comparator;
import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class B2Protocol extends AbstractProtocol {

@Override
Expand Down
3 changes: 3 additions & 0 deletions box/src/main/java/ch/cyberduck/core/box/BoxProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/

import ch.cyberduck.core.AbstractProtocol;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.synchronization.ComparisonService;
import ch.cyberduck.core.synchronization.DefaultComparisonService;
import ch.cyberduck.core.synchronization.ETagComparisonService;
import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class BoxProtocol extends AbstractProtocol {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import ch.cyberduck.core.Credentials;
import ch.cyberduck.core.CredentialsConfigurator;
import ch.cyberduck.core.LoginOptions;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.features.Pairing;

import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class BrickProtocol extends AbstractProtocol {

@Override
Expand Down
65 changes: 4 additions & 61 deletions cli/src/main/java/ch/cyberduck/cli/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,26 @@
*/

import ch.cyberduck.core.*;
import ch.cyberduck.core.azure.AzureProtocol;
import ch.cyberduck.core.b2.B2Protocol;
import ch.cyberduck.core.box.BoxProtocol;
import ch.cyberduck.core.brick.BrickProtocol;
import ch.cyberduck.core.cdn.Distribution;
import ch.cyberduck.core.ctera.CteraProtocol;
import ch.cyberduck.core.dav.DAVProtocol;
import ch.cyberduck.core.dav.DAVSSLProtocol;
import ch.cyberduck.core.deepbox.DeepboxProtocol;
import ch.cyberduck.core.dropbox.DropboxProtocol;
import ch.cyberduck.core.editor.DefaultEditorListener;
import ch.cyberduck.core.editor.Editor;
import ch.cyberduck.core.editor.EditorFactory;
import ch.cyberduck.core.exception.AccessDeniedException;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.ftp.FTPProtocol;
import ch.cyberduck.core.ftp.FTPTLSProtocol;
import ch.cyberduck.core.googledrive.DriveProtocol;
import ch.cyberduck.core.googlestorage.GoogleStorageProtocol;
import ch.cyberduck.core.hubic.HubicProtocol;
import ch.cyberduck.core.io.DisabledStreamListener;
import ch.cyberduck.core.irods.IRODSProtocol;
import ch.cyberduck.core.local.Application;
import ch.cyberduck.core.local.ApplicationFinder;
import ch.cyberduck.core.local.ApplicationFinderFactory;
import ch.cyberduck.core.local.TemporaryFileServiceFactory;
import ch.cyberduck.core.logging.LoggerPrintStream;
import ch.cyberduck.core.manta.MantaProtocol;
import ch.cyberduck.core.nextcloud.NextcloudProtocol;
import ch.cyberduck.core.nio.LocalProtocol;
import ch.cyberduck.core.onedrive.OneDriveProtocol;
import ch.cyberduck.core.onedrive.SharepointProtocol;
import ch.cyberduck.core.onedrive.SharepointSiteProtocol;
import ch.cyberduck.core.openstack.SwiftProtocol;
import ch.cyberduck.core.owncloud.OwncloudProtocol;
import ch.cyberduck.core.pool.SessionPool;
import ch.cyberduck.core.preferences.Preferences;
import ch.cyberduck.core.preferences.PreferencesFactory;
import ch.cyberduck.core.s3.S3Protocol;
import ch.cyberduck.core.sds.SDSProtocol;
import ch.cyberduck.core.sftp.SFTPProtocol;
import ch.cyberduck.core.smb.SMBProtocol;
import ch.cyberduck.core.spectra.SpectraProtocol;
import ch.cyberduck.core.serviceloader.AutoServiceLoaderFactory;
import ch.cyberduck.core.ssl.CertificateStoreX509TrustManager;
import ch.cyberduck.core.ssl.DefaultTrustManagerHostnameCallback;
import ch.cyberduck.core.ssl.PreferencesX509KeyManager;
import ch.cyberduck.core.storegate.StoregateProtocol;
import ch.cyberduck.core.threading.DisabledAlertCallback;
import ch.cyberduck.core.threading.DisconnectBackgroundAction;
import ch.cyberduck.core.threading.SessionBackgroundAction;
Expand Down Expand Up @@ -135,38 +107,9 @@ public Terminal(final TerminalPreferences defaults, final Options options, final
public Terminal(final ProtocolFactory protocols, final TerminalPreferences defaults, final Options options, final CommandLine input) {
this.protocols = protocols;
this.preferences = defaults.withDefaults(input);
this.protocols.register(
new FTPProtocol(),
new FTPTLSProtocol(),
new SFTPProtocol(),
new DAVProtocol(),
new DAVSSLProtocol(),
new SMBProtocol(),
new SwiftProtocol(),
new S3Protocol(),
new GoogleStorageProtocol(),
new AzureProtocol(),
new IRODSProtocol(),
new SpectraProtocol(),
new B2Protocol(),
new DriveProtocol(),
new HubicProtocol(),
new DropboxProtocol(),
new DropboxProtocol(),
new OneDriveProtocol(),
new SharepointProtocol(),
new SharepointSiteProtocol(),
new LocalProtocol(),
new SDSProtocol(),
new MantaProtocol(),
new StoregateProtocol(),
new BrickProtocol(),
new NextcloudProtocol(),
new OwncloudProtocol(),
new CteraProtocol(),
new BoxProtocol(),
new DeepboxProtocol()
);
for(Protocol p : AutoServiceLoaderFactory.<Protocol>get().load(Protocol.class)) {
protocols.register(p);
}
this.options = options;
log.info("Parsed options {} from input {}", options, input);
this.input = input;
Expand Down
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@
<groupId>com.joyent.util</groupId>
<artifactId>fast-md5</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using ikvm.runtime;
using java.lang;
using java.util;
using org.apache.logging.log4j;
using System;
using System.IO;
using System.Reflection;

namespace ch.cyberduck.core.serviceloader;

public sealed class AppContextServiceLoader : AutoServiceLoader
{
/*
* ServiceLoader is factory-created, and only lives for a short time.
* The logger doesn't need to survive for longer than the loader.
*/
private readonly Logger _logger = LogManager.getLogger(typeof(AppContextServiceLoader));

public Set load(Class c)
{
HashSet set = [];
var files = Directory.EnumerateFiles(AppContext.BaseDirectory, "*.dll", SearchOption.TopDirectoryOnly);
foreach (var item in files)
{
Assembly assembly;
try
{
assembly = Assembly.LoadFrom(item);
}
catch
{
/*
* Silently skip native libraries
* or otherwise bad files
*/
continue;
}

try
{
foreach (var service in ServiceLoader.load(c, AssemblyClassLoader.getAssemblyClassLoader(assembly)))
{
set.add(service);
}
}
catch (System.Exception e)
{
if (_logger.isDebugEnabled())
{
_logger.debug($"Activating {c.getName()} instances in {assembly.FullName}", e);
}
}
}

return set;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import ch.cyberduck.core.serializer.impl.dd.PlistWriter;
import ch.cyberduck.core.serializer.impl.dd.ProfilePlistReader;
import ch.cyberduck.core.serializer.impl.dd.TransferPlistReader;
import ch.cyberduck.core.serviceloader.AnnotationAutoServiceLoader;
import ch.cyberduck.core.socket.NetworkInterfaceHardwareAddress;
import ch.cyberduck.core.threading.DefaultThreadPool;
import ch.cyberduck.core.threading.DisabledActionOperationBatcher;
Expand Down Expand Up @@ -480,6 +481,7 @@ public boolean getBoolean(final String key) {
}

protected void setFactories() {
this.setDefault("factory.autoserviceloader.class", AnnotationAutoServiceLoader.class.getName());
this.setDefault("factory.serializer.class", PlistSerializer.class.getName());
this.setDefault("factory.deserializer.class", PlistDeserializer.class.getName());
this.setDefault("factory.reader.profile.class", ProfilePlistReader.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ch.cyberduck.core.serviceloader;

/*
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;

public class AnnotationAutoServiceLoader<T> implements AutoServiceLoader<T> {

@Override
public Set<T> load(final Class<T> type) {
final ServiceLoader<T> loader = ServiceLoader.load(type);
final Set<T> services = new HashSet<>();
for(T t : loader) {
services.add(t);
}
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ch.cyberduck.core.serviceloader;

/*
* Copyright (c) 2002-2024 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import java.util.Set;

public interface AutoServiceLoader<T> {
Set<T> load(Class<T> type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch.cyberduck.core.serviceloader;

/*
* Copyright (c) 2012 David Kocher. All rights reserved.
* http://cyberduck.ch/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Bug fixes, suggestions and comments should be sent to:
* dkocher@cyberduck.ch
*/

import ch.cyberduck.core.Factory;

public class AutoServiceLoaderFactory extends Factory<AutoServiceLoader> {

private AutoServiceLoaderFactory() {
super("factory.autoserviceloader.class");
}

public static <T> AutoServiceLoader<T> get() {
return new AutoServiceLoaderFactory().create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
import ch.cyberduck.core.AbstractProtocol;
import ch.cyberduck.core.Credentials;
import ch.cyberduck.core.LoginOptions;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.dav.DAVSSLProtocol;
import ch.cyberduck.core.preferences.PreferencesFactory;
import ch.cyberduck.core.synchronization.ComparisonService;
import ch.cyberduck.core.synchronization.DefaultComparisonService;
import ch.cyberduck.core.synchronization.ETagComparisonService;

import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class CteraProtocol extends AbstractProtocol {

public static final String CTERA_REDIRECT_URI = String.format("%s:websso",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
*/

import ch.cyberduck.core.AbstractProtocol;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;

import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class DeepboxProtocol extends AbstractProtocol {
@Override
public String getIdentifier() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import ch.cyberduck.core.AbstractProtocol;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.features.Pairing;
import ch.cyberduck.core.features.Scheduler;
Expand All @@ -29,6 +30,9 @@

import org.apache.commons.lang3.StringUtils;

import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class SDSProtocol extends AbstractProtocol {
@Override
public String getIdentifier() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import ch.cyberduck.core.AbstractProtocol;
import ch.cyberduck.core.LocaleFactory;
import ch.cyberduck.core.Protocol;
import ch.cyberduck.core.Scheme;

import com.google.auto.service.AutoService;

@AutoService(Protocol.class)
public class DropboxProtocol extends AbstractProtocol {

@Override
Expand Down
Loading