KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > deploy > ApplyDomainDeployConfirmAction


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ApplyDomainDeployConfirmAction.java,v 1.1.2.1 2005/08/12 11:13:44 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.deploy;
27
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41 import org.objectweb.jonas.jmx.JonasManagementRepr;
42 import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
43
44 /**
45  * @author Patrick Smith
46  * @author Gregory Lapouchnian
47  */

48 public class ApplyDomainDeployConfirmAction extends BaseDeployAction {
49
50     // --------------------------------------------------------- Properties
51

52     private ArrayList JavaDoc servers;
53
54     // --------------------------------------------------------- Public Methods
55

56     public ActionForward executeAction(ActionMapping p_Mapping,
57             ActionForm p_Form, HttpServletRequest JavaDoc p_Request,
58             HttpServletResponse JavaDoc p_Response) throws IOException JavaDoc,
59             ServletException JavaDoc {
60
61         DomainDeployForm oForm = (DomainDeployForm) p_Form;
62
63         // Create a new deployment thread only if there is no other deployment
64
// in progress and not after a deployment has been completed.
65

66         // These flags are in place to prevent multiple threads from being created
67
// when this action is called again and again by the domainDeployProgress
68
// JSP page.
69
if (!oForm.getDeploymentInProgress() && !oForm.getDeploymentComplete()) {
70
71             oForm.setDeploymentInProgress(true);
72
73             // Create a new thread to deploy the applications to the servers
74
// so that the progress JSP can be refreshed with updated progress.
75
new DeployThread(p_Mapping, p_Form, p_Request, p_Response) {
76
77                 public void run() {
78                     // Form used
79
DomainDeployForm oForm = (DomainDeployForm) p_Form;
80
81                     try {
82                         // target servers
83
servers = oForm.getListTargetServers();
84                         // apps to be deployed on these servers
85
ArrayList JavaDoc alApps = oForm.getListDeploy();
86                         // deploy each app the set of selected servers
87
Map JavaDoc reportList = oForm.getReports();
88
89                         String JavaDoc selectedAction = oForm.getSelectedAction();
90                         boolean replaceExisting = oForm.getReplacementOption();
91
92                         for (int i = 0; i < alApps.size(); i++) {
93                             String JavaDoc current = (String JavaDoc) alApps.get(i);
94
95                             // attempt to deploy the application, save the report
96
// to display to the user on the progress page and
97
// reset the progress to allow the next deployment
98
// to go through
99
if (selectedAction.equals(DomainDeployForm.DEPLOY)) {
100                                 if (doDeployOperation(current)) {
101                                     reportList.put(current, getReport());
102                                     resetProgress();
103                                 }
104                                 else {
105                                     createException("Another domain deployment operation is running on this server.");
106                                 }
107                             } else if (selectedAction.equals(DomainDeployForm.UPLOAD)) {
108                                 if (doUploadOperation(current, replaceExisting)) {
109                                     reportList.put(current, getReport());
110                                     resetProgress();
111                                 }
112                                 else {
113                                     createException("Another domain deployment operation is running on this server.");
114                                 }
115                             } else if (selectedAction.equals(DomainDeployForm.UPLOADDEPLOY)) {
116                                 if (doUploadDeployOperation(current, replaceExisting)) {
117                                     reportList.put(current, getReport());
118                                     resetProgress();
119                                 }
120                                 else {
121                                     createException("Another domain deployment operation is running on this server.");
122                                 }
123                             }
124                         }
125
126                         // there is no longer a deployment in progress
127
oForm.setDeploymentInProgress(false);
128                         // this current deployment has been completed
129
oForm.setDeploymentComplete(true);
130                     } catch (Throwable JavaDoc t) {
131                         oForm.setException(true);
132                         addGlobalError(t);
133                         saveErrors(p_Request, m_Errors);
134                     }
135                 }
136             }.start();
137         }
138
139         // Forward to the jsp.
140
return (p_Mapping.findForward("Domain Deploy Progress"));
141     }
142
143     private void createException(String JavaDoc message) throws OperationInProgressException {
144         throw new OperationInProgressException(message);
145     }
146
147     /**
148      * Deploy the application with name 'appName'
149      * @param appName the name of the application
150      * @return true if the operation was executed, false if the operation was
151      * rejected
152      */

153     private boolean doDeployOperation(String JavaDoc appName) {
154         ObjectName JavaDoc on = JonasAdminJmx.getJ2eeDomainObjectName();
155
156         // store the servers in a String[]
157
String JavaDoc[] serverArray = new String JavaDoc[servers.size()];
158         for (int i = 0; i < servers.size(); i++) {
159             serverArray[i] = (String JavaDoc) servers.get(i);
160         }
161
162         // define the paramters and the signature for the invocation
163
Object JavaDoc[] params = new Object JavaDoc[] {serverArray, appName};
164         String JavaDoc[] sig = new String JavaDoc[] {"[Ljava.lang.String;", "java.lang.String"};
165
166         return ((Boolean JavaDoc) JonasManagementRepr.invoke(on,
167                 getDomainDeploymentMethodName(), params, sig)).booleanValue();
168     }
169
170     /**
171      * Upload the application with name 'appName'
172      * @param appName the name of the application
173      * @param replaceExisting if the upload operation should overwrite an existing file.
174      * @return true if the operation was executed, false if the operation was
175      * rejected
176      */

177     private boolean doUploadOperation(String JavaDoc appName, boolean replaceExisting) {
178         ObjectName JavaDoc on = JonasAdminJmx.getJ2eeDomainObjectName();
179
180         // store the servers in a String[]
181
String JavaDoc[] serverArray = new String JavaDoc[servers.size()];
182         for (int i = 0; i < servers.size(); i++) {
183             serverArray[i] = (String JavaDoc) servers.get(i);
184         }
185
186         // define the paramters and the signature for the invocation
187
String JavaDoc opName = "uploadFile";
188         Object JavaDoc[] params = new Object JavaDoc[] {serverArray, appName, Boolean.valueOf(replaceExisting)};
189         String JavaDoc[] sig = new String JavaDoc[] {"[Ljava.lang.String;", "java.lang.String", "boolean"};
190
191         return ((Boolean JavaDoc) JonasManagementRepr.invoke(on,
192                 opName, params, sig)).booleanValue();
193     }
194
195     /**
196      * Upload and redeploy the application with name 'appName'
197      * @param appName the application to upload and redeploy
198      * @param replaceExisting if the upload operation should overwrite an existing file.
199      * @return true if the operation was executed, false if the operation was
200      * rejected
201      */

202     private boolean doUploadDeployOperation(String JavaDoc appName, boolean replaceExisting) {
203         ObjectName JavaDoc on = JonasAdminJmx.getJ2eeDomainObjectName();
204
205         // store the servers in a String[]
206
String JavaDoc[] serverArray = new String JavaDoc[servers.size()];
207         for (int i = 0; i < servers.size(); i++) {
208             serverArray[i] = (String JavaDoc) servers.get(i);
209         }
210
211         // define the paramters and the signature for the invocation
212
String JavaDoc opName = getDomainUploadDeployMethodName();
213         Object JavaDoc[] params = new Object JavaDoc[] {serverArray, appName, Boolean.valueOf(replaceExisting)};
214         String JavaDoc[] sig = new String JavaDoc[] {"[Ljava.lang.String;", "java.lang.String", "boolean"};
215
216         return ((Boolean JavaDoc) JonasManagementRepr.invoke(on,
217                 opName, params, sig)).booleanValue();
218     }
219
220     /**
221      * Invoke the resetProgress method of the J2EEDomain.
222      */

223     private void resetProgress() {
224         ObjectName JavaDoc on = JonasAdminJmx.getJ2eeDomainObjectName();
225         Object JavaDoc[] params = new Object JavaDoc[] {};
226         String JavaDoc[] sig = new String JavaDoc[] {};
227
228         JonasManagementRepr.invoke(on, "resetProgress", params, sig);
229     }
230
231     /**
232      * Get the report for the deployment operation from J2EEDomain.
233      * @return a HashMap containing the results of the deployment of an application
234      * on each of the selected servers.
235      */

236     private HashMap JavaDoc getReport() {
237         ObjectName JavaDoc on = JonasAdminJmx.getJ2eeDomainObjectName();
238         HashMap JavaDoc report = (HashMap JavaDoc) JonasManagementRepr.getAttribute(on,
239                 "progressReport");
240
241         return report;
242     }
243
244     /**
245      * A custom thread class used for doing the deployment operations.
246      */

247     protected class DeployThread extends Thread JavaDoc {
248
249         protected ActionMapping p_Mapping;
250
251         protected ActionForm p_Form;
252
253         protected HttpServletRequest JavaDoc p_Request;
254
255         protected HttpServletResponse JavaDoc p_Response;
256
257         public DeployThread(ActionMapping p_Mapping, ActionForm p_Form,
258                 HttpServletRequest JavaDoc p_Request, HttpServletResponse JavaDoc p_Response) {
259             this.p_Mapping = p_Mapping;
260             this.p_Form = p_Form;
261             this.p_Request = p_Request;
262             this.p_Response = p_Response;
263         }
264
265     }
266
267
268 }
269
Popular Tags