1 package org.apache.maven.continuum.web.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40
41
42
43
44
45 public class AddMavenTwoProjectAction
46 extends AddMavenProjectAction
47 {
48
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
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
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
148
149 public File getM2PomFile()
150 {
151 return getPomFile();
152 }
153
154
155
156
157 public void setM2PomFile( File pomFile )
158 {
159 setPomFile( pomFile );
160 }
161
162
163
164
165 public String getM2PomUrl()
166 {
167 return getPomUrl();
168 }
169
170
171
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 }