]> source.dussan.org Git - archiva.git/blob
290a523012d75cec8a0a891c5c5b89b85980f975
[archiva.git] /
1 package org.apache.maven.repository.configuration;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.maven.artifact.repository.ArtifactRepository;
20 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
22 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
23 import org.apache.maven.repository.proxy.ProxiedArtifactRepository;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30
31 /**
32  * Create artifact repositories from a configuration.
33  *
34  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
35  * @plexus.component role="org.apache.maven.repository.configuration.ConfiguredRepositoryFactory"
36  */
37 public class DefaultConfiguredRepositoryFactory
38     implements ConfiguredRepositoryFactory
39 {
40     /**
41      * @plexus.requirement role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
42      */
43     private Map repositoryLayouts;
44
45     /**
46      * @plexus.requirement
47      */
48     private ArtifactRepositoryFactory repoFactory;
49
50     public ArtifactRepository createRepository( RepositoryConfiguration configuration )
51     {
52         File repositoryDirectory = new File( configuration.getDirectory() );
53         String repoDir = repositoryDirectory.toURI().toString();
54
55         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( configuration.getLayout() );
56         return repoFactory.createArtifactRepository( configuration.getId(), repoDir, layout, null, null );
57     }
58
59     public ProxiedArtifactRepository createProxiedRepository( ProxiedRepositoryConfiguration configuration )
60     {
61         boolean enabled = isEnabled( configuration.getSnapshotsPolicy() );
62         String updatePolicy =
63             getUpdatePolicy( configuration.getSnapshotsPolicy(), configuration.getSnapshotsInterval() );
64         ArtifactRepositoryPolicy snapshotsPolicy =
65             new ArtifactRepositoryPolicy( enabled, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
66
67         enabled = isEnabled( configuration.getReleasesPolicy() );
68         updatePolicy = getUpdatePolicy( configuration.getReleasesPolicy(), configuration.getReleasesInterval() );
69         ArtifactRepositoryPolicy releasesPolicy =
70             new ArtifactRepositoryPolicy( enabled, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
71
72         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( configuration.getLayout() );
73         ArtifactRepository artifactRepository = repoFactory.createArtifactRepository( configuration.getId(),
74                                                                                       configuration.getUrl(), layout,
75                                                                                       snapshotsPolicy, releasesPolicy );
76         ProxiedArtifactRepository repository = new ProxiedArtifactRepository( artifactRepository );
77         repository.setCacheFailures( configuration.isCacheFailures() );
78         repository.setHardFail( configuration.isHardFail() );
79         repository.setName( configuration.getName() );
80         repository.setUseNetworkProxy( configuration.isUseNetworkProxy() );
81         return repository;
82     }
83
84     public List createRepositories( Configuration configuration )
85     {
86         List managedRepositories = configuration.getRepositories();
87         List repositories = new ArrayList( managedRepositories.size() );
88
89         for ( Iterator i = managedRepositories.iterator(); i.hasNext(); )
90         {
91             repositories.add( createRepository( (RepositoryConfiguration) i.next() ) );
92         }
93
94         return repositories;
95     }
96
97     public List createProxiedRepositories( Configuration configuration )
98     {
99         List proxiedRepositories = configuration.getProxiedRepositories();
100         List repositories = new ArrayList( proxiedRepositories.size() );
101
102         for ( Iterator i = proxiedRepositories.iterator(); i.hasNext(); )
103         {
104             repositories.add( createProxiedRepository( (ProxiedRepositoryConfiguration) i.next() ) );
105         }
106
107         return repositories;
108     }
109
110     public ArtifactRepository createLocalRepository( Configuration configuration )
111     {
112         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( "default" );
113         File localRepository = new File( configuration.getLocalRepository() );
114         localRepository.mkdirs();
115         return repoFactory.createArtifactRepository( "local", localRepository.toURI().toString(), layout, null, null );
116     }
117
118     private static String getUpdatePolicy( String policy, int interval )
119     {
120         return "interval".equals( policy ) ? policy + ":" + interval : policy;
121     }
122
123     private static boolean isEnabled( String policy )
124     {
125         return !"disabled".equals( policy );
126     }
127 }