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.io.IOException;
024    import java.util.Set;
025    import java.util.SortedSet;
026    import java.util.TreeSet;
027    
028    import javax.security.auth.login.FailedLoginException;
029    
030    import org.apache.geronimo.gbean.GBeanInfo;
031    import org.apache.geronimo.gbean.GBeanInfoBuilder;
032    import org.apache.geronimo.kernel.repository.Artifact;
033    import org.apache.geronimo.kernel.repository.FileWriteMonitor;
034    import org.apache.geronimo.kernel.repository.Version;
035    import org.apache.geronimo.system.plugin.LocalOpenResult;
036    import org.apache.geronimo.system.plugin.OpenResult;
037    import org.apache.geronimo.system.plugin.SourceRepository;
038    import org.apache.geronimo.system.plugin.model.PluginListType;
039    import org.apache.geronimo.system.repository.Maven2Repository;
040    
041    /**
042     * Helps adapt Geronimo repositories to Maven repositories for packaging building.
043     *
044     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
045     */
046    public class Maven2RepositoryAdapter extends Maven2Repository implements SourceRepository {
047        private ArtifactLookup lookup;
048    
049        private Set<org.apache.maven.artifact.Artifact> dependencyTree;
050    
051        public Maven2RepositoryAdapter(Set<org.apache.maven.artifact.Artifact> dependencyTree, final ArtifactLookup lookup) {
052            super(lookup.getBasedir());
053            this.dependencyTree = dependencyTree;
054            this.lookup = lookup;
055        }
056    
057        public File getLocation(final Artifact artifact) {
058            assert artifact != null;
059    
060            return lookup.getLocation(artifact);
061        }
062    
063        public SortedSet list() {
064            TreeSet<Artifact> list = new TreeSet<Artifact>();
065            listInternal(list, null, null, null, null);
066            return list;
067        }
068    
069        public SortedSet list(Artifact query) {
070            TreeSet<Artifact> list = new TreeSet<Artifact>();
071            listInternal(list, query.getGroupId(), query.getArtifactId(), query.getVersion(), query.getType());
072            return list;
073        }
074    
075        private void listInternal(TreeSet<Artifact> list, String groupId, String artifactId, Version version, String type) {
076            for (org.apache.maven.artifact.Artifact artifact: dependencyTree) {
077                if (matches(artifact, groupId, artifactId, version, type)) {
078                    list.add(mavenToGeronimoArtifact(artifact));
079                }
080            }
081        }
082    
083        private boolean matches(org.apache.maven.artifact.Artifact artifact, String groupId, String artifactId, Version version, String type) {
084            return (groupId == null || artifact.getGroupId().equals(groupId))
085                    && (artifactId == null || artifact.getArtifactId().equals(artifactId))
086                    && (version == null || artifact.getVersion().equals(version.toString()))
087                    && (type == null || artifact.getType().equals(type));
088        }
089    
090        protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(final org.apache.maven.artifact.Artifact artifact) {
091            assert artifact != null;
092    
093            return new org.apache.geronimo.kernel.repository.Artifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType());
094        }
095    
096        public PluginListType getPluginList() {
097            throw new RuntimeException("Not implemented");
098        }
099    
100        public OpenResult open(Artifact artifact, FileWriteMonitor fileWriteMonitor) throws IOException, FailedLoginException {
101            if (!artifact.isResolved()) {
102                SortedSet<Artifact> list = list(artifact);
103                if (list.isEmpty()) {
104                    throw new IOException("Could not resolve artifact " + artifact + " in repo " + rootFile);
105                }
106                artifact = list.first();
107            }
108            File location = getLocation(artifact);
109            return new LocalOpenResult(artifact, location);
110    
111        }
112    
113        //
114        // ArtifactLookup
115        //
116    
117        public static interface ArtifactLookup {
118            File getLocation(Artifact artifact);
119    
120            File getBasedir();
121        }
122    
123        //
124        // GBean
125        //
126    
127        public static final GBeanInfo GBEAN_INFO;
128    
129        static {
130            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Maven2RepositoryAdapter.class, "Repository");
131            infoFactory.addAttribute("lookup", ArtifactLookup.class, true);
132            infoFactory.addAttribute("dependencies", Set.class, true);
133            infoFactory.setConstructor(new String[]{"dependencies", "lookup"});
134            GBEAN_INFO = infoFactory.getBeanInfo();
135        }
136    
137        public static GBeanInfo getGBeanInfo() {
138            return GBEAN_INFO;
139        }
140    }