Repository: commons-crypto Updated Branches: refs/heads/master 50e4e0fe1 -> 1cc854857
CRYPTO-64: Remove code uses sun classes Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/1cc85485 Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/1cc85485 Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/1cc85485 Branch: refs/heads/master Commit: 1cc85485736120e8920fe9dfce2410ce646ee8f4 Parents: 50e4e0f Author: Sun Dapeng <s...@apache.org> Authored: Tue Jun 14 14:01:33 2016 +0800 Committer: Sun Dapeng <s...@apache.org> Committed: Thu Jun 16 10:52:15 2016 +0800 ---------------------------------------------------------------------- .../org/apache/commons/crypto/utils/Utils.java | 35 +++++++++++++++----- src/site/resources/profile.noanimal | 19 ----------- 2 files changed, 27 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/1cc85485/src/main/java/org/apache/commons/crypto/utils/Utils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/utils/Utils.java b/src/main/java/org/apache/commons/crypto/utils/Utils.java index 95eea1a..f3a82e9 100644 --- a/src/main/java/org/apache/commons/crypto/utils/Utils.java +++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java @@ -19,6 +19,7 @@ package org.apache.commons.crypto.utils; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.security.GeneralSecurityException; import java.util.ArrayList; @@ -26,22 +27,21 @@ import java.util.Enumeration; import java.util.List; import java.util.Properties; +import org.apache.commons.crypto.cipher.CipherTransformation; import org.apache.commons.crypto.cipher.CryptoCipher; import org.apache.commons.crypto.cipher.CryptoCipherFactory; -import org.apache.commons.crypto.cipher.CipherTransformation; import org.apache.commons.crypto.conf.ConfigurationKeys; - -import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_STREAM_BUFFER_SIZE_DEFAULT; -import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_STREAM_BUFFER_SIZE_KEY; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_DEFAULT; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_CIPHER_CLASSES_KEY; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_CIPHER_JCE_PROVIDER_KEY; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_LIB_NAME_KEY; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_LIB_PATH_KEY; +import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_LIB_TEMPDIR_KEY; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_SECURE_RANDOM_DEVICE_FILE_PATH_DEFAULT; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_SECURE_RANDOM_DEVICE_FILE_PATH_KEY; +import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_STREAM_BUFFER_SIZE_DEFAULT; +import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_STREAM_BUFFER_SIZE_KEY; import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_SYSTEM_PROPERTIES_FILE; -import static org.apache.commons.crypto.conf.ConfigurationKeys.COMMONS_CRYPTO_LIB_TEMPDIR_KEY; /** * General utility methods. @@ -68,7 +68,7 @@ public final class Utils { /** * loads system properties when configuration file of the name - * {@link #COMMONS_CRYPTO_SYSTEM_PROPERTIES_FILE} is found. + * {@link ConfigurationKeys#COMMONS_CRYPTO_SYSTEM_PROPERTIES_FILE} is found. */ private static void loadSystemProperties() { try { @@ -104,8 +104,27 @@ public final class Utils { * @param buffer the bytebuffer to be freed. */ public static void freeDirectBuffer(ByteBuffer buffer) { - if (buffer instanceof sun.nio.ch.DirectBuffer) { - ((sun.nio.ch.DirectBuffer) buffer).cleaner().clean(); + try { + /* Using reflection to implement sun.nio.ch.DirectBuffer.cleaner() + .clean(); */ + Class<?> sunClass = Class.forName("sun.nio.ch.DirectBuffer"); + Class<?> cleanerClass = Class.forName("sun.misc.Cleaner"); + + Class<?>[] interfaces = buffer.getClass().getInterfaces(); + + for (Class clazz : interfaces) { + if (clazz.getName().equals(sunClass.getName())) { + /* DirectBuffer#cleaner() */ + Method getCleaner = sunClass.getMethod("cleaner"); + Object cleaner = getCleaner.invoke(buffer, null); + /* Cleaner#clean() */ + Method cleanMethod = cleanerClass.getMethod("clean"); + cleanMethod.invoke(cleaner, null); + return; + } + } + } catch (ReflectiveOperationException e) { + // Ignore the Reflection exception. } } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/1cc85485/src/site/resources/profile.noanimal ---------------------------------------------------------------------- diff --git a/src/site/resources/profile.noanimal b/src/site/resources/profile.noanimal deleted file mode 100644 index a9143dc..0000000 --- a/src/site/resources/profile.noanimal +++ /dev/null @@ -1,19 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ----------------------------------------------------------------------------- -# Empty file used to bypass the check of running the Animal Sniffer plugin. -# Animal Sniffer plugin is checking generated code does not included references to -# methods/classes etc that are not present in the standard Java runtime for the defined target -# version.