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

svn commit: r799470 - in /maven/components/trunk: maven-core/src/main/java/org/apache/maven/project/ maven-model-builder/src/main/java/org/apache/maven/model/building/

Author: bentmann
Date: Thu Jul 30 23:03:32 2009
New Revision: 799470

URL: http://svn.apache.org/viewvc?rev=799470&view=rev
Log:
o Enabled model cache

Added:
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/ReactorModelCache.java   (with props)
Modified:
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
    maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
    maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelCacheTag.java

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java?rev=799470&r1=799469&r2=799470&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java Thu Jul 30 23:03:32 2009
@@ -301,7 +301,9 @@
 
         ReactorModelPool reactorModelPool = new ReactorModelPool();
 
-        boolean errors = build( results, interimResults, pomFiles, recursive, config, reactorModelPool );
+        ReactorModelCache modelCache = new ReactorModelCache();
+
+        boolean errors = build( results, interimResults, pomFiles, recursive, config, reactorModelPool, modelCache );
 
         for ( InterimResult interimResult : interimResults )
         {
@@ -344,17 +346,19 @@
         return results;
     }
 
-    private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults, List<File> pomFiles,
-                        boolean recursive, ProjectBuildingRequest config, ReactorModelPool reactorModelPool )
+    private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults,
+                           List<File> pomFiles, boolean recursive, ProjectBuildingRequest config,
+                           ReactorModelPool reactorModelPool, ReactorModelCache modelCache )
     {
         boolean errors = false;
-        
+
         for ( File pomFile : pomFiles )
         {
             ModelBuildingRequest request = getModelBuildingRequest( config, reactorModelPool );
 
             request.setPomFile( pomFile );
             request.setTwoPhaseBuilding( true );
+            request.setModelCache( modelCache );
 
             DefaultModelBuildingListener listener = new DefaultModelBuildingListener( projectBuildingHelper, config );
             request.setModelBuildingListeners( Arrays.asList( listener ) );
@@ -421,7 +425,8 @@
                     }
 
                     errors =
-                        build( results, interimResults, moduleFiles, recursive, config, reactorModelPool ) || errors;
+                        build( results, interimResults, moduleFiles, recursive, config, reactorModelPool, modelCache )
+                            || errors;
                 }
             }
             catch ( ModelBuildingException e )

Added: maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/ReactorModelCache.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/ReactorModelCache.java?rev=799470&view=auto
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/ReactorModelCache.java (added)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/ReactorModelCache.java Thu Jul 30 23:03:32 2009
@@ -0,0 +1,103 @@
+package org.apache.maven.project;
+
+/*
+ * 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.util.HashMap;
+import java.util.Map;
+
+import org.apache.maven.model.building.ModelCache;
+
+/**
+ * A simple model cache used to accelerate model building during a reactor build.
+ * 
+ * @author Benjamin Bentmann
+ */
+class ReactorModelCache
+    implements ModelCache
+{
+
+    private final Map<CacheKey, Object> models = new HashMap<CacheKey, Object>( 256 );
+
+    public Object get( String groupId, String artifactId, String version, String tag )
+    {
+        return models.get( new CacheKey( groupId, artifactId, version, tag ) );
+    }
+
+    public void put( String groupId, String artifactId, String version, String tag, Object data )
+    {
+        models.put( new CacheKey( groupId, artifactId, version, tag ), data );
+    }
+
+    private static final class CacheKey
+    {
+
+        private final String groupId;
+
+        private final String artifactId;
+
+        private final String version;
+
+        private final String tag;
+
+        private final int hashCode;
+
+        public CacheKey( String groupId, String artifactId, String version, String tag )
+        {
+            this.groupId = ( groupId != null ) ? groupId : "";
+            this.artifactId = ( artifactId != null ) ? artifactId : "";
+            this.version = ( version != null ) ? version : "";
+            this.tag = ( tag != null ) ? tag : "";
+
+            int hash = 17;
+            hash = hash * 31 + this.groupId.hashCode();
+            hash = hash * 31 + this.artifactId.hashCode();
+            hash = hash * 31 + this.version.hashCode();
+            hash = hash * 31 + this.tag.hashCode();
+            hashCode = hash;
+        }
+
+        @Override
+        public boolean equals( Object obj )
+        {
+            if ( this == obj )
+            {
+                return true;
+            }
+
+            if ( !( obj instanceof CacheKey ) )
+            {
+                return false;
+            }
+
+            CacheKey that = (CacheKey) obj;
+
+            return artifactId.equals( that.artifactId ) && groupId.equals( that.groupId )
+                && version.equals( that.version ) && tag.equals( that.tag );
+        }
+
+        @Override
+        public int hashCode()
+        {
+            return hashCode;
+        }
+
+    }
+
+}

