lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
lisaacs | d4abc34 | 2008-02-23 00:18:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2007, 2008 SAS Institute, Inc. and others. |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Larry Isaacs - Initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.jst.server.tomcat.ui.internal; |
| 12 | |
| 13 | import org.eclipse.core.runtime.CoreException; |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 14 | import org.eclipse.core.runtime.IProgressMonitor; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 15 | import org.eclipse.core.runtime.IStatus; |
| 16 | import org.eclipse.core.runtime.MultiStatus; |
| 17 | import org.eclipse.core.runtime.Status; |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 18 | import org.eclipse.core.runtime.jobs.IJobChangeEvent; |
| 19 | import org.eclipse.core.runtime.jobs.Job; |
| 20 | import org.eclipse.core.runtime.jobs.JobChangeAdapter; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 21 | import org.eclipse.jface.dialogs.Dialog; |
| 22 | import org.eclipse.jface.dialogs.IDialogConstants; |
| 23 | import org.eclipse.jst.server.core.IWebModule; |
| 24 | import org.eclipse.jst.server.tomcat.core.internal.ITomcatWebModule; |
| 25 | import org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour; |
| 26 | import org.eclipse.jst.server.tomcat.core.internal.WebModule; |
| 27 | import org.eclipse.osgi.util.NLS; |
| 28 | import org.eclipse.swt.SWT; |
| 29 | import org.eclipse.swt.layout.GridData; |
| 30 | import org.eclipse.swt.widgets.Button; |
| 31 | import org.eclipse.swt.widgets.Composite; |
| 32 | import org.eclipse.swt.widgets.Control; |
| 33 | import org.eclipse.swt.widgets.Label; |
| 34 | import org.eclipse.swt.widgets.Shell; |
lisaacs | e9d0841 | 2007-01-24 23:40:28 +0000 | [diff] [blame] | 35 | import org.eclipse.ui.PlatformUI; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 36 | import org.eclipse.wst.server.core.IModule; |
| 37 | import org.eclipse.wst.server.core.IServer; |
lisaacs | e9d0841 | 2007-01-24 23:40:28 +0000 | [diff] [blame] | 38 | import org.eclipse.wst.server.core.ServerUtil; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 39 | import org.eclipse.wst.server.core.IServer.IOperationListener; |
| 40 | |
| 41 | /** |
| 42 | * Dialog to confirm deletion of the work directory for a module on a |
| 43 | * server, or the work directory for the entire server. Handling |
| 44 | * includes stopping and restarting the server if it is running at |
| 45 | * the time of the deletion. |
| 46 | * |
| 47 | */ |
| 48 | public class CleanWorkDirDialog extends Dialog { |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 49 | protected IServer server; |
| 50 | protected IModule module; |
| 51 | protected int state; |
| 52 | protected String mode; |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 53 | protected IStatus completionStatus; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Creates a dialog instance confirm deletion of the work directory for a |
| 57 | * module on a server, or the work directory for the entire server. |
| 58 | * |
| 59 | * @param parentShell the parent shell, or <code>null</code> to create a |
| 60 | * top-level shell |
| 61 | * @param server server on which to delete the work directory |
| 62 | * @param module module whose work directory is to be deleted, or <code>null</code> if |
| 63 | * if these server's entire work directory is to be deleted. |
| 64 | */ |
| 65 | public CleanWorkDirDialog(Shell parentShell, IServer server, IModule module) { |
| 66 | super(parentShell); |
| 67 | |
| 68 | if (server == null) |
| 69 | throw new IllegalArgumentException(); |
| 70 | |
| 71 | this.server = server; |
| 72 | this.module = module; |
| 73 | |
| 74 | } |
| 75 | |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 76 | protected void configureShell(Shell newShell) { |
| 77 | super.configureShell(newShell); |
| 78 | newShell.setText(Messages.confirmCleanWorkDirTitle); |
| 79 | } |
| 80 | |
| 81 | protected Control createDialogArea(Composite parent) { |
| 82 | if (state < 0 || state == IServer.STATE_UNKNOWN) |
| 83 | captureServerState(); |
| 84 | |
| 85 | // create a composite with standard margins and spacing |
| 86 | Composite composite = (Composite)super.createDialogArea(parent); |
lisaacs | e9d0841 | 2007-01-24 23:40:28 +0000 | [diff] [blame] | 87 | // Since there are only label widgets on this page, set the help on the parent |
| 88 | PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, ContextIds.SERVER_CLEAN_WORK_DIR); |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 89 | |
| 90 | Label label = new Label(composite, SWT.WRAP); |
| 91 | if (state == IServer.STATE_STARTING || state == IServer.STATE_STOPPING || state == IServer.STATE_UNKNOWN) { |
| 92 | label.setText(NLS.bind(Messages.cleanServerStateChanging, server.getName())); |
| 93 | } |
| 94 | else { |
| 95 | if (module != null) |
| 96 | label.setText(NLS.bind(Messages.cleanModuleWorkDir, module.getName(), server.getName())); |
| 97 | else |
| 98 | label.setText(NLS.bind(Messages.cleanServerWorkDir, server.getName())); |
| 99 | GridData data = new GridData(); |
| 100 | data.widthHint = 300; |
| 101 | label.setLayoutData(data); |
| 102 | |
| 103 | if (state == IServer.STATE_STARTED) { |
| 104 | label = new Label(composite, SWT.WRAP); |
| 105 | label.setText(Messages.cleanServerRunning); |
| 106 | data = new GridData(); |
| 107 | data.widthHint = 300; |
| 108 | label.setLayoutData(data); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | applyDialogFont(composite); |
| 113 | return composite; |
| 114 | } |
| 115 | |
| 116 | protected void createButtonsForButtonBar(Composite parent) { |
| 117 | super.createButtonsForButtonBar(parent); |
| 118 | |
| 119 | if (state < 0 || state == IServer.STATE_UNKNOWN) |
| 120 | captureServerState(); |
| 121 | |
| 122 | // If server is transitioning, only allow Cancel |
| 123 | if (state == IServer.STATE_STARTING || state == IServer.STATE_STOPPING) { |
| 124 | Button button = getButton(IDialogConstants.OK_ID); |
| 125 | if (button != null) |
| 126 | button.setEnabled(false); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | protected void okPressed() { |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 131 | String jobName = NLS.bind(Messages.cleanServerTask, |
| 132 | module != null ? module.getName() : server.getName()); |
| 133 | // Create job to perform the cleaning, including stopping and starting the server if necessary |
| 134 | CleanWorkDirJob job = new CleanWorkDirJob(jobName); |
| 135 | // Note: Since stop and start, if needed, will set scheduling rules in their jobs, |
| 136 | // don't set one here. Instead do the actual deletion in a child job too with the |
| 137 | // scheduling rule on that job, like stop and start. |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 138 | job.schedule(); |
| 139 | |
| 140 | super.okPressed(); |
| 141 | } |
| 142 | |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 143 | /* |
| 144 | * Job to clean the appropriate Tomcat work directory. It includes |
| 145 | * stopping and starting the server if the server is currently running. |
| 146 | * The stopping, deletion, and starting are all done with child jobs, |
| 147 | * each using the server scheduling rule. Thus, this job should |
| 148 | * not use this rule or it will block these child jobs. |
| 149 | */ |
| 150 | class CleanWorkDirJob extends Job { |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 151 | /** |
| 152 | * @param name name for job |
| 153 | */ |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 154 | public CleanWorkDirJob(String jobName) { |
| 155 | super(jobName); |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 156 | } |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 157 | |
lisaacs | 3e5cf8a | 2007-02-10 20:37:23 +0000 | [diff] [blame] | 158 | /** |
| 159 | * @see Job#belongsTo(Object) |
| 160 | */ |
| 161 | public boolean belongsTo(Object family) { |
| 162 | return ServerUtil.SERVER_JOB_FAMILY.equals(family); |
| 163 | } |
| 164 | |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 165 | protected IStatus run(IProgressMonitor monitor) { |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 166 | final Object mutex = new Object(); |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 167 | |
| 168 | IWebModule webModule = null; |
| 169 | if (module != null) { |
| 170 | webModule = (IWebModule)module.loadAdapter(IWebModule.class, null); |
| 171 | if (webModule == null) { |
| 172 | return newErrorStatus(NLS.bind(Messages.errorCantIdentifyWebApp, module.getName()), null); |
| 173 | } |
| 174 | } |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 175 | |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 176 | // If state has changed since dialog was open, abort |
| 177 | if (server.getServerState() != state) { |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 178 | return newErrorStatus( |
| 179 | NLS.bind(Messages.errorCouldNotCleanStateChange, server.getName()), null); |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | IOperationListener listener = new IOperationListener() { |
| 183 | public void done(IStatus result) { |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 184 | synchronized (mutex) { |
| 185 | completionStatus = result; |
| 186 | mutex.notifyAll(); |
| 187 | } |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 188 | } |
| 189 | }; |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 190 | |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 191 | boolean restart = false; |
| 192 | IStatus status = Status.OK_STATUS; |
| 193 | // If server isn't stopped, try to stop, clean, and restart |
| 194 | if (state != IServer.STATE_STOPPED) { |
| 195 | status = server.canStop(); |
| 196 | if (!status.isOK()) { |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 197 | return wrapErrorStatus(status, |
| 198 | NLS.bind(Messages.errorCouldNotCleanCantStop, server.getName())); |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 199 | } |
| 200 | |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 201 | boolean done = false; |
| 202 | boolean force = false; |
| 203 | while (!done) { |
| 204 | // Stop the server and wait for completion |
| 205 | synchronized (mutex) { |
| 206 | server.stop(force, listener); |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 207 | |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 208 | while (completionStatus == null) { |
| 209 | try { |
| 210 | mutex.wait(); |
| 211 | } catch (InterruptedException e) { |
| 212 | // Ignore |
| 213 | } |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 216 | // If forced, or there was an error (doesn't include timeout), or we are stopped, time to exit |
| 217 | if (force || !completionStatus.isOK() || server.getServerState() == IServer.STATE_STOPPED) { |
| 218 | done = true; |
| 219 | } |
| 220 | else { |
| 221 | force = TomcatUIPlugin.queryCleanTermination(server); |
| 222 | completionStatus = null; |
| 223 | } |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 224 | } |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 225 | |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 226 | if (!completionStatus.isOK()) { |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 227 | // If stop job failed, assume error was displayed for that job |
| 228 | return Status.OK_STATUS; |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 229 | } |
| 230 | if (server.getServerState() != IServer.STATE_STOPPED) { |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 231 | return newErrorStatus( |
| 232 | NLS.bind(Messages.errorCouldNotCleanStopFailed, server.getName()), null); |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 233 | } |
| 234 | restart = true; |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 235 | completionStatus = null; |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 236 | } |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 237 | |
| 238 | DeleteWorkDirJob deleteJob = new DeleteWorkDirJob(getName(), webModule, restart); |
| 239 | deleteJob.setRule(ServerUtil.getServerSchedulingRule(server)); |
| 240 | |
| 241 | deleteJob.addJobChangeListener(new JobChangeAdapter() { |
| 242 | public void done(IJobChangeEvent event) { |
| 243 | synchronized (mutex) { |
| 244 | completionStatus = event.getResult(); |
| 245 | mutex.notifyAll(); |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 246 | } |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 247 | |
| 248 | } |
| 249 | }); |
| 250 | |
| 251 | // Perform the work directory deletion job |
| 252 | synchronized (mutex) { |
| 253 | deleteJob.schedule(); |
| 254 | |
| 255 | while (completionStatus == null) { |
| 256 | try { |
| 257 | mutex.wait(); |
| 258 | } catch (InterruptedException e) { |
| 259 | // Ignore |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 260 | } |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 261 | } |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 262 | } |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 263 | if (!completionStatus.isOK()) { |
| 264 | // If delete job failed, assume error was displayed for that job |
| 265 | return Status.OK_STATUS; |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 266 | } |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 267 | completionStatus = null; |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 268 | |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 269 | if (restart) { |
| 270 | status = server.canStart(mode); |
| 271 | if (!status.isOK()) { |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 272 | return wrapErrorStatus(status, |
| 273 | NLS.bind(Messages.errorCleanCantRestart, server.getName())); |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 274 | } |
lisaacs | 465f2c2 | 2008-02-19 21:16:58 +0000 | [diff] [blame] | 275 | |
| 276 | // Restart the server and wait for completion |
| 277 | synchronized (mutex) { |
| 278 | server.start(mode, listener); |
| 279 | |
| 280 | while (completionStatus == null) { |
| 281 | try { |
| 282 | mutex.wait(); |
| 283 | } catch (InterruptedException e) { |
| 284 | // Ignore |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 289 | if (!completionStatus.isOK()) { |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 290 | // If start job failed, assume error was displayed for that job |
| 291 | return Status.OK_STATUS; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 294 | return status; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 295 | } |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 296 | } |
| 297 | |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 298 | /* |
| 299 | * Job to actually delete the work directory. This is done |
| 300 | * in a separate job so it can be a "sibling" of potential |
| 301 | * stop and start jobs. This allows it to have a server |
| 302 | * scheduling rule. |
| 303 | */ |
| 304 | class DeleteWorkDirJob extends Job { |
| 305 | private IWebModule webModule; |
| 306 | private boolean restart; |
| 307 | |
| 308 | /** |
| 309 | * @param name name for job |
| 310 | */ |
| 311 | public DeleteWorkDirJob(String jobName, IWebModule webModule, boolean restart) { |
| 312 | super(jobName); |
| 313 | this.webModule = webModule; |
| 314 | this.restart = restart; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @see Job#belongsTo(Object) |
| 319 | */ |
| 320 | public boolean belongsTo(Object family) { |
| 321 | return ServerUtil.SERVER_JOB_FAMILY.equals(family); |
| 322 | } |
| 323 | |
| 324 | protected IStatus run(IProgressMonitor monitor) { |
| 325 | |
| 326 | IStatus status = Status.OK_STATUS; |
| 327 | // If server isn't stopped, abort the attempt to delete the work directory |
| 328 | if (server.getServerState() != IServer.STATE_STOPPED) { |
| 329 | return newErrorStatus( |
| 330 | NLS.bind(Messages.errorCantDeleteServerNotStopped, |
| 331 | webModule != null ? module.getName() : server.getName()), null); |
| 332 | } |
| 333 | |
| 334 | // Delete the work directory |
| 335 | TomcatServerBehaviour tsb = (TomcatServerBehaviour)server.loadAdapter( |
| 336 | TomcatServerBehaviour.class, monitor); |
| 337 | try { |
| 338 | if (webModule != null) { |
| 339 | ITomcatWebModule tcWebModule = new WebModule(webModule.getContextRoot(), "", "", true); |
| 340 | status = tsb.cleanContextWorkDir(tcWebModule, null); |
| 341 | } |
| 342 | else { |
| 343 | status = tsb.cleanServerWorkDir(null); |
| 344 | } |
| 345 | } catch (CoreException ce) { |
| 346 | status = ce.getStatus(); |
| 347 | } |
| 348 | if (!status.isOK()) { |
| 349 | String cleanName = module != null ? module.getName() : server.getName(); |
| 350 | return wrapErrorStatus(status, |
| 351 | restart ? |
| 352 | NLS.bind(Messages.errorErrorDuringCleanWasRunning, |
| 353 | cleanName , server.getName()) : |
| 354 | NLS.bind(Messages.errorErrorDuringClean, cleanName)); |
| 355 | } |
| 356 | return status; |
| 357 | } |
| 358 | } |
| 359 | |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 360 | private void captureServerState() { |
| 361 | state = server.getServerState(); |
| 362 | if (state != IServer.STATE_STOPPED) { |
| 363 | mode = server.getMode(); |
| 364 | } |
| 365 | } |
| 366 | |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 367 | protected IStatus newErrorStatus(String message, Throwable throwable) { |
| 368 | return new Status(IStatus.ERROR, TomcatUIPlugin.PLUGIN_ID, 0, |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 369 | message, throwable); |
| 370 | } |
| 371 | |
lisaacs | 8832d44 | 2008-04-24 01:04:51 +0000 | [diff] [blame] | 372 | protected IStatus wrapErrorStatus(IStatus status, String message) { |
| 373 | MultiStatus ms = new MultiStatus(TomcatUIPlugin.PLUGIN_ID, 0, message, null); |
lisaacs | 24d02bb | 2007-01-21 03:06:17 +0000 | [diff] [blame] | 374 | ms.add(status); |
| 375 | return ms; |
lisaacs | 6d01705 | 2007-01-16 03:01:58 +0000 | [diff] [blame] | 376 | } |
| 377 | } |