/*******************************************************************************
 * Copyright (c) 2014 Red Hat, Inc..
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/
package org.commonjava.maven.ext.manip;

import java.text.MessageFormat;

public class ManipulationException
    extends Exception
{

    private static final long serialVersionUID = 1L;

    private final Object[] params;

    private String formattedMessage;

    public ManipulationException( final String messageFormat, final Throwable cause, final Object... params )
    {
        super( messageFormat, cause );
        this.params = params;
    }

    public ManipulationException( String string )
    {
        super( string );
        params = null;
    }

    @Override
    public synchronized String getMessage()
    {
        if ( formattedMessage == null )
        {
            final String format = super.getMessage();
            if ( params == null || params.length < 1 )
            {
                formattedMessage = format;
            }
            else
            {
                final String original = format;
                try
                {
                    formattedMessage = String.format( format, params );
                }
                catch ( final Error e )
                {
                }
                catch ( final RuntimeException e )
                {
                }
                catch ( final Exception e )
                {
                }

                if ( formattedMessage == null || original == formattedMessage )
                {
                    try
                    {
                        formattedMessage = MessageFormat.format( format, params );
                    }
                    catch ( final Error e )
                    {
                        formattedMessage = format;
                        throw e;
                    }
                    catch ( final RuntimeException e )
                    {
                        formattedMessage = format;
                        throw e;
                    }
                    catch ( final Exception e )
                    {
                        formattedMessage = format;
                    }
                }
            }
        }

        return formattedMessage;
    }

}

@@ -704,22 +704,16 @@
 private 
-final 
 Object%5B%5D
@@ -985,16 +985,22 @@
 ception(
+ final
  String 
@@ -2644,10 +2644,328 @@
 %0A    %7D%0A%0A
+    private Object writeReplace()%0A    %7B%0A        final Object%5B%5D newParams = new Object%5Bparams.length%5D;%0A        int i = 0;%0A        for ( final Object object : params )%0A        %7B%0A            newParams%5Bi%5D = String.valueOf( object );%0A            i++;%0A        %7D%0A%0A        this.params = newParams;%0A        return this;%0A    %7D%0A%0A
 %7D%0A

make sure exception is serializable by calling String.valueOf() for all message params on serialize.