Propchange: maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/ReactorModelCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core/src/main/java/org/apache/maven/project/ReactorModelCache.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java?rev=799470&r1=799469&r2=799470&view=diff
==============================================================================
--- maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java (original)
+++ maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java Thu Jul 30 23:03:32 2009
@@ -237,9 +237,6 @@
             throw new ModelBuildingException( problems );
         }
 
-        putCache( request.getModelCache(), resultModel.getGroupId(), resultModel.getArtifactId(),
-                  resultModel.getVersion(), ModelCacheTag.EFFECTIVE, resultModel );
-
         return result;
     }
 
@@ -449,9 +446,23 @@
             }
             else
             {
-                parentData =
-                    new ModelData( ModelUtils.cloneModel( parentData.getModel() ), parentData.getGroupId(),
-                                   parentData.getArtifactId(), parentData.getVersion() );
+                /*
+                 * NOTE: This is a sanity check of the cache hit. If the cached parent POM was locally resolved, the
+                 * child's <relativePath> should point at that parent, too. If it doesn't, we ignore the cache and
+                 * resolve externally, to mimic the behavior if the cache didn't exist in the first place. Otherwise,
+                 * the cache would obscure a bad POM.
+                 */
+
+                File pomFile = parentData.getModel().getPomFile();
+                if ( pomFile != null )
+                {
+                    File expectedParentFile = getParentPomFile( childModel );
+
+                    if ( !pomFile.equals( expectedParentFile ) )
+                    {
+                        parentData = readParentExternally( childModel, request, problems );
+                    }
+                }
             }
         }
         else
@@ -465,20 +476,9 @@
     private ModelData readParentLocally( Model childModel, ModelBuildingRequest request, List<ModelProblem> problems )
         throws ModelBuildingException
     {
-        File projectDirectory = childModel.getProjectDirectory();
-        if ( projectDirectory == null )
-        {
-            return null;
-        }
+        File pomFile = getParentPomFile( childModel );
 
-        Parent parent = childModel.getParent();
-
-        File pomFile = new File( new File( projectDirectory, parent.getRelativePath() ).toURI().normalize() );
-        if ( pomFile.isDirectory() )
-        {
-            pomFile = new File( pomFile, "pom.xml" );
-        }
-        if ( !pomFile.isFile() )
+        if ( pomFile == null || !pomFile.isFile() )
         {
             return null;
         }
@@ -497,6 +497,8 @@
             version = candidateModel.getParent().getVersion();
         }
 
+        Parent parent = childModel.getParent();
+
         if ( groupId == null || !groupId.equals( parent.getGroupId() ) )
         {
             return null;
@@ -515,6 +517,27 @@
         return parentData;
     }
 
