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