Skip to content

Commit

Permalink
Add factory.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Nov 25, 2024
1 parent b3016be commit 19bec48
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cli/src/main/java/ch/cyberduck/cli/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import ch.cyberduck.core.pool.SessionPool;
import ch.cyberduck.core.preferences.Preferences;
import ch.cyberduck.core.preferences.PreferencesFactory;
import ch.cyberduck.core.serviceloader.AutoServiceLoaderFactory;
import ch.cyberduck.core.ssl.CertificateStoreX509TrustManager;
import ch.cyberduck.core.ssl.DefaultTrustManagerHostnameCallback;
import ch.cyberduck.core.ssl.PreferencesX509KeyManager;
Expand Down Expand Up @@ -74,7 +75,6 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -107,7 +107,7 @@ 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);
for(Protocol p : ServiceLoader.load(Protocol.class)) {
for(Protocol p : AutoServiceLoaderFactory.<Protocol>get().load(Protocol.class)) {
protocols.register(p);
}
this.options = options;
Expand Down
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();
}
}
5 changes: 2 additions & 3 deletions osx/src/main/java/ch/cyberduck/ui/cocoa/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
import ch.cyberduck.core.logging.LoggerPrintStream;
import ch.cyberduck.core.preferences.Preferences;
import ch.cyberduck.core.preferences.PreferencesFactory;
import ch.cyberduck.core.serviceloader.AutoServiceLoaderFactory;
import ch.cyberduck.core.threading.ActionOperationBatcher;
import ch.cyberduck.core.threading.AutoreleaseActionOperationBatcher;
import ch.cyberduck.ui.cocoa.controller.MainController;

import java.util.ServiceLoader;

public final class MainApplication {

static {
Expand All @@ -56,7 +55,7 @@ public static void main(String... arguments) {
PreferencesFactory.set(preferences);

final ProtocolFactory protocols = ProtocolFactory.get();
for(Protocol p : ServiceLoader.load(Protocol.class)) {
for(Protocol p : AutoServiceLoaderFactory.<Protocol>get().load(Protocol.class)) {
protocols.register(p);
}
protocols.load();
Expand Down

0 comments on commit 19bec48

Please sign in to comment.