1 11 package org.eclipse.ui.internal.ide; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.OutputStream ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Properties ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IConfigurationElement; 24 import org.eclipse.core.runtime.IExecutableExtension; 25 import org.eclipse.core.runtime.IPlatformRunnable; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.jface.dialogs.MessageDialog; 30 import org.eclipse.osgi.service.datalocation.Location; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.swt.widgets.MessageBox; 35 import org.eclipse.swt.widgets.Shell; 36 import org.eclipse.ui.PlatformUI; 37 38 43 public class IDEApplication implements IPlatformRunnable, IExecutableExtension { 44 45 48 public static final String METADATA_FOLDER = ".metadata"; 50 private static final String VERSION_FILENAME = "version.ini"; 52 private static final String WORKSPACE_VERSION_KEY = "org.eclipse.core.runtime"; 54 private static final String WORKSPACE_VERSION_VALUE = "1"; 56 private static final String PROP_EXIT_CODE = "eclipse.exitcode"; 58 62 private static final Integer EXIT_RELAUNCH = new Integer (24); 63 64 67 public IDEApplication() { 68 } 70 71 74 public Object run(Object args) throws Exception { 75 Display display = createDisplay(); 76 77 try { 78 Shell shell = new Shell(display, SWT.ON_TOP); 79 80 try { 81 if (!checkInstanceLocation(shell)) { 82 Platform.endSplash(); 83 return EXIT_OK; 84 } 85 } finally { 86 if (shell != null) { 87 shell.dispose(); 88 } 89 } 90 91 int returnCode = PlatformUI.createAndRunWorkbench(display, 96 new IDEWorkbenchAdvisor()); 97 98 if (returnCode != PlatformUI.RETURN_RESTART) { 102 return EXIT_OK; 103 } 104 105 return EXIT_RELAUNCH.equals(Integer.getInteger(PROP_EXIT_CODE)) ? EXIT_RELAUNCH 108 : EXIT_RESTART; 109 } finally { 110 if (display != null) { 111 display.dispose(); 112 } 113 } 114 } 115 116 121 protected Display createDisplay() { 122 return PlatformUI.createDisplay(); 123 } 124 125 128 public void setInitializationData(IConfigurationElement config, 129 String propertyName, Object data) throws CoreException { 130 } 132 133 140 private boolean checkInstanceLocation(Shell shell) { 141 Location instanceLoc = Platform.getInstanceLocation(); 143 if (instanceLoc == null) { 144 MessageDialog 145 .openError( 146 shell, 147 IDEWorkbenchMessages.IDEApplication_workspaceMandatoryTitle, 148 IDEWorkbenchMessages.IDEApplication_workspaceMandatoryMessage); 149 return false; 150 } 151 152 if (instanceLoc.isSet()) { 154 if (!checkValidWorkspace(shell, instanceLoc.getURL())) { 157 return false; 158 } 159 160 try { 163 if (instanceLoc.lock()) { 164 writeWorkspaceVersion(); 165 return true; 166 } 167 MessageDialog 168 .openError( 169 shell, 170 IDEWorkbenchMessages.IDEApplication_workspaceCannotLockTitle, 171 IDEWorkbenchMessages.IDEApplication_workspaceCannotLockMessage); 172 } catch (IOException e) { 173 IDEWorkbenchPlugin.log("Could not obtain lock for workspace location", e); 175 MessageDialog 176 .openError( 177 shell, 178 IDEWorkbenchMessages.InternalError, 179 e.getMessage()); 180 } 181 return false; 182 } 183 184 ChooseWorkspaceData launchData = new ChooseWorkspaceData(instanceLoc 186 .getDefault()); 187 188 boolean force = false; 189 while (true) { 190 URL workspaceUrl = promptForWorkspace(shell, launchData, force); 191 if (workspaceUrl == null) { 192 return false; 193 } 194 195 force = true; 198 199 try { 200 if (instanceLoc.setURL(workspaceUrl, true)) { 203 launchData.writePersistedData(); 204 writeWorkspaceVersion(); 205 return true; 206 } 207 } catch (IllegalStateException e) { 208 MessageDialog 209 .openError( 210 shell, 211 IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle, 212 IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage); 213 return false; 214 } 215 216 MessageDialog.openError(shell, IDEWorkbenchMessages.IDEApplication_workspaceInUseTitle, 219 IDEWorkbenchMessages.IDEApplication_workspaceInUseMessage); 220 } 221 } 222 223 237 private URL promptForWorkspace(Shell shell, ChooseWorkspaceData launchData, 238 boolean force) { 239 URL url = null; 240 do { 241 new ChooseWorkspaceDialog(null, launchData, false, true).prompt(force); 244 String instancePath = launchData.getSelection(); 245 if (instancePath == null) { 246 return null; 247 } 248 249 force = true; 253 254 if (instancePath.length() <= 0) { 256 MessageDialog 257 .openError( 258 shell, 259 IDEWorkbenchMessages.IDEApplication_workspaceEmptyTitle, 260 IDEWorkbenchMessages.IDEApplication_workspaceEmptyMessage); 261 continue; 262 } 263 264 File workspace = new File (instancePath); 266 if (!workspace.exists()) { 267 workspace.mkdir(); 268 } 269 270 try { 271 String path = workspace.getAbsolutePath().replace( 274 File.separatorChar, '/'); 275 url = new URL ("file", null, path); } catch (MalformedURLException e) { 277 MessageDialog 278 .openError( 279 shell, 280 IDEWorkbenchMessages.IDEApplication_workspaceInvalidTitle, 281 IDEWorkbenchMessages.IDEApplication_workspaceInvalidMessage); 282 continue; 283 } 284 } while (!checkValidWorkspace(shell, url)); 285 286 return url; 287 } 288 289 298 private boolean checkValidWorkspace(Shell shell, URL url) { 299 if (url == null) { 301 return false; 302 } 303 304 String version = readWorkspaceVersion(url); 305 306 if (version == null) { 310 return true; 311 } 312 313 final int ide_version = Integer.parseInt(WORKSPACE_VERSION_VALUE); 314 int workspace_version = Integer.parseInt(version); 315 316 if (workspace_version == ide_version) { 319 return true; 320 } 321 322 String title = IDEWorkbenchMessages.IDEApplication_versionTitle; 326 String message = NLS.bind(IDEWorkbenchMessages.IDEApplication_versionMessage, url.getFile()); 327 328 MessageBox mbox = new MessageBox(shell, SWT.OK | SWT.CANCEL 329 | SWT.ICON_WARNING | SWT.APPLICATION_MODAL); 330 mbox.setText(title); 331 mbox.setMessage(message); 332 return mbox.open() == SWT.OK; 333 } 334 335 339 private static String readWorkspaceVersion(URL workspace) { 340 File versionFile = getVersionFile(workspace, false); 341 if (versionFile == null || !versionFile.exists()) { 342 return null; 343 } 344 345 try { 346 Properties props = new Properties (); 350 FileInputStream is = new FileInputStream (versionFile); 351 try { 352 props.load(is); 353 } finally { 354 is.close(); 355 } 356 357 return props.getProperty(WORKSPACE_VERSION_KEY); 358 } catch (IOException e) { 359 IDEWorkbenchPlugin.log("Could not read version file", new Status( IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 361 IStatus.ERROR, 362 e.getMessage() == null ? "" : e.getMessage(), e)); 364 return null; 365 } 366 } 367 368 373 private static void writeWorkspaceVersion() { 374 Location instanceLoc = Platform.getInstanceLocation(); 375 if (instanceLoc == null || instanceLoc.isReadOnly()) { 376 return; 377 } 378 379 File versionFile = getVersionFile(instanceLoc.getURL(), true); 380 if (versionFile == null) { 381 return; 382 } 383 384 OutputStream output = null; 385 try { 386 String versionLine = WORKSPACE_VERSION_KEY + '=' 387 + WORKSPACE_VERSION_VALUE; 388 389 output = new FileOutputStream (versionFile); 390 output.write(versionLine.getBytes("UTF-8")); } catch (IOException e) { 392 IDEWorkbenchPlugin.log("Could not write version file", StatusUtil.newStatus(IStatus.ERROR, e.getMessage(), e)); 394 } finally { 395 try { 396 if (output != null) { 397 output.close(); 398 } 399 } catch (IOException e) { 400 } 402 } 403 } 404 405 416 private static File getVersionFile(URL workspaceUrl, boolean create) { 417 if (workspaceUrl == null) { 418 return null; 419 } 420 421 try { 422 File metaDir = new File (workspaceUrl.getPath(), METADATA_FOLDER); 424 if (!metaDir.exists() && (!create || !metaDir.mkdir())) { 425 return null; 426 } 427 428 File versionFile = new File (metaDir, VERSION_FILENAME); 430 if (!versionFile.exists() 431 && (!create || !versionFile.createNewFile())) { 432 return null; 433 } 434 435 return versionFile; 436 } catch (IOException e) { 437 return null; 439 } 440 } 441 } 442 | Popular Tags |