1 /*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.security.tools;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.lang.String;
31 import java.util.StringTokenizer;
32 import java.net.URL;
33 import java.net.URLClassLoader;
34 import java.net.MalformedURLException;
35
36 /**
37 * A utility class for handle path list
38 *
39 */
40 public class PathList {
41 /**
42 * Utility method for appending path from pathFrom to pathTo.
43 *
44 * @param pathTo the target path
45 * @param pathFrom the path to be appended to pathTo
46 * @return the resulting path
47 */
48 public static String appendPath(String pathTo, String pathFrom) {
49 if (pathTo == null || pathTo.length() == 0) {
50 return pathFrom;
51 } else if (pathFrom == null || pathFrom.length() == 0) {
52 return pathTo;
53 } else {
54 return pathTo + File.pathSeparator + pathFrom;
55 }
56 }
57
58 /**
59 * Utility method for converting a search path string to an array
60 * of directory and JAR file URLs.
61 *
62 * @param path the search path string
63 * @return the resulting array of directory and JAR file URLs
64 */
65 public static URL[] pathToURLs(String path) {
66 StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
67 URL[] urls = new URL[st.countTokens()];
68 int count = 0;
69 while (st.hasMoreTokens()) {
70 URL url = fileToURL(new File(st.nextToken()));
71 if (url != null) {
72 urls[count++] = url;
73 }
74 }
75 if (urls.length != count) {
76 URL[] tmp = new URL[count];
77 System.arraycopy(urls, 0, tmp, 0, count);
78 urls = tmp;
79 }
80 return urls;
81 }
82
83 /**
84 * Returns the directory or JAR file URL corresponding to the specified
85 * local file name.
86 *
87 * @param file the File object
88 * @return the resulting directory or JAR file URL, or null if unknown
89 */
90 private static URL fileToURL(File file) {
91 String name;
92 try {
93 name = file.getCanonicalPath();
94 } catch (IOException e) {
95 name = file.getAbsolutePath();
96 }
97 name = name.replace(File.separatorChar, '/');
98 if (!name.startsWith("/")) {
99 name = "/" + name;
100 }
|
1 /*
2 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.security.tools;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import java.nio.file.Paths;
33
34 /**
35 * A utility class for handle path list
36 *
37 */
38 public class PathList {
39 /**
40 * Utility method for appending path from pathFrom to pathTo.
41 *
42 * @param pathTo the target path
43 * @param pathFrom the path to be appended to pathTo
44 * @return the resulting path
45 */
46 public static String appendPath(String pathTo, String pathFrom) {
47 if (pathTo == null || pathTo.length() == 0) {
48 return pathFrom;
49 } else if (pathFrom == null || pathFrom.length() == 0) {
50 return pathTo;
51 } else {
52 return pathTo + File.pathSeparator + pathFrom;
53 }
54 }
55
56 /**
57 * Utility method for converting a search path string to an array
58 * of directory and JAR file URLs.
59 *
60 * @param path the search path string
61 * @return the resulting array of directory and JAR file URLs
62 */
63 public static URL[] pathToURLs(String path) {
64 return Paths.pathToStrings(path)
65 .stream()
66 .map(s -> fileToURL(new File(s)))
67 .toArray(URL[]::new);
68 }
69
70 /**
71 * Returns the directory or JAR file URL corresponding to the specified
72 * local file name.
73 *
74 * @param file the File object
75 * @return the resulting directory or JAR file URL, or null if unknown
76 */
77 private static URL fileToURL(File file) {
78 String name;
79 try {
80 name = file.getCanonicalPath();
81 } catch (IOException e) {
82 name = file.getAbsolutePath();
83 }
84 name = name.replace(File.separatorChar, '/');
85 if (!name.startsWith("/")) {
86 name = "/" + name;
87 }
|