001    package org.apache.maven.project;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Collections;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.codehaus.plexus.classworlds.realm.ClassRealm;
028    import org.codehaus.plexus.component.annotations.Component;
029    import org.eclipse.aether.graph.DependencyFilter;
030    
031    /**
032     * Default project realm cache implementation. Assumes cached data does not change.
033     */
034    @Component( role = ProjectRealmCache.class )
035    public class DefaultProjectRealmCache
036        implements ProjectRealmCache
037    {
038    
039        private static class CacheKey
040        {
041    
042            private final List<? extends ClassRealm> extensionRealms;
043    
044            private final int hashCode;
045    
046            public CacheKey( List<? extends ClassRealm> extensionRealms )
047            {
048                this.extensionRealms = ( extensionRealms != null ) ? extensionRealms : Collections.<ClassRealm> emptyList();
049    
050                this.hashCode = this.extensionRealms.hashCode();
051            }
052    
053            @Override
054            public int hashCode()
055            {
056                return hashCode;
057            }
058    
059            @Override
060            public boolean equals( Object o )
061            {
062                if ( o == this )
063                {
064                    return true;
065                }
066    
067                if ( !( o instanceof CacheKey ) )
068                {
069                    return false;
070                }
071    
072                CacheKey other = (CacheKey) o;
073    
074                return extensionRealms.equals( other.extensionRealms );
075            }
076    
077        }
078    
079        private final Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
080    
081        public CacheRecord get( List<? extends ClassRealm> extensionRealms )
082        {
083            return cache.get( new CacheKey( extensionRealms ) );
084        }
085    
086        public CacheRecord put( List<? extends ClassRealm> extensionRealms, ClassRealm projectRealm,
087                                DependencyFilter extensionArtifactFilter )
088        {
089            if ( projectRealm == null )
090            {
091                throw new NullPointerException();
092            }
093    
094            CacheKey key = new CacheKey( extensionRealms );
095    
096            if ( cache.containsKey( key ) )
097            {
098                throw new IllegalStateException( "Duplicate project realm for extensions " + extensionRealms );
099            }
100    
101            CacheRecord record = new CacheRecord( projectRealm, extensionArtifactFilter );
102    
103            cache.put( key, record );
104    
105            return record;
106        }
107    
108        public void flush()
109        {
110            cache.clear();
111        }
112    
113        public void register( MavenProject project, CacheRecord record )
114        {
115            // default cache does not track record usage
116        }
117    
118    }