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