1 11 package org.eclipse.update.core; 12 13 import java.io.*; 14 import java.text.DateFormat ; 15 import java.util.Date ; 16 import java.util.HashMap ; 17 import java.util.Locale ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.update.core.model.*; 22 import org.eclipse.update.internal.core.*; 23 24 34 public class Utilities { 35 36 private static Map entryMap; 37 private static final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault()); 38 private static long tmpseed = (new Date ()).getTime(); 39 private static String dirRoot = null; 40 41 50 public static synchronized File createWorkingDirectory() throws IOException { 51 52 if (dirRoot == null) { 53 dirRoot = System.getProperty("java.io.tmpdir"); if (!dirRoot.endsWith(File.separator)) 56 dirRoot += File.separator; 57 58 if (!Platform.getOS().equals("win32")) { String home = System.getProperty("user.home"); home = Integer.toString(home.hashCode()); 63 dirRoot += home + File.separator; 64 } 65 dirRoot += "eclipse" + File.separator + ".update" + File.separator + Long.toString(tmpseed) + File.separator; } 67 68 String tmpName = dirRoot + Long.toString(++tmpseed) + File.separator; 69 70 File tmpDir = new File(tmpName); 71 verifyPath(tmpDir, false); 72 if (!tmpDir.exists()) 73 throw new FileNotFoundException(tmpName); 74 return tmpDir; 75 } 76 77 89 public static synchronized File createLocalFile(File tmpDir, String name) throws IOException { 90 File temp; 92 String filePath; 93 if (name != null) { 94 filePath = name.replace('/', File.separatorChar); 96 if (filePath.startsWith(File.separator)) 97 filePath = filePath.substring(1); 98 temp = new File(tmpDir, filePath); 99 } else { 100 temp = File.createTempFile("eclipse", null, tmpDir); } 103 temp.deleteOnExit(); 104 verifyPath(temp, true); 105 106 return temp; 107 } 108 109 115 public synchronized static void mapLocalFile(String key, File temp) { 116 if (key != null) { 118 if (entryMap == null) 119 entryMap = new HashMap (); 120 entryMap.put(key, temp); 121 } 122 } 123 124 132 public static synchronized File lookupLocalFile(String key) { 133 if (entryMap == null) 134 return null; 135 return (File) entryMap.get(key); 136 } 137 138 144 public synchronized static void flushLocalFile() { 145 entryMap = null; 146 } 147 148 155 public static synchronized void removeLocalFile(String key) { 156 if (entryMap != null) 157 entryMap.remove(key); 158 } 159 160 171 public static void copy(InputStream is, OutputStream os, InstallMonitor monitor) throws IOException, InstallAbortedException { 172 long offset = UpdateManagerUtils.copy(is, os, monitor, 0); 173 if (offset != -1) { 174 if (monitor.isCanceled()) { 175 String msg = Messages.Feature_InstallationCancelled; 176 throw new InstallAbortedException(msg, null); 177 } else { 178 throw new IOException(); 179 } 180 } 181 } 182 183 197 public static CoreException newCoreException(String s, int code, Throwable e) { 198 String id = UpdateCore.getPlugin().getBundle().getSymbolicName(); 199 200 IStatus status; 202 if (e instanceof FeatureDownloadException) 203 return (FeatureDownloadException)e; 204 else if (e instanceof CoreException) { 205 if (s == null) 206 s = ""; status = new MultiStatus(id, code, s, e); 208 IStatus childrenStatus = ((CoreException) e).getStatus(); 209 ((MultiStatus) status).add(childrenStatus); 210 ((MultiStatus) status).addAll(childrenStatus); 211 } else { 212 StringBuffer completeString = new StringBuffer (""); if (s != null) 214 completeString.append(s); 215 if (e != null) { 216 completeString.append(" ["); String msg = e.getLocalizedMessage(); 218 completeString.append(msg!=null?msg:e.toString()); 219 completeString.append("]"); } 221 status = new Status(IStatus.ERROR, id, code, completeString.toString(), e); 222 } 223 CoreException ce = new CoreException(status); 224 225 if ( e instanceof FatalIOException) { 226 ce = new CoreExceptionWithRootCause(status); 227 ((CoreExceptionWithRootCause)ce).setRootException(e); 228 } 229 237 return ce; 238 } 239 240 253 public static CoreException newCoreException(String s, Throwable e) { 254 return newCoreException(s, IStatus.OK, e); 255 } 256 257 268 public static CoreException newCoreException(String s, String s1, String s2, CoreException e1, CoreException e2) { 269 String id = UpdateCore.getPlugin().getBundle().getSymbolicName(); 270 if (s == null) 271 s = ""; 273 IStatus childStatus1 = e1.getStatus(); 274 IStatus childStatus2 = e2.getStatus(); 275 int code = (childStatus1.getCode() == childStatus2.getCode()) ? childStatus1.getCode() : IStatus.OK; 276 MultiStatus multi = new MultiStatus(id, code, s, null); 277 278 multi.add(childStatus1); 279 multi.addAll(childStatus1); 280 multi.add(childStatus2); 281 multi.addAll(childStatus2); 282 283 return new CoreException(multi); 284 } 285 286 294 public static String format(Date date) { 295 if (date == null) 296 return ""; return dateFormat.format(date); 298 } 299 300 310 public static void shutdown() { 311 if (dirRoot == null) 312 return; 313 314 File temp = new File(dirRoot); cleanupTemp(temp); 316 temp.delete(); 317 } 318 319 private static void cleanupTemp(File root) { 320 File[] files = root.listFiles(); 321 for (int i = 0; files != null && i < files.length; i++) { 322 if (files[i].isDirectory()) 323 cleanupTemp(files[i]); 324 files[i].delete(); 325 } 326 } 327 328 private static void verifyPath(File path, boolean isFile) { 329 if (isFile) { 331 if (path.getAbsolutePath().endsWith(File.separator)) { 332 path = path.getParentFile(); 334 isFile = false; 335 } 336 } 337 338 if (path.exists()) 340 return; 341 342 File parent = path.getParentFile(); 344 verifyPath(parent, false); 345 346 if (!isFile) 348 path.mkdir(); 349 path.deleteOnExit(); 350 } 351 } 352 | Popular Tags |