View Javadoc

1   package org.apache.maven.continuum.web.action.admin;
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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.continuum.configuration.BuildAgentGroupConfiguration;
28  import org.apache.maven.continuum.installation.InstallationService;
29  import org.apache.maven.continuum.model.system.Installation;
30  import org.apache.maven.continuum.model.system.Profile;
31  import org.apache.maven.continuum.profile.AlreadyExistsProfileException;
32  import org.apache.maven.continuum.profile.ProfileException;
33  import org.apache.maven.continuum.profile.ProfileService;
34  import org.apache.maven.continuum.security.ContinuumRoleConstants;
35  import org.apache.maven.continuum.web.action.ContinuumActionSupport;
36  import org.apache.struts2.ServletActionContext;
37  import org.codehaus.plexus.redback.rbac.Resource;
38  import org.codehaus.redback.integration.interceptor.SecureAction;
39  import org.codehaus.redback.integration.interceptor.SecureActionBundle;
40  import org.codehaus.redback.integration.interceptor.SecureActionException;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import com.opensymphony.xwork2.Preparable;
45  
46  /**
47   * @author <a href="mailto:olamy@codehaus.org">olamy</a>
48   * @version $Id: ProfileAction.java 781726 2009-06-04 13:04:02Z jzurbano $
49   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="profileAdministration"
50   * @since 7 juin 07
51   */
52  public class ProfileAction
53      extends ContinuumActionSupport
54      implements Preparable, SecureAction
55  {
56      private static final Logger logger = LoggerFactory.getLogger( ProfileAction.class );
57  
58      /**
59       * @plexus.requirement role-hint="default"
60       */
61      private ProfileService profileService;
62  
63      /**
64       * @plexus.requirement role-hint="default"
65       */
66      private InstallationService installationService;
67  
68      private List<Profile> profiles;
69  
70      private Profile profile;
71  
72      private int installationId;
73  
74      private List<Installation> allInstallations;
75  
76      private List<Installation> profileInstallations;
77  
78      private List<BuildAgentGroupConfiguration> buildAgentGroups;
79  
80      public void prepare()
81          throws Exception
82      {
83          super.prepare();
84  
85          List<BuildAgentGroupConfiguration> agentGroups = getContinuum().getConfiguration().getBuildAgentGroups();
86          if ( agentGroups == null )
87          {
88              agentGroups = Collections.EMPTY_LIST;
89          }
90          this.setBuildAgentGroups( agentGroups );
91      }
92  
93      // -------------------------------------------------------
94      //  Webwork Methods
95      // -------------------------------------------------------
96  
97      public String input()
98          throws Exception
99      {
100         this.allInstallations = installationService.getAllInstallations();
101         return INPUT;
102     }
103 
104     public String list()
105         throws Exception
106     {
107         this.profiles = profileService.getAllProfiles();
108         return SUCCESS;
109     }
110 
111     public String edit()
112         throws Exception
113     {
114         if ( logger.isDebugEnabled() )
115         {
116             logger.debug( "edit profile with id " + profile.getId() );
117         }
118         this.profile = profileService.getProfile( profile.getId() );
119         return SUCCESS;
120     }
121 
122     public String save()
123         throws Exception
124     {
125         try
126         {
127             Profile stored = profileService.getProfile( profile.getId() );
128 
129             if ( StringUtils.isBlank( profile.getName() ) )
130             {
131                 if ( stored != null )
132                 {
133                     profile = stored;
134                 }
135 
136                 this.addFieldError( "profile.name", getResourceBundle().getString( "profile.name.required" ) );
137                 return INPUT;
138             }
139 
140             if ( stored == null )
141             {
142                 this.profile = profileService.addProfile( profile );
143                 this.allInstallations = installationService.getAllInstallations();
144                 return "editProfile";
145             }
146             else
147             {
148                 // olamy : the only thing to change here is the profile name
149                 // but in the UI maybe some installations has been we retrieve it
150                 // and only set the name related to CONTINUUM-1361
151                 String name = profile.getName();
152                 String buildAgentGroup = profile.getBuildAgentGroup();
153 
154                 profile = profileService.getProfile( profile.getId() );
155                 // CONTINUUM-1746 we update the profile only if the name has changed 
156                 // jancajas: added build agent group. updated profile if agent group is changed also.
157                 if ( !StringUtils.equals( name, profile.getName() ) ||
158                     !StringUtils.equals( buildAgentGroup, profile.getBuildAgentGroup() ) )
159                 {
160                     profile.setName( name );
161                     profile.setBuildAgentGroup( buildAgentGroup );
162                     profileService.updateProfile( profile );
163                 }
164             }
165         }
166         catch ( AlreadyExistsProfileException e )
167         {
168             this.addActionError( getResourceBundle().getString( "profile.name.already.exists" ) );
169             return INPUT;
170         }
171         this.profiles = profileService.getAllProfiles();
172         return SUCCESS;
173     }
174 
175     public String delete()
176         throws Exception
177     {
178         try
179         {
180             profileService.deleteProfile( profile.getId() );
181             this.profiles = profileService.getAllProfiles();
182         }
183         catch ( ProfileException e )
184         {
185             // display action error in default/success page -- CONTINUUM-2250
186             addActionError( getText( "profile.remove.error" ) );
187         }
188         return SUCCESS;
189     }
190 
191     public String confirmDelete()
192         throws ProfileException
193     {
194         this.profile = getContinuum().getProfileService().getProfile( profile.getId() );
195         return SUCCESS;
196     }
197 
198     public String addInstallation()
199         throws Exception
200     {
201         Installation installation = installationService.getInstallation( this.getInstallationId() );
202         if ( installation != null )
203         {
204             profileService.addInstallationInProfile( profile, installation );
205             // read again
206             this.profile = profileService.getProfile( profile.getId() );
207         }
208         return SUCCESS;
209     }
210 
211     public String removeInstallation()
212         throws Exception
213     {
214 
215         Installation installation = installationService.getInstallation( this.getInstallationId() );
216         profileService.removeInstallationFromProfile( profile, installation );
217         this.profile = profileService.getProfile( profile.getId() );
218         return SUCCESS;
219     }
220 
221     // -----------------------------------------------------
222     // security
223     // -----------------------------------------------------    
224 
225     public SecureActionBundle getSecureActionBundle()
226         throws SecureActionException
227     {
228         SecureActionBundle bundle = new SecureActionBundle();
229         bundle.setRequiresAuthentication( true );
230         bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_PROFILES, Resource.GLOBAL );
231 
232         return bundle;
233     }
234 
235     // -------------------------------------------------------
236     // Webwork setter/getter
237     // -------------------------------------------------------
238 
239     public List<Profile> getProfiles()
240     {
241         return profiles;
242     }
243 
244     public void setProfiles( List<Profile> profiles )
245     {
246         this.profiles = profiles;
247     }
248 
249     public Profile getProfile()
250     {
251         return profile;
252     }
253 
254     public void setProfile( Profile profile )
255     {
256         this.profile = profile;
257     }
258 
259     public List<Installation> getAllInstallations()
260         throws Exception
261     {
262         if ( this.allInstallations == null )
263         {
264             this.allInstallations = installationService.getAllInstallations();
265         }
266         // CONTINUUM-1742 (olamy) don't display already attached en var
267         if ( this.profile != null )
268         {
269             this.allInstallations.removeAll( this.profile.getEnvironmentVariables() );
270         }
271         return allInstallations;
272     }
273 
274     public void setAllInstallations( List<Installation> allInstallations )
275     {
276         this.allInstallations = allInstallations;
277     }
278 
279     public List<Installation> getProfileInstallations()
280     {
281         if ( this.profile != null )
282         {
283             if ( this.profileInstallations == null )
284             {
285                 this.profileInstallations = new ArrayList<Installation>();
286                 if ( this.profile.getJdk() != null )
287                 {
288                     this.profileInstallations.add( this.profile.getJdk() );
289                 }
290                 if ( this.profile.getBuilder() != null )
291                 {
292                     this.profileInstallations.add( this.profile.getBuilder() );
293                 }
294                 if ( this.profile.getEnvironmentVariables() != null &&
295                     !this.profile.getEnvironmentVariables().isEmpty() )
296                 {
297                     this.profileInstallations.addAll( this.profile.getEnvironmentVariables() );
298                 }
299             }
300             return profileInstallations;
301         }
302         return Collections.EMPTY_LIST;
303     }
304 
305     public void setProfileInstallations( List<Installation> profileInstallations )
306     {
307         this.profileInstallations = profileInstallations;
308     }
309 
310     public int getInstallationId()
311     {
312         return installationId;
313     }
314 
315     public void setInstallationId( int installationId )
316     {
317         this.installationId = installationId;
318     }
319 
320     public List<BuildAgentGroupConfiguration> getBuildAgentGroups()
321     {
322         return buildAgentGroups;
323     }
324 
325     public void setBuildAgentGroups( List<BuildAgentGroupConfiguration> buildAgentGroups )
326     {
327         this.buildAgentGroups = buildAgentGroups;
328     }
329 }