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 java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.List;
27
28 import org.apache.continuum.web.util.AuditLog;
29 import org.apache.continuum.web.util.AuditLogConstants;
30 import org.apache.maven.continuum.ContinuumException;
31 import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
32 import org.apache.maven.model.Model;
33 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
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 boolean nonRecursiveProject;
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( new FileReader( 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 if ( result == null )
108 {
109 result = getContinuum().addMavenTwoProject( pomUrl, selectedProjectGroup, checkProtocol, scmUseCache,
110 !this.isNonRecursiveProject(),
111 this.getBuildDefinitionTemplateId() );
112 }
113
114 AuditLog event = new AuditLog( hidePasswordInUrl( pomUrl ), AuditLogConstants.ADD_M2_PROJECT );
115 event.setCategory( AuditLogConstants.PROJECT );
116 event.setCurrentUser( getPrincipal() );
117
118 if ( result == null || result.hasErrors() )
119 {
120 event.setAction( AuditLogConstants.ADD_M2_PROJECT_FAILED );
121 }
122
123 event.log();
124 return result;
125 }
126
127
128
129
130 public File getM2PomFile()
131 {
132 return getPomFile();
133 }
134
135
136
137
138 public void setM2PomFile( File pomFile )
139 {
140 setPomFile( pomFile );
141 }
142
143
144
145
146 public String getM2PomUrl()
147 {
148 return getPomUrl();
149 }
150
151
152
153
154 public void setM2PomUrl( String pomUrl )
155 {
156 setPomUrl( pomUrl );
157 }
158
159 public boolean isNonRecursiveProject()
160 {
161 return nonRecursiveProject;
162 }
163
164 public void setNonRecursiveProject( boolean nonRecursiveProject )
165 {
166 this.nonRecursiveProject = nonRecursiveProject;
167 }
168 }