Skip to content

Commit

Permalink
Obtain session from pool to return feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Jan 9, 2025
1 parent f5c26d1 commit 1374aac
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
28 changes: 28 additions & 0 deletions core/src/main/java/ch/cyberduck/core/FeatureFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ch.cyberduck.core;

/*
* Copyright (c) 2002-2025 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.
*/

public interface FeatureFactory {
@SuppressWarnings("unchecked")
<T> T getFeature(Class<T> type);

FeatureFactory disabled = new FeatureFactory() {
@Override
public <T> T getFeature(final Class<T> type) {
return null;
}
};
}
3 changes: 2 additions & 1 deletion core/src/main/java/ch/cyberduck/core/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Map;
import java.util.Set;

public interface Protocol extends Comparable<Protocol>, Serializable {
public interface Protocol extends FeatureFactory, Comparable<Protocol>, Serializable {

/**
* Check login credentials for validity for this protocol.
Expand Down Expand Up @@ -365,5 +365,6 @@ enum VersioningMode {
}

@SuppressWarnings("unchecked")
@Override
<T> T getFeature(final Class<T> type);
}
5 changes: 3 additions & 2 deletions core/src/main/java/ch/cyberduck/core/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public abstract class Session<C> implements TranscriptListener {
public abstract class Session<C> implements FeatureFactory, TranscriptListener {
private static final Logger log = LogManager.getLogger(Session.class);

/**
Expand Down Expand Up @@ -255,8 +255,8 @@ public void log(final Type request, final String message) {
* @return Feature implementation or null when not supported
*/
@SuppressWarnings("unchecked")
@Override
public <T> T getFeature(final Class<T> type) {
metrics.increment(type);
return this.getFeature(type, this._getFeature(type));
}

Expand All @@ -269,6 +269,7 @@ public <T> T getFeature(final Class<T> type) {
*/
@SuppressWarnings("unchecked")
public <T> T getFeature(final Class<T> type, final T feature) {
metrics.increment(type);
return registry.getFeature(this, type, feature);
}

Expand Down
49 changes: 24 additions & 25 deletions core/src/main/java/ch/cyberduck/core/pool/DefaultSessionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
import ch.cyberduck.core.ConnectionService;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.Session;
import ch.cyberduck.core.SessionFactory;
import ch.cyberduck.core.TranscriptListener;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.ConnectionCanceledException;
import ch.cyberduck.core.ssl.DefaultX509KeyManager;
import ch.cyberduck.core.ssl.DisabledX509TrustManager;
import ch.cyberduck.core.ssl.X509KeyManager;
import ch.cyberduck.core.ssl.X509TrustManager;
import ch.cyberduck.core.threading.BackgroundActionState;
Expand Down Expand Up @@ -53,37 +50,36 @@ public class DefaultSessionPool implements SessionPool {
private final FailureDiagnostics<BackgroundException> diagnostics
= new DefaultFailureDiagnostics();

private final ConnectionService connect;
private final TranscriptListener transcript;
private final Host bookmark;

private final VaultRegistry registry;

private final GenericObjectPool<Session> pool;

private SessionPool features = SessionPool.DISCONNECTED;
private static final GenericObjectPoolConfig<Session> configuration = new GenericObjectPoolConfig<>();

public DefaultSessionPool(final ConnectionService connect, final X509TrustManager trust, final X509KeyManager key,
final VaultRegistry registry, final TranscriptListener transcript,
final Host bookmark) {
this.connect = connect;
this.registry = registry;
this.bookmark = bookmark;
this.transcript = transcript;
final GenericObjectPoolConfig<Session> configuration = new GenericObjectPoolConfig<>();
static {
configuration.setJmxEnabled(false);
configuration.setEvictionPolicyClassName(CustomPoolEvictionPolicy.class.getName());
configuration.setBlockWhenExhausted(true);
configuration.setMaxWait(Duration.ofMillis(BORROW_MAX_WAIT_INTERVAL));
this.pool = new GenericObjectPool<>(new PooledSessionFactory(connect, trust, key, bookmark, registry), configuration);
final AbandonedConfig abandon = new AbandonedConfig();
}

private static final AbandonedConfig abandon = new AbandonedConfig();

{
abandon.setUseUsageTracking(true);
this.pool.setAbandonedConfig(abandon);
}

public DefaultSessionPool(final ConnectionService connect, final VaultRegistry registry,
final TranscriptListener transcript, final Host bookmark, final GenericObjectPool<Session> pool) {
this.connect = connect;
public DefaultSessionPool(final ConnectionService connect, final X509TrustManager trust, final X509KeyManager key,
final VaultRegistry registry, final TranscriptListener transcript, final Host bookmark) {
this(connect, registry, transcript, bookmark,
new GenericObjectPool<>(new PooledSessionFactory(connect, trust, key, bookmark, registry), configuration, abandon));
}

public DefaultSessionPool(final ConnectionService connect, final VaultRegistry registry, final TranscriptListener transcript,
final Host bookmark, final GenericObjectPool<Session> pool) {
this.transcript = transcript;
this.bookmark = bookmark;
this.registry = registry;
Expand Down Expand Up @@ -132,9 +128,6 @@ public Session<?> borrow(final BackgroundActionState callback) throws Background
log.info("Borrow session from pool {}", this);
final Session<?> session = pool.borrowObject();
log.info("Borrowed session {} from pool {}", session, this);
if(DISCONNECTED == features) {
features = new StatelessSessionPool(connect, session, transcript, registry);
}
return session.withListener(transcript);
}
catch(IllegalStateException e) {
Expand Down Expand Up @@ -259,10 +252,16 @@ public Session.State getState() {

@Override
public <T> T getFeature(final Class<T> type) {
if(DISCONNECTED == features) {
return SessionFactory.create(bookmark, new DisabledX509TrustManager(), new DefaultX509KeyManager()).getFeature(type);
try {
final Session<?> session = this.borrow(BackgroundActionState.running);
final T feature = session.getFeature(type);
this.release(session, null);
return feature;
}
catch(BackgroundException e) {
log.warn("Failure {} obtaining feature from {}", e.toString(), this);
return null;
}
return features.getFeature(type);
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/ch/cyberduck/core/pool/SessionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import ch.cyberduck.core.AbstractProtocol;
import ch.cyberduck.core.FeatureFactory;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.Session;
Expand All @@ -27,7 +28,7 @@

import org.apache.commons.lang3.StringUtils;

public interface SessionPool {
public interface SessionPool extends FeatureFactory {
SessionPool DISCONNECTED = new DisconnectedSessionPool();

/**
Expand Down Expand Up @@ -70,6 +71,7 @@ public interface SessionPool {
/**
* Obtain feature from connection type
*/
@Override
<T> T getFeature(final Class<T> type);

/**
Expand Down Expand Up @@ -116,7 +118,7 @@ public Session.State getState() {

@Override
public <T> T getFeature(final Class<T> type) {
return null;
return FeatureFactory.disabled.getFeature(type);
}

@Override
Expand Down

0 comments on commit 1374aac

Please sign in to comment.