o Restored legacy code for backward-compat

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@812614 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-09-08 18:35:16 +00:00
parent 73d7e4f891
commit e4c0baae36
10 changed files with 1167 additions and 1 deletions

View File

@ -36,6 +36,7 @@
* @version $Id$
*/
@Component(role = MavenProjectBuilder.class)
@Deprecated
public class DefaultMavenProjectBuilder
implements MavenProjectBuilder
{
@ -165,4 +166,27 @@ public MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration con
return projectBuilder.buildStandaloneSuperProject( config ).getProject();
}
}
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
throws ProjectBuildingException
{
return buildStandaloneSuperProject( localRepository, null );
}
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, ProfileManager profileManager )
throws ProjectBuildingException
{
ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
configuration.setLocalRepository( localRepository );
configuration.setProcessPlugins( false );
configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
if ( profileManager != null )
{
configuration.setActiveProfileIds( profileManager.getExplicitlyActivatedIds() );
configuration.setInactiveProfileIds( profileManager.getExplicitlyDeactivatedIds() );
}
return buildStandaloneSuperProject( configuration );
}
}

View File

@ -1679,6 +1679,10 @@ public String toString()
return sb.toString();
}
/**
* @deprecated Use {@link org.apache.maven.model.io.ModelWriter}.
*/
@Deprecated
public void writeModel( Writer writer )
throws IOException
{
@ -1686,6 +1690,17 @@ public void writeModel( Writer writer )
pomWriter.write( writer, getModel() );
}
/**
* @deprecated Use {@link org.apache.maven.model.io.ModelWriter}.
*/
@Deprecated
public void writeOriginalModel( Writer writer )
throws IOException
{
MavenXpp3Writer pomWriter = new MavenXpp3Writer();
pomWriter.write( writer, getOriginalModel() );
}
/**
* @throws CloneNotSupportedException
* @since 2.0.9

View File

@ -47,4 +47,10 @@ MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> re
MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration configuration )
throws ProjectBuildingException;
MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
throws ProjectBuildingException;
MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, ProfileManager profileManager )
throws ProjectBuildingException;
}

View File

@ -0,0 +1,405 @@
package org.apache.maven.project.interpolation;
/*
* 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 org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.ProjectBuilderConfiguration;
import org.apache.maven.project.path.PathTranslator;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper;
import org.codehaus.plexus.interpolation.RecursionInterceptor;
import org.codehaus.plexus.interpolation.ValueSource;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* Use a regular expression search to find and resolve expressions within the POM.
*
* @author jdcasey Created on Feb 3, 2005
* @version $Id$
* @todo Consolidate this logic with the PluginParameterExpressionEvaluator, minus deprecations/bans.
*/
@Deprecated
public abstract class AbstractStringBasedModelInterpolator
extends AbstractLogEnabled
implements ModelInterpolator, Initializable
{
private static final List<String> PROJECT_PREFIXES = Arrays.asList( new String[]{ "pom.", "project." } );
private static final List<String> TRANSLATED_PATH_EXPRESSIONS;
static
{
List<String> translatedPrefixes = new ArrayList<String>();
// MNG-1927, MNG-2124, MNG-3355:
// If the build section is present and the project directory is non-null, we should make
// sure interpolation of the directories below uses translated paths.
// Afterward, we'll double back and translate any paths that weren't covered during interpolation via the
// code below...
translatedPrefixes.add( "build.directory" );
translatedPrefixes.add( "build.outputDirectory" );
translatedPrefixes.add( "build.testOutputDirectory" );
translatedPrefixes.add( "build.sourceDirectory" );
translatedPrefixes.add( "build.testSourceDirectory" );
translatedPrefixes.add( "build.scriptSourceDirectory" );
translatedPrefixes.add( "reporting.outputDirectory" );
TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes;
}
private PathTranslator pathTranslator;
private Interpolator interpolator;
private RecursionInterceptor recursionInterceptor;
// for testing.
protected AbstractStringBasedModelInterpolator( PathTranslator pathTranslator )
{
this.pathTranslator = pathTranslator;
}
/**
* @todo: Remove the throws clause.
* @throws IOException This exception is not thrown any more, and needs to be removed.
*/
protected AbstractStringBasedModelInterpolator()
{
}
public Model interpolate( Model model, Map<String, ?> context )
throws ModelInterpolationException
{
return interpolate( model, context, true );
}
/**
* Serialize the inbound Model instance to a StringWriter, perform the regex replacement to resolve
* POM expressions, then re-parse into the resolved Model instance.
* <br/>
* <b>NOTE:</b> This will result in a different instance of Model being returned!!!
*
* @param model The inbound Model instance, to serialize and reference for expression resolution
* @param context The other context map to be used during resolution
* @return The resolved instance of the inbound Model. This is a different instance!
*
* @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead.
*/
public Model interpolate( Model model, Map<String, ?> context, boolean strict )
throws ModelInterpolationException
{
Properties props = new Properties();
props.putAll( context );
return interpolate( model,
null,
new DefaultProjectBuilderConfiguration().setExecutionProperties( props ),
true );
}
public Model interpolate( Model model,
File projectDir,
ProjectBuilderConfiguration config,
boolean debugEnabled )
throws ModelInterpolationException
{
StringWriter sWriter = new StringWriter( 1024 );
MavenXpp3Writer writer = new MavenXpp3Writer();
try
{
writer.write( sWriter, model );
}
catch ( IOException e )
{
throw new ModelInterpolationException( "Cannot serialize project model for interpolation.", e );
}
String serializedModel = sWriter.toString();
serializedModel = interpolate( serializedModel, model, projectDir, config, debugEnabled );
StringReader sReader = new StringReader( serializedModel );
MavenXpp3Reader modelReader = new MavenXpp3Reader();
try
{
model = modelReader.read( sReader );
}
catch ( IOException e )
{
throw new ModelInterpolationException(
"Cannot read project model from interpolating filter of serialized version.", e );
}
catch ( XmlPullParserException e )
{
throw new ModelInterpolationException(
"Cannot read project model from interpolating filter of serialized version.", e );
}
return model;
}
/**
* Interpolates all expressions in the src parameter.
* <p>
* The algorithm used for each expression is:
* <ul>
* <li>If it starts with either "pom." or "project.", the expression is evaluated against the model.</li>
* <li>If the value is null, get the value from the context.</li>
* <li>If the value is null, but the context contains the expression, don't replace the expression string
* with the value, and continue to find other expressions.</li>
* <li>If the value is null, get it from the model properties.</li>
* <li>
* @param overrideContext
* @param outputDebugMessages
*/
public String interpolate( String src,
Model model,
final File projectDir,
ProjectBuilderConfiguration config,
boolean debug )
throws ModelInterpolationException
{
try
{
List<ValueSource> valueSources = createValueSources( model, projectDir, config );
List<InterpolationPostProcessor> postProcessors = createPostProcessors( model, projectDir, config );
return interpolateInternal( src, valueSources, postProcessors, debug );
}
finally
{
interpolator.clearAnswers();
}
}
protected List<ValueSource> createValueSources( final Model model, final File projectDir, final ProjectBuilderConfiguration config )
{
String timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT;
Properties modelProperties = model.getProperties();
if ( modelProperties != null )
{
timestampFormat = modelProperties.getProperty( BUILD_TIMESTAMP_FORMAT_PROPERTY, timestampFormat );
}
ValueSource modelValueSource1 = new PrefixedObjectValueSource( PROJECT_PREFIXES, model, false );
ValueSource modelValueSource2 = new ObjectBasedValueSource( model );
ValueSource basedirValueSource = new PrefixedValueSourceWrapper( new AbstractValueSource( false ){
public Object getValue( String expression )
{
if ( projectDir != null && "basedir".equals( expression ) )
{
return projectDir.getAbsolutePath();
}
return null;
}
},
PROJECT_PREFIXES, true );
ValueSource baseUriValueSource = new PrefixedValueSourceWrapper( new AbstractValueSource( false ){
public Object getValue( String expression )
{
if ( projectDir != null && "baseUri".equals( expression ) )
{
return projectDir.getAbsoluteFile().toURI().toString();
}
return null;
}
},
PROJECT_PREFIXES, false );
List<ValueSource> valueSources = new ArrayList<ValueSource>( 9 );
// NOTE: Order counts here!
valueSources.add( basedirValueSource );
valueSources.add( baseUriValueSource );
valueSources.add( new BuildTimestampValueSource( config.getBuildStartTime(), timestampFormat ) );
valueSources.add( modelValueSource1 );
valueSources.add( new MapBasedValueSource( config.getUserProperties() ) );
valueSources.add( new MapBasedValueSource( modelProperties ) );
valueSources.add( new MapBasedValueSource( config.getSystemProperties() ) );
valueSources.add( new AbstractValueSource( false )
{
public Object getValue( String expression )
{
return config.getSystemProperties().getProperty( "env." + expression );
}
} );
valueSources.add( modelValueSource2 );
return valueSources;
}
protected List<InterpolationPostProcessor> createPostProcessors( final Model model, final File projectDir,
final ProjectBuilderConfiguration config )
{
return Collections.singletonList( (InterpolationPostProcessor) new PathTranslatingPostProcessor(
PROJECT_PREFIXES,
TRANSLATED_PATH_EXPRESSIONS,
projectDir,
pathTranslator ) );
}
@SuppressWarnings("unchecked")
protected String interpolateInternal( String src, List<ValueSource> valueSources,
List<InterpolationPostProcessor> postProcessors, boolean debug )
throws ModelInterpolationException
{
if ( src.indexOf( "${" ) < 0 )
{
return src;
}
Logger logger = getLogger();
String result = src;
synchronized( this )
{
for ( ValueSource vs : valueSources )
{
interpolator.addValueSource( vs );
}
for ( InterpolationPostProcessor postProcessor : postProcessors )
{
interpolator.addPostProcessor( postProcessor );
}
try
{
try
{
result = interpolator.interpolate( result, recursionInterceptor );
}
catch( InterpolationException e )
{
throw new ModelInterpolationException( e.getMessage(), e );
}
if ( debug )
{
List<Object> feedback = interpolator.getFeedback();
if ( feedback != null && !feedback.isEmpty() )
{
logger.debug( "Maven encountered the following problems during initial POM interpolation:" );
Object last = null;
for ( Object next : feedback )
{
if ( next instanceof Throwable )
{
if ( last == null )
{
logger.debug( "", ( (Throwable) next ) );
}
else
{
logger.debug( String.valueOf( last ), ( (Throwable) next ) );
}
}
else
{
if ( last != null )
{
logger.debug( String.valueOf( last ) );
}
last = next;
}
}
if ( last != null )
{
logger.debug( String.valueOf( last ) );
}
}
}
interpolator.clearFeedback();
}
finally
{
for ( ValueSource vs : valueSources )
{
interpolator.removeValuesSource( vs );
}
for ( InterpolationPostProcessor postProcessor : postProcessors )
{
interpolator.removePostProcessor( postProcessor );
}
}
}
return result;
}
protected RecursionInterceptor getRecursionInterceptor()
{
return recursionInterceptor;
}
protected void setRecursionInterceptor( RecursionInterceptor recursionInterceptor )
{
this.recursionInterceptor = recursionInterceptor;
}
protected abstract Interpolator createInterpolator();
public void initialize()
throws InitializationException
{
interpolator = createInterpolator();
recursionInterceptor = new PrefixAwareRecursionInterceptor( PROJECT_PREFIXES );
}
protected final Interpolator getInterpolator()
{
return interpolator;
}
}

