View Javadoc
1   package org.codehaus.mojo.siteskinner;
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  
25  import org.apache.maven.plugin.logging.Log;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmResult;
29  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
30  import org.apache.maven.scm.command.update.UpdateScmResult;
31  import org.apache.maven.scm.manager.ScmManager;
32  import org.apache.maven.scm.provider.ScmProvider;
33  import org.apache.maven.scm.repository.ScmRepository;
34  
35  /**
36   *  Executes scm command
37   */
38  public class ScmCommandExecutor
39  {
40      private ScmManager manager;
41  
42      private String connectionUrl;
43      
44      private Log log;
45  
46      /**
47       * The constructor.
48       * 
49       * @param manager the scmManager
50       * @param connectionUrl the connection URL
51       * @param log the mojo logger
52       */
53      public ScmCommandExecutor( ScmManager manager, String connectionUrl, Log log )
54      {
55          this.manager = manager;
56          
57          this.connectionUrl = connectionUrl;
58          
59          this.log = log;
60      }
61  
62      /**
63       * Check out sources in the {@code targetDirectory}.
64       *  
65       * @param targetDirectory the directory where the sources will be checked out 
66       * @throws ScmException if the checkout throws an exception
67       */
68      public void checkout( String targetDirectory )
69          throws ScmException
70      {
71          checkout( targetDirectory, null, null );
72      }
73  
74      /**
75       * Check out sources in the {@code targetDirectory}.
76       * 
77       * @param targetDirectory the directory where the sources will be checked out
78       * @param includes the sources to include
79       * @param excludes the sources to exclude
80       * @throws ScmException if the checkout throws an exception
81       */
82      public void checkout( String targetDirectory, String includes, String excludes )
83          throws ScmException
84      {
85          try
86          {
87              ScmRepository repository = manager.makeScmRepository( connectionUrl );
88  
89              ScmProvider provider = manager.getProviderByRepository( repository );
90  
91              ScmFileSet fileSet = getFileSet( targetDirectory, includes, excludes );
92  
93              CheckOutScmResult result = provider.checkOut( repository, fileSet );
94  
95              if ( !checkResult( result ) ) 
96              {
97                  throw new ScmException( "checkout failed with provider message" );
98              }
99          }
100         catch ( Exception ex )
101         {
102             throw new ScmException( "checkout failed.", ex );
103         }
104     }
105     
106     /**
107      * Update the sources in the {@code targetDirectory}.
108      * 
109      * @param targetDirectory the directory where the sources will be updated
110      * @throws ScmException if the update throws an exception
111      */
112     public void update( String targetDirectory )
113         throws ScmException
114     {
115         update( targetDirectory, null, null );
116     }
117     
118     /**
119      * Update the sources in the {@code targetDirectory}.
120      * 
121      * @param targetDirectory the directory where the sources will be updated
122      * @param includes the sources to include
123      * @param excludes the sources to exclude
124      * @throws ScmException if the update throws an exception
125      */
126     public void update( String targetDirectory, String includes, String excludes  )
127         throws ScmException
128     {
129         try
130         {
131             ScmRepository repository = manager.makeScmRepository( connectionUrl );
132 
133             ScmProvider provider = manager.getProviderByRepository( repository );
134             
135             ScmFileSet fileSet = getFileSet( targetDirectory, includes, excludes );
136             
137             UpdateScmResult result = provider.update( repository, fileSet );
138 
139             if ( !checkResult( result ) )
140             {
141                 throw new ScmException( "checkout failed with provider message" );
142             }
143         }
144         catch ( Exception ex )
145         {
146             throw new ScmException( "checkout failed.", ex );
147         }
148     }
149     
150     private ScmFileSet getFileSet( String path, String includes, String excludes ) throws IOException
151     {
152         File dir = new File( path );
153         
154         if ( includes != null || excludes != null )
155         {
156             return new ScmFileSet( dir, includes, excludes );
157         }
158         else
159         {
160             return new ScmFileSet( dir );
161         }
162     }
163 
164     private boolean checkResult( ScmResult result )
165     {
166         if ( !result.isSuccess() )
167         {
168 
169             log.warn( "Provider message:" );
170 
171             log.warn( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
172 
173             log.warn( "Command output:" );
174 
175             log.warn( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
176             
177             return false;
178         }
179         else 
180         {
181             return true;
182         }
183     }
184 }