View Javadoc

1   package org.apache.maven.surefire.its;
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.io.File;
23  import java.io.IOException;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Random;
27  import org.apache.maven.settings.Profile;
28  import org.apache.maven.settings.Repository;
29  import org.apache.maven.settings.RepositoryPolicy;
30  import org.apache.maven.settings.Settings;
31  import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
32  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
33  import org.apache.maven.shared.utils.ReaderFactory;
34  import org.apache.maven.shared.utils.WriterFactory;
35  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36  
37  /**
38   * Helper class to assist in using verifier with a staged local repository.
39   *
40   * @author Stephen Connolly
41   * @since 05-Jan-2010 07:36:22
42   */
43  public final class StagedLocalRepoHelper
44  {
45      private StagedLocalRepoHelper()
46      {
47          throw new IllegalAccessError( "Helper class" );
48      }
49  
50      private static String toUrl( String filename )
51      {
52          /*
53          * NOTE: Maven fails to properly handle percent-encoded "file:" URLs (WAGON-111) so don't use File.toURI() here
54          * as-is but use the decoded path component in the URL.
55          */
56          String url = "file://" + new File( filename ).toURI().getPath();
57          if ( url.endsWith( "/" ) )
58          {
59              url = url.substring( 0, url.length() - 1 );
60          }
61          return url;
62      }
63  
64  
65      public static void createStagedSettingsXml( File originalSettingsXml, File stagedLocalRepo, File stagedSettingsXml )
66          throws IOException
67      {
68          Random entropy = new Random();
69          try
70          {
71              Settings settings = new SettingsXpp3Reader().read( ReaderFactory.newXmlReader( originalSettingsXml ) );
72  
73              String localRepo = System.getProperty( "maven.repo.local" );
74  
75              if ( localRepo == null )
76              {
77                  localRepo = settings.getLocalRepository();
78              }
79  
80              if ( localRepo == null )
81              {
82                  localRepo = System.getProperty( "user.home" ) + "/.m2/repository";
83              }
84  
85              File repoDir = new File( localRepo );
86  
87              if ( !repoDir.exists() )
88              {
89                  repoDir.mkdirs();
90              }
91  
92              // normalize path
93              localRepo = repoDir.getAbsolutePath();
94  
95              Profile profile = new Profile();
96              do
97              {
98                  profile.setId( "stagedLocalRepo" + entropy.nextLong() );
99              }
100             while ( settings.getProfilesAsMap().containsKey( profile.getId() ) );
101 
102             Repository repository = new Repository();
103             repository.setId( profile.getId() + entropy.nextLong() );
104             RepositoryPolicy policy = new RepositoryPolicy();
105             policy.setEnabled( true );
106             policy.setChecksumPolicy( "ignore" );
107             policy.setUpdatePolicy( "never" );
108             repository.setReleases( policy );
109             repository.setSnapshots( policy );
110             repository.setLayout( "default" );
111             repository.setName( "Original Local Repository" );
112             repository.setUrl( toUrl( localRepo ) );
113             profile.addPluginRepository( repository );
114             profile.addRepository( repository );
115             settings.addProfile( profile );
116             settings.addActiveProfile( profile.getId() );
117             settings.setLocalRepository( stagedLocalRepo.getAbsolutePath() );
118 
119             for ( Iterator<Profile> it = settings.getProfiles().iterator(); it.hasNext(); )
120             {
121                 profile = it.next();
122                 disableUpdates( profile.getRepositories() );
123                 disableUpdates( profile.getPluginRepositories() );
124             }
125 
126             new SettingsXpp3Writer().write( WriterFactory.newXmlWriter( stagedSettingsXml ), settings );
127         }
128         catch ( XmlPullParserException e )
129         {
130             IOException ioe = new IOException( e.getMessage() );
131             ioe.initCause( e );
132             throw ioe;
133         }
134     }
135 
136     private static void disableUpdates( List<Repository> repositories )
137     {
138         if ( repositories != null )
139         {
140             for ( Iterator<Repository> it = repositories.iterator(); it.hasNext(); )
141             {
142                 Repository repo = it.next();
143                 repo.setReleases( disableUpdates( repo.getReleases() ) );
144                 repo.setSnapshots( disableUpdates( repo.getSnapshots() ) );
145             }
146         }
147     }
148 
149     private static RepositoryPolicy disableUpdates( RepositoryPolicy policy )
150     {
151         if ( policy == null )
152         {
153             policy = new RepositoryPolicy();
154         }
155 
156         policy.setUpdatePolicy( "never" );
157 
158         return policy;
159     }
160 
161 }