View File

@ -0,0 +1,64 @@
package org.apache.maven.project.interpolation;
/*
* 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 java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.plexus.interpolation.AbstractValueSource;
/**
*
* @version $Id$
*/
@Deprecated
public class BuildTimestampValueSource
extends AbstractValueSource
{
private final Date startTime;
private final String format;
private String formattedDate;
public BuildTimestampValueSource( Date startTime, String format )
{
super( false );
this.startTime = startTime;
this.format = format;
}
public Object getValue( String expression )
{
if ( "build.timestamp".equals( expression ) || "maven.build.timestamp".equals( expression ) )
{
if ( formattedDate == null && startTime != null )
{
formattedDate = new SimpleDateFormat( format ).format( startTime );
}
return formattedDate;
}
return null;
}
}

View File

@ -0,0 +1,72 @@
package org.apache.maven.project.interpolation;
/*
* 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.
*/
/**
* @author jdcasey
* <p/>
* Created on Feb 2, 2005
*/
@SuppressWarnings("serial")
@Deprecated
public class ModelInterpolationException
extends Exception
{
private String expression;
private String originalMessage;
public ModelInterpolationException( String message )
{
super( message );
}
public ModelInterpolationException( String message, Throwable cause )
{
super( message, cause );
}
public ModelInterpolationException( String expression, String message, Throwable cause )
{
super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message, cause );
this.expression = expression;
this.originalMessage = message;
}
public ModelInterpolationException( String expression, String message )
{
super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message );
this.expression = expression;
this.originalMessage = message;
}
public String getExpression()
{
return expression;
}
public String getOriginalMessage()
{
return originalMessage;
}
}

