001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ApplyDomainDeployConfirmAction.java 9767 2006-10-19 15:12:09Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.deploy;
025:
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.TreeMap;
032:
033: import javax.management.MalformedObjectNameException;
034: import javax.management.ObjectName;
035: import javax.servlet.ServletException;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import org.apache.struts.action.ActionForm;
040: import org.apache.struts.action.ActionForward;
041: import org.apache.struts.action.ActionMapping;
042: import org.objectweb.jonas.jmx.JonasManagementRepr;
043: import org.objectweb.jonas.jmx.JonasObjectName;
044: import org.objectweb.jonas.management.j2eemanagement.J2EEDomain;
045: import org.objectweb.jonas.management.monitoring.DomainMonitor;
046:
047: /**
048: * @author Patrick Smith
049: * @author Gregory Lapouchnian
050: * @author Adriana Danes
051: */
052: public class ApplyDomainDeployConfirmAction extends BaseDeployAction {
053:
054: // --------------------------------------------------------- Public Methods
055:
056: public ActionForward executeAction(ActionMapping p_Mapping,
057: ActionForm p_Form, HttpServletRequest p_Request,
058: HttpServletResponse p_Response) throws IOException,
059: ServletException {
060:
061: String currentServerName = m_WhereAreYou
062: .getCurrentJonasServerName();
063:
064: DomainDeployForm oForm = (DomainDeployForm) p_Form;
065: boolean deploymentInProgress = false;
066: Map reports = new TreeMap();
067: try {
068: DomainMonitor dm = J2EEDomain.getInstance()
069: .getDomainMonitor();
070: if (dm == null) {
071: // TO DO
072: // normally this is not possible as this is a master
073: }
074: // apps to be deployed on the selected targets
075: ArrayList alApps = oForm.getListDeploy();
076: for (int i = 0; i < alApps.size(); i++) {
077: String app = (String) alApps.get(i);
078: if (!oForm.getDeploymentInProgress()) {
079: String selectedAction = oForm.getSelectedAction();
080: boolean replaceExisting = oForm
081: .getReplacementOption();
082: boolean autoload = oForm.isAutoload();
083: ArrayList selectedTargets = oForm
084: .getListTargetsSelected();
085: doSelectedOperation(app, selectedAction,
086: replaceExisting, autoload, selectedTargets,
087: currentServerName);
088: }
089: Map appReports = new HashMap();
090: reports.put(app, appReports);
091: // Get effective target server list
092: String[] targets = dm.getDeployServers(app);
093: for (int j = 0; j < targets.length; j++) {
094: String target = targets[j];
095: String status = dm.getDeployState(app, target);
096: if (status.startsWith("IN PROGR")) {
097: deploymentInProgress = true;
098: }
099: if (status.startsWith("FAIL")) {
100: String error = getError(app, target,
101: currentServerName);
102: status = status + error;
103: }
104: appReports.put(target, status);
105: }
106: }
107: oForm.setDeploymentInProgress(deploymentInProgress);
108: if (!deploymentInProgress) {
109: oForm.setDeploymentCompleted(true);
110: resetReport(currentServerName);
111: }
112: oForm.setReports(reports);
113: } catch (Throwable t) {
114: oForm.setException(true);
115: addGlobalError(t);
116: saveErrors(p_Request, m_Errors);
117: }
118: // Forward to the jsp.
119: return (p_Mapping.findForward("Domain Deploy Progress"));
120: }
121:
122: private String getError(String app, String target, String serverName)
123: throws MalformedObjectNameException {
124: ObjectName on = JonasObjectName.domainMonitor();
125: String opName = "getErrorMessage";
126: Object[] params = new Object[] { app, target };
127: String[] sig = new String[] { "java.lang.String",
128: "java.lang.String" };
129: return (String) JonasManagementRepr.invoke(on, opName, params,
130: sig, serverName);
131: }
132:
133: /**
134: * A custom thread class used for doing the deployment operations.
135: */
136: protected class DeployThread extends Thread {
137:
138: protected ActionMapping p_Mapping;
139:
140: protected ActionForm p_Form;
141:
142: protected HttpServletRequest p_Request;
143:
144: protected HttpServletResponse p_Response;
145:
146: public DeployThread(ActionMapping p_Mapping, ActionForm p_Form,
147: HttpServletRequest p_Request,
148: HttpServletResponse p_Response) {
149: this .p_Mapping = p_Mapping;
150: this .p_Form = p_Form;
151: this .p_Request = p_Request;
152: this .p_Response = p_Response;
153: }
154:
155: }
156:
157: /**
158: * Deploy the module with name 'appName' on the target
159: * The operation to invoke has the same name: 'deployModule' for both
160: * ServerProxy or LogicalCluster.
161: * @param appName the name of the application
162: * @param on the ObjectNamer of the target (ServerProxy if server, LogicalCluster if cluster)
163: * rejected
164: */
165: private void doDeployOperation(String appName, ObjectName on,
166: String serverName) {
167: String opName = "deployModule";
168: Object[] params = new Object[] { appName };
169: String[] sig = new String[] { "java.lang.String" };
170: JonasManagementRepr.invoke(on, opName, params, sig, serverName);
171: }
172:
173: /**
174: * Undeploy the module with name 'appName' from the target
175: * The operation to invoke has the same name: 'undeployModule' for both
176: * ServerProxy or LogicalCluster.
177: * @param appName the name of the application
178: * @param on the ObjectNamer of the target (ServerProxy if server, LogicalCluster if cluster)
179: * rejected
180: */
181: private void doUndeployOperation(String appName, ObjectName on,
182: String serverName) {
183: String opName = "undeployModule";
184: Object[] params = new Object[] { appName };
185: String[] sig = new String[] { "java.lang.String" };
186: JonasManagementRepr.invoke(on, opName, params, sig, serverName);
187: }
188:
189: /**
190: * Upload the module with name 'appName' on the target
191: * The operation to invoke has the same name: 'uploadFile' for both
192: * ServerProxy or LogicalCluster.
193: * @param appName the name of the application
194: * @param on the ObjectNamer of the target (ServerProxy if server, LogicalCluster if cluster)
195: * rejected
196: */
197: private void doUploadOperation(String appName,
198: boolean replaceExisting, boolean autoload, ObjectName on,
199: String serverName) {
200: if (autoload) {
201: //System.out.println("Should call uploadFile with autoload option");
202: }
203: String opName = "uploadFile";
204: Object[] params = new Object[] { appName,
205: new Boolean(replaceExisting) };
206: String[] sig = new String[] { "java.lang.String", "boolean" };
207: JonasManagementRepr.invoke(on, opName, params, sig, serverName);
208: }
209:
210: /**
211: * Upload and reploy the module with name 'appName' on the target
212: * The operation to invoke has the same name: 'uploadDeployModule' for both
213: * ServerProxy or LogicalCluster.
214: * @param appName the name of the application
215: * @param on the ObjectNamer of the target (ServerProxy if server, LogicalCluster if cluster)
216: * rejected
217: */
218: private void doUploadDeployOperation(String appName,
219: boolean replaceExisting, boolean autoload, ObjectName on,
220: String serverName) {
221: if (autoload) {
222: //System.out.println("Should call uploadDeployModule with autoload option");
223: }
224: String opName = "uploadDeployModule";
225: Object[] params = new Object[] { appName,
226: new Boolean(replaceExisting) };
227: String[] sig = new String[] { "java.lang.String", "boolean" };
228: JonasManagementRepr.invoke(on, opName, params, sig, serverName);
229: }
230:
231: private void resetReport(String serverName)
232: throws MalformedObjectNameException {
233: ObjectName on = JonasObjectName.domainMonitor();
234: String opName = "forgetAllDeploy";
235: JonasManagementRepr.invoke(on, opName, null, null, serverName);
236: }
237:
238: /**
239: * Do selected deployment action with the application named 'appName' on the selected targets
240: * @param appName the name of the application
241: * @param action action to do
242: * @param replaceExisting true if must replace the file in case of a 'distribute' operation
243: * @param targets the targets to deployment on
244: * @param currentServerName the currently managed server
245: */
246: private void doSelectedOperation(String appName, String action,
247: boolean replaceExisting, boolean autoload,
248: ArrayList targets, String currentServerName)
249: throws Exception {
250: Iterator it = targets.iterator();
251: while (it.hasNext()) {
252: ObjectName targetOn = ObjectName.getInstance((String) it
253: .next());
254: String j2eeType = targetOn.getKeyProperty("j2eeType");
255: String serverName = null;
256: if ("J2EEServer".equals(j2eeType)) {
257: // The target server
258: serverName = targetOn.getKeyProperty("name");
259: targetOn = JonasObjectName.serverProxy(serverName);
260: }
261: if (action.equals(DomainDeployForm.DEPLOY)) {
262: /*result = */doDeployOperation(appName, targetOn,
263: currentServerName);
264: } else if (action.equals(DomainDeployForm.UPLOAD)) {
265: /*result = */doUploadOperation(appName,
266: replaceExisting, autoload, targetOn,
267: currentServerName);
268: } else if (action.equals(DomainDeployForm.UPLOADDEPLOY)) {
269: /*result = */doUploadDeployOperation(appName,
270: replaceExisting, autoload, targetOn,
271: currentServerName);
272: } else if (action.equals(DomainDeployForm.UNDEPLOY)) {
273: /*result = */doUndeployOperation(appName, targetOn,
274: currentServerName);
275: }
276: }
277: }
278: }
|