src/java.base/share/classes/sun/net/www/protocol/jrt/JavaRuntimeURLConnection.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     1
/*
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     4
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    10
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    15
 * accompanied this code).
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    16
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    20
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    23
 * questions.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    24
 */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    25
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    26
package sun.net.www.protocol.jrt;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    27
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    28
import java.io.ByteArrayInputStream;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    29
import java.io.IOException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    30
import java.io.InputStream;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    31
import java.net.MalformedURLException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    32
import java.net.URL;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 37781
diff changeset
    33
import java.security.AccessController;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    34
import java.security.Permission;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 37781
diff changeset
    35
import java.security.PrivilegedAction;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    36
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    37
import jdk.internal.jimage.ImageLocation;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    38
import jdk.internal.jimage.ImageReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    39
import jdk.internal.jimage.ImageReaderFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    40
36674
7ab530dd6f10 8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents: 36511
diff changeset
    41
import jdk.internal.loader.URLClassPath;
7ab530dd6f10 8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents: 36511
diff changeset
    42
import jdk.internal.loader.Resource;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    43
import sun.net.www.ParseUtil;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    44
import sun.net.www.URLConnection;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    45
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    46
/**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    47
 * URLConnection implementation that can be used to connect to resources
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    48
 * contained in the runtime image.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    49
 */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    50
public class JavaRuntimeURLConnection extends URLConnection {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    51
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    52
    // ImageReader to access resources in jimage
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 37781
diff changeset
    53
    private static final ImageReader reader;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 37781
diff changeset
    54
    static {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 37781
diff changeset
    55
        PrivilegedAction<ImageReader> pa = ImageReaderFactory::getImageReader;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 37781
diff changeset
    56
        reader = AccessController.doPrivileged(pa);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 37781
diff changeset
    57
    }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    58
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    59
    // the module and resource name in the URL
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    60
    private final String module;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    61
    private final String name;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    62
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    63
    // the Resource when connected
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    64
    private volatile Resource resource;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    65
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    66
    JavaRuntimeURLConnection(URL url) throws IOException {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    67
        super(url);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    68
        String path = url.getPath();
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
    69
        if (path.isEmpty() || path.charAt(0) != '/')
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    70
            throw new MalformedURLException(url + " missing path or /");
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    71
        if (path.length() == 1) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    72
            this.module = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    73
            this.name = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    74
        } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    75
            int pos = path.indexOf('/', 1);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    76
            if (pos == -1) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    77
                this.module = path.substring(1);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    78
                this.name = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    79
            } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    80
                this.module = path.substring(1, pos);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    81
                this.name = ParseUtil.decode(path.substring(pos+1));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    82
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    83
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    84
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    85
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    86
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    87
     * Finds a resource in a module, returning {@code null} if the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    88
     * is not found.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    89
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    90
    private static Resource findResource(String module, String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    91
        if (reader != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    92
            URL url = toJrtURL(module, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    93
            ImageLocation location = reader.findLocation(module, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    94
            if (location != null && URLClassPath.checkURL(url) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    95
                return new Resource() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    96
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    97
                    public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    98
                        return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
    99
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   100
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   101
                    public URL getURL() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   102
                        return url;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   103
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   104
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   105
                    public URL getCodeSourceURL() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   106
                        return toJrtURL(module);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   107
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   108
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   109
                    public InputStream getInputStream() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   110
                        byte[] resource = reader.getResource(location);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   111
                        return new ByteArrayInputStream(resource);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   112
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   113
                    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   114
                    public int getContentLength() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   115
                        long size = location.getUncompressedSize();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   116
                        return (size > Integer.MAX_VALUE) ? -1 : (int) size;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   117
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   118
                };
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   119
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   120
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   121
        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   122
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   123
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   124
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   125
    public synchronized void connect() throws IOException {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   126
        if (!connected) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   127
            if (name == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   128
                String s = (module == null) ? "" : module;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   129
                throw new IOException("cannot connect to jrt:/" + s);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   130
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   131
            resource = findResource(module, name);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   132
            if (resource == null)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   133
                throw new IOException(module + "/" + name + " not found");
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   134
            connected = true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   135
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   136
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   137
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   138
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   139
    public InputStream getInputStream() throws IOException {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   140
        connect();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   141
        return resource.getInputStream();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   142
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   143
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   144
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   145
    public long getContentLengthLong() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   146
        try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   147
            connect();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   148
            return resource.getContentLength();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   149
        } catch (IOException ioe) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   150
            return -1L;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   151
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   152
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   153
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   154
    @Override
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   155
    public int getContentLength() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   156
        long len = getContentLengthLong();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   157
        return len > Integer.MAX_VALUE ? -1 : (int)len;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   158
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   159
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   160
    @Override
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   161
    public Permission getPermission() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   162
        return new RuntimePermission("accessSystemModules");
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   163
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   165
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   166
     * Returns a jrt URL for the given module and resource name.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   167
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   168
    private static URL toJrtURL(String module, String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   169
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   170
            return new URL("jrt:/" + module + "/" + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   171
        } catch (MalformedURLException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   172
            throw new InternalError(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   173
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   174
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   175
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   176
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   177
     * Returns a jrt URL for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   178
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   179
    private static URL toJrtURL(String module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   180
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   181
            return new URL("jrt:/" + module);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   182
        } catch (MalformedURLException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   183
            throw new InternalError(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   184
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   185
    }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   186
}