1 // 2 // Getdown - application installer, patcher and launcher 3 // Copyright (C) 2004-2018 Getdown authors 4 // https://github.com/threerings/getdown/blob/master/LICENSE 5 6 package com.threerings.getdown.data; 7 8 import java.io.File; 9 import java.net.MalformedURLException; 10 import java.net.URL; 11 import java.net.URLClassLoader; 12 import java.util.Collections; 13 import java.util.LinkedHashSet; 14 import java.util.Set; 15 16 /** 17 * Represents the class path and it's elements of the application to be launched. The class path 18 * can either be represented as an {@link #asArgumentString() argument string} for the java command 19 * line or as an {@link #asUrls() array of URLs} to be used by a {@link URLClassLoader}. 20 */ 21 public class ClassPath 22 { ClassPath(LinkedHashSet<File> classPathEntries)23 public ClassPath (LinkedHashSet<File> classPathEntries) 24 { 25 _classPathEntries = Collections.unmodifiableSet(classPathEntries); 26 } 27 28 /** 29 * Returns the class path as an java command line argument string, e.g. 30 * 31 * <pre> 32 * /path/to/a.jar:/path/to/b.jar 33 * </pre> 34 */ asArgumentString()35 public String asArgumentString () 36 { 37 StringBuilder builder = new StringBuilder(); 38 String delimiter = ""; 39 for (File entry: _classPathEntries) { 40 builder.append(delimiter).append(entry.getAbsolutePath()); 41 delimiter = File.pathSeparator; 42 } 43 return builder.toString(); 44 } 45 46 /** 47 * Returns the class path entries as an array of URLs to be used for example by an 48 * {@link URLClassLoader}. 49 */ asUrls()50 public URL[] asUrls () 51 { 52 URL[] urls = new URL[_classPathEntries.size()]; 53 int i = 0; 54 for (File entry : _classPathEntries) { 55 urls[i++] = getURL(entry); 56 } 57 return urls; 58 } 59 getClassPathEntries()60 public Set<File> getClassPathEntries () 61 { 62 return _classPathEntries; 63 } 64 65 getURL(File file)66 private static URL getURL (File file) 67 { 68 try { 69 return file.toURI().toURL(); 70 } catch (MalformedURLException e) { 71 throw new IllegalStateException("URL of file is illegal: " + file.getAbsolutePath(), e); 72 } 73 } 74 75 private final Set<File> _classPathEntries; 76 } 77