View File

@ -0,0 +1,66 @@
package org.apache.maven.project.interpolation;
/*
* 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 org.apache.maven.model.Model;
import org.apache.maven.project.ProjectBuilderConfiguration;
import java.io.File;
import java.util.Map;
/**
* @author jdcasey
* <p/>
* Created on Feb 2, 2005
*/
@Deprecated
public interface ModelInterpolator
{
String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyyMMdd-HHmm";
String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format";
String ROLE = ModelInterpolator.class.getName();
/**
* @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead.
*/
Model interpolate( Model project, Map<String, ?> context )
throws ModelInterpolationException;
/**
* @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead.
*/
Model interpolate( Model model, Map<String, ?> context, boolean strict )
throws ModelInterpolationException;
Model interpolate( Model model,
File projectDir,
ProjectBuilderConfiguration config,
boolean debugEnabled )
throws ModelInterpolationException;
String interpolate( String src,
Model model,
File projectDir,
ProjectBuilderConfiguration config,
boolean debugEnabled )
throws ModelInterpolationException;
}

View File

@ -0,0 +1,64 @@
package org.apache.maven.project.interpolation;
/*
* 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 org.apache.maven.project.path.PathTranslator;
import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
import org.codehaus.plexus.interpolation.util.ValueSourceUtils;
import java.io.File;
import java.util.List;
/**
*
* @version $Id$
*/
@Deprecated
public class PathTranslatingPostProcessor
implements InterpolationPostProcessor
{
private final List<String> unprefixedPathKeys;
private final File projectDir;
private final PathTranslator pathTranslator;
private final List<String> expressionPrefixes;
public PathTranslatingPostProcessor( List<String> expressionPrefixes, List<String> unprefixedPathKeys, File projectDir, PathTranslator pathTranslator )
{
this.expressionPrefixes = expressionPrefixes;
this.unprefixedPathKeys = unprefixedPathKeys;
this.projectDir = projectDir;
this.pathTranslator = pathTranslator;
}
public Object execute( String expression,
Object value )
{
expression = ValueSourceUtils.trimPrefix( expression, expressionPrefixes, true );
if ( projectDir != null && value != null && unprefixedPathKeys.contains( expression ) )
{
return pathTranslator.alignToBaseDirectory( String.valueOf( value ), projectDir );
}
return value;
}
}

