001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    
020    package org.apache.geronimo.mavenplugins.car;
021    
022    import java.io.File;
023    import java.util.SortedSet;
024    import java.util.TreeSet;
025    import java.util.Iterator;
026    
027    import org.apache.geronimo.system.repository.Maven2Repository;
028    import org.apache.geronimo.kernel.repository.Artifact;
029    import org.apache.geronimo.kernel.repository.Version;
030    import org.apache.geronimo.gbean.GBeanInfo;
031    import org.apache.geronimo.gbean.GBeanInfoBuilder;
032    
033    import org.codehaus.mojo.pluginsupport.dependency.DependencyTree;
034    
035    /**
036     * Helps adapt Geronimo repositories to Maven repositories for packaging building.
037     *
038     * @version $Rev: 524717 $ $Date: 2007-04-01 23:39:19 -0400 (Sun, 01 Apr 2007) $
039     */
040    public class Maven2RepositoryAdapter
041        extends Maven2Repository
042    {
043        private ArtifactLookup lookup;
044    
045        private DependencyTree dependencyTree;
046    
047        public Maven2RepositoryAdapter(DependencyTree dependencyTree, final ArtifactLookup lookup) {
048            super(lookup.getBasedir());
049            this.dependencyTree = dependencyTree;
050            this.lookup = lookup;
051        }
052    
053        public File getLocation(final Artifact artifact) {
054            assert artifact != null;
055    
056            return lookup.getLocation(artifact);
057        }
058    
059        public SortedSet list() {
060            TreeSet list = new TreeSet();
061            listInternal(list, dependencyTree.getRootNode(), null, null, null, null);
062            return list;
063        }
064    
065        public SortedSet list(Artifact query) {
066            TreeSet list = new TreeSet();
067            listInternal(list, dependencyTree.getRootNode(), query.getGroupId(), query.getArtifactId(), query.getVersion(), query.getType());
068            return list;
069        }
070    
071        private void listInternal(TreeSet list, DependencyTree.Node node, String groupId, String artifactId, Version version, String type) {
072            if (matches(node.getArtifact(), groupId, artifactId, version, type)) {
073                list.add(mavenToGeronimoArtifact(node.getArtifact()));
074            }
075            for (Iterator iterator = node.getChildren().iterator(); iterator.hasNext();) {
076                DependencyTree.Node childNode = (DependencyTree.Node) iterator.next();
077                listInternal(list, childNode, groupId, artifactId, version, type);
078            }
079        }
080    
081        private boolean matches(org.apache.maven.artifact.Artifact artifact, String groupId, String artifactId, Version version, String type) {
082            return (groupId == null || artifact.getGroupId().equals(groupId))
083                    && (artifactId == null || artifact.getArtifactId().equals(artifactId))
084                    && (version == null || artifact.getVersion().equals(version.toString()))
085                    && (type == null || artifact.getType().equals(type));
086        }
087    
088        protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(final org.apache.maven.artifact.Artifact artifact) {
089            assert artifact != null;
090    
091            return new org.apache.geronimo.kernel.repository.Artifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType());
092        }
093    
094        //
095        // ArtifactLookup
096        //
097    
098        public static interface ArtifactLookup
099        {
100            File getLocation(Artifact artifact);
101    
102            File getBasedir();
103        }
104    
105        //
106        // GBean
107        //
108    
109        public static final GBeanInfo GBEAN_INFO;
110    
111        static {
112            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Maven2RepositoryAdapter.class, "Repository");
113            infoFactory.addAttribute("lookup", ArtifactLookup.class, true);
114            infoFactory.addAttribute("dependencies", DependencyTree.class, true);
115            infoFactory.setConstructor(new String[]{"dependencies", "lookup" });
116            GBEAN_INFO = infoFactory.getBeanInfo();
117        }
118    
119        public static GBeanInfo getGBeanInfo() {
120            return GBEAN_INFO;
121        }
122    }