]> source.dussan.org Git - archiva.git/blob
d7b16fa9d4516425aacd40f7eb78047ec799b17c
[archiva.git] /
1 package org.apache.maven.archiva.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.commons.lang.StringUtils;
20 import org.apache.maven.archiva.proxy.ProxiedArtifactRepository;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
24 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33  * Create artifact repositories from a configuration.
34  *
35  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
36  * @plexus.component role="org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory"
37  */
38 public class DefaultConfiguredRepositoryFactory
39     implements ConfiguredRepositoryFactory
40 {
41     /**
42      * @plexus.requirement role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
43      */
44     private Map repositoryLayouts;
45
46     /**
47      * @plexus.requirement
48      */
49     private ArtifactRepositoryFactory repoFactory;
50
51     public ArtifactRepository createRepository( RepositoryConfiguration configuration )
52     {
53         File repositoryDirectory = new File( configuration.getDirectory() );
54         String repoDir = repositoryDirectory.toURI().toString();
55
56         //workaround for spaces non converted by PathUtils in wagon
57         //todo: remove it when PathUtils will be fixed
58         if ( repoDir.indexOf( "%20" ) >= 0 )
59         {
60             repoDir = StringUtils.replace( repoDir, "%20", " " );
61         }
62
63         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( configuration.getLayout() );
64         return repoFactory.createArtifactRepository( configuration.getId(), repoDir, layout, null, null );
65     }
66
67     public ProxiedArtifactRepository createProxiedRepository( ProxiedRepositoryConfiguration configuration )
68     {
69         boolean enabled = isEnabled( configuration.getSnapshotsPolicy() );
70         String updatePolicy =
71             getUpdatePolicy( configuration.getSnapshotsPolicy(), configuration.getSnapshotsInterval() );
72         ArtifactRepositoryPolicy snapshotsPolicy =
73             new ArtifactRepositoryPolicy( enabled, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
74
75         enabled = isEnabled( configuration.getReleasesPolicy() );
76         updatePolicy = getUpdatePolicy( configuration.getReleasesPolicy(), configuration.getReleasesInterval() );
77         ArtifactRepositoryPolicy releasesPolicy =
78             new ArtifactRepositoryPolicy( enabled, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
79
80         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( configuration.getLayout() );
81
82         if ( layout == null )
83         {
84             throw new IllegalArgumentException( "Invalid layout: " + configuration.getLayout() );
85         }
86
87         ArtifactRepository artifactRepository = repoFactory.createArtifactRepository( configuration.getId(),
88                                                                                       configuration.getUrl(), layout,
89                                                                                       snapshotsPolicy, releasesPolicy );
90         ProxiedArtifactRepository repository = new ProxiedArtifactRepository( artifactRepository );
91         repository.setCacheFailures( configuration.isCacheFailures() );
92         repository.setHardFail( configuration.isHardFail() );
93         repository.setName( configuration.getName() );
94         repository.setUseNetworkProxy( configuration.isUseNetworkProxy() );
95         return repository;
96     }
97
98     public List createRepositories( Configuration configuration )
99     {
100         List managedRepositories = configuration.getRepositories();
101         List repositories = new ArrayList( managedRepositories.size() );
102
103         for ( Iterator i = managedRepositories.iterator(); i.hasNext(); )
104         {
105             repositories.add( createRepository( (RepositoryConfiguration) i.next() ) );
106         }
107
108         return repositories;
109     }
110
111     public List createProxiedRepositories( Configuration configuration )
112     {
113         List proxiedRepositories = configuration.getProxiedRepositories();
114         List repositories = new ArrayList( proxiedRepositories.size() );
115
116         for ( Iterator i = proxiedRepositories.iterator(); i.hasNext(); )
117         {
118             repositories.add( createProxiedRepository( (ProxiedRepositoryConfiguration) i.next() ) );
119         }
120
121         return repositories;
122     }
123
124     public ArtifactRepository createLocalRepository( Configuration configuration )
125     {
126         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( "default" );
127         File localRepository = new File( configuration.getLocalRepository() );
128         localRepository.mkdirs();
129         return repoFactory.createArtifactRepository( "local", localRepository.toURI().toString(), layout, null, null );
130     }
131
132     private static String getUpdatePolicy( String policy, int interval )
133     {
134         return "interval".equals( policy ) ? policy + ":" + interval : policy;
135     }
136
137     private static boolean isEnabled( String policy )
138     {
139         return !"disabled".equals( policy );
140     }
141 }