1 package org.apache.maven.continuum.web.action.admin;
2
3 import com.opensymphony.xwork2.Preparable;
4 import org.apache.commons.lang.StringUtils;
5 import org.apache.maven.continuum.installation.AlreadyExistsInstallationException;
6 import org.apache.maven.continuum.installation.InstallationService;
7 import org.apache.maven.continuum.model.system.Installation;
8 import org.apache.maven.continuum.profile.AlreadyExistsProfileException;
9 import org.apache.maven.continuum.security.ContinuumRoleConstants;
10 import org.apache.maven.continuum.web.action.ContinuumConfirmAction;
11 import org.codehaus.plexus.redback.rbac.Resource;
12 import org.codehaus.redback.integration.interceptor.SecureAction;
13 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
14 import org.codehaus.redback.integration.interceptor.SecureActionException;
15
16 import java.util.ArrayList;
17 import java.util.LinkedHashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.ResourceBundle;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class InstallationAction
48 extends ContinuumConfirmAction
49 implements Preparable, SecureAction
50 {
51
52
53
54
55 private InstallationService installationService;
56
57 private List<Installation> installations;
58
59 private Installation installation;
60
61 private Map<String, String> typesLabels;
62
63 private List<String> types;
64
65 private boolean varNameUpdatable = false;
66
67 private boolean automaticProfile;
68
69 private boolean varNameDisplayable = false;
70
71 private boolean displayTypes = true;
72
73 private String installationType;
74
75 private Map<String, String> installationTypes;
76
77 private static final String TOOL_TYPE_KEY = "tool";
78
79 private boolean automaticProfileDisplayable = true;
80
81 private boolean confirmed;
82
83
84
85
86
87 public String list()
88 throws Exception
89 {
90 this.installations = installationService.getAllInstallations();
91 return SUCCESS;
92 }
93
94 public String edit()
95 throws Exception
96 {
97 this.installation = installationService.getInstallation( installation.getInstallationId() );
98
99 if ( this.installation != null )
100 {
101 this.configureUiFlags();
102 }
103 this.automaticProfileDisplayable = false;
104 return SUCCESS;
105 }
106
107 public String input()
108 throws Exception
109 {
110 if ( InstallationService.ENVVAR_TYPE.equalsIgnoreCase( this.getInstallationType() ) )
111 {
112 this.installation = new Installation();
113 this.installation.setType( InstallationService.ENVVAR_TYPE );
114 this.setDisplayTypes( false );
115 this.setVarNameUpdatable( true );
116 this.setVarNameDisplayable( true );
117 }
118 else
119 {
120 this.setVarNameUpdatable( false );
121 this.setVarNameDisplayable( false );
122 }
123 return INPUT;
124 }
125
126 public String save()
127 throws Exception
128 {
129 if ( InstallationService.ENVVAR_TYPE.equalsIgnoreCase( this.getInstallationType() ) )
130 {
131 this.installation.setType( InstallationService.ENVVAR_TYPE );
132 if ( StringUtils.isEmpty( installation.getVarName() ) )
133 {
134 addFieldError( "installation.varName", getResourceBundle().getString(
135 "installation.varName.required" ) );
136 return INPUT;
137 }
138
139 }
140 if ( installation.getInstallationId() == 0 )
141 {
142 try
143 {
144 installationService.add( installation, this.automaticProfile );
145 }
146 catch ( AlreadyExistsInstallationException e )
147 {
148 this.addActionError( getResourceBundle().getString( "installation.name.duplicate" ) );
149 return INPUT;
150 }
151 catch ( AlreadyExistsProfileException e )
152 {
153 this.addActionError( getResourceBundle().getString( "profile.name.already.exists" ) );
154 return INPUT;
155 }
156 }
157 else
158 {
159 this.configureUiFlags();
160 try
161 {
162 installationService.update( installation );
163 }
164 catch ( AlreadyExistsInstallationException e )
165 {
166 this.addActionError( getResourceBundle().getString( "installation.name.duplicate" ) );
167 return INPUT;
168 }
169 }
170 return SUCCESS;
171 }
172
173 public String delete()
174 throws Exception
175 {
176 if ( confirmed )
177 {
178 Installation installationToDelete = installationService.getInstallation( installation.getInstallationId() );
179 installationService.delete( installationToDelete );
180 this.installations = installationService.getAllInstallations();
181 }
182 else
183 {
184 return CONFIRM;
185 }
186 return SUCCESS;
187 }
188
189 public String listTypes()
190 {
191 this.installationTypes = new LinkedHashMap<String, String>();
192 ResourceBundle resourceBundle = getResourceBundle();
193 this.installationTypes.put( TOOL_TYPE_KEY, resourceBundle.getString( "installationTypeChoice.tool.label" ) );
194 this.installationTypes.put( InstallationService.ENVVAR_TYPE, resourceBundle.getString(
195 "installationTypeChoice.envar.label" ) );
196
197 return SUCCESS;
198 }
199
200
201
202
203
204 public SecureActionBundle getSecureActionBundle()
205 throws SecureActionException
206 {
207 SecureActionBundle bundle = new SecureActionBundle();
208 bundle.setRequiresAuthentication( true );
209 bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_INSTALLATIONS, Resource.GLOBAL );
210
211 return bundle;
212 }
213
214
215
216
217 private void configureUiFlags()
218 {
219
220 if ( !InstallationService.ENVVAR_TYPE.equals( this.installation.getType() ) )
221 {
222 this.setDisplayTypes( true );
223 this.setVarNameUpdatable( false );
224 }
225 else
226 {
227 this.setDisplayTypes( false );
228 this.setVarNameUpdatable( true );
229 this.setVarNameDisplayable( true );
230 }
231 this.setInstallationType( this.getInstallation().getType() );
232 }
233
234
235
236
237
238 public List<Installation> getInstallations()
239 {
240 return installations;
241 }
242
243 public void setInstallations( List<Installation> installations )
244 {
245 this.installations = installations;
246 }
247
248 public Installation getInstallation()
249 {
250 return installation;
251 }
252
253 public void setInstallation( Installation installation )
254 {
255 this.installation = installation;
256 }
257
258 public Map<String, String> getTypesLabels()
259 {
260 if ( this.typesLabels == null )
261 {
262 this.typesLabels = new LinkedHashMap<String, String>();
263 ResourceBundle resourceBundle = getResourceBundle();
264 this.typesLabels.put( InstallationService.JDK_TYPE, resourceBundle.getString(
265 "installation.jdk.type.label" ) );
266 this.typesLabels.put( InstallationService.MAVEN2_TYPE, resourceBundle.getString(
267 "installation.maven2.type.label" ) );
268 this.typesLabels.put( InstallationService.MAVEN1_TYPE, resourceBundle.getString(
269 "installation.maven1.type.label" ) );
270 this.typesLabels.put( InstallationService.ANT_TYPE, resourceBundle.getString(
271 "installation.ant.type.label" ) );
272
273
274
275 }
276 return typesLabels;
277 }
278
279 public void setTypesLabels( Map<String, String> typesLabels )
280 {
281 this.typesLabels = typesLabels;
282 }
283
284 public boolean isVarNameUpdatable()
285 {
286 return varNameUpdatable;
287 }
288
289 public void setVarNameUpdatable( boolean varNameUpdatable )
290 {
291 this.varNameUpdatable = varNameUpdatable;
292 }
293
294 public List<String> getTypes()
295 {
296 if ( this.types == null )
297 {
298 this.types = new ArrayList<String>( 5 );
299 this.types.add( InstallationService.JDK_TYPE );
300 this.types.add( InstallationService.MAVEN2_TYPE );
301 this.types.add( InstallationService.MAVEN1_TYPE );
302 this.types.add( InstallationService.ANT_TYPE );
303
304
305
306 }
307 return types;
308 }
309
310 public void setTypes( List<String> types )
311 {
312 this.types = types;
313 }
314
315 public boolean isAutomaticProfile()
316 {
317 return automaticProfile;
318 }
319
320 public void setAutomaticProfile( boolean automaticProfile )
321 {
322 this.automaticProfile = automaticProfile;
323 }
324
325 public Map<String, String> getInstallationTypes()
326 {
327 return installationTypes;
328 }
329
330 public void setInstallationTypes( Map<String, String> installationTypes )
331 {
332 this.installationTypes = installationTypes;
333 }
334
335 public boolean isVarNameDisplayable()
336 {
337 return varNameDisplayable;
338 }
339
340 public void setVarNameDisplayable( boolean varNameDisplayable )
341 {
342 this.varNameDisplayable = varNameDisplayable;
343 }
344
345 public boolean isDisplayTypes()
346 {
347 return displayTypes;
348 }
349
350 public void setDisplayTypes( boolean displayTypes )
351 {
352 this.displayTypes = displayTypes;
353 }
354
355 public String getInstallationType()
356 {
357 return installationType;
358 }
359
360 public void setInstallationType( String installationType )
361 {
362 this.installationType = installationType;
363 }
364
365 public boolean isAutomaticProfileDisplayable()
366 {
367 return automaticProfileDisplayable;
368 }
369
370 public void setAutomaticProfileDisplayable( boolean automaticProfileDisplayable )
371 {
372 this.automaticProfileDisplayable = automaticProfileDisplayable;
373 }
374
375 public boolean isConfirmed()
376 {
377 return confirmed;
378 }
379
380 public void setConfirmed( boolean confirmed )
381 {
382 this.confirmed = confirmed;
383 }
384 }