You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2017/07/03 22:01:39 UTC

svn commit: r1800716 - in /maven/shared/trunk/maven-shared-utils/src: main/java/org/apache/maven/shared/utils/logging/MessageUtils.java test/java/org/apache/maven/shared/utils/logging/MessageUtilsTest.java

Author: rfscholte
Date: Mon Jul  3 22:01:39 2017
New Revision: 1800716

URL: http://svn.apache.org/viewvc?rev=1800716&view=rev
Log:
[MSHARED-648] Add registerShutdownHook to MessageUtils

Added:
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/logging/MessageUtilsTest.java
Modified:
    maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java

Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java?rev=1800716&r1=1800715&r2=1800716&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java Mon Jul  3 22:01:39 2017
@@ -34,6 +34,12 @@ public class MessageUtils
 {
     private static final boolean JANSI;
 
+    /** Reference to the JVM shutdown hook, if registered */
+    private static Thread shutdownHook;
+
+    /** Synchronization monitor for the "uninstall" */
+    private static final Object STARTUP_SHUTDOWN_MONITOR = new Object();
+
     static
     {
         boolean jansi = true;
@@ -68,6 +74,31 @@ public class MessageUtils
      */
     public static void systemUninstall()
     {
+        synchronized ( STARTUP_SHUTDOWN_MONITOR )
+        {
+            doSystemUninstall();
+
+            // hook can only set when JANSI is true 
+            if ( shutdownHook != null )
+            {
+                // if out and system_out are same instance again, ansi is assumed to be uninstalled 
+                if ( AnsiConsole.out == AnsiConsole.system_out )
+                {
+                    try
+                    {
+                        Runtime.getRuntime().removeShutdownHook( shutdownHook );
+                    }
+                    catch ( IllegalStateException ex )
+                    {
+                        // ignore - VM is already shutting down
+                    }
+                }
+            }
+        }
+    }
+
+    private static void doSystemUninstall()
+    {
         if ( JANSI )
         {
             AnsiConsole.systemUninstall();
@@ -142,4 +173,32 @@ public class MessageUtils
         return msg.replaceAll( "\u001B\\[[;\\d]*[ -/]*[@-~]", "" );
     }
 
+    /**
+     * Register a shutdown hook with the JVM runtime, uninstalling Ansi support on
+     * JVM shutdown unless is has already been uninstalled at that time.
+     * <p>Delegates to {@link #doSystemUninstall()} for the actual uninstall procedure
+     * 
+     * @see Runtime#addShutdownHook(Thread)
+     * @see MessageUtils#systemUninstall()
+     * @see #doSystemUninstall()
+     */
+    public static void registerShutdownHook()
+    {
+        if ( JANSI && shutdownHook == null )
+        {
+            // No shutdown hook registered yet.
+            shutdownHook = new Thread()
+            {
+                @Override
+                public void run()
+                {
+                    synchronized ( STARTUP_SHUTDOWN_MONITOR )
+                    {
+                        doSystemUninstall();
+                    }
+                }
+            };
+            Runtime.getRuntime().addShutdownHook( shutdownHook );
+        }
+    }
 }

Added: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/logging/MessageUtilsTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/logging/MessageUtilsTest.java?rev=1800716&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/logging/MessageUtilsTest.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/logging/MessageUtilsTest.java Mon Jul  3 22:01:39 2017
@@ -0,0 +1,48 @@
+package org.apache.maven.shared.utils.logging;
+
+/*
+ * 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.
+ */
+
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.junit.Assert.assertThat;
+
+import java.io.PrintStream;
+
+import org.junit.Test;
+
+public class MessageUtilsTest
+{
+    @Test
+    public void testSystem()
+    {
+        PrintStream currentOut = System.out;
+        try
+        {
+            MessageUtils.systemInstall();
+            assertThat( System.out, not( sameInstance( currentOut ) ) );
+            MessageUtils.systemUninstall();
+            assertThat( System.out, sameInstance( currentOut ) );
+        }
+        finally
+        {
+            System.setOut( currentOut );
+        }
+    }
+}