View Javadoc
1   package org.apache.maven.scm.provider.synergy.command.checkout;
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.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmResult;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
34  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
35  import org.apache.maven.scm.provider.ScmProviderRepository;
36  import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
37  import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
38  import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
39  import org.codehaus.plexus.util.FileUtils;
40  
41  /**
42   * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
43   *
44   */
45  public class SynergyCheckOutCommand
46      extends AbstractCheckOutCommand
47      implements SynergyCommand
48  {
49  
50      /** {@inheritDoc} */
51      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repository, ScmFileSet fileSet,
52                                                          ScmVersion version, boolean recursive )
53          throws ScmException
54      {
55          if ( fileSet.getFileList().size() != 0 )
56          {
57              throw new ScmException( "This provider doesn't support checking out subsets of a project" );
58          }
59  
60          if ( getLogger().isDebugEnabled() )
61          {
62              getLogger().debug( "executing checkout command..." );
63          }
64  
65          SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
66  
67          if ( getLogger().isDebugEnabled() )
68          {
69              getLogger().debug( fileSet.toString() );
70          }
71  
72          String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
73  
74          File waPath;
75          try
76          {
77              String projectSpec =
78                  SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
79              if ( projectSpec != null )
80              {
81                  if ( getLogger().isInfoEnabled() )
82                  {
83                      getLogger().info( "A working project already exists [" + projectSpec + "]." );
84                  }
85                  SynergyUtil.synchronize( getLogger(), projectSpec, ccmAddr );
86              }
87              else
88              {
89                  SynergyUtil.checkoutProject( getLogger(), null, repo.getProjectSpec(), version,
90                                               repo.getProjectPurpose(), repo.getProjectRelease(), ccmAddr );
91                  projectSpec =
92                      SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
93                  if ( getLogger().isInfoEnabled() )
94                  {
95                      getLogger().info( "A new working project [" + projectSpec + "] was created." );
96                  }
97              }
98              SynergyUtil.reconfigure( getLogger(), projectSpec, ccmAddr );
99              waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
100 
101         }
102         finally
103         {
104             SynergyUtil.stop( getLogger(), ccmAddr );
105         }
106 
107         File source = new File( waPath, repo.getProjectName() );
108 
109         if ( getLogger().isInfoEnabled() )
110         {
111             getLogger().info(
112                               "We will now copy files from Synergy Work Area [" + source
113                                   + "] to expected folder [" + fileSet.getBasedir() + "]" );
114         }
115 
116         // Move files to the expected folder
117         try
118         {
119             FileUtils.copyDirectoryStructure( source, fileSet.getBasedir() );
120         }
121         catch ( IOException e1 )
122         {
123             throw new ScmException( "Unable to copy directory structure", e1 );
124         }
125 
126         if ( getLogger().isDebugEnabled() )
127         {
128             getLogger().debug( "We will list content of checkout directory." );
129         }
130 
131         // We need to list files in the directory
132         List<ScmFile> files = new ArrayList<ScmFile>();
133         try
134         {
135             @SuppressWarnings( "unchecked" )
136             List<File> realFiles = FileUtils.getFiles( fileSet.getBasedir(), null, "_ccmwaid.inf" );
137             for ( File f : realFiles )
138             {
139                 files.add( new ScmFile( f.getPath(), ScmFileStatus.CHECKED_OUT ) );
140             }
141         }
142         catch ( IOException e )
143         {
144             throw new ScmException( "Unable to list files in checkout directory", e );
145         }
146 
147         if ( getLogger().isDebugEnabled() )
148         {
149             getLogger().debug( "checkout command end successfully ..." );
150         }
151 
152         return new CheckOutScmResult( files, new ScmResult( "multiple commandline", "OK", "OK", true ) );
153     }
154 
155 }