View File

@ -0,0 +1,59 @@
package org.apache.maven.project.interpolation;
/*
* 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 java.io.IOException;
import java.util.Properties;
import org.apache.maven.project.path.PathTranslator;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
/**
* Use a regular expression search to find and resolve expressions within the POM.
*
* @author jdcasey Created on Feb 3, 2005
* @version $Id$
* @todo Consolidate this logic with the PluginParameterExpressionEvaluator, minus deprecations/bans.
*/
@Deprecated
public class RegexBasedModelInterpolator
extends AbstractStringBasedModelInterpolator
{
public RegexBasedModelInterpolator()
throws IOException
{
}
public RegexBasedModelInterpolator( PathTranslator pathTranslator )
{
super( pathTranslator );
}
public RegexBasedModelInterpolator( Properties envars )
{
}
protected Interpolator createInterpolator()
{
return new RegexBasedInterpolator( true );
}
}

View File

@ -0,0 +1,391 @@
package org.apache.maven.project.interpolation;
/*
* 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 org.apache.maven.model.Model;
import org.apache.maven.project.ProjectBuilderConfiguration;
import org.apache.maven.project.path.PathTranslator;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.codehaus.plexus.interpolation.ValueSource;
import org.codehaus.plexus.logging.Logger;
import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
@Deprecated
@Component( role = ModelInterpolator.class )
public class StringSearchModelInterpolator
extends AbstractStringBasedModelInterpolator
{
private static final Map<Class<?>, Field[]> fieldsByClass = new WeakHashMap<Class<?>, Field[]>();
private static final Map<Class<?>, Boolean> fieldIsPrimitiveByClass = new WeakHashMap<Class<?>, Boolean>();
public StringSearchModelInterpolator()
{
}
public StringSearchModelInterpolator( PathTranslator pathTranslator )
{
super( pathTranslator );
}
public Model interpolate( Model model, File projectDir, ProjectBuilderConfiguration config, boolean debugEnabled )
throws ModelInterpolationException
{
interpolateObject( model, model, projectDir, config, debugEnabled );
return model;
}
protected void interpolateObject( Object obj, Model model, File projectDir, ProjectBuilderConfiguration config,
boolean debugEnabled )
throws ModelInterpolationException
{
try
{
List<ValueSource> valueSources = createValueSources( model, projectDir, config );
List<InterpolationPostProcessor> postProcessors = createPostProcessors( model, projectDir, config );
InterpolateObjectAction action =
new InterpolateObjectAction( obj, valueSources, postProcessors, debugEnabled,
this, getLogger() );
ModelInterpolationException error = AccessController.doPrivileged( action );
if ( error != null )
{
throw error;
}
}
finally
{
getInterpolator().clearAnswers();
}
}
protected Interpolator createInterpolator()
{
StringSearchInterpolator interpolator = new StringSearchInterpolator();
interpolator.setCacheAnswers( true );
return interpolator;
}
private static final class InterpolateObjectAction implements PrivilegedAction<ModelInterpolationException>
{
private final boolean debugEnabled;
private final LinkedList<Object> interpolationTargets;
private final StringSearchModelInterpolator modelInterpolator;
private final Logger logger;
private final List<ValueSource> valueSources;
private final List<InterpolationPostProcessor> postProcessors;
public InterpolateObjectAction( Object target, List<ValueSource> valueSources,
List<InterpolationPostProcessor> postProcessors, boolean debugEnabled,
StringSearchModelInterpolator modelInterpolator, Logger logger )
{
this.valueSources = valueSources;
this.postProcessors = postProcessors;
this.debugEnabled = debugEnabled;
this.interpolationTargets = new LinkedList<Object>();
interpolationTargets.add( target );
this.modelInterpolator = modelInterpolator;
this.logger = logger;
}
public ModelInterpolationException run()
{
while( !interpolationTargets.isEmpty() )
{
Object obj = interpolationTargets.removeFirst();
try
{
traverseObjectWithParents( obj.getClass(), obj );
}
catch ( ModelInterpolationException e )
{
return e;
}
}
return null;
}
@SuppressWarnings("unchecked")
private void traverseObjectWithParents( Class<?> cls, Object target )
throws ModelInterpolationException
{
if ( cls == null )
{
return;
}
if ( cls.isArray() )
{
evaluateArray( target );
}
else if ( isQualifiedForInterpolation( cls ) )
{
Field[] fields = fieldsByClass.get( cls );
if ( fields == null )
{
fields = cls.getDeclaredFields();
fieldsByClass.put( cls, fields );
}
for ( int i = 0; i < fields.length; i++ )
{
Class<?> type = fields[i].getType();
if ( isQualifiedForInterpolation( fields[i], type ) )
{
boolean isAccessible = fields[i].isAccessible();
fields[i].setAccessible( true );
try
{
try
{
if ( String.class == type )
{
String value = (String) fields[i].get( target );
if ( value != null )
{
String interpolated = modelInterpolator.interpolateInternal( value, valueSources, postProcessors, debugEnabled );
if ( !interpolated.equals( value ) )
{
fields[i].set( target, interpolated );
}
}
}
else if ( Collection.class.isAssignableFrom( type ) )
{
Collection<Object> c = (Collection<Object>) fields[i].get( target );
if ( c != null && !c.isEmpty() )
{
List<Object> originalValues = new ArrayList<Object>( c );
try
{
c.clear();
}
catch( UnsupportedOperationException e )
{
if ( debugEnabled && logger != null )
{
logger.debug( "Skipping interpolation of field: " + fields[i] + " in: " + cls.getName() + "; it is an unmodifiable collection." );
}
continue;
}
for ( Object value : originalValues )
{
if ( value != null )
{
if( String.class == value.getClass() )
{
String interpolated = modelInterpolator.interpolateInternal( (String) value, valueSources, postProcessors, debugEnabled );
if ( !interpolated.equals( value ) )
{
c.add( interpolated );
}
else
{
c.add( value );
}
}
else
{
c.add( value );
if ( value.getClass().isArray() )
{
evaluateArray( value );
}
else
{
interpolationTargets.add( value );
}
}
}
else
{
// add the null back in...not sure what else to do...
c.add( value );
}
}
}
}
else if ( Map.class.isAssignableFrom( type ) )
{
Map<Object, Object> m = (Map<Object, Object>) fields[i].get( target );
if ( m != null && !m.isEmpty() )
{
for ( Map.Entry<Object, Object> entry : m.entrySet() )
{
Object value = entry.getValue();
if ( value != null )
{
if( String.class == value.getClass() )
{
String interpolated = modelInterpolator.interpolateInternal( (String) value, valueSources, postProcessors, debugEnabled );
if ( !interpolated.equals( value ) )
{
try
{
entry.setValue( interpolated );
}
catch( UnsupportedOperationException e )
{
if ( debugEnabled && logger != null )
{
logger.debug( "Skipping interpolation of field: " + fields[i] + " (key: " + entry.getKey() + ") in: " + cls.getName() + "; it is an unmodifiable collection." );
}
continue;
}
}
}
else
{
if ( value.getClass().isArray() )
{
evaluateArray( value );
}
else
{
interpolationTargets.add( value );
}
}
}
}
}
}
else
{
Object value = fields[i].get( target );
if ( value != null )
{
if ( fields[i].getType().isArray() )
{
evaluateArray( value );
}
else
{
interpolationTargets.add( value );
}
}
}
}
catch ( IllegalArgumentException e )
{
throw new ModelInterpolationException( "Failed to interpolate field: " + fields[i] + " on class: " + cls.getName(), e );
}
catch ( IllegalAccessException e )
{
throw new ModelInterpolationException( "Failed to interpolate field: " + fields[i] + " on class: " + cls.getName(), e );
}
}
finally
{
fields[i].setAccessible( isAccessible );
}
}
}
traverseObjectWithParents( cls.getSuperclass(), target );
}
}
private boolean isQualifiedForInterpolation( Class<?> cls )
{
return !cls.getPackage().getName().startsWith( "java" );
}
private boolean isQualifiedForInterpolation( Field field, Class<?> fieldType )
{
if ( !fieldIsPrimitiveByClass.containsKey( fieldType ) )
{
fieldIsPrimitiveByClass.put( fieldType, Boolean.valueOf( fieldType.isPrimitive() ) );
}
if ( fieldIsPrimitiveByClass.get( fieldType ).booleanValue() )
{
return false;
}
// if ( fieldType.isPrimitive() )
// {
// return false;
// }
if ( "parent".equals( field.getName() ) )
{
return false;
}
return true;
}
private void evaluateArray( Object target )
throws ModelInterpolationException
{
int len = Array.getLength( target );
for( int i = 0; i < len; i++ )
{
Object value = Array.get( target, i );
if ( value != null )
{
if ( String.class == value.getClass() )
{
String interpolated = modelInterpolator.interpolateInternal( (String) value, valueSources, postProcessors, debugEnabled );
if ( !interpolated.equals( value ) )
{
Array.set( target, i, interpolated );
}
}
else
{
interpolationTargets.add( value );
}
}
}
}
}
}