Skip to content

Commit

Permalink
Fix command line arg interpolation for Booter
Browse files Browse the repository at this point in the history
Closes #84
  • Loading branch information
adamretter authored and khmarbaise committed May 1, 2019
1 parent 4389b59 commit b472009
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,27 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLStreamException;

import org.codehaus.mojo.appassembler.model.ClasspathElement;
import org.codehaus.mojo.appassembler.model.Daemon;
import org.codehaus.mojo.appassembler.model.JvmSettings;
import org.codehaus.mojo.appassembler.model.io.stax.AppassemblerModelStaxReader;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.InterpolationFilterReader;
import org.codehaus.plexus.util.StringUtils;

/**
* Reads the appassembler manifest file from the repo, and executes the specified main class.
Expand Down Expand Up @@ -223,7 +230,7 @@ public static void executeMain( URLClassLoader classLoader, String[] args )
arguments.addAll( Arrays.asList( args ) );
}

String[] commandLineArgs = (String[]) arguments.toArray( new String[arguments.size()] );
String[] commandLineArgs = interpolateArguments((String[])arguments.toArray( new String[arguments.size()] ));

// -----------------------------------------------------------------------
// Setup environment
Expand All @@ -238,6 +245,46 @@ public static void executeMain( URLClassLoader classLoader, String[] args )
main.invoke( null, new Object[] { commandLineArgs } );
}

private static String[] interpolateArguments(String[] arguments) {
if (arguments == null) {
return null;
}

for (int i = 0; i < arguments.length; i++) {
arguments[i] = interpolateBaseDirAndRepo(arguments[i]);
}
return arguments;
}

private static String interpolateBaseDirAndRepo( String content )
{
StringReader sr = new StringReader( content );
StringWriter result = new StringWriter();

Map<Object, Object> context = new HashMap<Object, Object>();

final String baseDir = System.getProperty( "basedir", System.getProperty( "app.home" ) );
if ( baseDir != null && baseDir.length() > 0) {
context.put( "BASEDIR", StringUtils.quoteAndEscape(baseDir, '"') );
}

final String repo = System.getProperty( "app.repo" );
if ( repo != null && repo.length() > 0 ) {
context.put("REPO", StringUtils.quoteAndEscape(repo, '"'));
}

InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( sr, context, "@", "@" );
try
{
IOUtil.copy( interpolationFilterReader, result );
}
catch ( IOException e )
{
// shouldn't happen...
}
return result.toString();
}

// -----------------------------------------------------------------------
// Utils
// -----------------------------------------------------------------------
Expand Down

0 comments on commit b472009

Please sign in to comment.