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.ResourceMappingForm;
19  import org.figure8.join.businessobjects.environment.ResourceMapping;
20  import org.figure8.join.businessobjects.environment.AbstractResource;
21  import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
22  import org.figure8.join.businessfacades.environment.ResourceManager;
23  import org.figure8.join.businessfacades.environment.EnvironmentManager;
24  import org.figure8.join.util.LogUtil;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  /**
34   * This is a Struts action for managing all operations related mappings between Resources and Physical environments.
35   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
36   * @version $Revision: 1.1 $
37   *
38   * @struts.action path="/resourceMapping" name="resMappingForm"
39   *                scope="request" parameter="op" validate="true"
40   *                input="/pages/mainpage.jsp?body=/jsp/environment/environmentmappings.jsp"
41   * @struts.action-forward name="Environments" path="/action/physicalEnvironment?op=view"
42   * @struts.action-forward name="ResourceMappings" path="/jsp/environment/resourcemappings.jsp"
43   */
44  public class ResourceMappingActions extends JoinAction{
45  
46     // Static -------------------------------------------------------------------
47  
48     /** Get a comons logger */
49     private static final Log log = LogUtil.getLog(ResourceMappingActions.class);
50  
51     /** Operation code for loading a specific ResourceMapping */
52     public static final String LOAD_OP = "load";
53     /** Operation code for saving a ResourceMapping (create) */
54     public static final String CREATE_OP = "create";
55     /** Operation code for closing a ResourceMapping (close) */
56     public static final String CLOSE_OP = "close";
57     /** Operation code for retrieving ResourceMappings for a resource */
58     public static final String VIEW_OP = "view";
59  
60     /**
61      * The session scope attribute under which the ResourceMapping object
62      * currently selected by our logged-in User is stored.
63      */
64     public static final String MAPPING_KEY = "mapping";
65     /**
66      * The request scope attribute under which the retrieved ResourceMappings
67      * list is stored.
68      */
69     public static final String MAPPINGS_KEY = "mappings";
70  
71  
72     // Attributes ---------------------------------------------------------------
73  
74     /** The ResourceManager to use */
75     private ResourceManager resourceManager = null;
76     /** The EnvironmentManager to use */
77     private EnvironmentManager environmentManager = null;
78  
79  
80     // Constructors -------------------------------------------------------------
81  
82     /** Creates a new instance of ResourceMappingActions */
83     public ResourceMappingActions(){
84     }
85  
86  
87     // Public -------------------------------------------------------------------
88  
89     /** @param manager The ResourceManager implementation to use */
90     public void setResourceManager(ResourceManager manager){
91        this.resourceManager = manager;
92     }
93     /** @param manager The EnvironmentManager implementation to use */
94     public void setEnvironmentManager(EnvironmentManager manager){
95        this.environmentManager = manager;
96     }
97  
98  
99     // Implementation of JoinAction ---------------------------------------------
100 
101    /**
102     *
103     * @param operation String representing the operation to invoke on Action
104     * @param mapping Mapping between forwards name and path for this action
105     * @param form The form object containing request parameters
106     * @param request The servlet container request wrapper
107     * @param response The servlet container response wrapper
108     * @return A forward to the next view to render and display
109     * @throws Exception such as InfraStructureExceptions ...
110     */
111    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
112       // Trace operation to execute.
113       log.debug("doExecute() called for '" + operation + "' operation");
114 
115       if (LOAD_OP.equals(operation)){
116          return loadMapping(mapping, form, request, response);
117       }
118       else if (CREATE_OP.equals(operation)){
119          return createMapping(mapping, form, request, response);
120       }
121       else if (CLOSE_OP.equals(operation)){
122          return closeMapping(mapping, form, request, response);
123       }
124       // This should not happen...
125       return null;
126    }
127 
128 
129    // Protected ----------------------------------------------------------------
130 
131    /**
132     * Load a specified resource mapping from datastore and fill form with it.
133     * @return A forward to the next view to render and display
134     */
135    protected ActionForward loadMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
136       throws Exception{
137 
138       // This should not happen...
139       return null;
140    }
141 
142    /**
143     * Create a resource mapping into datastore and bind specified resource
144     * and physical environment to it.
145     * @return A forward to the next view to render and display
146     */
147    protected ActionForward createMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
148       throws Exception{
149 
150       if (form instanceof ResourceMappingForm){
151          // Get the requested environments from manager.
152          ResourceMappingForm rForm = (ResourceMappingForm)form;
153          AbstractResource resource = (AbstractResource)resourceManager.getResource(rForm.getResourceId());
154          PhysicalEnvironment physicalEnv = environmentManager.getPhysicalEnvironment(rForm.getEnvironmentKey());
155 
156          // Map resource and environment by creating a mapping.
157          log.info("Creating a mapping for " + resource.getName() + " resource onto "
158                  + physicalEnv.getKey() + " physical environment");
159          resourceManager.mapResourceOnEnvironment(resource, physicalEnv);
160 
161          // Store physicalEnv into request, asking to refresh its view, before forwarding...
162          request.setAttribute(PhysicalEnvironmentActions.ENVIRONMENT_KEY, physicalEnv);
163          return mapping.findForward("Environments");
164       }
165       // This should not happen...
166       return null;
167    }
168 
169    /**
170     * Close a resource mapping and update the related resource and physical environment.
171     * @return A forward to the next view to render and display
172     */
173    protected ActionForward closeMapping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
174       throws Exception{
175 
176       if (form instanceof ResourceMappingForm){
177          // Get the requested resource mapping from manager.
178          ResourceMappingForm rForm = (ResourceMappingForm)form;
179          ResourceMapping resMapping = resourceManager.getResourceMapping(rForm.getId());
180 
181          // Close resource mapping using manager.
182          log.info("Closing the resource mapping having identifier '" + resMapping.getId() + "'");
183          resourceManager.closeResourceMapping(resMapping);
184 
185          // Store physicalEnv into request, asking to refresh its view, before forwarding...
186          request.setAttribute(PhysicalEnvironmentActions.ENVIRONMENT_KEY, resMapping.getPhysicalEnvironment());
187          return mapping.findForward("Environments");
188       }
189       // This should not happen...
190       return null;
191    }
192 }