View Javadoc

1   package org.apache.maven.archiva.dependency.graph;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.maven.archiva.model.ArchivaProjectModel;
31  import org.apache.maven.archiva.model.Dependency;
32  import org.apache.maven.archiva.model.Exclusion;
33  import org.apache.maven.archiva.model.Keys;
34  import org.apache.maven.archiva.model.VersionedReference;
35  
36  /**
37   * AbstractMemoryRepository 
38   *
39   * @version $Id: AbstractMemoryRepository.java 755277 2009-03-17 15:18:35Z brett $
40   */
41  public abstract class AbstractMemoryRepository
42      implements MemoryRepository
43  {
44      private Map<String,ArchivaProjectModel> modelMap = new HashMap<String, ArchivaProjectModel>();
45  
46      public AbstractMemoryRepository()
47      {
48          initialize();
49      }
50  
51      public void addModel( ArchivaProjectModel model )
52      {
53          String key = Keys.toKey( model );
54          modelMap.put( key, model );
55      }
56  
57      public ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version )
58      {
59          String key = Keys.toKey( groupId, artifactId, version );
60  
61          return (ArchivaProjectModel) modelMap.get( key );
62      }
63  
64      public abstract void initialize();
65  
66      protected void addExclusion( Dependency dependency, String key )
67      {
68          String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
69  
70          if ( parts.length != 2 )
71          {
72              throw new IllegalArgumentException( "Exclusion key [" + key + "] should be 2 parts. (detected "
73                  + parts.length + " instead)" );
74          }
75  
76          Exclusion exclusion = new Exclusion();
77          exclusion.setGroupId( parts[0] );
78          exclusion.setArtifactId( parts[1] );
79  
80          dependency.addExclusion( exclusion );
81      }
82  
83      protected Dependency toDependency( String key )
84      {
85          String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
86  
87          if ( parts.length != 5 )
88          {
89              throw new IllegalArgumentException( "Dependency key [" + key + "] should be 5 parts. (detected "
90                  + parts.length + " instead)" );
91          }
92  
93          Dependency dep = new Dependency();
94  
95          dep.setGroupId( parts[0] );
96          dep.setArtifactId( parts[1] );
97          dep.setVersion( parts[2] );
98          dep.setClassifier( parts[3] );
99          dep.setType( parts[4] );
100 
101         return dep;
102     }
103 
104     protected Dependency toDependency( String key, String scope )
105     {
106         Dependency dependency = toDependency( key );
107         dependency.setScope( scope );
108 
109         return dependency;
110     }
111 
112     protected ArchivaProjectModel toModel( String key )
113     {
114         return toModel( key, Collections.<Dependency>emptyList() );
115     }
116 
117     protected ArchivaProjectModel toModel( String key, Dependency[] deps )
118     {
119         List<Dependency> depList = new ArrayList<Dependency>();
120 
121         if ( deps != null )
122         {
123             depList.addAll( Arrays.asList( deps ) );
124         }
125 
126         return toModel( key, depList );
127     }
128 
129     protected ArchivaProjectModel toModel( String key, List<Dependency> deps )
130     {
131         String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
132 
133         if ( parts.length != 3 )
134         {
135             throw new IllegalArgumentException( "Project/Model key [" + key + "] should be 3 parts. (detected "
136                 + parts.length + " instead)" );
137         }
138 
139         ArchivaProjectModel model = new ArchivaProjectModel();
140         model.setGroupId( parts[0] );
141         model.setArtifactId( parts[1] );
142         model.setVersion( parts[2] );
143         model.setOrigin( "testcase" );
144         model.setPackaging( "jar" );
145 
146         for ( Dependency dep : deps )
147         {
148             model.addDependency( dep );
149         }
150 
151         return model;
152     }
153 
154     protected VersionedReference toParent( String key )
155     {
156         String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
157 
158         if ( parts.length != 3 )
159         {
160             throw new IllegalArgumentException( "Parent key [" + key + "] should be 3 parts. (detected " + parts.length
161                 + " instead)" );
162         }
163 
164         VersionedReference ref = new VersionedReference();
165         ref.setGroupId( parts[0] );
166         ref.setArtifactId( parts[1] );
167         ref.setVersion( parts[2] );
168 
169         return ref;
170     }
171 
172 }