1 package org.apache.maven.archiva.dependency.graph;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.model.ArchivaProjectModel;
24 import org.apache.maven.archiva.model.Dependency;
25 import org.apache.maven.archiva.model.Exclusion;
26 import org.apache.maven.archiva.model.Keys;
27 import org.apache.maven.archiva.model.VersionedReference;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
38 * AbstractMemoryRepository
42 public abstract class AbstractMemoryRepository
43 implements MemoryRepository
45 private Map modelMap = new HashMap();
47 public AbstractMemoryRepository()
52 public void addModel( ArchivaProjectModel model )
54 String key = Keys.toKey( model );
55 modelMap.put( key, model );
58 public ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version )
60 String key = Keys.toKey( groupId, artifactId, version );
62 return (ArchivaProjectModel) modelMap.get( key );
65 public abstract void initialize();
67 protected void addExclusion( Dependency dependency, String key )
69 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
71 if ( parts.length != 2 )
73 throw new IllegalArgumentException( "Exclusion key [" + key + "] should be 2 parts. (detected "
74 + parts.length + " instead)" );
77 Exclusion exclusion = new Exclusion();
78 exclusion.setGroupId( parts[0] );
79 exclusion.setArtifactId( parts[1] );
81 dependency.addExclusion( exclusion );
84 protected Dependency toDependency( String key )
86 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
88 if ( parts.length != 5 )
90 throw new IllegalArgumentException( "Dependency key [" + key + "] should be 5 parts. (detected "
91 + parts.length + " instead)" );
94 Dependency dep = new Dependency();
96 dep.setGroupId( parts[0] );
97 dep.setArtifactId( parts[1] );
98 dep.setVersion( parts[2] );
99 dep.setClassifier( parts[3] );
100 dep.setType( parts[4] );
105 protected Dependency toDependency( String key, String scope )
107 Dependency dependency = toDependency( key );
108 dependency.setScope( scope );
113 protected ArchivaProjectModel toModel( String key )
115 return toModel( key, Collections.EMPTY_LIST );
118 protected ArchivaProjectModel toModel( String key, Dependency deps[] )
120 List depList = new ArrayList();
124 depList.addAll( Arrays.asList( deps ) );
127 return toModel( key, depList );
130 protected ArchivaProjectModel toModel( String key, List deps )
132 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
134 if ( parts.length != 3 )
136 throw new IllegalArgumentException( "Project/Model key [" + key + "] should be 3 parts. (detected "
137 + parts.length + " instead)" );
140 ArchivaProjectModel model = new ArchivaProjectModel();
141 model.setGroupId( parts[0] );
142 model.setArtifactId( parts[1] );
143 model.setVersion( parts[2] );
144 model.setOrigin( "testcase" );
145 model.setPackaging( "jar" );
147 Iterator it = deps.iterator();
148 while ( it.hasNext() )
150 Dependency dep = (Dependency) it.next();
151 model.addDependency( dep );
157 protected VersionedReference toParent( String key )
159 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
161 if ( parts.length != 3 )
163 throw new IllegalArgumentException( "Parent key [" + key + "] should be 3 parts. (detected " + parts.length
167 VersionedReference ref = new VersionedReference();
168 ref.setGroupId( parts[0] );
169 ref.setArtifactId( parts[1] );
170 ref.setVersion( parts[2] );