View Javadoc

1   package org.apache.maven.continuum.web.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.web.util.AuditLog;
23  import org.apache.continuum.web.util.AuditLogConstants;
24  import org.apache.maven.continuum.ContinuumException;
25  import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
26  import org.apache.maven.model.Model;
27  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
28  import org.codehaus.plexus.util.ReaderFactory;
29  import org.codehaus.plexus.util.StringUtils;
30  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31  
32  import java.io.File;
33  import java.io.FileNotFoundException;
34  import java.io.IOException;
35  import java.util.List;
36  
37  /**
38   * Add a Maven 2 project to Continuum.
39   *
40   * @author Nick Gonzalez
41   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
42   * @version $Id: AddMavenTwoProjectAction.java 1372260 2012-08-13 04:29:09Z brett $
43   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="addMavenTwoProject"
44   */
45  public class AddMavenTwoProjectAction
46      extends AddMavenProjectAction
47  {
48      // TODO: remove this part once uploading of an m2 project with modules is supported ( CONTINUUM-1098 )
49      public static final String ERROR_UPLOADING_M2_PROJECT_WITH_MODULES = "add.m2.project.upload.modules.error";
50  
51      public static final String ERROR_READING_POM_EXCEPTION_MESSAGE = "Error reading POM";
52  
53      public static final String FILE_SCHEME = "file:/";
54  
55      private String checkoutOption;
56  
57      protected ContinuumProjectBuildingResult doExecute( String pomUrl, int selectedProjectGroup, boolean checkProtocol,
58                                                          boolean scmUseCache )
59          throws ContinuumException
60      {
61          ContinuumProjectBuildingResult result = null;
62  
63          // TODO: remove this part once uploading of an m2 project with modules is supported ( CONTINUUM-1098 )
64          if ( !checkProtocol )
65          {
66              MavenXpp3Reader m2pomReader = new MavenXpp3Reader();
67  
68              try
69              {
70                  String filePath = pomUrl;
71  
72                  if ( !filePath.startsWith( FILE_SCHEME + "/" ) && filePath.startsWith( FILE_SCHEME ) )
73                  {
74                      //Little hack for linux (CONTINUUM-1169)
75                      filePath = StringUtils.replace( filePath, FILE_SCHEME, FILE_SCHEME + "/" );
76                  }
77  
78                  if ( filePath.startsWith( FILE_SCHEME ) )
79                  {
80                      filePath = filePath.substring( FILE_SCHEME.length() );
81                  }
82  
83                  Model model = m2pomReader.read( ReaderFactory.newXmlReader( new File( filePath ) ) );
84  
85                  List modules = model.getModules();
86  
87                  if ( modules != null && modules.size() != 0 )
88                  {
89                      result = new ContinuumProjectBuildingResult();
90                      result.addError( ERROR_UPLOADING_M2_PROJECT_WITH_MODULES );
91                  }
92              }
93              catch ( FileNotFoundException e )
94              {
95                  throw new ContinuumException( ERROR_READING_POM_EXCEPTION_MESSAGE, e );
96              }
97              catch ( IOException e )
98              {
99                  throw new ContinuumException( ERROR_READING_POM_EXCEPTION_MESSAGE, e );
100             }
101             catch ( XmlPullParserException e )
102             {
103                 throw new ContinuumException( ERROR_READING_POM_EXCEPTION_MESSAGE, e );
104             }
105         }
106 
107         boolean nonRecursiveProject;
108         boolean checkoutInSingleDirectory;
109 
110         if ( "checkoutInSingleDirectory".equals( checkoutOption ) )
111         {
112             checkoutInSingleDirectory = true;
113             nonRecursiveProject = false;
114         }
115         else if ( "nonRecursiveProject".equals( checkoutOption ) )
116         {
117             checkoutInSingleDirectory = false;
118             nonRecursiveProject = true;
119         }
120         else
121         {
122             checkoutInSingleDirectory = false;
123             nonRecursiveProject = false;
124         }
125 
126         if ( result == null )
127         {
128             result = getContinuum().addMavenTwoProject( pomUrl, selectedProjectGroup, checkProtocol, scmUseCache,
129                                                         !nonRecursiveProject, this.getBuildDefinitionTemplateId(),
130                                                         checkoutInSingleDirectory );
131         }
132 
133         AuditLog event = new AuditLog( hidePasswordInUrl( pomUrl ), AuditLogConstants.ADD_M2_PROJECT );
134         event.setCategory( AuditLogConstants.PROJECT );
135         event.setCurrentUser( getPrincipal() );
136 
137         if ( result == null || result.hasErrors() )
138         {
139             event.setAction( AuditLogConstants.ADD_M2_PROJECT_FAILED );
140         }
141 
142         event.log();
143         return result;
144     }
145 
146     /**
147      * @deprecated Use {@link #getPomFile()} instead
148      */
149     public File getM2PomFile()
150     {
151         return getPomFile();
152     }
153 
154     /**
155      * @deprecated Use {@link #setPomFile(File)} instead
156      */
157     public void setM2PomFile( File pomFile )
158     {
159         setPomFile( pomFile );
160     }
161 
162     /**
163      * @deprecated Use {@link #getPomUrl()} instead
164      */
165     public String getM2PomUrl()
166     {
167         return getPomUrl();
168     }
169 
170     /**
171      * @deprecated Use {@link #setPomUrl(String)} instead
172      */
173     public void setM2PomUrl( String pomUrl )
174     {
175         setPomUrl( pomUrl );
176     }
177 
178     public String getCheckoutOption()
179     {
180         return checkoutOption;
181     }
182 
183     public void setCheckoutOption( String checkoutOption )
184     {
185         this.checkoutOption = checkoutOption;
186     }
187 }