jdk/src/share/classes/sun/security/util/PathList.java
changeset 10801 7e570cc378fb
parent 10777 c049b778ca61
parent 10800 b98de7e93a2b
child 10802 6d6175356561
equal deleted inserted replaced
10777:c049b778ca61 10801:7e570cc378fb
     1 /*
       
     2  * Copyright (c) 2004, 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.util;
       
    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 }