author | mullan |
Thu, 13 Oct 2011 13:50:17 -0400 | |
changeset 10782 | 01689c7b34ac |
parent 5506 | jdk/src/share/classes/sun/security/util/PathList.java@202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10782
01689c7b34ac
6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
10782
01689c7b34ac
6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents:
5506
diff
changeset
|
26 |
package sun.security.tools; |
2 | 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 pathSource 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 |
} |
|
101 |
// If the file does not exist, then assume that it's a directory |
|
102 |
if (!file.isFile()) { |
|
103 |
name = name + "/"; |
|
104 |
} |
|
105 |
try { |
|
106 |
return new URL("file", "", name); |
|
107 |
} catch (MalformedURLException e) { |
|
108 |
throw new IllegalArgumentException("file"); |
|
109 |
} |
|
110 |
} |
|
111 |
} |