View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control.action;
16  
17  import org.figure8.join.control.JoinAction;
18  import org.figure8.join.control.form.DeploymentForm;
19  import org.figure8.join.businessobjects.artifact.Assembly;
20  import org.figure8.join.businessobjects.commons.Target;
21  import org.figure8.join.businessobjects.commons.Status;
22  import org.figure8.join.businessobjects.environment.Deployment;
23  import org.figure8.join.businessobjects.environment.LogicalEnvironment;
24  import org.figure8.join.businessfacades.artifact.AssemblyManager;
25  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
26  import org.figure8.join.businessfacades.environment.EnvironmentManager;
27  import org.figure8.join.services.workflow.OperationNotAllowedException;
28  import org.figure8.join.util.LogUtil;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import java.util.List;
39  import java.util.ArrayList;
40  /**
41   * This is a Struts action controller for managind Deployment related operations.
42   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
43   * @version $Revision: 1.2 $
44   *
45   * @struts.action path="/deployment" name="deploymentForm"
46   *                scope="request" parameter="op" validate="true"
47   *                input="/pages/mainpage.jsp?body=/jsp/environment/deployment.jsp"
48   * @struts.action-forward name="Form" path="/jsp/environment/deploymentform.jsp"
49   * @struts.action-forward name="Status" path="/jsp/forcestatus.jsp"
50   * @struts.action-forward name="Deployment" path="/jsp/environment/deployment.jsp"
51   * @struts.action-forward name="Deployments" path="/jsp/environment/deployments.jsp"
52   * @struts.action-forward name="Environments" path="/action/logicalEnvironment?op=view"
53   */
54  public class DeploymentActions extends JoinAction{
55  
56     // Static -------------------------------------------------------------------
57  
58     /** Get a commons logger. */
59     private static final Log log = LogUtil.getLog(DeploymentActions.class);
60  
61     /** Operation code for showing a deployment creation form */
62     public static final String FORM_OP = "form";
63     /** Operation code for loading a specific Deployment */
64     public static final String LOAD_OP = "load";
65     /** Operation code for creating a new Deployment */
66     public static final String CREATE_OP = "create";
67     /** Operation code for preparing a specified Deployment */
68     public static final String PREPARE_OP = "prepare";
69     /** Operation code for getting deployments being prepared at that time */
70     public static final String PREPARING_OP = "getPreparing";
71     /** Operation code for realizing a specified Deployment */
72     public static final String REALIZE_OP = "realize";
73     /** Operation code for getting deployments being realized at that time */
74     public static final String REALIZING_OP = "getRealizing";
75     /** Operation code for cancelling a specified Deployment */
76     public static final String CANCEL_OP = "cancel";
77     /** Operation code for ending a specified Deployment */
78     public static final String END_OP = "end";
79     /** Operation code for forcing status of a specified Deployment */
80     public static final String STATUS_OP = "forceStatus";
81  
82     /** The request scope for specifying the title of the screen (used by view) */
83     public static final String TITLE_KEY = "title";
84     /** The request scope for specifying on which screen we are (used by view) */
85     public static final String SCREEN_KEY = "screen";
86     /**
87      * The request scope attribute under which the Deployment object currently
88      * selected by our logged-in User is stored.
89      */
90     public static final String DEPLOYMENT_KEY = "deployment";
91     /**
92      * The request scope attribute under which the retrivied deployments
93      * list is stored.
94      */
95     public static final String DEPLOYMENTS_KEY = "deployments";
96     /**
97      * The request scope attribute under which is stored the code of operation that
98      * has raised a NotAllowedOperationException during workflow.
99      */
100    public static final String NOT_ALLOWED_OPERATION_KEY = "notAllowed";
101    /**
102     * The request scope attribute under which is stored the message of
103     * raised NotAllowedOperationException during workflow.
104     */
105    public static final String NOT_ALLOWED_OPERATION_MSG = "notAllowedMsg";
106 
107 
108    // Attributes ---------------------------------------------------------------
109 
110    /** The AssemblyManager implementation to use */
111    protected AssemblyManager assemblyManager = null;
112    /** The EnvironmentManager implementation to use */
113    protected EnvironmentManager environmentManager = null;
114    /** The IntegrationProcessManager implementation to use */
115    protected IntegrationProcessManager processManager = null;
116 
117 
118    // Constructors -------------------------------------------------------------
119 
120    /** Creates a new instance of DeploymentActions */
121    public DeploymentActions(){
122    }
123 
124 
125    // Public -------------------------------------------------------------------
126 
127    /** @param assemblyManager The AssemblyManager implementation to use */
128    public void setAssemblyManager(AssemblyManager assemblyManager){
129       this.assemblyManager = assemblyManager;
130    }
131    /** @param environmentManager The EnvironmentManager implementation to use */
132    public void setEnvironmentManager(EnvironmentManager environmentManager){
133       this.environmentManager = environmentManager;
134    }
135    /** @param processManager The IntegrationProcessManager implementation to use */
136    public void setIntegrationProcessManager(IntegrationProcessManager processManager){
137       this.processManager = processManager;
138    }
139 
140 
141    // Implementation of JoinAction ---------------------------------------------
142 
143    /**
144     *
145     * @param operation String representing the operation to invoke on Action
146     * @param mapping Mapping between forwards name and path for this action
147     * @param form The form object containing request parameters
148     * @param request The servlet container request wrapper
149     * @param response The servlet container response wrapper
150     * @return A forward to the next view to render and display
151     * @throws Exception such as InfraStructureExceptions ...
152     */
153    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
154            throws Exception{
155       // Trace operation to execute.
156       log.debug("doExecute() called for '" + operation + "' operation");
157 
158       if (form instanceof DeploymentForm){
159          // Get deployment form for operations.
160          DeploymentForm dForm = (DeploymentForm)form;
161 
162          if (FORM_OP.equals(operation)){
163             return showDeploymentForm(mapping, dForm, request, response);
164          }
165          else if (LOAD_OP.equals(operation)){
166             return loadDeployment(mapping, dForm, request, response);
167          }
168          else if (CREATE_OP.equals(operation)){
169             return createDeployment(mapping, dForm, request, response);
170          }
171          else if (PREPARE_OP.equals(operation)){
172             return prepareDeployment(mapping, dForm, request, response);
173          }
174          else if (PREPARING_OP.equals(operation)){
175             return getPreparingDeployments(mapping, dForm, request, response);
176          }
177          else if (REALIZE_OP.equals(operation)){
178             return realizeDeployment(mapping, dForm, request, response);
179          }
180          else if (REALIZING_OP.equals(operation)){
181             return getRealizingDeployments(mapping, dForm, request, response);
182          }
183          else if (CANCEL_OP.equals(operation)){
184             return cancelDeployment(mapping, dForm, request, response);
185          }
186          else if (END_OP.equals(operation)){
187             return endDeployment(mapping, dForm, request, response);
188          }
189          else if (STATUS_OP.equals(operation)){
190             return forceStatus(mapping, dForm, request, response);
191          }
192       }
193       // This should not happen...
194       return null;
195    }
196 
197 
198    // Protected ----------------------------------------------------------------
199 
200    /**
201     * Prepare all we need for displaying a deployment creation form
202     * @return A forward to the next view ti render and display
203     */
204    protected ActionForward showDeploymentForm(ActionMapping mapping, DeploymentForm form, HttpServletRequest request, HttpServletResponse response)
205          throws Exception{
206       // Deployment request can specify an environment or an assembly.
207       if (form.getLogicalEnvKey() != null){
208          // Retrieve environment and list of candidate assemblies.
209          LogicalEnvironment environment = environmentManager.getLogicalEnvironment(form.getLogicalEnvKey());
210          List assemblies = assemblyManager.getAssemblies(environment.getRelease());
211          // Store informations into request attributes.
212          request.setAttribute(AssemblyActions.ASSEMBLIES_KEY, assemblies);
213          request.setAttribute(LogicalEnvironmentActions.ENVIRONMENT_KEY, environment);
214       }
215       else if (form.getAssemblyKey() != null){
216          // Retrieve assembly and candidate environments.
217          List candidates = new ArrayList();
218          Assembly assembly = assemblyManager.getAssembly(form.getAssemblyKey());
219          List environments = environmentManager.getLogicalEnvironments();
220          // Environments should be active and dedicated to release.
221          for (int i=0; i<environments.size(); i++){
222             LogicalEnvironment environment = (LogicalEnvironment)environments.get(i);
223             if (environment.isActive() && environment.getRelease().equals(assembly.getRelease()))
224                candidates.add(environment);
225          }
226          // Store informations into request attributes.
227          request.setAttribute(AssemblyActions.ASSEMBLY_KEY, assembly);
228          request.setAttribute(LogicalEnvironmentActions.ENVIRONMENTS_KEY, candidates);
229       }
230       // Forward after adding targets (we always need them).
231       request.setAttribute(TargetActions.TARGETS_KEY, processManager.getTargets());
232       return mapping.findForward("Form");
233    }
234 
235    /**
236     * Load a specified deployment into request attribute.
237     * @return A forward to the next view to render and display
238     */
239    protected ActionForward loadDeployment(ActionMapping mapping, DeploymentForm form,
240                                           HttpServletRequest request, HttpServletResponse response)
241            throws Exception{
242       // Get the requested deployment from manager.
243       Deployment deployment = environmentManager.getDeployment(form.getId());
244 
245       // Store deployment within request before forwarding.
246       request.setAttribute(DEPLOYMENT_KEY, deployment);
247       return mapping.findForward("Deployment");
248    }
249 
250    /**
251     * Create a new deployment into environment manager.
252     * @return A forward to the next view to render and display
253     */
254    protected ActionForward createDeployment(ActionMapping mapping, DeploymentForm form,
255                                             HttpServletRequest request, HttpServletResponse response)
256            throws Exception{
257       // Retrieve related objects we need.
258       Target target = processManager.getTarget(form.getTargetName());
259       Assembly assembly = assemblyManager.getAssembly(form.getAssemblyKey());
260       LogicalEnvironment environment = environmentManager.getLogicalEnvironment(form.getLogicalEnvKey());
261 
262       // Retrieve applicant identifier.
263       String applicantId = getUserContainer(request).getView().getUser().getLogin();
264 
265       // Create a new deployment from form.
266       Deployment deployment = new Deployment(applicantId, form.getApplicantComments(),
267               environment.getActiveEnvironmentMapping(), assembly, target);
268       if (form.getWishedDate() != null)
269          deployment.setWishedDate(form.getWishedDate());
270       log.info("Creation of a new deployment request for assembly '" + form.getAssemblyKey() + "' on "
271               + "environment '" + form.getLogicalEnvKey() + "'");
272 
273       try {environmentManager.createDeployment(deployment);}
274       catch (OperationNotAllowedException e){
275          // Store exception within request.
276          request.setAttribute(NOT_ALLOWED_OPERATION_KEY, CREATE_OP);
277          request.setAttribute(NOT_ALLOWED_OPERATION_MSG, e.getMessage());
278          return showDeploymentForm(mapping, form, request, response);
279       }
280 
281       return getPreparingDeployments(mapping, form, request, response);
282    }
283 
284    /**
285     * Launch preparation of specified deployment
286     * @return A forward to the next view to render and display
287     */
288    protected ActionForward prepareDeployment(ActionMapping mapping, DeploymentForm form,
289                                              HttpServletRequest request, HttpServletResponse response)
290            throws Exception{
291       // Retrieve specified deployment and prepare it.
292       Deployment deployment = environmentManager.getDeployment(form.getId());
293       environmentManager.prepareDeployment(deployment);
294       // Forward to preparing deployments.
295       return getPreparingDeployments(mapping, form, request, response);
296    }
297 
298    /**
299     * Retrieve deployments being prepared at that time.
300     * @return A forward to the next view to render and display
301     */
302    protected ActionForward getPreparingDeployments(ActionMapping mapping, DeploymentForm form,
303                                                    HttpServletRequest request, HttpServletResponse response)
304            throws Exception{
305       // Retrieve deployments from manager.
306       List deployments = environmentManager.getPreparingDeployments();
307       // Store into request attribute before forwarding.
308       request.setAttribute(DEPLOYMENTS_KEY, deployments);
309       request.setAttribute(TITLE_KEY, "title.deployments.preparation");
310       request.setAttribute(SCREEN_KEY, "preparation");
311 
312       return mapping.findForward("Deployments");
313    }
314 
315    /**
316     * Launch realization of specified deployment.
317     * @return A forward to the next view to render and display
318     */
319    protected ActionForward realizeDeployment(ActionMapping mapping, DeploymentForm form,
320                                              HttpServletRequest request, HttpServletResponse response)
321            throws Exception{
322       // Retrieve assignee id and specified deployment.
323       String assigneeId = getUserContainer(request).getView().getUser().getLogin();
324       Deployment deployment = environmentManager.getDeployment(form.getId());
325       // Start realizing deployment if allowed.
326       deployment.setAssigneeId(assigneeId);
327       try {environmentManager.realizeDeployment(deployment);}
328       catch (OperationNotAllowedException e){
329          // Store exception within request.
330          request.setAttribute(NOT_ALLOWED_OPERATION_KEY, CREATE_OP);
331          request.setAttribute(NOT_ALLOWED_OPERATION_MSG, e.getMessage());
332       }
333       // Forward to realizing deployments.
334       return getRealizingDeployments(mapping, form, request, response);
335    }
336 
337    /**
338     * Retrieve deployments being realized at that time.
339     * @return A forward to the next view to render and display
340     */
341    protected ActionForward getRealizingDeployments(ActionMapping mapping, DeploymentForm form,
342                                                    HttpServletRequest request, HttpServletResponse response)
343            throws Exception{
344       // Retrieve deployments from manager.
345       List deployments = environmentManager.getRealizingDeployments();
346       // Store into request attribute before forwarding.
347       request.setAttribute(DEPLOYMENTS_KEY, deployments);
348       request.setAttribute(TITLE_KEY, "title.deployments.realization");
349       request.setAttribute(SCREEN_KEY, "realization");
350 
351       return mapping.findForward("Deployments");
352    }
353 
354    /**
355     * Cancel a specified deployment.
356     * @return A forward to the next view to render and display
357     */
358    protected ActionForward cancelDeployment(ActionMapping mapping, DeploymentForm form,
359                                             HttpServletRequest request, HttpServletResponse response)
360            throws Exception{
361       // First submission, get deployment and rediret to form.
362       if (form.getAssigneeComments() == null || form.getAssigneeComments().length() == 0){
363          request.setAttribute(DEPLOYMENT_KEY, environmentManager.getDeployment(form.getId()));
364          return mapping.findForward("Form");
365       }
366       // Retrieve specified deployment.
367       Deployment deployment = environmentManager.getDeployment(form.getId());
368       environmentManager.cancelDeployment(deployment, form.getAssigneeComments());
369 
370       return getPreparingDeployments(mapping, form, request, response);
371    }
372 
373    /**
374     * End a specified deployment.
375     * @return A forward to the next view to render and display
376     */
377    protected ActionForward endDeployment(ActionMapping mapping, DeploymentForm form,
378                                          HttpServletRequest request, HttpServletResponse response)
379            throws Exception{
380       // First submission, get deployment and rediret to form.
381       if (form.getAssigneeComments() == null || form.getAssigneeComments().length() == 0){
382          request.setAttribute(DEPLOYMENT_KEY, environmentManager.getDeployment(form.getId()));
383          return mapping.findForward("Form");
384       }
385       // Retrieve specified deployment.
386       Deployment deployment = environmentManager.getDeployment(form.getId());
387       environmentManager.endDeployment(deployment, form.getAssigneeComments());
388 
389       // Store logicalEnv into request, asking to refresh its view, before forwarding...
390       request.setAttribute(LogicalEnvironmentActions.ENVIRONMENT_KEY,
391               deployment.getEnvironmentMapping().getLogicalEnvironment());
392       return mapping.findForward("Environments");
393    }
394 
395    /**
396     * Force the status of a given deployment. This is a 2 step operation : retrieve possible
397     * status first and display a form ; then set the choosen status for deployment.
398     * @return A forward to the next view to render and display
399     */
400    protected ActionForward forceStatus(ActionMapping mapping, DeploymentForm form,
401                                             HttpServletRequest request, HttpServletResponse response)
402            throws Exception{
403       // If no status, forward to page.
404       if (form.getStatusKey() == null){
405          request.setAttribute("statusType", "deployment");
406          request.setAttribute(DEPLOYMENT_KEY, environmentManager.getDeployment(form.getId()));
407          // Build a list of possible status.
408          ArrayList statusList = new ArrayList();
409          statusList.addAll(processManager.getStatusForType(Status.DEPLOYMENT_PREPARATION_TYPE));
410          statusList.addAll(processManager.getStatusForType(Status.DEPLOYMENT_REALIZATION_TYPE));
411          request.setAttribute(StatusActions.STATUS_LIST_KEY, statusList);
412          return mapping.findForward("Status");
413       }
414       // Find and set status to deployment.
415       Status status = processManager.getStatus(form.getStatusKey());
416       Deployment deployment = environmentManager.getDeployment(form.getId());
417       deployment.setStatus(status);
418       environmentManager.saveDeployment(deployment);
419       // Forward to preparing deployments.
420       return getPreparingDeployments(mapping, form, request, response);
421    }
422 }