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.List;
25  import java.util.Random;
26  import org.apache.maven.settings.Profile;
27  import org.apache.maven.settings.Repository;
28  import org.apache.maven.settings.RepositoryPolicy;
29  import org.apache.maven.settings.Settings;
30  import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
31  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
32  import org.apache.maven.shared.utils.ReaderFactory;
33  import org.apache.maven.shared.utils.WriterFactory;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  /**
37   * Helper class to assist in using verifier with a staged local repository.
38   *
39   * @author Stephen Connolly
40   * @since 05-Jan-2010 07:36:22
41   */
42  public final class StagedLocalRepoHelper
43  {
44      private StagedLocalRepoHelper()
45      {
46          throw new IllegalAccessError( "Helper class" );
47      }
48  
49      private static String toUrl( String filename )
50      {
51          /*
52          * NOTE: Maven fails to properly handle percent-encoded "file:" URLs (WAGON-111) so don't use File.toURI() here
53          * as-is but use the decoded path component in the URL.
54          */
55          String url = "file://" + new File( filename ).toURI().getPath();
56          if ( url.endsWith( "/" ) )
57          {
58              url = url.substring( 0, url.length() - 1 );
59          }
60          return url;
61      }
62  
63  
64      public static void createStagedSettingsXml( File originalSettingsXml, File stagedLocalRepo, File stagedSettingsXml )
65          throws IOException
66      {
67          Random entropy = new Random();
68          try
69          {
70              System.out.println( Settings.class.getClassLoader()
71                                          .getResource( "org/apache/maven/settings/Server.class" ) );
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 ( Object o : settings.getProfiles() )
121             {
122                 profile = (Profile) o;
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 (Repository repo : repositories) {
142                 repo.setReleases(disableUpdates(repo.getReleases()));
143                 repo.setSnapshots(disableUpdates(repo.getSnapshots()));
144             }
145         }
146     }
147 
148     private static RepositoryPolicy disableUpdates( RepositoryPolicy policy )
149     {
150         if ( policy == null )
151         {
152             policy = new RepositoryPolicy();
153         }
154 
155         policy.setUpdatePolicy( "never" );
156 
157         return policy;
158     }
159 
160 }