KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > car > Maven2RepositoryAdapter


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */

19
20 package org.apache.geronimo.mavenplugins.car;
21
22 import java.io.File JavaDoc;
23 import java.util.SortedSet JavaDoc;
24 import java.util.TreeSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.geronimo.system.repository.Maven2Repository;
28 import org.apache.geronimo.kernel.repository.Artifact;
29 import org.apache.geronimo.kernel.repository.Version;
30 import org.apache.geronimo.gbean.GBeanInfo;
31 import org.apache.geronimo.gbean.GBeanInfoBuilder;
32 import org.apache.geronimo.genesis.dependency.DependencyTree;
33
34 /**
35  * Helps adapt Geronimo repositories to Maven repositories for packaging building.
36  *
37  * @version $Rev: 480326 $ $Date: 2006-11-28 20:48:06 -0500 (Tue, 28 Nov 2006) $
38  */

39 public class Maven2RepositoryAdapter
40     extends Maven2Repository
41 {
42     private ArtifactLookup lookup;
43
44     private DependencyTree dependencyTree;
45
46     public Maven2RepositoryAdapter(DependencyTree dependencyTree, final ArtifactLookup lookup) {
47         super(lookup.getBasedir());
48         this.dependencyTree = dependencyTree;
49         this.lookup = lookup;
50     }
51
52     public File JavaDoc getLocation(final Artifact artifact) {
53         assert artifact != null;
54
55         return lookup.getLocation(artifact);
56     }
57
58     public SortedSet JavaDoc list() {
59         TreeSet JavaDoc list = new TreeSet JavaDoc();
60         listInternal(list, dependencyTree.getRootNode(), null, null, null, null);
61         return list;
62     }
63
64     public SortedSet JavaDoc list(Artifact query) {
65         TreeSet JavaDoc list = new TreeSet JavaDoc();
66         listInternal(list, dependencyTree.getRootNode(), query.getGroupId(), query.getArtifactId(), query.getVersion(), query.getType());
67         return list;
68     }
69
70     private void listInternal(TreeSet JavaDoc list, DependencyTree.Node node, String JavaDoc groupId, String JavaDoc artifactId, Version version, String JavaDoc type) {
71         if (matches(node.getArtifact(), groupId, artifactId, version, type)) {
72             list.add(mavenToGeronimoArtifact(node.getArtifact()));
73         }
74         for (Iterator JavaDoc iterator = node.getChildren().iterator(); iterator.hasNext();) {
75             DependencyTree.Node childNode = (DependencyTree.Node) iterator.next();
76             listInternal(list, childNode, groupId, artifactId, version, type);
77         }
78     }
79
80     private boolean matches(org.apache.maven.artifact.Artifact artifact, String JavaDoc groupId, String JavaDoc artifactId, Version version, String JavaDoc type) {
81         return (groupId == null || artifact.getGroupId().equals(groupId))
82                 && (artifactId == null || artifact.getArtifactId().equals(artifactId))
83                 && (version == null || artifact.getVersion().equals(version.toString()))
84                 && (type == null || artifact.getType().equals(type));
85     }
86
87     protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(final org.apache.maven.artifact.Artifact artifact) {
88         assert artifact != null;
89
90         return new org.apache.geronimo.kernel.repository.Artifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType());
91     }
92
93     //
94
// ArtifactLookup
95
//
96

97     public static interface ArtifactLookup
98     {
99         File JavaDoc getLocation(Artifact artifact);
100
101         File JavaDoc getBasedir();
102     }
103
104     //
105
// GBean
106
//
107

108     public static final GBeanInfo GBEAN_INFO;
109
110     static {
111         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Maven2RepositoryAdapter.class, "Repository");
112         infoFactory.addAttribute("lookup", ArtifactLookup.class, true);
113         infoFactory.addAttribute("dependencies", DependencyTree.class, true);
114         infoFactory.setConstructor(new String JavaDoc[]{"dependencies", "lookup" });
115         GBEAN_INFO = infoFactory.getBeanInfo();
116     }
117
118     public static GBeanInfo getGBeanInfo() {
119         return GBEAN_INFO;
120     }
121 }
122
Popular Tags