http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java index ae037c5..dfc9f0b 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java @@ -32,23 +32,41 @@ import org.apache.sshd.common.util.io.IoUtils; * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ -public class BufferUtils { +public final class BufferUtils { - public static String printHex(byte ... array) { + public static final char DEFAULT_HEX_SEPARATOR = ' '; + + public static final char EMPTY_HEX_SEPARATOR = '\0'; + + public static final String HEX_DIGITS = "0123456789abcdef"; + + public static final Int2IntFunction DEFAULT_BUFFER_GROWTH_FACTOR = + new Int2IntFunction() { + @Override + public int apply(int value) { + return getNextPowerOf2(value); + } + }; + + /** + * Private Constructor + */ + private BufferUtils() { + throw new UnsupportedOperationException("No instance allowed"); + } + + public static String printHex(byte... array) { return printHex(array, 0, GenericUtils.length(array)); } - public static String printHex(char sep, byte ... array) { + public static String printHex(char sep, byte... array) { return printHex(array, 0, GenericUtils.length(array), sep); } - public static final char DEFAULT_HEX_SEPARATOR = ' ', EMPTY_HEX_SEPARATOR = '\0'; public static String printHex(byte[] array, int offset, int len) { return printHex(array, offset, len, DEFAULT_HEX_SEPARATOR); } - public static final String HEX_DIGITS="0123456789abcdef"; - public static String printHex(byte[] array, int offset, int len, char sep) { if (len <= 0) { return ""; @@ -69,8 +87,9 @@ public class BufferUtils { /** * Read a 32-bit value in network order + * * @param input The {@link InputStream} - * @param buf Work buffer to use + * @param buf Work buffer to use * @return The read 32-bit value * @throws IOException If failed to read 4 bytes or not enough room in * @see #readInt(InputStream, byte[], int, int) @@ -81,13 +100,14 @@ public class BufferUtils { /** * Read a 32-bit value in network order - * @param input The {@link InputStream} - * @param buf Work buffer to use + * + * @param input The {@link InputStream} + * @param buf Work buffer to use * @param offset Offset in buffer to us - * @param len Available length - must have at least 4 bytes available + * @param len Available length - must have at least 4 bytes available * @return The read 32-bit value * @throws IOException If failed to read 4 bytes or not enough room in - * work buffer + * work buffer * @see #readUInt(InputStream, byte[], int, int) */ public static int readInt(InputStream input, byte[] buf, int offset, int len) throws IOException { @@ -96,8 +116,9 @@ public class BufferUtils { /** * Read a 32-bit value in network order + * * @param input The {@link InputStream} - * @param buf Work buffer to use + * @param buf Work buffer to use * @return The read 32-bit value * @throws IOException If failed to read 4 bytes or not enough room in * @see #readUInt(InputStream, byte[], int, int) @@ -108,13 +129,14 @@ public class BufferUtils { /** * Read a 32-bit value in network order - * @param input The {@link InputStream} - * @param buf Work buffer to use + * + * @param input The {@link InputStream} + * @param buf Work buffer to use * @param offset Offset in buffer to us - * @param len Available length - must have at least 4 bytes available + * @param len Available length - must have at least 4 bytes available * @return The read 32-bit value * @throws IOException If failed to read 4 bytes or not enough room in - * work buffer + * work buffer * @see #getUInt(byte[], int, int) */ public static long readUInt(InputStream input, byte[] buf, int offset, int len) throws IOException { @@ -127,29 +149,29 @@ public class BufferUtils { // TODO use Integer.BYTES for JDK-8 IoUtils.readFully(input, buf, offset, Integer.SIZE / Byte.SIZE); return getUInt(buf, offset, len); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { throw new StreamCorruptedException(e.getMessage()); } } /** * @param buf A buffer holding a 32-bit unsigned integer in <B>big endian</B> - * format. <B>Note:</B> if more than 4 bytes are available, then only the - * <U>first</U> 4 bytes in the buffer will be used + * format. <B>Note:</B> if more than 4 bytes are available, then only the + * <U>first</U> 4 bytes in the buffer will be used * @return The result as a {@code long} whose 32 high-order bits are zero * @see #getUInt(byte[], int, int) */ - public static long getUInt(byte ... buf) { + public static long getUInt(byte... buf) { return getUInt(buf, 0, GenericUtils.length(buf)); } - + /** * @param buf A buffer holding a 32-bit unsigned integer in <B>big endian</B> - * format. + * format. * @param off The offset of the data in the buffer * @param len The available data length. <B>Note:</B> if more than 4 bytes - * are available, then only the <U>first</U> 4 bytes in the buffer will be - * used (starting at the specified <tt>offset</tt>) + * are available, then only the <U>first</U> 4 bytes in the buffer will be + * used (starting at the specified <tt>offset</tt>) * @return The result as a {@code long} whose 32 high-order bits are zero */ public static long getUInt(byte[] buf, int off, int len) { @@ -158,17 +180,18 @@ public class BufferUtils { throw new IllegalArgumentException("Not enough data for a UINT: required=" + (Integer.SIZE / Byte.SIZE) + ", available=" + len); } - return ((buf[off] << 24) & 0xff000000L) - | ((buf[off + 1] << 16) & 0x00ff0000L) - | ((buf[off + 2] << 8) & 0x0000ff00L) - | ((buf[off + 3] ) & 0x000000ffL) - ; + long l = (buf[off] << 24) & 0xff000000L; + l |= (buf[off + 1] << 16) & 0x00ff0000L; + l |= (buf[off + 2] << 8) & 0x0000ff00L; + l |= (buf[off + 3]) & 0x000000ffL; + return l; } /** * Writes a 32-bit value in network order (i.e., MSB 1st) - * @param value The 32-bit value - * @param buf A work buffer to use - must have enough space to contain 4 bytes + * + * @param value The 32-bit value + * @param buf A work buffer to use - must have enough space to contain 4 bytes * @throws IOException If failed to write the value or work buffer to small * @see #writeInt(OutputStream, int, byte[], int, int) */ @@ -178,10 +201,11 @@ public class BufferUtils { /** * Writes a 32-bit value in network order (i.e., MSB 1st) - * @param value The 32-bit value - * @param buf A work buffer to use - must have enough space to contain 4 bytes - * @param off The offset to write the value - * @param len The available space + * + * @param value The 32-bit value + * @param buf A work buffer to use - must have enough space to contain 4 bytes + * @param off The offset to write the value + * @param len The available space * @throws IOException If failed to write the value or work buffer to small * @see #writeUInt(OutputStream, long, byte[], int, int) */ @@ -191,8 +215,9 @@ public class BufferUtils { /** * Writes a 32-bit value in network order (i.e., MSB 1st) - * @param value The 32-bit value - * @param buf A work buffer to use - must have enough space to contain 4 bytes + * + * @param value The 32-bit value + * @param buf A work buffer to use - must have enough space to contain 4 bytes * @throws IOException If failed to write the value or work buffer to small * @see #writeUInt(OutputStream, long, byte[], int, int) */ @@ -202,10 +227,11 @@ public class BufferUtils { /** * Writes a 32-bit value in network order (i.e., MSB 1st) - * @param value The 32-bit value - * @param buf A work buffer to use - must have enough space to contain 4 bytes - * @param off The offset to write the value - * @param len The available space + * + * @param value The 32-bit value + * @param buf A work buffer to use - must have enough space to contain 4 bytes + * @param off The offset to write the value + * @param len The available space * @throws IOException If failed to write the value or work buffer to small * @see #putUInt(long, byte[], int, int) */ @@ -213,15 +239,16 @@ public class BufferUtils { try { int writeLen = putUInt(value, buf, off, len); output.write(buf, off, writeLen); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { throw new StreamCorruptedException(e.getMessage()); } } /** * Writes a 32-bit value in network order (i.e., MSB 1st) - * @param value The 32-bit value - * @param buf The buffer + * + * @param value The 32-bit value + * @param buf The buffer * @return The number of bytes used in the buffer * @throws IllegalArgumentException if not enough space available * @see #putUInt(long, byte[], int, int) @@ -232,25 +259,26 @@ public class BufferUtils { /** * Writes a 32-bit value in network order (i.e., MSB 1st) - * @param value The 32-bit value - * @param buf The buffer - * @param off The offset to write the value - * @param len The available space + * + * @param value The 32-bit value + * @param buf The buffer + * @param off The offset to write the value + * @param len The available space * @return The number of bytes used in the buffer * @throws IllegalArgumentException if not enough space available */ public static int putUInt(long value, byte[] buf, int off, int len) { // TODO use Integer.BYTES for JDK-8 - if (len < (Integer.SIZE / Byte.SIZE)) { + if (len < Integer.SIZE / Byte.SIZE) { throw new IllegalArgumentException("Not enough data for a UINT: required=" + (Integer.SIZE / Byte.SIZE) + ", available=" + len); } - buf[off] = (byte) ((value >> 24) & 0xFF); + buf[off] = (byte) ((value >> 24) & 0xFF); buf[off + 1] = (byte) ((value >> 16) & 0xFF); - buf[off + 2] = (byte) ((value >> 8) & 0xFF); + buf[off + 2] = (byte) ((value >> 8) & 0xFF); buf[off + 3] = (byte) (value & 0xFF); - return (Integer.SIZE / Byte.SIZE); + return Integer.SIZE / Byte.SIZE; } public static boolean equals(byte[] a1, byte[] a2) { @@ -278,14 +306,6 @@ public class BufferUtils { return true; } - public static final Int2IntFunction DEFAULT_BUFFER_GROWTH_FACTOR = - new Int2IntFunction() { - @Override - public int apply(int value) { - return getNextPowerOf2(value); - } - }; - public static int getNextPowerOf2(int i) { int j = 1; while (j < i) { @@ -293,7 +313,7 @@ public class BufferUtils { } return j; } - + /** * Used for encodings where we don't know the data length before adding it * to the buffer. The idea is to place a 32-bit "placeholder", @@ -301,10 +321,11 @@ public class BufferUtils { * length. The method calculates the encoded data length, moves the write * position to the specified placeholder position, updates the length value * and then moves the write position it back to its original value. + * * @param buffer The {@link Buffer} * @param lenPos The offset in the buffer where the length placeholder is - * to be update - <B>Note:</B> assumption is that the encoded data starts - * <U>immediately</U> after the placeholder + * to be update - <B>Note:</B> assumption is that the encoded data starts + * <U>immediately</U> after the placeholder * @return The amount of data that has been encoded */ public static int updateLengthPlaceholder(Buffer buffer, int lenPos) { @@ -316,15 +337,16 @@ public class BufferUtils { buffer.wpos(endPos); return dataLength; } - + /** * Updates a 32-bit "placeholder" location for data length - moves * the write position to the specified placeholder position, updates the length * value and then moves the write position it back to its original value. - * @param buffer The {@link Buffer} - * @param lenPos The offset in the buffer where the length placeholder is - * to be update - <B>Note:</B> assumption is that the encoded data starts - * <U>immediately</U> after the placeholder + * + * @param buffer The {@link Buffer} + * @param lenPos The offset in the buffer where the length placeholder is + * to be update - <B>Note:</B> assumption is that the encoded data starts + * <U>immediately</U> after the placeholder * @param dataLength The length to update */ public static void updateLengthPlaceholder(Buffer buffer, int lenPos, int dataLength) { @@ -333,9 +355,10 @@ public class BufferUtils { buffer.putInt(dataLength); buffer.wpos(curPos); } - + /** * Invokes {@link Buffer#clear()} + * * @param buffer A {@link Buffer} instance - ignored if {@code null} * @return The same as the input instance */ @@ -343,7 +366,7 @@ public class BufferUtils { if (buffer != null) { buffer.clear(); } - + return buffer; } }
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java index 19cc401..615778f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/ByteArrayBuffer.java @@ -26,6 +26,7 @@ import org.apache.sshd.common.util.Readable; /** * Provides an implementation of {@link Buffer} using a backing byte array + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public final class ByteArrayBuffer extends Buffer { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/CloseableEmptyInputStream.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/CloseableEmptyInputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/CloseableEmptyInputStream.java index ecb0144..c6abb7a 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/CloseableEmptyInputStream.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/CloseableEmptyInputStream.java @@ -26,11 +26,12 @@ import java.util.concurrent.atomic.AtomicBoolean; /** * A {@code /dev/null} stream that can be closed - in which case it will throw * {@link IOException}s if invoked after being closed + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class CloseableEmptyInputStream extends EmptyInputStream implements Channel { private final AtomicBoolean open = new AtomicBoolean(true); - + public CloseableEmptyInputStream() { super(); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERParser.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERParser.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERParser.java index 1177133..c72b1db 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERParser.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERParser.java @@ -33,13 +33,22 @@ import org.apache.sshd.common.util.buffer.BufferUtils; /** * A bare minimum DER parser - just enough to be able to decode * signatures and private keys + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class DERParser extends FilterInputStream { + + /** + * Maximum size of data allowed by {@link #readLength()} - it is a bit + * arbitrary since one can encode 32-bit length data, but it is good + * enough for the keys + */ + public static final int MAX_DER_VALUE_LENGTH = 2 * Short.MAX_VALUE; + // TODO in JDK-8 use Integer.BYTES private final byte[] lenBytes = new byte[Integer.SIZE / Byte.SIZE]; - public DERParser(byte ... bytes) { + public DERParser(byte... bytes) { this(bytes, 0, GenericUtils.length(bytes)); } @@ -52,30 +61,24 @@ public class DERParser extends FilterInputStream { } /** - * Maximum size of data allowed by {@link #readLength()} - it is a bit - * arbitrary since one can encode 32-bit length data, but it is good - * enough for the keys - */ - public static final int MAX_DER_VALUE_LENGTH=2 * Short.MAX_VALUE; - - /** * Decode the length of the field. Can only support length * encoding up to 4 octets. In BER/DER encoding, length can * be encoded in 2 forms: * <ul> - * <li> - * Short form - One octet. Bit 8 has value "0" and bits 7-1 - * give the length. - * </li> - * - * <li> - * Long form - Two to 127 octets (only 4 is supported here). - * Bit 8 of first octet has value "1" and bits 7-1 give the - * number of additional length octets. Second and following - * octets give the length, base 256, most significant digit - * first. - * </li> + * <li> + * Short form - One octet. Bit 8 has value "0" and bits 7-1 + * give the length. + * </li> + * <p/> + * <li> + * Long form - Two to 127 octets (only 4 is supported here). + * Bit 8 of first octet has value "1" and bits 7-1 give the + * number of additional length octets. Second and following + * octets give the length, base 256, most significant digit + * first. + * </li> * </ul> + * * @return The length as integer * @throws IOException */ @@ -103,7 +106,7 @@ public class DERParser extends FilterInputStream { throw new StreamCorruptedException("Invalid DER: length data too short: expected=" + num + ", actual=" + n); } - long len=BufferUtils.getUInt(lenBytes); + long len = BufferUtils.getUInt(lenBytes); if (len < 0x7FL) { // according to standard: "the shortest possible length encoding must be used" throw new StreamCorruptedException("Invalid DER: length not in shortest form: " + len); } @@ -115,20 +118,20 @@ public class DERParser extends FilterInputStream { // we know the cast is safe since it is less than MAX_DER_VALUE_LENGTH which is ~64K return (int) len; } - + public BigInteger readBigInteger() throws IOException { int type = read(); if (type != 0x02) { throw new StreamCorruptedException("Invalid DER: data type is not an INTEGER: 0x" + Integer.toHexString(type)); } - + int len = readLength(); byte[] value = new byte[len]; int n = read(value); if (n < len) { throw new StreamCorruptedException("Invalid DER: stream too short, missing value: read " + n + " out of required " + len); } - + return new BigInteger(value); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERWriter.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERWriter.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERWriter.java index b2d6ccf..6080d7f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERWriter.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/DERWriter.java @@ -34,6 +34,7 @@ import org.apache.sshd.common.util.buffer.ByteArrayBuffer; /** * A bare-minimum DER encoder - just enough so we can encoder signatures * and keys data + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class DERWriter extends FilterOutputStream { @@ -51,12 +52,12 @@ public class DERWriter extends FilterOutputStream { public DERWriter(OutputStream stream) { super(ValidateUtils.checkNotNull(stream, "No output stream")); } - + public void writeBigInteger(BigInteger value) throws IOException { writeBigInteger(ValidateUtils.checkNotNull(value, "No value").toByteArray()); } - public void writeBigInteger(byte ... bytes) throws IOException { + public void writeBigInteger(byte... bytes) throws IOException { writeBigInteger(bytes, 0, GenericUtils.length(bytes)); } @@ -74,7 +75,7 @@ public class DERWriter extends FilterOutputStream { write(bytes, off, len); } - public void writeObject(byte tag, int len, byte ... data) throws IOException { + public void writeObject(byte tag, int len, byte... data) throws IOException { write(tag & 0xFF); writeLength(len); write(data, 0, len); @@ -92,7 +93,7 @@ public class DERWriter extends FilterOutputStream { BufferUtils.putUInt(len, lenBytes); int nonZeroPos = 0; - for ( ; nonZeroPos < lenBytes.length; nonZeroPos++) { + for (; nonZeroPos < lenBytes.length; nonZeroPos++) { if (lenBytes[nonZeroPos] != 0) { break; } @@ -102,11 +103,11 @@ public class DERWriter extends FilterOutputStream { throw new StreamCorruptedException("All zeroes length representation for len=" + len); } - int bytesLen=lenBytes.length - nonZeroPos; + int bytesLen = lenBytes.length - nonZeroPos; write(0x80 | bytesLen); // indicate number of octets write(lenBytes, nonZeroPos, bytesLen); } - + public byte[] toByteArray() throws IOException { if (this.out instanceof ByteArrayOutputStream) { return ((ByteArrayOutputStream) this.out).toByteArray(); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/EmptyInputStream.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/EmptyInputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/EmptyInputStream.java index 1047a5f..84fbfa2 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/EmptyInputStream.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/EmptyInputStream.java @@ -24,6 +24,7 @@ import java.io.InputStream; /** * A {@code /dev/null} implementation - always open + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class EmptyInputStream extends InputStream { @@ -35,12 +36,12 @@ public class EmptyInputStream extends InputStream { @Override public int read() throws IOException { - return (-1); + return -1; } @Override public int read(byte[] b, int off, int len) throws IOException { - return (-1); + return -1; } @Override http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/FileInfoExtractor.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/FileInfoExtractor.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/FileInfoExtractor.java index 56be141..06e90b8 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/FileInfoExtractor.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/FileInfoExtractor.java @@ -31,40 +31,39 @@ import java.util.Set; * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public interface FileInfoExtractor<T> { - T infoOf(Path file, LinkOption ... options) throws IOException; - + FileInfoExtractor<Boolean> EXISTS = new FileInfoExtractor<Boolean>() { @Override public Boolean infoOf(Path file, LinkOption... options) throws IOException { - return Boolean.valueOf(Files.exists(file, options)); + return Files.exists(file, options); } }; FileInfoExtractor<Boolean> ISDIR = new FileInfoExtractor<Boolean>() { @Override public Boolean infoOf(Path file, LinkOption... options) throws IOException { - return Boolean.valueOf(Files.isDirectory(file, options)); + return Files.isDirectory(file, options); } }; FileInfoExtractor<Boolean> ISREG = new FileInfoExtractor<Boolean>() { @Override public Boolean infoOf(Path file, LinkOption... options) throws IOException { - return Boolean.valueOf(Files.isRegularFile(file, options)); + return Files.isRegularFile(file, options); } }; FileInfoExtractor<Boolean> ISSYMLINK = new FileInfoExtractor<Boolean>() { @Override public Boolean infoOf(Path file, LinkOption... options) throws IOException { - return Boolean.valueOf(Files.isSymbolicLink(file)); + return Files.isSymbolicLink(file); } }; FileInfoExtractor<Long> SIZE = new FileInfoExtractor<Long>() { @Override public Long infoOf(Path file, LinkOption... options) throws IOException { - return Long.valueOf(Files.size(file)); + return Files.size(file); } }; @@ -82,4 +81,7 @@ public interface FileInfoExtractor<T> { } }; + + T infoOf(Path file, LinkOption ... options) throws IOException; + } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java index 7b37d50..6eb8c4c 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java @@ -47,10 +47,23 @@ import org.apache.sshd.common.util.OsUtils; * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public final class IoUtils { + public static final OpenOption[] EMPTY_OPEN_OPTIONS = new OpenOption[0]; public static final CopyOption[] EMPTY_COPY_OPTIONS = new CopyOption[0]; public static final LinkOption[] EMPTY_LINK_OPTIONS = new LinkOption[0]; - private static final LinkOption[] NO_FOLLOW_OPTIONS = new LinkOption[] { LinkOption.NOFOLLOW_LINKS }; + + public static final List<String> WINDOWS_EXECUTABLE_EXTENSIONS = Collections.unmodifiableList(Arrays.asList(".bat", ".exe", ".cmd")); + + public static final int DEFAULT_COPY_SIZE = 8192; + + private static final LinkOption[] NO_FOLLOW_OPTIONS = new LinkOption[]{LinkOption.NOFOLLOW_LINKS}; + + /** + * Private Constructor + */ + private IoUtils() { + throw new UnsupportedOperationException("No instance allowed"); + } public static LinkOption[] getLinkOptions(boolean followLinks) { if (followLinks) { @@ -60,8 +73,6 @@ public final class IoUtils { } } - public static final int DEFAULT_COPY_SIZE=8192; - public static long copy(InputStream source, OutputStream sink) throws IOException { return copy(source, sink, DEFAULT_COPY_SIZE); } @@ -89,8 +100,6 @@ public final class IoUtils { } } - public static final List<String> WINDOWS_EXECUTABLE_EXTENSIONS = Collections.unmodifiableList(Arrays.asList(".bat", ".exe", ".cmd")); - /** * @param fileName The file name to be evaluated - ignored if {@code null}/empty * @return {@code true} if the file ends in one of the {@link #WINDOWS_EXECUTABLE_EXTENSIONS} @@ -111,14 +120,15 @@ public final class IoUtils { * If the "posix" view is supported, then it returns * {@link Files#getPosixFilePermissions(Path, LinkOption...)}, otherwise * uses the {@link #getPermissionsFromFile(File)} method + * * @param path The {@link Path} * @return A {@link Set} of {@link PosixFilePermission} * @throws IOException If failed to access the file system in order to - * retrieve the permissions + * retrieve the permissions */ - public static Set<PosixFilePermission> getPermissions(Path path, LinkOption ... options) throws IOException { - FileSystem fs = path.getFileSystem(); - Collection<String> views = fs.supportedFileAttributeViews(); + public static Set<PosixFilePermission> getPermissions(Path path, LinkOption... options) throws IOException { + FileSystem fs = path.getFileSystem(); + Collection<String> views = fs.supportedFileAttributeViews(); if (views.contains("posix")) { return Files.getPosixFilePermissions(path, options); } else { @@ -159,13 +169,14 @@ public final class IoUtils { * If the "posix" view is supported, then it invokes * {@link Files#setPosixFilePermissions(Path, Set)}, otherwise * uses the {@link #setPermissionsToFile(File, Collection)} method - * @param path The {@link Path} + * + * @param path The {@link Path} * @param perms The {@link Set} of {@link PosixFilePermission}s * @throws IOException If failed to access the file system */ public static void setPermissions(Path path, Set<PosixFilePermission> perms) throws IOException { - FileSystem fs = path.getFileSystem(); - Collection<String> views = fs.supportedFileAttributeViews(); + FileSystem fs = path.getFileSystem(); + Collection<String> views = fs.supportedFileAttributeViews(); if (views.contains("posix")) { Files.setPosixFilePermissions(path, perms); } else { @@ -174,28 +185,28 @@ public final class IoUtils { } /** - * @param f The {@link File} + * @param f The {@link File} * @param perms A {@link Collection} of {@link PosixFilePermission}s to set on it. - * <B>Note:</B> the file is set to readable/writable/executable not only by the - * owner if <U>any</U> of relevant the owner/group/others permission is set + * <B>Note:</B> the file is set to readable/writable/executable not only by the + * owner if <U>any</U> of relevant the owner/group/others permission is set */ public static void setPermissionsToFile(File f, Collection<PosixFilePermission> perms) { - boolean readable = perms != null && - (perms.contains(PosixFilePermission.OWNER_READ) - || perms.contains(PosixFilePermission.GROUP_READ) - || perms.contains(PosixFilePermission.OTHERS_READ)); + boolean readable = perms != null + && (perms.contains(PosixFilePermission.OWNER_READ) + || perms.contains(PosixFilePermission.GROUP_READ) + || perms.contains(PosixFilePermission.OTHERS_READ)); f.setReadable(readable, false); - boolean writable = perms != null && - (perms.contains(PosixFilePermission.OWNER_WRITE) - || perms.contains(PosixFilePermission.GROUP_WRITE) - || perms.contains(PosixFilePermission.OTHERS_WRITE)); + boolean writable = perms != null + && (perms.contains(PosixFilePermission.OWNER_WRITE) + || perms.contains(PosixFilePermission.GROUP_WRITE) + || perms.contains(PosixFilePermission.OTHERS_WRITE)); f.setWritable(writable, false); - boolean executable = perms != null && - (perms.contains(PosixFilePermission.OWNER_EXECUTE) - || perms.contains(PosixFilePermission.GROUP_EXECUTE) - || perms.contains(PosixFilePermission.OTHERS_EXECUTE)); + boolean executable = perms != null + && (perms.contains(PosixFilePermission.OWNER_EXECUTE) + || perms.contains(PosixFilePermission.GROUP_EXECUTE) + || perms.contains(PosixFilePermission.OTHERS_EXECUTE)); f.setExecutable(executable, false); } @@ -204,27 +215,28 @@ public final class IoUtils { * <A HREF="http://docs.oracle.com/javase/tutorial/essential/io/check.html">Java tutorial - Checking a File or Directory</A>: * </P></BR> * <PRE> - * The methods in the Path class are syntactic, meaning that they operate - * on the Path instance. But eventually you must access the file system - * to verify that a particular Path exists, or does not exist. You can do - * so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...) - * methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path). - * When you are testing a file's existence, three results are possible: - * - * - The file is verified to exist. - * - The file is verified to not exist. - * - The file's status is unknown. - * - * This result can occur when the program does not have access to the file. - * If both exists and notExists return false, the existence of the file cannot - * be verified. + * The methods in the Path class are syntactic, meaning that they operate + * on the Path instance. But eventually you must access the file system + * to verify that a particular Path exists, or does not exist. You can do + * so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...) + * methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path). + * When you are testing a file's existence, three results are possible: + * <p/> + * - The file is verified to exist. + * - The file is verified to not exist. + * - The file's status is unknown. + * <p/> + * This result can occur when the program does not have access to the file. + * If both exists and notExists return false, the existence of the file cannot + * be verified. * </PRE> - * @param path The {@link Path} to be tested + * + * @param path The {@link Path} to be tested * @param options The {@link LinkOption}s to use * @return {@link Boolean#TRUE}/{@link Boolean#FALSE} or {@code null} * according to the file status as explained above */ - public static Boolean checkFileExists(Path path, LinkOption ... options) { + public static Boolean checkFileExists(Path path, LinkOption... options) { if (Files.exists(path, options)) { return Boolean.TRUE; } else if (Files.notExists(path, options)) { @@ -236,9 +248,10 @@ public final class IoUtils { /** * Read the requested number of bytes or fail if there are not enough left. - * @param input where to read input from + * + * @param input where to read input from * @param buffer destination - * @throws IOException if there is a problem reading the file + * @throws IOException if there is a problem reading the file * @throws EOFException if the number of bytes read was incorrect */ public static void readFully(InputStream input, byte[] buffer) throws IOException { @@ -247,11 +260,12 @@ public final class IoUtils { /** * Read the requested number of bytes or fail if there are not enough left. - * @param input where to read input from + * + * @param input where to read input from * @param buffer destination * @param offset initial offset into buffer * @param length length to read, must be >= 0 - * @throws IOException if there is a problem reading the file + * @throws IOException if there is a problem reading the file * @throws EOFException if the number of bytes read was incorrect */ public static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException { @@ -263,7 +277,8 @@ public final class IoUtils { /** * Read as many bytes as possible until EOF or achieved required length - * @param input where to read input from + * + * @param input where to read input from * @param buffer destination * @return actual length read; may be less than requested if EOF was reached * @throws IOException if a read error occurs @@ -274,7 +289,8 @@ public final class IoUtils { /** * Read as many bytes as possible until EOF or achieved required length - * @param input where to read input from + * + * @param input where to read input from * @param buffer destination * @param offset initial offset into buffer * @param length length to read - ignored if non-positive @@ -282,7 +298,7 @@ public final class IoUtils { * @throws IOException if a read error occurs */ public static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException { - for (int remaining = length, curOffset = offset; remaining > 0; ) { + for (int remaining = length, curOffset = offset; remaining > 0;) { int count = input.read(buffer, curOffset, remaining); if (count == (-1)) { // EOF before achieved required length return curOffset - offset; @@ -296,7 +312,7 @@ public final class IoUtils { } /** - * @param perms The current {@link PosixFilePermission}s - ignored if {@code null}/empty + * @param perms The current {@link PosixFilePermission}s - ignored if {@code null}/empty * @param excluded The permissions <U>not</U> allowed to exist - ignored if {@code null)/empty * @return The violating {@link PosixFilePermission} - {@code null} * if no violating permission found @@ -311,17 +327,17 @@ public final class IoUtils { return p; } } - + return null; } /** - * @param path The {@link Path} to check + * @param path The {@link Path} to check * @param options The {@link LinkOption}s to use when checking if path is a directory * @return The same input path if it is a directory * @throws UnsupportedOperationException if input path not a directory */ - public static Path ensureDirectory(Path path, LinkOption ... options) { + public static Path ensureDirectory(Path path, LinkOption... options) { if (!Files.isDirectory(path, options)) { throw new UnsupportedOperationException("Not a directory: " + path); } @@ -346,8 +362,5 @@ public final class IoUtils { } return true; } - - private IoUtils() { - throw new UnsupportedOperationException("No instance"); - } + } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/LimitInputStream.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/LimitInputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/LimitInputStream.java index b4af206..7acce67 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/LimitInputStream.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/LimitInputStream.java @@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicBoolean; /** * Reads from another {@link InputStream} up to specified max. length + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class LimitInputStream extends FilterInputStream implements Channel { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/LoggingFilterOutputStream.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/LoggingFilterOutputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/LoggingFilterOutputStream.java index abcd43b..d72fc27 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/LoggingFilterOutputStream.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/LoggingFilterOutputStream.java @@ -43,7 +43,7 @@ public class LoggingFilterOutputStream extends FilterOutputStream { @Override public void write(int b) throws IOException { - byte[] d = new byte[1]; + byte[] d = new byte[1]; d[0] = (byte) b; write(d, 0, 1); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/ModifiableFileWatcher.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/ModifiableFileWatcher.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/ModifiableFileWatcher.java index 584a241..80fb2a1 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/ModifiableFileWatcher.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/ModifiableFileWatcher.java @@ -29,21 +29,23 @@ import java.nio.file.attribute.FileTime; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; -import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.ValidateUtils; import org.apache.sshd.common.util.logging.AbstractLoggingBean; /** * Watches over changes for a file and re-loads them if file has changed - including * if file is deleted or (re-)created + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class ModifiableFileWatcher extends AbstractLoggingBean { + + protected final LinkOption[] options; + private final Path file; private final AtomicBoolean lastExisted = new AtomicBoolean(false); private final AtomicLong lastSize = new AtomicLong(Long.MIN_VALUE); private final AtomicLong lastModified = new AtomicLong(-1L); - protected final LinkOption[] options; public ModifiableFileWatcher(File file) { this(ValidateUtils.checkNotNull(file, "No file to watch").toPath()); @@ -53,19 +55,19 @@ public class ModifiableFileWatcher extends AbstractLoggingBean { this(file, IoUtils.getLinkOptions(false)); } - public ModifiableFileWatcher(Path file, LinkOption ... options) { + public ModifiableFileWatcher(Path file, LinkOption... options) { this.file = ValidateUtils.checkNotNull(file, "No path to watch"); // use a clone to avoid being sensitive to changes in the passed array this.options = (options == null) ? IoUtils.EMPTY_LINK_OPTIONS : options.clone(); } - + /** * @return The watched {@link Path} */ public final Path getPath() { return file; } - + public final boolean exists() throws IOException { return Files.exists(getPath(), options); } @@ -74,7 +76,7 @@ public class ModifiableFileWatcher extends AbstractLoggingBean { if (exists()) { return Files.size(getPath()); } else { - return (-1L); + return -1L; } } @@ -97,13 +99,13 @@ public class ModifiableFileWatcher extends AbstractLoggingBean { if (exists != lastExisted.getAndSet(exists)) { return true; } - + if (!exists) { // file did not exist and still does not exist resetReloadAttributes(); return false; } - + long size = size(); if (size < 0L) { // means file no longer exists @@ -127,10 +129,10 @@ public class ModifiableFileWatcher extends AbstractLoggingBean { if (timestamp != lastModified.getAndSet(timestamp)) { return true; } - + return false; } - + /** * Resets the state attributes used to detect changes to the initial * construction values - i.e., file assumed not to exist and no known @@ -147,6 +149,7 @@ public class ModifiableFileWatcher extends AbstractLoggingBean { * e.g., file existence, size and last-modified time once re-loading is * successfully completed. If the file does not exist then the attributes * are reset to an "unknown" state. + * * @throws IOException If failed to access the file (if exists) * @see #resetReloadAttributes() */ @@ -155,7 +158,7 @@ public class ModifiableFileWatcher extends AbstractLoggingBean { long size = size(); FileTime modifiedTime = lastModified(); - if ((size >= 0L) && (modifiedTime != null)) { + if (size >= 0L && modifiedTime != null) { lastExisted.set(true); lastSize.set(size); lastModified.set(modifiedTime.toMillis()); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseReader.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseReader.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseReader.java index 223bf85..0f36b5f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseReader.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseReader.java @@ -35,7 +35,7 @@ public class NoCloseReader extends FilterReader { public void close() throws IOException { // ignored } - + public static Reader resolveReader(Reader r, boolean okToClose) { if ((r == null) || okToClose) { return r; http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseWriter.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseWriter.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseWriter.java index a4e185f..1a564c3 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseWriter.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NoCloseWriter.java @@ -35,7 +35,7 @@ public class NoCloseWriter extends FilterWriter { public void close() throws IOException { // ignored } - + public static Writer resolveWriter(Writer r, boolean okToClose) { if ((r == null) || okToClose) { return r; http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/logging/AbstractLoggingBean.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/logging/AbstractLoggingBean.java b/sshd-core/src/main/java/org/apache/sshd/common/util/logging/AbstractLoggingBean.java index c041750..e6fd0dd 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/logging/AbstractLoggingBean.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/logging/AbstractLoggingBean.java @@ -26,21 +26,23 @@ import org.slf4j.LoggerFactory; * Serves as a common base class for the vast majority of classes that require * some kind of logging. Facilitates quick and easy replacement of the actual used * logger from one framework to another + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public abstract class AbstractLoggingBean { protected final Logger log; - + /** * Default constructor - creates a logger using the full class name */ protected AbstractLoggingBean() { log = LoggerFactory.getLogger(getClass()); } - + /** * Create a logger for instances of the same class for which we might * want to have a "discriminator" for them + * * @param discriminator The discriminator value */ protected AbstractLoggingBean(String discriminator) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/logging/LoggingUtils.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/logging/LoggingUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/logging/LoggingUtils.java index 4e9aae0..b391e9d 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/logging/LoggingUtils.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/logging/LoggingUtils.java @@ -28,36 +28,38 @@ import org.slf4j.Logger; * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public final class LoggingUtils { + private LoggingUtils() { throw new UnsupportedOperationException("No instance"); } /** * Verifies if the given level is above the required threshold for logging. - * @param level The {@link Level} to evaluate + * + * @param level The {@link Level} to evaluate * @param threshold The threshold {@link Level} * @return {@code true} if the evaluated level is above the required * threshold.</BR> * <P><B>Note(s):</B></BR></P> * <P><UL> - * <LI> - * If either argument is {@code null} then result is {@code false}. - * </LI> - * - * <LI> - * If the evaluated level is {@link Level#OFF} then result is {@code false} - * regardless of the threshold. - * </LI> - * - * <LI> - * If the threshold is {@link Level#ALL} and the evaluated level is - * <U>not</U> {@link Level#OFF} the result is {@code true}. - * </LI> - * - * <LI> - * Otherwise, the evaluated level {@link Level#intValue()} must be - * greater or equal to the threshold. - * </LI> + * <LI> + * If either argument is {@code null} then result is {@code false}. + * </LI> + * <p/> + * <LI> + * If the evaluated level is {@link Level#OFF} then result is {@code false} + * regardless of the threshold. + * </LI> + * <p/> + * <LI> + * If the threshold is {@link Level#ALL} and the evaluated level is + * <U>not</U> {@link Level#OFF} the result is {@code true}. + * </LI> + * <p/> + * <LI> + * Otherwise, the evaluated level {@link Level#intValue()} must be + * greater or equal to the threshold. + * </LI> * </UL></P> */ public static boolean isLoggable(Level level, Level threshold) { @@ -67,10 +69,8 @@ public final class LoggingUtils { return false; } else if (Level.ALL.equals(threshold)) { return true; - } else if (level.intValue() < threshold.intValue()) { - return false; } else { - return true; + return level.intValue() >= threshold.intValue(); } } @@ -84,9 +84,9 @@ public final class LoggingUtils { if (isEnabled(level)) { logMessage(logger, level, message, t); } - + } - + @Override public boolean isEnabled(Level level) { return isLoggable(logger, level); @@ -114,16 +114,16 @@ public final class LoggingUtils { /** * @param logger The {@link Logger} instance - ignored if {@code null} - * @param level The validate log {@link Level} - ignored if {@code null} + * @param level The validate log {@link Level} - ignored if {@code null} * @return <P>{@code true} if the level is enabled for the logger. The * mapping of the level to the logger is as follows:</P></BR> * <UL> - * <LI>{@link Level#OFF} always returns {@code false}</LI> - * <LI>{@link Level#SEVERE} returns {@link Logger#isErrorEnabled()}</LI> - * <LI>{@link Level#WARNING} returns {@link Logger#isWarnEnabled()}</LI> - * <LI>{@link Level#INFO} and {@link Level#ALL} returns {@link Logger#isInfoEnabled()}</LI> - * <LI>{@link Level#CONFIG} and {@link Level#FINE} returns {@link Logger#isDebugEnabled()}</LI> - * <LI>All other levels return {@link Logger#isTraceEnabled()}</LI> + * <LI>{@link Level#OFF} always returns {@code false}</LI> + * <LI>{@link Level#SEVERE} returns {@link Logger#isErrorEnabled()}</LI> + * <LI>{@link Level#WARNING} returns {@link Logger#isWarnEnabled()}</LI> + * <LI>{@link Level#INFO} and {@link Level#ALL} returns {@link Logger#isInfoEnabled()}</LI> + * <LI>{@link Level#CONFIG} and {@link Level#FINE} returns {@link Logger#isDebugEnabled()}</LI> + * <LI>All other levels return {@link Logger#isTraceEnabled()}</LI> * </UL> */ public static boolean isLoggable(Logger logger, Level level) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/logging/SimplifiedLog.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/logging/SimplifiedLog.java b/sshd-core/src/main/java/org/apache/sshd/common/util/logging/SimplifiedLog.java index 0599d37..dd1a52b 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/logging/SimplifiedLog.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/logging/SimplifiedLog.java @@ -16,7 +16,6 @@ * specific language governing permissions and limitations * under the License. */ - package org.apache.sshd.common.util.logging; import java.util.logging.Level; @@ -25,29 +24,30 @@ import java.util.logging.Level; * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public interface SimplifiedLog { + + /** + * An "empty" {@link SimplifiedLog} that does nothing + */ + SimplifiedLog EMPTY = new AbstractSimplifiedLog() { + @Override + public boolean isEnabled(Level level) { + return false; + } + + @Override + public void log(Level level, Object message, Throwable t) { + return; + } + + @Override + public String toString() { + return "EMPTY"; + } + }; + boolean isEnabled(Level level); void log(Level level, Object message); void log(Level level, Object message, Throwable t); - - /** - * An "empty" {@link SimplifiedLog} that does nothing - */ - SimplifiedLog EMPTY=new AbstractSimplifiedLog() { - @Override - public boolean isEnabled(Level level) { - return false; - } - - @Override - public void log(Level level, Object message, Throwable t) { - return; - } - - @Override - public String toString() { - return "EMPTY"; - } - }; } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceCarrier.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceCarrier.java b/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceCarrier.java index e208352..d816853 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceCarrier.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceCarrier.java @@ -26,6 +26,7 @@ import java.util.concurrent.ExecutorService; */ public interface ExecutorServiceCarrier { ExecutorService getExecutorService(); + /** * @return If {@code true} then the {@link ExecutorService#shutdownNow()} * will be called (unless it is an internally allocated service which is always http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceConfigurer.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceConfigurer.java b/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceConfigurer.java index a29415c..d281988 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceConfigurer.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ExecutorServiceConfigurer.java @@ -26,5 +26,6 @@ import java.util.concurrent.ExecutorService; */ public interface ExecutorServiceConfigurer extends ExecutorServiceCarrier { void setExecutorService(ExecutorService service); + void setShutdownOnExit(boolean shutdown); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java index a09e490..f20e8e3 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java @@ -38,7 +38,11 @@ import java.util.concurrent.atomic.AtomicInteger; * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ -public class ThreadUtils { +public final class ThreadUtils { + + private ThreadUtils() { + throw new UnsupportedOperationException("No instance"); + } /** * Wraps an {@link ExecutorService} in such a way as to "protect" @@ -90,22 +94,23 @@ public class ThreadUtils { /** * Attempts to find the most suitable {@link ClassLoader} as follows:</BR> * <UL> - * <LI> - * Check the {@link Thread#getContextClassLoader()} value - * </LI> - * - * <LI> - * If no thread context class loader then check the anchor - * class (if given) for its class loader - * </LI> - * - * <LI> - * If still no loader available, then use {@link ClassLoader#getSystemClassLoader()} - * </LI> + * <LI> + * Check the {@link Thread#getContextClassLoader()} value + * </LI> + * <p/> + * <LI> + * If no thread context class loader then check the anchor + * class (if given) for its class loader + * </LI> + * <p/> + * <LI> + * If still no loader available, then use {@link ClassLoader#getSystemClassLoader()} + * </LI> * </UL> + * * @param anchor The anchor {@link Class} to use if no current thread - * - ignored if {@code null} - * context class loader + * - ignored if {@code null} + * context class loader * @return The resolver {@link ClassLoader} */ public static ClassLoader resolveDefaultClassLoader(Class<?> anchor) { @@ -128,18 +133,18 @@ public class ThreadUtils { public static ExecutorService newFixedThreadPool(String poolName, int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, - 0L, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue<Runnable>(), - new SshdThreadFactory(poolName), - new ThreadPoolExecutor.CallerRunsPolicy()); + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<Runnable>(), + new SshdThreadFactory(poolName), + new ThreadPoolExecutor.CallerRunsPolicy()); } public static ExecutorService newCachedThreadPool(String poolName) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, - 60L, TimeUnit.SECONDS, - new SynchronousQueue<Runnable>(), - new SshdThreadFactory(poolName), - new ThreadPoolExecutor.CallerRunsPolicy()); + 60L, TimeUnit.SECONDS, + new SynchronousQueue<Runnable>(), + new SshdThreadFactory(poolName), + new ThreadPoolExecutor.CallerRunsPolicy()); } public static ScheduledExecutorService newSingleThreadScheduledExecutor(String poolName) { @@ -178,8 +183,4 @@ public class ThreadUtils { } - private ThreadUtils() { - // no instance allowe - } - } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/AsyncCommand.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/AsyncCommand.java b/sshd-core/src/main/java/org/apache/sshd/server/AsyncCommand.java index 55337ab..37369a7 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/AsyncCommand.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/AsyncCommand.java @@ -30,18 +30,21 @@ public interface AsyncCommand extends Command { /** * Set the input stream that can be used by the shell to read input. + * * @param in */ void setIoInputStream(IoInputStream in); /** * Set the output stream that can be used by the shell to write its output. + * * @param out */ void setIoOutputStream(IoOutputStream out); /** * Set the error stream that can be used by the shell to write its errors. + * * @param err */ void setIoErrorStream(IoOutputStream err); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/ChannelSessionAware.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/ChannelSessionAware.java b/sshd-core/src/main/java/org/apache/sshd/server/ChannelSessionAware.java index a148d88..cff2843 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/ChannelSessionAware.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/ChannelSessionAware.java @@ -28,8 +28,7 @@ public interface ChannelSessionAware { /** * Receives the channel in which the command is being executed. * - * @param session - * never null + * @param session never null */ void setChannelSession(ChannelSession session); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/Command.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/Command.java b/sshd-core/src/main/java/org/apache/sshd/server/Command.java index efb608e..ade5065 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/Command.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/Command.java @@ -24,7 +24,7 @@ import java.io.OutputStream; /** * Represents a command, shell or subsystem that can be used to send command. - * + * <p/> * This command have direct streams, meaning those streams will be provided by the ssh server * for the shell to use directy. This interface is suitable for implementing commands in java, * rather than using external processes. For wrapping such processes or using inverted streams, @@ -34,24 +34,28 @@ public interface Command { /** * Set the input stream that can be used by the shell to read input. + * * @param in */ void setInputStream(InputStream in); /** * Set the output stream that can be used by the shell to write its output. + * * @param out */ void setOutputStream(OutputStream out); /** * Set the error stream that can be used by the shell to write its errors. + * * @param err */ void setErrorStream(OutputStream err); /** * Set the callback that the shell has to call when it is closed. + * * @param callback */ void setExitCallback(ExitCallback callback); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/Environment.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/Environment.java b/sshd-core/src/main/java/org/apache/sshd/server/Environment.java index f834a99..eeafb3d 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/Environment.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/Environment.java @@ -54,38 +54,44 @@ public interface Environment { /** * Retrieve the environment map + * * @return the environment map */ Map<String, String> getEnv(); /** * Retrieve the pty modes + * * @return the map of pty modes */ Map<PtyMode, Integer> getPtyModes(); /** * Add a qualified listener for the specific signal + * * @param listener the listener to register - * @param signal the signal the listener is interested in + * @param signal the signal the listener is interested in */ void addSignalListener(SignalListener listener, Signal... signal); /** * Add a qualified listener for the specific set of signal + * * @param listener the listener to register - * @param signals the signals the listener is interested in + * @param signals the signals the listener is interested in */ void addSignalListener(SignalListener listener, Collection<Signal> signals); /** * Add a global listener for all signals + * * @param listener the listener to register */ void addSignalListener(SignalListener listener); /** * Remove a previously registered listener for all the signals it was registered + * * @param listener the listener to remove */ void removeSignalListener(SignalListener listener); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/ExitCallback.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/ExitCallback.java b/sshd-core/src/main/java/org/apache/sshd/server/ExitCallback.java index b2103cc..275a993 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/ExitCallback.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/ExitCallback.java @@ -33,7 +33,7 @@ public interface ExitCallback { /** * Informs the SSH client/server that the shell has exited * - * @param exitValue the exit value + * @param exitValue the exit value * @param exitMessage exit value description */ void onExit(int exitValue, String exitMessage); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/ServerBuilder.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/ServerBuilder.java b/sshd-core/src/main/java/org/apache/sshd/server/ServerBuilder.java index 0e60bc5..8335d53 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/ServerBuilder.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/ServerBuilder.java @@ -27,7 +27,6 @@ import org.apache.sshd.common.BaseBuilder; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.channel.Channel; import org.apache.sshd.common.channel.RequestHandler; -import org.apache.sshd.common.kex.BuiltinDHFactories; import org.apache.sshd.common.kex.DHFactory; import org.apache.sshd.common.kex.KeyExchange; import org.apache.sshd.common.session.ConnectionService; @@ -47,19 +46,36 @@ import org.apache.sshd.server.kex.DHGServer; * SshServer builder */ public class ServerBuilder extends BaseBuilder<SshServer, ServerBuilder> { - public static final Transformer<DHFactory,NamedFactory<KeyExchange>> DH2KEX = - new Transformer<DHFactory, NamedFactory<KeyExchange>>() { - @Override - public NamedFactory<KeyExchange> transform(DHFactory factory) { - if (factory == null) { - return null; - } else if (factory.isGroupExchange()) { - return DHGEXServer.newFactory(factory); - } else { - return DHGServer.newFactory(factory); - } + + public static final Transformer<DHFactory, NamedFactory<KeyExchange>> DH2KEX = + new Transformer<DHFactory, NamedFactory<KeyExchange>>() { + @Override + public NamedFactory<KeyExchange> transform(DHFactory factory) { + if (factory == null) { + return null; + } else if (factory.isGroupExchange()) { + return DHGEXServer.newFactory(factory); + } else { + return DHGServer.newFactory(factory); } - }; + } + }; + + public static final List<NamedFactory<Channel>> DEFAULT_CHANNEL_FACTORIES = + Collections.unmodifiableList(Arrays.<NamedFactory<Channel>>asList( + ChannelSessionFactory.INSTANCE, + DirectTcpipFactory.INSTANCE + )); + + public static final List<RequestHandler<ConnectionService>> DEFAULT_GLOBAL_REQUEST_HANDLERS = + Collections.unmodifiableList(Arrays.<RequestHandler<ConnectionService>>asList( + KeepAliveHandler.INSTANCE, + NoMoreSessionsHandler.INSTANCE, + TcpipForwardHandler.INSTANCE, + CancelTcpipForwardHandler.INSTANCE + )); + + public static final PublickeyAuthenticator DEFAULT_PUBLIC_KEY_AUTHENTICATOR = DefaultAuthorizedKeysAuthenticator.INSTANCE; protected PublickeyAuthenticator pubkeyAuthenticator; @@ -72,20 +88,6 @@ public class ServerBuilder extends BaseBuilder<SshServer, ServerBuilder> { return this; } - public static final List<NamedFactory<Channel>> DEFAULT_CHANNEL_FACTORIES = - Collections.unmodifiableList(Arrays.<NamedFactory<Channel>>asList( - ChannelSessionFactory.INSTANCE, - DirectTcpipFactory.INSTANCE - )); - public static final List<RequestHandler<ConnectionService>> DEFAULT_GLOBAL_REQUEST_HANDLERS = - Collections.unmodifiableList(Arrays.<RequestHandler<ConnectionService>>asList( - KeepAliveHandler.INSTANCE, - NoMoreSessionsHandler.INSTANCE, - TcpipForwardHandler.INSTANCE, - CancelTcpipForwardHandler.INSTANCE - )); - public static final PublickeyAuthenticator DEFAULT_PUBLIC_KEY_AUTHENTICATOR = DefaultAuthorizedKeysAuthenticator.INSTANCE; - @Override protected ServerBuilder fillWithDefaultValues() { super.fillWithDefaultValues(); @@ -109,7 +111,7 @@ public class ServerBuilder extends BaseBuilder<SshServer, ServerBuilder> { if (factory == null) { factory = SshServer.DEFAULT_SSH_SERVER_FACTORY; } - + return me(); } @@ -122,15 +124,15 @@ public class ServerBuilder extends BaseBuilder<SshServer, ServerBuilder> { /** * @param ignoreUnsupported If {@code true} then all the default - * key exchanges are included, regardless of whether they are currently - * supported by the JCE. Otherwise, only the supported ones out of the - * list are included + * key exchanges are included, regardless of whether they are currently + * supported by the JCE. Otherwise, only the supported ones out of the + * list are included * @return A {@link List} of the default {@link NamedFactory} * instances of the {@link KeyExchange}s according to the preference * order defined by {@link #DEFAULT_KEX_PREFERENCE}. * <B>Note:</B> the list may be filtered to exclude unsupported JCE * key exchanges according to the <tt>ignoreUnsupported</tt> parameter - * @see BuiltinDHFactories#isSupported() + * @see org.apache.sshd.common.kex.BuiltinDHFactories#isSupported() */ public static List<NamedFactory<KeyExchange>> setUpDefaultKeyExchanges(boolean ignoreUnsupported) { return NamedFactory.Utils.setUpTransformedFactories(ignoreUnsupported, DEFAULT_KEX_PREFERENCE, DH2KEX); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/ServerFactoryManager.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/ServerFactoryManager.java b/sshd-core/src/main/java/org/apache/sshd/server/ServerFactoryManager.java index 4e3c4f3..0e02163 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/ServerFactoryManager.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/ServerFactoryManager.java @@ -127,7 +127,7 @@ public interface ServerFactoryManager extends FactoryManager { /** * Retrieve the <code>GSSAuthenticator</code> to be used by the SSH server. * If no authenticator has been configured (i.e. this method returns - * {@code null}), then client authentication requests based on gssapi + * {@code null}), then client authentication requests based on gssapi * will be rejected. * * @return the <code>GSSAuthenticator</code> or {@code null} @@ -138,7 +138,7 @@ public interface ServerFactoryManager extends FactoryManager { * Retrieve the <code>ShellFactory</code> object to be used to create shells. * * @return a valid <code>ShellFactory</code> object or {@code null} if shells - * are not supported on this server + * are not supported on this server */ Factory<Command> getShellFactory(); @@ -146,7 +146,7 @@ public interface ServerFactoryManager extends FactoryManager { * Retrieve the <code>CommandFactory</code> to be used to process commands requests. * * @return a valid <code>CommandFactory</code> object or {@code null} if commands - * are not supported on this server + * are not supported on this server */ CommandFactory getCommandFactory(); @@ -155,7 +155,7 @@ public interface ServerFactoryManager extends FactoryManager { * be used to create subsystems. * * @return a list of named <code>CommandFactory.Command</code> factories - * or {@code null} if subsystems are not supported on this server + * or {@code null} if subsystems are not supported on this server */ List<NamedFactory<Command>> getSubsystemFactories(); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/Signal.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/Signal.java b/sshd-core/src/main/java/org/apache/sshd/server/Signal.java index bfcc406..7d75a1d 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/Signal.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/Signal.java @@ -67,24 +67,24 @@ public enum Signal { */ public static final Set<Signal> SIGNALS = Collections.unmodifiableSet(EnumSet.allOf(Signal.class)); - private static final Map<String, Signal> lookupTable = new HashMap<String, Signal>(40); + private static final Map<String, Signal> LOOKUP_TABLE = new HashMap<String, Signal>(40); + + private final int numeric; + + Signal(int numeric) { + this.numeric = numeric; + } static { // registering the signals in the lookup table to allow faster // string based signal lookups for (Signal s : Signal.values()) { - lookupTable.put(s.name(), s); + LOOKUP_TABLE.put(s.name(), s); } } public static Signal get(String name) { - return lookupTable.get(name); - } - - private final int numeric; - - Signal(int numeric) { - this.numeric = numeric; + return LOOKUP_TABLE.get(name); } public int getNumeric() { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/SignalListener.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/SignalListener.java b/sshd-core/src/main/java/org/apache/sshd/server/SignalListener.java index b5bd746..b9dab23 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/SignalListener.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/SignalListener.java @@ -24,7 +24,6 @@ package org.apache.sshd.server; public interface SignalListener { /** - * * @param signal */ void signal(Signal signal); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java b/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java index c84a710..98ad760 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java @@ -67,7 +67,7 @@ import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; /** * The SshServer class is the main entry point for the server side of the SSH protocol. - * + * <p/> * The SshServer has to be configured before being started. Such configuration can be * done either using a dependency injection mechanism (such as the Spring framework) * or programmatically. Basic setup is usually done using the {@link #setUpDefaultServer()} @@ -75,18 +75,16 @@ import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; * Besides this basic setup, a few things have to be manually configured such as the * port number, {@link Factory}, the {@link org.apache.sshd.common.keyprovider.KeyPairProvider} * and the {@link PasswordAuthenticator}. - * + * <p/> * Some properties can also be configured using the {@link #setProperties(java.util.Map)} * method. - * + * <p/> * Once the SshServer instance has been configured, it can be started using the * {@link #start()} method and stopped using the {@link #stop()} method. * + * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> * @see ServerFactoryManager * @see org.apache.sshd.common.FactoryManager - * - * - * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class SshServer extends AbstractFactoryManager implements ServerFactoryManager, Closeable { @@ -97,6 +95,24 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa } }; + public static final List<ServiceFactory> DEFAULT_SERVICE_FACTORIES = + Collections.unmodifiableList(Arrays.asList( + ServerUserAuthServiceFactory.INSTANCE, + ServerConnectionServiceFactory.INSTANCE + )); + public static final UserAuthPublicKeyFactory DEFAULT_USER_AUTH_PUBLIC_KEY_FACTORY = + UserAuthPublicKeyFactory.INSTANCE; + + public static final UserAuthGSSFactory DEFAULT_USER_AUTH_GSS_FACTORY = + UserAuthGSSFactory.INSTANCE; + + public static final UserAuthPasswordFactory DEFAULT_USER_AUTH_PASSWORD_FACTORY = + UserAuthPasswordFactory.INSTANCE; + + public static final UserAuthKeyboardInteractiveFactory DEFAULT_USER_AUTH_KB_INTERACTIVE_FACTORY = + UserAuthKeyboardInteractiveFactory.INSTANCE; + + protected IoAcceptor acceptor; protected String host; protected int port; @@ -198,11 +214,11 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa @Override public GSSAuthenticator getGSSAuthenticator() { - return gssAuthenticator; + return gssAuthenticator; } public void setGSSAuthenticator(GSSAuthenticator gssAuthenticator) { - this.gssAuthenticator = gssAuthenticator; + this.gssAuthenticator = gssAuthenticator; } @Override @@ -210,22 +226,8 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa this.tcpipForwardingFilter = forwardingFilter; } - public static final List<ServiceFactory> DEFAULT_SERVICE_FACTORIES = - Collections.unmodifiableList(Arrays.asList( - ServerUserAuthServiceFactory.INSTANCE, - ServerConnectionServiceFactory.INSTANCE - )); - public static final UserAuthPublicKeyFactory DEFAULT_USER_AUTH_PUBLIC_KEY_FACTORY = - UserAuthPublicKeyFactory.INSTANCE; - public static final UserAuthGSSFactory DEFAULT_USER_AUTH_GSS_FACTORY = - UserAuthGSSFactory.INSTANCE; - public static final UserAuthPasswordFactory DEFAULT_USER_AUTH_PASSWORD_FACTORY = - UserAuthPasswordFactory.INSTANCE; - public static final UserAuthKeyboardInteractiveFactory DEFAULT_USER_AUTH_KB_INTERACTIVE_FACTORY = - UserAuthKeyboardInteractiveFactory.INSTANCE; - @Override - protected void checkConfig() { + protected void checkConfig() { super.checkConfig(); ValidateUtils.checkTrue(getPort() >= 0 /* zero means not set yet */, "Bad port number: %d", Integer.valueOf(getPort())); @@ -244,7 +246,7 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa if (getGSSAuthenticator() != null) { factories.add(DEFAULT_USER_AUTH_GSS_FACTORY); } - + ValidateUtils.checkTrue(factories.size() > 0, "UserAuthFactories not set"); setUserAuthFactories(factories); } @@ -260,7 +262,7 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa /** * Start the SSH server and accept incoming exceptions on the configured port. - * + * * @throws IOException */ public void start() throws IOException { @@ -273,7 +275,7 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa setupSessionTimeout(sessionFactory); - String hostsList=getHost(); + String hostsList = getHost(); if (!GenericUtils.isEmpty(hostsList)) { String[] hosts = GenericUtils.split(hostsList, ','); for (String host : hosts) { @@ -441,9 +443,9 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa } System.err.println("Starting SSHD on port " + port); - + SshServer sshd = SshServer.setUpDefaultServer(); - Map<String,Object> props = sshd.getProperties(); + Map<String, Object> props = sshd.getProperties(); FactoryManagerUtils.updateProperty(props, ServerFactoryManager.WELCOME_BANNER, "Welcome to SSHD\n"); props.putAll(options); sshd.setPort(port); @@ -454,21 +456,21 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("key.ser"))); } - sshd.setShellFactory(InteractiveProcessShellFactory.INSTANCE); + sshd.setShellFactory(new InteractiveProcessShellFactory()); sshd.setPasswordAuthenticator(new PasswordAuthenticator() { - @Override - public boolean authenticate(String username, String password, ServerSession session) { - return (username != null) && username.equals(password); - } - }); + @Override + public boolean authenticate(String username, String password, ServerSession session) { + return (username != null) && username.equals(password); + } + }); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setTcpipForwardingFilter(AcceptAllForwardingFilter.INSTANCE); sshd.setCommandFactory(new ScpCommandFactory.Builder().withDelegate(new CommandFactory() { - @Override - public Command createCommand(String command) { - return new ProcessShellFactory(GenericUtils.split(command, ' ')).create(); - } - }).build()); + @Override + public Command createCommand(String command) { + return new ProcessShellFactory(GenericUtils.split(command, ' ')).create(); + } + }).build()); sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); sshd.start(); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/main/java/org/apache/sshd/server/auth/CachingPublicKeyAuthenticator.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/CachingPublicKeyAuthenticator.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/CachingPublicKeyAuthenticator.java index 9be7f81..0555404 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/auth/CachingPublicKeyAuthenticator.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/CachingPublicKeyAuthenticator.java @@ -29,6 +29,7 @@ import org.apache.sshd.server.session.ServerSession; /** * Caches the result per session + * * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ public class CachingPublicKeyAuthenticator implements PublickeyAuthenticator, SessionListener { @@ -48,7 +49,7 @@ public class CachingPublicKeyAuthenticator implements PublickeyAuthenticator, Se cache.put(session, map); session.addListener(this); } - + Boolean result = map.get(key); if (result == null) { result = authenticator.authenticate(username, key, session);