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: */package org.apache.geronimo.mavenplugins.car;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.Iterator;
023: import java.util.SortedSet;
024: import java.util.TreeSet;
025:
026: import javax.security.auth.login.FailedLoginException;
027:
028: import org.apache.geronimo.gbean.GBeanInfo;
029: import org.apache.geronimo.gbean.GBeanInfoBuilder;
030: import org.apache.geronimo.kernel.repository.Artifact;
031: import org.apache.geronimo.kernel.repository.FileWriteMonitor;
032: import org.apache.geronimo.kernel.repository.Version;
033: import org.apache.geronimo.system.plugin.LocalOpenResult;
034: import org.apache.geronimo.system.plugin.OpenResult;
035: import org.apache.geronimo.system.plugin.SourceRepository;
036: import org.apache.geronimo.system.plugin.model.PluginListType;
037: import org.apache.geronimo.system.repository.Maven2Repository;
038: import org.codehaus.mojo.pluginsupport.dependency.DependencyTree;
039:
040: /**
041: * Helps adapt Geronimo repositories to Maven repositories for packaging building.
042: *
043: * @version $Rev: 615381 $ $Date: 2008-01-25 14:36:57 -0800 (Fri, 25 Jan 2008) $
044: */
045: public class Maven2RepositoryAdapter extends Maven2Repository implements
046: SourceRepository {
047: private ArtifactLookup lookup;
048:
049: private DependencyTree dependencyTree;
050:
051: public Maven2RepositoryAdapter(DependencyTree dependencyTree,
052: final ArtifactLookup lookup) {
053: super (lookup.getBasedir());
054: this .dependencyTree = dependencyTree;
055: this .lookup = lookup;
056: }
057:
058: public File getLocation(final Artifact artifact) {
059: assert artifact != null;
060:
061: return lookup.getLocation(artifact);
062: }
063:
064: public SortedSet list() {
065: TreeSet<Artifact> list = new TreeSet<Artifact>();
066: listInternal(list, dependencyTree.getRootNode(), null, null,
067: null, null);
068: return list;
069: }
070:
071: public SortedSet list(Artifact query) {
072: TreeSet<Artifact> list = new TreeSet<Artifact>();
073: listInternal(list, dependencyTree.getRootNode(), query
074: .getGroupId(), query.getArtifactId(), query
075: .getVersion(), query.getType());
076: return list;
077: }
078:
079: private void listInternal(TreeSet<Artifact> list,
080: DependencyTree.Node node, String groupId,
081: String artifactId, Version version, String type) {
082: if (matches(node.getArtifact(), groupId, artifactId, version,
083: type)) {
084: list.add(mavenToGeronimoArtifact(node.getArtifact()));
085: }
086: for (Iterator iterator = node.getChildren().iterator(); iterator
087: .hasNext();) {
088: DependencyTree.Node childNode = (DependencyTree.Node) iterator
089: .next();
090: listInternal(list, childNode, groupId, artifactId, version,
091: type);
092: }
093: }
094:
095: private boolean matches(
096: org.apache.maven.artifact.Artifact artifact,
097: String groupId, String artifactId, Version version,
098: String type) {
099: return (groupId == null || artifact.getGroupId()
100: .equals(groupId))
101: && (artifactId == null || artifact.getArtifactId()
102: .equals(artifactId))
103: && (version == null || artifact.getVersion().equals(
104: version.toString()))
105: && (type == null || artifact.getType().equals(type));
106: }
107:
108: protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(
109: final org.apache.maven.artifact.Artifact artifact) {
110: assert artifact != null;
111:
112: return new org.apache.geronimo.kernel.repository.Artifact(
113: artifact.getGroupId(), artifact.getArtifactId(),
114: artifact.getVersion(), artifact.getType());
115: }
116:
117: public PluginListType getPluginList() {
118: throw new RuntimeException("Not implemented");
119: }
120:
121: public OpenResult open(Artifact artifact,
122: FileWriteMonitor fileWriteMonitor) throws IOException,
123: FailedLoginException {
124: if (!artifact.isResolved()) {
125: SortedSet<Artifact> list = list(artifact);
126: if (list.isEmpty()) {
127: throw new IOException("Could not resolve artifact "
128: + artifact + " in repo " + rootFile);
129: }
130: artifact = list.first();
131: }
132: File location = getLocation(artifact);
133: return new LocalOpenResult(artifact, location);
134:
135: }
136:
137: //
138: // ArtifactLookup
139: //
140:
141: public static interface ArtifactLookup {
142: File getLocation(Artifact artifact);
143:
144: File getBasedir();
145: }
146:
147: //
148: // GBean
149: //
150:
151: public static final GBeanInfo GBEAN_INFO;
152:
153: static {
154: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
155: Maven2RepositoryAdapter.class, "Repository");
156: infoFactory.addAttribute("lookup", ArtifactLookup.class, true);
157: infoFactory.addAttribute("dependencies", DependencyTree.class,
158: true);
159: infoFactory.setConstructor(new String[] { "dependencies",
160: "lookup" });
161: GBEAN_INFO = infoFactory.getBeanInfo();
162: }
163:
164: public static GBeanInfo getGBeanInfo() {
165: return GBEAN_INFO;
166: }
167: }
|