Skip to content

Commit

Permalink
Moved some helper methods and classes to more natural locations
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyor Goldstein committed Mar 5, 2021
1 parent da23f55 commit 73883a5
Show file tree
Hide file tree
Showing 118 changed files with 649 additions and 523 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
## Major code re-factoring

* [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Re-factored locations and names of `ServerSession` and server-side `ChannelSession` related classes
* Moved some helper methods and classes to more natural locations

## Potential compatibility issues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
import org.apache.sshd.common.mac.MacFactory;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.signature.SignatureFactory;
import org.apache.sshd.common.util.ExceptionUtils;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.ValidateUtils;
Expand Down Expand Up @@ -287,7 +289,7 @@ public static SftpClientFactory resolveSftpClientFactory(ClientSession session)
.append(" to instantiate ").append(factoryName)
.append(": ").println(t.getMessage());
System.err.flush();
throw GenericUtils.toRuntimeException(t, true);
throw ExceptionUtils.toRuntimeException(t, true);
}
}

Expand Down Expand Up @@ -540,7 +542,7 @@ public boolean executeCommand(

Map<String, byte[]> extensions = sftp.getServerExtensions();
Map<String, ?> parsed = ParserUtils.parse(extensions);
if (GenericUtils.size(extensions) > 0) {
if (MapEntryUtils.size(extensions) > 0) {
stdout.println();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
import org.apache.sshd.common.mac.Mac;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.ValidateUtils;
Expand Down Expand Up @@ -311,7 +312,7 @@ public static Path resolveIdentityFile(String id) throws IOException {
}

public static Map<String, ?> resolveClientEnvironment(Map<String, ?> options) {
if (GenericUtils.isEmpty(options)) {
if (MapEntryUtils.isEmpty(options)) {
return Collections.emptyMap();
}

Expand Down Expand Up @@ -350,7 +351,7 @@ public static PtyChannelConfiguration resolveClientPtyOptions(PropertyResolver r

public static PtyChannelConfiguration resolveClientPtyOptions(Map<String, ?> options)
throws IOException, InterruptedException {
Object v = GenericUtils.isEmpty(options)
Object v = MapEntryUtils.isEmpty(options)
? null
: options.get(SshClientConfigFileReader.REQUEST_TTY_OPTION);
String s = Objects.toString(v, "auto");
Expand All @@ -367,7 +368,7 @@ public static PtyChannelConfiguration resolveClientPtyOptions(Map<String, ?> opt

// TODO add support for height/width, rows/columns and TERM(inal) type
Map<PtyMode, Integer> ptyModes = resolveClientPtyModes(options);
if (GenericUtils.isNotEmpty(ptyModes)) {
if (MapEntryUtils.isNotEmpty(ptyModes)) {
config.setPtyModes(ptyModes);
}

Expand All @@ -376,7 +377,7 @@ public static PtyChannelConfiguration resolveClientPtyOptions(Map<String, ?> opt

public static Map<PtyMode, Integer> resolveClientPtyModes(Map<String, ?> options)
throws IOException, InterruptedException {
Object v = GenericUtils.isEmpty(options) ? null : options.get(PtyMode.class.getSimpleName());
Object v = MapEntryUtils.isEmpty(options) ? null : options.get(PtyMode.class.getSimpleName());
String s = Objects.toString(v, null);
if (GenericUtils.isEmpty(s)) {
return Collections.emptyMap();
Expand Down Expand Up @@ -473,7 +474,7 @@ public static SshClient setupClient(
setupSessionExtensions(client, resolver, stdin, stdout, stderr);

Map<String, ?> options = resolver.getProperties();
if (GenericUtils.isNotEmpty(options)) {
if (MapEntryUtils.isNotEmpty(options)) {
Map<String, Object> props = client.getProperties();
props.putAll(options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@
import org.apache.sshd.common.session.SessionListener;
import org.apache.sshd.common.signature.BuiltinSignatures;
import org.apache.sshd.common.signature.Signature;
import org.apache.sshd.common.util.ExceptionUtils;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.io.IoUtils;
import org.apache.sshd.common.util.io.NoCloseInputStream;
Expand Down Expand Up @@ -199,8 +201,8 @@ public Void call() throws Exception {
}
}

ValidateUtils.checkTrue(!GenericUtils.isEmpty(pairsMap), "No client key pairs");
ValidateUtils.checkTrue(!GenericUtils.isEmpty(sigFactories), "No signature factories");
ValidateUtils.checkTrue(!MapEntryUtils.isEmpty(pairsMap), "No client key pairs");
ValidateUtils.checkTrue(!MapEntryUtils.isEmpty(sigFactories), "No signature factories");

Exception err = null;
try {
Expand Down Expand Up @@ -272,7 +274,7 @@ public void welcome(ClientSession session, String banner, String lang) {
if (isEnabledLevel(Level.FINE)) {
log(Level.FINE, "Failed to retrieve keys from " + h, e);
}
err = GenericUtils.accumulateException(err, e);
err = ExceptionUtils.accumulateException(err, e);
} finally {
currentHostFingerprints.clear();
}
Expand All @@ -283,7 +285,7 @@ public void welcome(ClientSession session, String banner, String lang) {
try {
close();
} catch (IOException e) {
err = GenericUtils.accumulateException(err, e);
err = ExceptionUtils.accumulateException(err, e);
}
}

Expand Down Expand Up @@ -600,7 +602,7 @@ public void close() throws IOException {
try {
input.close();
} catch (IOException e) {
err = GenericUtils.accumulateException(err, e);
err = ExceptionUtils.accumulateException(err, e);
} finally {
input = null;
}
Expand All @@ -610,7 +612,7 @@ public void close() throws IOException {
try {
client.close();
} catch (IOException e) {
err = GenericUtils.accumulateException(err, e);
err = ExceptionUtils.accumulateException(err, e);
} finally {
try {
client.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.sshd.common.config.keys.IdentityUtils;
import org.apache.sshd.common.config.keys.PublicKeyEntry;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;
import org.apache.sshd.common.util.MapEntryUtils.NavigableMapBuilder;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.ValidateUtils;
Expand Down Expand Up @@ -308,7 +309,7 @@ public String getProperty(String name) {
public String getProperty(String name, String defaultValue) {
String key = ValidateUtils.checkNotNullAndNotEmpty(name, "No property name");
Map<String, String> props = getProperties();
if (GenericUtils.isEmpty(props)) {
if (MapEntryUtils.isEmpty(props)) {
return defaultValue;
}

Expand Down Expand Up @@ -342,7 +343,7 @@ public boolean processGlobalValues(HostConfigEntry globalEntry) {
modified = updateGlobalIdentityOnly(globalEntry.isIdentitiesOnly()) || modified;

Map<String, String> updated = updateGlobalProperties(globalEntry.getProperties());
modified = (GenericUtils.size(updated) > 0) || modified;
modified = (MapEntryUtils.size(updated) > 0) || modified;

return modified;
}
Expand All @@ -354,7 +355,7 @@ public boolean processGlobalValues(HostConfigEntry globalEntry) {
* @return A {@link Map} of the <U>updated</U> properties
*/
public Map<String, String> updateGlobalProperties(Map<String, String> props) {
if (GenericUtils.isEmpty(props)) {
if (MapEntryUtils.isEmpty(props)) {
return Collections.emptyMap();
}

Expand Down Expand Up @@ -545,7 +546,7 @@ public String setProperty(String name, String value) {
}

String key = ValidateUtils.checkNotNullAndNotEmpty(name, "No property name");
if (GenericUtils.isEmpty(properties)) {
if (MapEntryUtils.isEmpty(properties)) {
properties = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}

Expand All @@ -559,7 +560,7 @@ public String setProperty(String name, String value) {
public String removeProperty(String name) {
String key = ValidateUtils.checkNotNullAndNotEmpty(name, "No property name");
Map<String, String> props = getProperties();
if (GenericUtils.isEmpty(props)) {
if (MapEntryUtils.isEmpty(props)) {
return null;
} else {
return props.remove(key);
Expand Down Expand Up @@ -618,7 +619,7 @@ public static <A extends Appendable> A appendNonEmptyPort(A sb, String name, int
* @see #appendNonEmptyProperty(Appendable, String, Object)
*/
public static <A extends Appendable> A appendNonEmptyProperties(A sb, Map<String, ?> props) throws IOException {
if (GenericUtils.isEmpty(props)) {
if (MapEntryUtils.isEmpty(props)) {
return sb;
}

Expand Down Expand Up @@ -741,7 +742,7 @@ public static HostConfigEntry normalizeEntry(
normal.setProxyJump(entry.resolveProxyJump(proxyJump));

Map<String, String> props = entry.getProperties();
if (GenericUtils.size(props) > 0) {
if (MapEntryUtils.size(props) > 0) {
normal.setProperties(
NavigableMapBuilder.<String, String> builder(String.CASE_INSENSITIVE_ORDER)
.putAll(props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Map;
import java.util.Objects;

import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;

/**
* @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a>
Expand Down Expand Up @@ -111,12 +111,12 @@ public int getAttributesCount() {
@SuppressWarnings("unchecked")
public <T> T getAttribute(AttributeKey<T> key) {
Objects.requireNonNull(key, "No key provided");
return GenericUtils.isEmpty(attributes) ? null : (T) attributes.get(key);
return MapEntryUtils.isEmpty(attributes) ? null : (T) attributes.get(key);
}

@Override
public Collection<AttributeKey<?>> attributeKeys() {
return GenericUtils.isEmpty(attributes)
return MapEntryUtils.isEmpty(attributes)
? Collections.emptySet()
: new HashSet<>(attributes.keySet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Collections;
import java.util.Map;

import org.apache.sshd.common.util.MapEntryUtils;

/**
* Indicates an entity that can be configured using properties. The properties are simple name-value pairs where the
* actual value type depends on the property. Some automatic conversions may be available - e.g., from a string to a
Expand All @@ -48,6 +50,11 @@ public Map<String, Object> getProperties() {
return Collections.emptyMap();
}

@Override
public boolean isEmpty() {
return true;
}

@Override
public String toString() {
return "EMPTY";
Expand Down Expand Up @@ -81,6 +88,10 @@ public String toString() {
*/
Map<String, Object> getProperties();

default boolean isEmpty() {
return MapEntryUtils.isEmpty(getProperties());
}

default long getLongProperty(String name, long def) {
return PropertyResolverUtils.getLongProperty(this, name, def);
}
Expand Down Expand Up @@ -122,4 +133,7 @@ default Charset getCharset(String name, Charset defaultValue) {
return (value == null) ? defaultValue : PropertyResolverUtils.toCharset(value);
}

static boolean isEmpty(PropertyResolver resolver) {
return (resolver == null) || resolver.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.concurrent.ConcurrentHashMap;

import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;
import org.apache.sshd.common.util.ValidateUtils;

/**
Expand Down Expand Up @@ -128,7 +129,7 @@ public static Long getLong(Map<String, ?> props, String name) {
* <LI>If the value is {@code null} then returns {@code null}.</LI>
*
* <LI>If the value is already a {@link Long} then it is returned as such.</LI>
*
*
* <LI>If value is a {@link Number} then its {@link Number#longValue()} is wrapped as a {@link Long}</LI>
*
* <LI>Otherwise, the value's {@code toString()} is parsed as a {@link Long}</LI>
Expand Down Expand Up @@ -317,7 +318,7 @@ public static Boolean getBoolean(Map<String, ?> props, String name) {
* <LI>Otherwise, throws an {@link UnsupportedOperationException}</LI>
* </P>
* </UL>
*
*
* @param value The value to be converted
* @return The result - {@code null} if {@code null} or an empty string
* @throws UnsupportedOperationException If value cannot be converted to a boolean - e.g., a number.
Expand Down Expand Up @@ -504,7 +505,7 @@ public static Map<String, Object> resolvePropertiesSource(PropertyResolver resol
}

public static PropertyResolver toPropertyResolver(Properties props) {
if (GenericUtils.isEmpty(props)) {
if (MapEntryUtils.isEmpty(props)) {
return PropertyResolver.EMPTY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.ToIntFunction;

import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;

/**
* A enum describing the tty modes according to <a href="https://tools.ietf.org/html/rfc4254#section-8">RFC 4254 -
Expand Down Expand Up @@ -294,7 +295,7 @@ public enum PtyMode {
public static final Set<PtyMode> MODES = Collections.unmodifiableSet(EnumSet.allOf(PtyMode.class));

public static final NavigableMap<Integer, PtyMode> COMMANDS = Collections.unmodifiableNavigableMap(
GenericUtils.toSortedMap(MODES, PtyMode::toInt, Function.identity(), Comparator.naturalOrder()));
MapEntryUtils.toSortedMap(MODES, PtyMode::toInt, Function.identity(), Comparator.naturalOrder()));

/**
* A {@code null}-safe {@link ToIntFunction} that returns the {@link PtyMode#toInt()} value and (-1) for
Expand All @@ -304,7 +305,7 @@ public enum PtyMode {

/**
* A {@code null}-safe {@link Comparator} of {@link PtyMode} values according to their {@link PtyMode#toInt()} value
*
*
* @see #OPCODE_EXTRACTOR
*/
public static final Comparator<PtyMode> BY_OPCODE = new Comparator<PtyMode>() {
Expand Down Expand Up @@ -385,7 +386,7 @@ public static Set<PtyMode> resolveEnabledOptions(Map<PtyMode, ?> modes, PtyMode.
* @see #getBooleanSettingValue(Map, PtyMode)
*/
public static Set<PtyMode> resolveEnabledOptions(Map<PtyMode, ?> modes, Collection<PtyMode> options) {
if (GenericUtils.isEmpty(modes) || GenericUtils.isEmpty(options)) {
if (MapEntryUtils.isEmpty(modes) || GenericUtils.isEmpty(options)) {
return Collections.emptySet();
}

Expand Down Expand Up @@ -413,7 +414,7 @@ public static Set<PtyMode> resolveEnabledOptions(Map<PtyMode, ?> modes, Collecti
* @see #getBooleanSettingValue(Object)
*/
public static boolean getBooleanSettingValue(Map<PtyMode, ?> modes, PtyMode m) {
if ((m == null) || GenericUtils.isEmpty(modes)) {
if ((m == null) || MapEntryUtils.isEmpty(modes)) {
return false;
} else {
return getBooleanSettingValue(modes.get(m));
Expand Down Expand Up @@ -441,7 +442,7 @@ public static boolean getBooleanSettingValue(Map<PtyMode, ?> modes, PtyMode m) {
*/
public static boolean getBooleanSettingValue(
Map<PtyMode, ?> modes, Collection<PtyMode> enablers, boolean defaultValue) {
if (GenericUtils.isEmpty(modes) || GenericUtils.isEmpty(enablers)) {
if (MapEntryUtils.isEmpty(modes) || GenericUtils.isEmpty(enablers)) {
return defaultValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.TimeUnit;

import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils;

/**
* @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a>
Expand Down Expand Up @@ -159,7 +160,7 @@ public static Map<TimeValueConfig, Long> parse(String s) throws IllegalArgumentE
* @throws IllegalArgumentException If negative count for a time unit
*/
public static long durationOf(Map<TimeValueConfig, ? extends Number> spec) throws IllegalArgumentException {
if (GenericUtils.isEmpty(spec)) {
if (MapEntryUtils.isEmpty(spec)) {
return -1L;
}

Expand Down
Loading

0 comments on commit 73883a5

Please sign in to comment.