jdk/src/share/classes/sun/security/util/PropertyExpander.java
author schien
Mon, 02 May 2011 09:36:49 -0700
changeset 9390 76bb81c6327c
parent 5506 202f599c92aa
child 24969 afa6934dd8e8
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A utility class to expand properties embedded in a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Strings of the form ${some.property.name} are expanded to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * be the value of the property. Also, the special ${/} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is expanded to be the same as file.separator. If a property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is not set, a GeneralSecurityException will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class PropertyExpander {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static class ExpandException extends GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        private static final long serialVersionUID = -7941948581406161702L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        public ExpandException(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            super(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static String expand(String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        throws ExpandException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return expand(value, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     public static String expand(String value, boolean encodeURL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         throws ExpandException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int p = value.indexOf("${", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // no special characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (p == -1) return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        StringBuffer sb = new StringBuffer(value.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int max = value.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int i = 0;  // index of last character we copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    scanner:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        while (p < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (p > i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // copy in anything before the special stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                sb.append(value.substring(i, p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                i = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int pe = p+2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // do not expand ${{ ... }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (pe < max && value.charAt(pe) == '{') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                pe = value.indexOf("}}", pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (pe == -1 || pe+2 == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    // append remaining chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    sb.append(value.substring(p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    break scanner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    // append as normal text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    pe++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    sb.append(value.substring(p, pe+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                while ((pe < max) && (value.charAt(pe) != '}')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    pe++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (pe == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    // no matching '}' found, just add in as normal text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    sb.append(value.substring(p, pe));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    break scanner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                String prop = value.substring(p+2, pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                if (prop.equals("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    sb.append(java.io.File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    String val = System.getProperty(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    if (val != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        if (encodeURL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            // encode 'val' unless it's an absolute URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            // at the beginning of the string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                if (sb.length() > 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                    !(new URI(val)).isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                    val = sun.net.www.ParseUtil.encodePath(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            } catch (URISyntaxException use) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                val = sun.net.www.ParseUtil.encodePath(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        sb.append(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        throw new ExpandException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                             "unable to expand property " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                             prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            i = pe+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            p = value.indexOf("${", i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (p == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                // no more to expand. copy in any extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (i < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    sb.append(value.substring(i, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                // break out of loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                break scanner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}