View Javadoc
1   package org.apache.maven.scm.provider.local.command.mkdir;
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.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.CommandParameter;
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFile;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmFileStatus;
32  import org.apache.maven.scm.command.add.AddScmResult;
33  import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
34  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
35  import org.apache.maven.scm.provider.ScmProviderRepository;
36  import org.apache.maven.scm.provider.local.command.add.LocalAddCommand;
37  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
38  import org.codehaus.plexus.util.FileUtils;
39  
40  /**
41   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
42   *
43   */
44  public class LocalMkdirCommand
45      extends AbstractMkdirCommand
46  {
47      protected MkdirScmResult executeMkdirCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
48                                                    boolean createInLocal )
49          throws ScmException
50      {
51          LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
52          List<ScmFile> createdDirs = new ArrayList<ScmFile>();
53  
54          // create/commit the directory directly in the repository
55          if ( !createInLocal )
56          {
57              File file = (File) fileSet.getFileList().get( 0 );
58              File modulePath = new File( repo.getRoot(), repo.getModule() );
59              File dir = new File( modulePath, file.getName() );
60  
61              if ( dir.exists() )
62              {
63                  return new MkdirScmResult( null, "Directory already exists!", "Directory already exists.", false );
64              }
65              else
66              {
67                  if ( getLogger().isInfoEnabled() )
68                  {
69                      getLogger().info( "Creating directory in '" + modulePath.getAbsolutePath() + "'" );
70                  }
71  
72                  FileUtils.mkdir( dir.getAbsolutePath() );
73                  createdDirs.add( new ScmFile( dir.getPath(), ScmFileStatus.ADDED ) );
74              }
75          }
76          else
77          {
78              // add the directory, but not commit
79              LocalAddCommand addCmd = new LocalAddCommand();
80              addCmd.setLogger( getLogger() );
81  
82              CommandParameters parameters = new CommandParameters();
83              parameters.setString( CommandParameter.MESSAGE, message );
84              parameters.setString( CommandParameter.BINARY, "false" );
85  
86              String path = ( (File) fileSet.getFileList().get( 0 ) ).getPath();
87              if ( repo.isFileAdded( path ) )
88              {
89                  return new MkdirScmResult( null, "Directory already exists!", "Directory already exists.", false );
90              }
91  
92              AddScmResult result = (AddScmResult) addCmd.execute( repository, fileSet, parameters );
93              createdDirs.addAll( result.getAddedFiles() );
94          }
95  
96          return new MkdirScmResult( null, createdDirs );
97      }
98  }