src/jdk.compiler/share/classes/com/sun/tools/sjavac/CopyFile.java
author rkennke
Fri, 12 Oct 2018 16:25:24 +0200
changeset 52107 0c1e44da019c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8212053: A few more missing object equals barriers Reviewed-by: shade, zgu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     1
/*
34914
dfdc5df47041 8145557: Unused method in JavacState should be removed
alundblad
parents: 32542
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     4
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    10
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    15
 * accompanied this code).
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    16
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    20
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    23
 * questions.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    24
 */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    25
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    26
package com.sun.tools.sjavac;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    27
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    28
import java.io.File;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    29
import java.io.FileInputStream;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    30
import java.io.FileOutputStream;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    31
import java.io.IOException;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    32
import java.io.InputStream;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    33
import java.io.OutputStream;
32542
f4e4f4c4f9f4 8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents: 32335
diff changeset
    34
import java.io.Writer;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    35
import java.net.URI;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    36
import java.util.HashSet;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    37
import java.util.Map;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    38
import java.util.Set;
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    39
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    40
import com.sun.tools.sjavac.comp.CompilationService;
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
    41
import com.sun.tools.sjavac.options.Options;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    42
import com.sun.tools.sjavac.pubapi.PubApi;
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
    43
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    44
/**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    45
 * The copy file transform simply copies a matching file from -src to -d .
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    46
 * Such files are typically images, xml documents and other data files.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    47
 *
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 25299
diff changeset
    48
 *  <p><b>This is NOT part of any supported API.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 25299
diff changeset
    49
 *  If you write code that depends on this, you do so at your own risk.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 25299
diff changeset
    50
 *  This code and its internal interfaces are subject to change or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 25299
diff changeset
    51
 *  deletion without notice.</b>
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    52
 */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    53
public class CopyFile implements Transformer {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    54
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    55
    public void setExtra(String e) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    56
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    57
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents: 22449
diff changeset
    58
    public void setExtra(Options a) {
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    59
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    60
32335
7df616378cf3 8048318: Refactor sjavac as a thin client
alundblad
parents: 31115
diff changeset
    61
    public boolean transform(CompilationService compilationService,
25299
b4a7dcd657f5 8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents: 24067
diff changeset
    62
                             Map<String,Set<URI>> pkgSrcs,
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    63
                             Set<URI> visibleSrcs,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    64
                             Map<String,Set<String>> oldPackageDependents,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    65
                             URI destRoot,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    66
                             Map<String,Set<URI>>    packageArtifacts,
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    67
                             Map<String,Map<String, Set<String>>> packageDependencies,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    68
                             Map<String,Map<String, Set<String>>> packageCpDependencies,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    69
                             Map<String, PubApi> packagePubapis,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents: 26107
diff changeset
    70
                             Map<String, PubApi> dependencyPubapis,
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    71
                             int debugLevel,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    72
                             boolean incremental,
36161
a025c0619f25 8147569: Error messages from sjavac server does not always get relayed back to client
alundblad
parents: 34914
diff changeset
    73
                             int numCores)
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    74
    {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    75
        boolean rc = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    76
        String dest_filename;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    77
        File dest;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    78
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    79
        for (String pkgName : pkgSrcs.keySet()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    80
            String pkgNameF = Util.toFileSystemPath(pkgName);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    81
            for (URI u : pkgSrcs.get(pkgName)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    82
                File src = new File(u);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    83
                File destDir;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    84
                destDir = new File(destRoot.getPath()+File.separator+pkgNameF);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    85
                dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    86
                dest = new File(dest_filename);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    87
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    88
                if (!destDir.isDirectory()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    89
                    if (!destDir.mkdirs()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    90
                       Log.error("Error: The copier could not create the directory "+
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    91
                                           destDir.getPath());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    92
                        return false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    93
                    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    94
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    95
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    96
                Set<URI> as = packageArtifacts.get(pkgName);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    97
                if (as == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 15368
diff changeset
    98
                    as = new HashSet<>();
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    99
                    packageArtifacts.put(pkgName, as);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   100
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   101
                as.add(dest.toURI());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   102
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   103
                if (dest.exists() && dest.lastModified() > src.lastModified()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   104
                    // A copied file exists, and its timestamp is newer than the source.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   105
                    continue;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   106
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   107
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   108
                Log.info("Copying "+pkgNameF+File.separator+src.getName());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   109
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   110
                try (InputStream fin = new FileInputStream(src);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   111
                     OutputStream fout = new FileOutputStream(dest)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   112
                    byte[] buf = new byte[1024];
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   113
                    int len;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   114
                    while ((len = fin.read(buf)) > 0){
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   115
                        fout.write(buf, 0, len);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   116
                    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   117
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   118
                catch(IOException e){
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   119
                    Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   120
                    rc = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   121
                }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   122
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   123
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   124
        return rc;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   125
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
   126
}