1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.lang.reflect.InvocationTargetException ; 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Properties ; 22 import java.util.Set ; 23 import java.util.Stack ; 24 import java.util.StringTokenizer ; 25 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.resources.IResource; 28 import org.eclipse.core.resources.ResourcesPlugin; 29 import org.eclipse.core.runtime.CoreException; 30 import org.eclipse.core.runtime.IPath; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 import org.eclipse.core.runtime.IStatus; 33 import org.eclipse.core.runtime.Path; 34 import org.eclipse.core.runtime.Status; 35 import org.eclipse.debug.core.ILaunchConfiguration; 36 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 37 import org.eclipse.jdt.core.IClasspathEntry; 38 import org.eclipse.jdt.core.IJavaProject; 39 import org.eclipse.jdt.core.JavaCore; 40 import org.eclipse.jdt.core.JavaModelException; 41 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 42 import org.eclipse.jface.dialogs.DialogSettings; 43 import org.eclipse.jface.dialogs.IDialogConstants; 44 import org.eclipse.jface.dialogs.IDialogSettings; 45 import org.eclipse.jface.dialogs.MessageDialog; 46 import org.eclipse.jface.preference.IPreferenceStore; 47 import org.eclipse.osgi.util.NLS; 48 import org.eclipse.pde.core.plugin.IPluginModelBase; 49 import org.eclipse.pde.core.plugin.PluginRegistry; 50 import org.eclipse.pde.internal.core.DependencyManager; 51 import org.eclipse.pde.internal.core.PDECore; 52 import org.eclipse.pde.internal.core.WorkspaceModelManager; 53 import org.eclipse.pde.internal.core.util.CoreUtility; 54 import org.eclipse.pde.internal.ui.IPDEUIConstants; 55 import org.eclipse.pde.internal.ui.IPreferenceConstants; 56 import org.eclipse.pde.internal.ui.PDEPlugin; 57 import org.eclipse.pde.internal.ui.PDEUIMessages; 58 import org.eclipse.pde.internal.ui.wizards.tools.IOrganizeManifestsSettings; 59 import org.eclipse.pde.internal.ui.wizards.tools.OrganizeManifestsOperation; 60 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 61 import org.eclipse.swt.widgets.Display; 62 import org.eclipse.swt.widgets.Shell; 63 import org.eclipse.ui.IWorkbenchWindow; 64 65 public class LauncherUtils { 66 67 private static final String TIMESTAMP = "timestamp"; private static final String FILE_NAME = "dep-timestamp.properties"; private static Properties fLastRun; 70 71 public static Display getDisplay() { 72 Display display = Display.getCurrent(); 73 if (display == null) { 74 display = Display.getDefault(); 75 } 76 return display; 77 } 78 79 public final static Shell getActiveShell() { 80 IWorkbenchWindow window = 81 PDEPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow(); 82 if (window == null) { 83 IWorkbenchWindow[] windows = PDEPlugin.getDefault().getWorkbench().getWorkbenchWindows(); 84 if (windows.length > 0) 85 return windows[0].getShell(); 86 } else 87 return window.getShell(); 88 return getDisplay().getActiveShell(); 89 } 90 91 public static boolean clearWorkspace(ILaunchConfiguration configuration, 92 String workspace, IProgressMonitor monitor) throws CoreException { 93 if (workspace.length() == 0) { 98 monitor.done(); 99 return true; 100 } 101 102 File workspaceFile = new Path(workspace).toFile().getAbsoluteFile(); 103 if (configuration.getAttribute(IPDELauncherConstants.DOCLEAR, false) 104 && workspaceFile.exists()) { 105 boolean doClear = !configuration.getAttribute(IPDELauncherConstants.ASKCLEAR, 106 true); 107 if (!doClear) { 108 int result = confirmDeleteWorkspace(workspaceFile); 109 if (result == 2 || result == -1) { 110 monitor.done(); 111 return false; 112 } 113 doClear = result == 0; 114 } 115 116 if (doClear) { 117 if(configuration.getAttribute(IPDEUIConstants.DOCLEARLOG, false)) { 118 LauncherUtils.clearWorkspaceLog(workspace); 119 } else { 120 CoreUtility.deleteContent(workspaceFile); 121 } 122 } 123 } 124 125 monitor.done(); 126 return true; 127 } 128 129 private static int confirmDeleteWorkspace(final File workspaceFile) { 130 String message = NLS.bind( 131 PDEUIMessages.WorkbenchLauncherConfigurationDelegate_confirmDeleteWorkspace, 132 workspaceFile.getPath()); 133 return generateDialog(message); 134 } 135 136 public static boolean generateConfigIni() { 137 String message = PDEUIMessages.LauncherUtils_generateConfigIni; 138 return generateDialog(message) == 0; 139 } 140 141 private static int generateDialog(final String message) { 142 final int[] result = new int[1]; 143 getDisplay().syncExec(new Runnable () { 144 public void run() { 145 String title = PDEUIMessages.LauncherUtils_title; 146 MessageDialog dialog = new MessageDialog(getActiveShell(), 147 title, null, message, MessageDialog.QUESTION, new String [] { 148 IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, 149 IDialogConstants.CANCEL_LABEL }, 0); 150 result[0] = dialog.open(); 151 } 152 }); 153 return result[0]; 154 } 155 156 public static void validateProjectDependencies(ILaunchConfiguration launch, final IProgressMonitor monitor) { 157 IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore(); 158 if (!store.getBoolean(IPreferenceConstants.PROP_AUTO_MANAGE)) 159 return; 160 161 String timeStamp, selected, deSelected; 162 boolean useDefault, autoAdd; 163 try { 164 timeStamp = launch.getAttribute(TIMESTAMP, "0"); selected = launch.getAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, ""); deSelected = launch.getAttribute(IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS, ""); autoAdd = launch.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true); 168 useDefault = launch.getAttribute(IPDELauncherConstants.USE_DEFAULT, true); 169 useDefault |= launch.getAttribute(IPDELauncherConstants.USEFEATURES, false); 170 final ArrayList projects = new ArrayList (); 171 if (useDefault) 172 handleUseDefault(timeStamp, projects); 173 else if (autoAdd) 174 handleDeselectedPlugins(timeStamp, deSelected, projects); 175 else 176 handleSelectedPlugins(timeStamp, selected, projects); 177 178 if (!projects.isEmpty()) 179 Display.getDefault().syncExec(new Runnable () { 180 public void run() { 181 OrganizeManifestsOperation op = new OrganizeManifestsOperation(projects); 182 op.setOperations(getSettings()); 183 try { 184 op.run(monitor); 185 Properties table = getLastRun(); 187 String ts = Long.toString(System.currentTimeMillis()); 188 Iterator it = projects.iterator(); 189 while (it.hasNext()) 190 table.put(((IProject)it.next()).getName(), ts); 191 } catch (InvocationTargetException e) { 192 } catch (InterruptedException e) { 193 } 194 } 195 }); 196 197 ILaunchConfigurationWorkingCopy wc = null; 198 if (launch.isWorkingCopy()) 199 wc = (ILaunchConfigurationWorkingCopy)launch; 200 else 201 wc = launch.getWorkingCopy(); 202 wc.setAttribute(TIMESTAMP, Long.toString(System.currentTimeMillis())); 203 wc.doSave(); 204 } catch (CoreException e) { 205 } 206 } 207 208 private static IDialogSettings getSettings() { 209 IDialogSettings settings = new DialogSettings(""); settings.put(IOrganizeManifestsSettings.PROP_ADD_MISSING, true); 211 settings.put(IOrganizeManifestsSettings.PROP_MARK_INTERNAL, true); 212 settings.put(IOrganizeManifestsSettings.PROP_REMOVE_UNRESOLVED_EX, true); 213 settings.put(IOrganizeManifestsSettings.PROP_MODIFY_DEP, true); 214 settings.put(IOrganizeManifestsSettings.PROP_RESOLVE_IMP_MARK_OPT, true); 215 settings.put(IOrganizeManifestsSettings.PROP_REMOVE_LAZY, true); 216 settings.put(IOrganizeManifestsSettings.PROP_ADD_DEPENDENCIES, true); 217 return settings; 218 } 219 220 private static String getTimeStamp(IProject project) { 221 IJavaProject jp = JavaCore.create(project); 222 try { 223 long timeStamp = 0; 224 IClasspathEntry[] entries = jp.getResolvedClasspath(true); 225 for (int i = 0; i < entries.length; i++) { 226 if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { 227 File file; 228 IPath location = entries[i].getOutputLocation(); 229 if (location == null) 230 location = jp.getOutputLocation(); 231 IResource res = project.getWorkspace().getRoot().findMember(location); 232 IPath path = res == null ? null : res.getLocation(); 233 if (path == null) 234 continue; 235 file = path.toFile(); 236 Stack files = new Stack (); 237 files.push(file); 238 while(!files.isEmpty()) { 239 file = (File ) files.pop(); 240 if (file.isDirectory()) { 241 File [] children = file.listFiles(); 242 for (int j =0 ; j < children.length; j++) 243 files.push(children[j]); 244 } else 245 if (file.getName().endsWith(".class") && timeStamp < file.lastModified()) timeStamp = file.lastModified(); 247 } 248 } 249 } 250 String [] otherFiles = {"META-INF/MANIFEST.MF", "build.properties"}; for (int i = 0; i < otherFiles.length; i++) { 252 IResource file = project.getFile(otherFiles[i]); 253 if (file != null) { 254 long fileTimeStamp = file.getRawLocation().toFile().lastModified(); 255 if (timeStamp < fileTimeStamp) 256 timeStamp = fileTimeStamp; 257 } 258 } 259 return Long.toString(timeStamp); 260 } catch (JavaModelException e) { 261 } 262 return "0"; } 264 265 private static void handleUseDefault(String launcherTimeStamp, ArrayList projects) { 266 IProject[] projs = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 267 for (int i = 0; i < projs.length; i++) { 268 if (!WorkspaceModelManager.isPluginProject(projs[i])) 269 continue; 270 String timestamp = getTimeStamp(projs[i]); 271 if (timestamp.compareTo(launcherTimeStamp) > 0 && 272 shouldAdd(projs[i], launcherTimeStamp, timestamp)) 273 projects.add(projs[i]); 274 } 275 } 276 277 private static void handleSelectedPlugins(String timeStamp, String value, ArrayList projects) { 278 StringTokenizer tokenizer = new StringTokenizer (value, ","); while(tokenizer.hasMoreTokens()) { 280 value = tokenizer.nextToken(); 281 int index = value.indexOf('@'); 282 String id = index > 0 ? value.substring(0, index) : value; 283 IPluginModelBase base = PluginRegistry.findModel(id); 284 if (base != null) { 285 IResource res = base.getUnderlyingResource(); 286 if (res != null) { 287 IProject project = res.getProject(); 288 String projTimeStamp = getTimeStamp(project); 289 if (projTimeStamp.compareTo(timeStamp) > 0 && 290 shouldAdd(project, timeStamp, projTimeStamp)) 291 projects.add(project); 292 } 293 } 294 } 295 } 296 297 private static void handleDeselectedPlugins(String launcherTimeStamp, String value, ArrayList projects) { 298 StringTokenizer tokenizer = new StringTokenizer (value, ","); HashSet deSelectedProjs = new HashSet (); 300 while (tokenizer.hasMoreTokens()) 301 deSelectedProjs.add(tokenizer.nextToken()); 302 303 IProject[] projs = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 304 for (int i = 0; i < projs.length; i++) { 305 if (!WorkspaceModelManager.isPluginProject(projs[i])) 306 continue; 307 IPluginModelBase base = PluginRegistry.findModel(projs[i]); 308 if (base == null || base != null && deSelectedProjs.contains(base.getPluginBase().getId())) 309 continue; 310 String timestamp = getTimeStamp(projs[i]); 311 if (timestamp.compareTo(launcherTimeStamp) > 0 && 312 shouldAdd(projs[i], launcherTimeStamp, timestamp)) 313 projects.add(projs[i]); 314 } 315 } 316 317 public static final void shutdown() { 318 if (fLastRun == null) 319 return; 320 FileOutputStream stream = null; 321 try { 322 stream = new FileOutputStream (new File (getDirectory(), FILE_NAME)); 323 fLastRun.store(stream, "Cached timestamps"); stream.flush(); 325 stream.close(); 326 } catch (IOException e) { 327 PDECore.logException(e); 328 } finally { 329 try { 330 if (stream != null) 331 stream.close(); 332 } catch (IOException e1) { 333 } 334 } 335 } 336 337 private static File getDirectory() { 338 IPath path = PDECore.getDefault().getStateLocation().append(".cache"); File directory = new File (path.toOSString()); 340 if (!directory.exists() || !directory.isDirectory()) 341 directory.mkdirs(); 342 return directory; 343 } 344 345 private static Properties getLastRun() { 346 if (fLastRun == null) { 347 fLastRun = new Properties (); 348 FileInputStream fis = null; 349 try { 350 File file = new File (getDirectory(), FILE_NAME); 351 if (file.exists()) { 352 fis = new FileInputStream (file); 353 fLastRun.load(fis); 354 fis.close(); 355 } 356 } catch (IOException e) { 357 PDECore.logException(e); 358 } finally { 359 try { 360 if (fis != null) 361 fis.close(); 362 } catch (IOException e1) { 363 } 364 } 365 } 366 return fLastRun; 367 } 368 369 private static boolean shouldAdd(IProject proj, String launcherTS, String fileSystemTS) { 370 String projTS = (String )getLastRun().get(proj.getName()); 371 if (projTS == null) 372 return true; 373 return ((projTS.compareTo(launcherTS) < 0) || (projTS.compareTo(fileSystemTS) < 0)); 374 } 375 376 public static boolean requiresUI(ILaunchConfiguration configuration) { 377 try { 378 String projectID = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); if (projectID.length() > 0) { 380 IResource project = PDEPlugin.getWorkspace().getRoot().findMember(projectID); 381 if (project instanceof IProject) { 382 IPluginModelBase model = PluginRegistry.findModel((IProject)project); 383 if (model != null) { 384 Set plugins = DependencyManager.getSelfAndDependencies(model); 385 return plugins.contains("org.eclipse.swt"); } 387 } 388 } 389 } catch (CoreException e) { 390 } 391 return true; 392 } 393 394 public static boolean clearWorkspaceLog(String workspace) { 395 File logFile = new File (workspace, ".metadata" + File.separator + ".log"); if(logFile != null && logFile.exists()) { 397 return logFile.delete(); 398 } 399 return true; 400 } 401 402 public static IStatus createErrorStatus(String message) { 403 return new Status( 404 IStatus.ERROR, 405 PDEPlugin.getPluginId(), 406 IStatus.OK, 407 message, 408 null); 409 } 410 411 } 412 | Popular Tags |