ignite-6742 sun.misc.Cleaner usages are removed Signed-off-by: Andrey Gura <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/4f739a31 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/4f739a31 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/4f739a31 Branch: refs/heads/ignite-zk Commit: 4f739a315c7c196ddfa59acfa7777dd052bd26ff Parents: 5fb4c13 Author: Dmitriy Sorokin <[email protected]> Authored: Tue Dec 26 16:19:59 2017 +0300 Committer: Andrey Gura <[email protected]> Committed: Tue Dec 26 16:19:59 2017 +0300 ---------------------------------------------------------------------- .../platform/memory/PlatformMemoryPool.java | 4 +- .../ignite/internal/util/GridCleaner.java | 99 ++++++++++++++++++++ .../ignite/internal/util/GridCleanerTest.java | 43 +++++++++ .../ignite/testsuites/IgniteBasicTestSuite.java | 3 + 4 files changed, 148 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/4f739a31/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/memory/PlatformMemoryPool.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/memory/PlatformMemoryPool.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/memory/PlatformMemoryPool.java index 0aec5f0..4cf0234 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/memory/PlatformMemoryPool.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/memory/PlatformMemoryPool.java @@ -17,6 +17,8 @@ package org.apache.ignite.internal.processors.platform.memory; +import org.apache.ignite.internal.util.GridCleaner; + import static org.apache.ignite.internal.processors.platform.memory.PlatformMemoryUtils.POOL_HDR_OFF_MEM_1; import static org.apache.ignite.internal.processors.platform.memory.PlatformMemoryUtils.POOL_HDR_OFF_MEM_2; import static org.apache.ignite.internal.processors.platform.memory.PlatformMemoryUtils.POOL_HDR_OFF_MEM_3; @@ -48,7 +50,7 @@ public class PlatformMemoryPool { public PlatformMemoryPool() { poolPtr = allocatePool(); - sun.misc.Cleaner.create(this, new CleanerRunnable(poolPtr)); + GridCleaner.create(this, new CleanerRunnable(poolPtr)); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/4f739a31/modules/core/src/main/java/org/apache/ignite/internal/util/GridCleaner.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/GridCleaner.java b/modules/core/src/main/java/org/apache/ignite/internal/util/GridCleaner.java new file mode 100644 index 0000000..be042fb --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/GridCleaner.java @@ -0,0 +1,99 @@ +/* + * 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. + */ + +package org.apache.ignite.internal.util; + +import java.lang.reflect.Method; +import java.util.Arrays; +import org.apache.ignite.IgniteException; + +/** + * The facade for Cleaner classes for compatibility between java 8 and 9. + */ +public class GridCleaner { + /** Pre-java9 cleaner class name. */ + private static final String PRE_JAVA9_CLEANER_CLASS_NAME = "sun.misc.Cleaner"; + + /** Java9 cleaner class name. */ + private static final String JAVA9_CLEANER_CLASS_NAME = "java.lang.ref.Cleaner"; + + /** Cleaner class. */ + private static final Class<?> cls; + + /** Cleaner object. */ + private static final Object instance; + + /** Cleaner register method. */ + private static final Method initMtd; + + static { + cls = findCleanerClass(); + + try { + String mtdName; + + if (JAVA9_CLEANER_CLASS_NAME.equals(cls.getName())) { + instance = cls.getMethod("create").invoke(null); + + mtdName = "register"; + } + else { + instance = null; + + mtdName = "create"; + } + + initMtd = cls.getMethod(mtdName, Object.class, Runnable.class); + } + catch (ReflectiveOperationException e) { + throw new ExceptionInInitializerError(e); + } + } + + /** + * + */ + private static Class<?> findCleanerClass() { + String[] clsNames = {PRE_JAVA9_CLEANER_CLASS_NAME, JAVA9_CLEANER_CLASS_NAME}; + + ClassLoader clsLdr = ClassLoader.getSystemClassLoader(); + + for (String clsName : clsNames) { + try { + return Class.forName(clsName, true, clsLdr); + } + catch (ClassNotFoundException e) { + // ignored; + } + } + + throw new IllegalStateException("None of cleaner classes found: " + Arrays.toString(clsNames)); + } + + /** + * @param obj Object. + * @param act Action. + */ + public static Object create(Object obj, Runnable act) { + try { + return initMtd.invoke(instance, obj, act); + } + catch (ReflectiveOperationException e) { + throw new IgniteException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/4f739a31/modules/core/src/test/java/org/apache/ignite/internal/util/GridCleanerTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/GridCleanerTest.java b/modules/core/src/test/java/org/apache/ignite/internal/util/GridCleanerTest.java new file mode 100644 index 0000000..deb87b0 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/util/GridCleanerTest.java @@ -0,0 +1,43 @@ +/* + * 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. + */ + +package org.apache.ignite.internal.util; + +import junit.framework.TestCase; + +/** + * Grid cleaner tests. + */ +public class GridCleanerTest extends TestCase { + /** + * @throws Exception If failed. + */ + public void testCreate() throws Exception { + Object cleaner = GridCleaner.create(this, new Runnable() { + @Override public void run() { + // no-op + } + }); + + assert cleaner != null; + + String clsName = cleaner.getClass().getName(); + + assert clsName.equals("sun.misc.Cleaner") + || clsName.equals("jdk.internal.ref.CleanerImpl$PhantomCleanableRef"); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/4f739a31/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java index 7ca9467..7cdc670 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java @@ -61,6 +61,7 @@ import org.apache.ignite.internal.processors.odbc.OdbcConfigurationValidationSel import org.apache.ignite.internal.processors.odbc.OdbcEscapeSequenceSelfTest; import org.apache.ignite.internal.processors.service.ClosureServiceClientsNodesTest; import org.apache.ignite.internal.product.GridProductVersionSelfTest; +import org.apache.ignite.internal.util.GridCleanerTest; import org.apache.ignite.internal.util.nio.IgniteExceptionInNioWorkerSelfTest; import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.marshaller.DynamicProxySerializationMultiJvmSelfTest; @@ -186,6 +187,8 @@ public class IgniteBasicTestSuite extends TestSuite { suite.addTestSuite(IgniteRejectConnectOnNodeStopTest.class); + suite.addTestSuite(GridCleanerTest.class); + return suite; } }