+    private File getParentPomFile( Model childModel )
+    {
+        File projectDirectory = childModel.getProjectDirectory();
+
+        if ( projectDirectory == null )
+        {
+            return null;
+        }
+
+        String parentPath = childModel.getParent().getRelativePath();
+
+        File pomFile = new File( new File( projectDirectory, parentPath ).toURI().normalize() );
+
+        if ( pomFile.isDirectory() )
+        {
+            pomFile = new File( pomFile, "pom.xml" );
+        }
+
+        return pomFile;
+    }
+
     private ModelData readParentExternally( Model childModel, ModelBuildingRequest request, List<ModelProblem> problems )
         throws ModelBuildingException
     {
@@ -588,12 +611,10 @@
             String artifactId = dependency.getArtifactId();
             String version = dependency.getVersion();
 
-            DependencyManagement importMngt;
-
-            Model importModel =
-                getCache( request.getModelCache(), groupId, artifactId, version, ModelCacheTag.EFFECTIVE );
+            DependencyManagement importMngt =
+                getCache( request.getModelCache(), groupId, artifactId, version, ModelCacheTag.IMPORT );
 
-            if ( importModel == null )
+            if ( importMngt == null )
             {
                 if ( modelResolver == null )
                 {
@@ -636,18 +657,16 @@
 
                 problems.addAll( importResult.getProblems() );
 
-                importModel = importResult.getEffectiveModel();
+                Model importModel = importResult.getEffectiveModel();
 
                 importMngt = importModel.getDependencyManagement();
-            }
-            else
-            {
-                importMngt = ModelUtils.cloneDependencyManagement( importModel.getDependencyManagement() );
-            }
 
-            if ( importMngt == null )
-            {
-                continue;
+                if ( importMngt == null )
+                {
+                    importMngt = new DependencyManagement();
+                }
+
+                putCache( request.getModelCache(), groupId, artifactId, version, ModelCacheTag.IMPORT, importMngt );
             }
 
             if ( importMngts == null )
@@ -666,7 +685,7 @@
     {
         if ( modelCache != null )
         {
-            modelCache.put( groupId, artifactId, version, tag.getName(), data );
+            modelCache.put( groupId, artifactId, version, tag.getName(), tag.intoCache( data ) );
         }
     }
 
@@ -675,7 +694,11 @@
     {
         if ( modelCache != null )
         {
-            return tag.getType().cast( modelCache.get( groupId, artifactId, version, tag.getName() ) );
+            Object data = modelCache.get( groupId, artifactId, version, tag.getName() );
+            if ( data != null )
+            {
+                return tag.fromCache( tag.getType().cast( data ) );
+            }
         }
         return null;
     }

Modified: maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelCacheTag.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelCacheTag.java?rev=799470&r1=799469&r2=799470&view=diff
==============================================================================
--- maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelCacheTag.java (original)
+++ maven/components/trunk/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelCacheTag.java Thu Jul 30 23:03:32 2009
@@ -19,11 +19,12 @@
  * under the License.
  */
 
+import org.apache.maven.model.DependencyManagement;
 import org.apache.maven.model.Model;
 
 /**
- * Describes a tag used by the model builder to access a {@link ModelCache}. This interface simply aggregates a name and
- * a class to provide some type safety when working with the otherwise untyped cache.
+ * Describes a tag used by the model builder to access a {@link ModelCache}. This interface basically aggregates a name
+ * and a class to provide some type safety when working with the otherwise untyped cache.
  * 
  * @author Benjamin Bentmann
  * @param <T> The type of data associated with the tag.
@@ -46,6 +47,24 @@
     Class<T> getType();
 
     /**
+     * Creates a copy of the data suitable for storage in the cache. The original data to store can be mutated after the
+     * cache is populated but the state of the cache must not change so we need to make a copy.
+     * 
+     * @param data The data to store in the cache, must not be {@code null}.
+     * @return The data being stored in the cache, never {@code null}.
+     */
+    T intoCache( T data );
+
+    /**
+     * Creates a copy of the data suitable for retrieval from the cache. The retrieved data can be mutated after the
+     * cache is queried but the state of the cache must not change so we need to make a copy.
+     * 
+     * @param data The data to retrieve from the cache, must not be {@code null}.
+     * @return The data being retrieved from the cache, never {@code null}.
+     */
+    T fromCache( T data );
+
+    /**
      * The tag used to denote raw model data.
      */
     public static final ModelCacheTag<ModelData> RAW = new ModelCacheTag<ModelData>()
@@ -61,24 +80,45 @@
             return ModelData.class;
         }
 
+        public ModelData intoCache( ModelData data )
+        {
+            Model model = ModelUtils.cloneModel( data.getModel() );
+            return new ModelData( model, data.getGroupId(), data.getArtifactId(), data.getVersion() );
+        }
+
+        public ModelData fromCache( ModelData data )
+        {
+            return intoCache( data );
+        }
+
     };
 
     /**
-     * The tag used to denote an effective model.
+     * The tag used to denote an effective dependency management section from an imported model.
      */
-    public static final ModelCacheTag<Model> EFFECTIVE = new ModelCacheTag<Model>()
+    public static final ModelCacheTag<DependencyManagement> IMPORT = new ModelCacheTag<DependencyManagement>()
     {
 
         public String getName()
         {
-            return "effective";
+            return "import";
         }
 
-        public Class<Model> getType()
+        public Class<DependencyManagement> getType()
         {
-            return Model.class;
+            return DependencyManagement.class;
         }
 
+        public DependencyManagement intoCache( DependencyManagement data )
+        {
+            return ModelUtils.cloneDependencyManagement( data );
+        };
+
+        public DependencyManagement fromCache( DependencyManagement data )
+        {
+            return intoCache( data );
+        };
+
     };
 
 }