jdk/src/java.base/share/classes/sun/net/www/MimeLauncher.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 34716 7477a052aecc
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
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) 1994, 1998, 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.net.www;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.StringTokenizer;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34716
diff changeset
    30
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
34716
7477a052aecc 8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents: 29920
diff changeset
    32
class MimeLauncher extends Thread {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    java.net.URLConnection uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    MimeEntry m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    String genericTempFileTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    String execPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    MimeLauncher (MimeEntry M, java.net.URLConnection uc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                  InputStream is, String tempFileTemplate, String threadName) throws ApplicationLaunchException {
34716
7477a052aecc 8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents: 29920
diff changeset
    41
        super(null, null, threadName, 0, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        m = M;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        this.uc = uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this.is = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        genericTempFileTemplate = tempFileTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        /* get the application to launch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String launchString = m.getLaunchString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        /* get a valid path to launch application - sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
           the execPath instance variable with the correct path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (!findExecutablePath(launchString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            /* strip off parameters i.e %s */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            String appName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            int index = launchString.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                appName = launchString.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                appName = launchString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new ApplicationLaunchException(appName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected String getTempFileName(URL url, String template) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        String tempFilename = template;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Replace all but last occurrance of "%s" with timestamp to insure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // uniqueness.  There's a subtle behavior here: if there is anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // _after_ the last "%s" we need to append it so that unusual launch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // strings that have the datafile in the middle can still be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int wildcard = tempFilename.lastIndexOf("%s");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String prefix = tempFilename.substring(0, wildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String suffix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (wildcard < tempFilename.length() - 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            suffix = tempFilename.substring(wildcard + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        long timestamp = System.currentTimeMillis()/1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int argIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        while ((argIndex = prefix.indexOf("%s")) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            prefix = prefix.substring(0, argIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                + timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                + prefix.substring(argIndex + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // Add a file name and file-extension if known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        String filename = url.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        String extension = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        int dot = filename.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // BugId 4084826:  Temp MIME file names not always valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // Fix:  don't allow slashes in the file name or extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (dot >= 0 && dot > filename.lastIndexOf('/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            extension = filename.substring(dot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        filename = "HJ" + url.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        tempFilename = prefix + filename + timestamp + extension + suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return tempFilename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            String ofn = m.getTempFileTemplate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (ofn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                ofn = genericTempFileTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ofn = getTempFileName(uc.getURL(), ofn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                OutputStream os = new FileOutputStream(ofn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                byte buf[] = new byte[2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    while ((i = is.read(buf)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        os.write(buf, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                  //System.err.println("Exception in write loop " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                  //e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
              //System.err.println("Exception in input or output stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
              //e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int inx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            String c = execPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            while ((inx = c.indexOf("%t")) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                c = c.substring(0, inx) + uc.getContentType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    + c.substring(inx + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            boolean substituted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            while ((inx = c.indexOf("%s")) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                c = c.substring(0, inx) + ofn + c.substring(inx + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                substituted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (!substituted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                c = c + " <" + ofn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // System.out.println("Execing " +c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            Runtime.getRuntime().exec(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /* This method determines the path for the launcher application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
       and sets the execPath instance variable.  It uses the exec.path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
       property to obtain a list of paths that is in turn used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
       location the application.  If a valid path is not found, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
       returns false else true.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private boolean findExecutablePath(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (str == null || str.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        String command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int index = str.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            command = str.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            command = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        File f = new File(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (f.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // Already executable as it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            execPath = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String execPathList;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34716
diff changeset
   186
        execPathList = GetPropertyAction.getProperty("exec.path");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (execPathList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // exec.path property not set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        StringTokenizer iter = new StringTokenizer(execPathList, "|");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        while (iter.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            String prefix = (String)iter.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            String fullCmd = prefix + File.separator + command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            f = new File(fullCmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (f.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                execPath = prefix + File.separator + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return false; // application not found in exec.path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}