View Javadoc

1   package org.apache.maven.continuum.core.action;
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.continuum.dao.ProjectDao;
23  import org.apache.maven.continuum.ContinuumException;
24  import org.apache.maven.continuum.model.project.Project;
25  import org.apache.maven.continuum.utils.WorkingDirectoryService;
26  
27  import java.io.File;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   * @version $Id: CheckWorkingDirectoryAction.java 1372260 2012-08-13 04:29:09Z brett $
34   * @plexus.component role="org.codehaus.plexus.action.Action"
35   * role-hint="check-working-directory"
36   */
37  public class CheckWorkingDirectoryAction
38      extends AbstractContinuumAction
39  {
40      /**
41       * @plexus.requirement
42       */
43      private WorkingDirectoryService workingDirectoryService;
44  
45      /**
46       * @plexus.requirement
47       */
48      private ProjectDao projectDao;
49  
50      private static final String KEY_WORKING_DIRECTORY_EXISTS = "working-directory-exists";
51  
52      public void execute( Map context )
53          throws Exception
54      {
55          Project project = projectDao.getProject( getProjectId( context ) );
56          List<Project> projectsWithCommonScmRoot = getListOfProjectsInGroupWithCommonScmRoot( context );
57          String projectScmRootUrl = getProjectScmRootUrl( context, project.getScmUrl() );
58  
59          File workingDirectory = workingDirectoryService.getWorkingDirectory( project, projectScmRootUrl,
60                                                                               projectsWithCommonScmRoot );
61  
62          if ( !workingDirectory.exists() )
63          {
64              setWorkingDirectoryExists( context, false );
65  
66              return;
67          }
68  
69          File[] files = workingDirectory.listFiles();
70  
71          if ( files == null )
72          {
73              //workingDirectory isn't a directory but a file. Not possible in theory.
74              String msg = workingDirectory.getAbsolutePath() + " isn't a directory but a file.";
75              getLogger().error( msg );
76              throw new ContinuumException( msg );
77          }
78  
79          setWorkingDirectoryExists( context, files.length > 0 );
80      }
81  
82      public static boolean isWorkingDirectoryExists( Map<String, Object> context )
83      {
84          return getBoolean( context, KEY_WORKING_DIRECTORY_EXISTS, false );
85      }
86  
87      public static void setWorkingDirectoryExists( Map<String, Object> context, boolean isWorkingDirectoryExists )
88      {
89          context.put( KEY_WORKING_DIRECTORY_EXISTS, isWorkingDirectoryExists );
90      }
91  
92  }