CopyFile.java revision 3012:adba44f6b471
1218822Sdim/*
2218822Sdim * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3218822Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4218822Sdim *
5218822Sdim * This code is free software; you can redistribute it and/or modify it
6218822Sdim * under the terms of the GNU General Public License version 2 only, as
7218822Sdim * published by the Free Software Foundation.  Oracle designates this
8218822Sdim * particular file as subject to the "Classpath" exception as provided
9218822Sdim * by Oracle in the LICENSE file that accompanied this code.
1038889Sjdp *
1138889Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT
1238889Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1338889Sjdp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1438889Sjdp * version 2 for more details (a copy is included in the LICENSE file that
1538889Sjdp * accompanied this code).
1638889Sjdp *
1738889Sjdp * You should have received a copy of the GNU General Public License version
1838889Sjdp * 2 along with this work; if not, write to the Free Software Foundation,
1938889Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2038889Sjdp *
2138889Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22218822Sdim * or visit www.oracle.com if you need additional information or have any
2338889Sjdp * questions.
2438889Sjdp */
2538889Sjdp
2638889Sjdppackage com.sun.tools.sjavac;
2738889Sjdp
2838889Sjdpimport java.io.File;
29218822Sdimimport java.io.FileInputStream;
30218822Sdimimport java.io.FileOutputStream;
31218822Sdimimport java.io.IOException;
32218822Sdimimport java.io.InputStream;
33218822Sdimimport java.io.OutputStream;
34218822Sdimimport java.io.PrintStream;
35218822Sdimimport java.net.URI;
36218822Sdimimport java.util.HashSet;
37218822Sdimimport java.util.Map;
38218822Sdimimport java.util.Set;
39218822Sdim
40218822Sdimimport com.sun.tools.sjavac.comp.CompilationService;
41218822Sdimimport com.sun.tools.sjavac.options.Options;
42218822Sdimimport com.sun.tools.sjavac.pubapi.PubApi;
43218822Sdim
44218822Sdim/**
45218822Sdim * The copy file transform simply copies a matching file from -src to -d .
46218822Sdim * Such files are typically images, xml documents and other data files.
47218822Sdim *
48218822Sdim *  <p><b>This is NOT part of any supported API.
49218822Sdim *  If you write code that depends on this, you do so at your own risk.
50218822Sdim *  This code and its internal interfaces are subject to change or
51218822Sdim *  deletion without notice.</b>
52218822Sdim */
53218822Sdimpublic class CopyFile implements Transformer {
54218822Sdim
55218822Sdim    public void setExtra(String e) {
56218822Sdim    }
57218822Sdim
58218822Sdim    public void setExtra(Options a) {
59218822Sdim    }
60218822Sdim
61218822Sdim    public boolean transform(CompilationService compilationService,
62218822Sdim                             Map<String,Set<URI>> pkgSrcs,
63218822Sdim                             Set<URI> visibleSrcs,
64218822Sdim                             Map<URI,Set<String>> visibleClasses,
65218822Sdim                             Map<String,Set<String>> oldPackageDependents,
66218822Sdim                             URI destRoot,
67218822Sdim                             Map<String,Set<URI>>    packageArtifacts,
68218822Sdim                             Map<String,Map<String, Set<String>>> packageDependencies,
69218822Sdim                             Map<String,Map<String, Set<String>>> packageCpDependencies,
70218822Sdim                             Map<String, PubApi> packagePubapis,
71218822Sdim                             Map<String, PubApi> dependencyPubapis,
72218822Sdim                             int debugLevel,
73218822Sdim                             boolean incremental,
74218822Sdim                             int numCores,
75218822Sdim                             PrintStream out,
76218822Sdim                             PrintStream err)
77218822Sdim    {
78218822Sdim        boolean rc = true;
79218822Sdim        String dest_filename;
80218822Sdim        File dest;
81218822Sdim
82218822Sdim        for (String pkgName : pkgSrcs.keySet()) {
83218822Sdim            String pkgNameF = Util.toFileSystemPath(pkgName);
84218822Sdim            for (URI u : pkgSrcs.get(pkgName)) {
85218822Sdim                File src = new File(u);
86218822Sdim                File destDir;
87218822Sdim                destDir = new File(destRoot.getPath()+File.separator+pkgNameF);
88218822Sdim                dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
89218822Sdim                dest = new File(dest_filename);
90218822Sdim
91218822Sdim                if (!destDir.isDirectory()) {
92218822Sdim                    if (!destDir.mkdirs()) {
93218822Sdim                       Log.error("Error: The copier could not create the directory "+
94218822Sdim                                           destDir.getPath());
95218822Sdim                        return false;
96218822Sdim                    }
97218822Sdim                }
98218822Sdim
99218822Sdim                Set<URI> as = packageArtifacts.get(pkgName);
100218822Sdim                if (as == null) {
101218822Sdim                    as = new HashSet<>();
102218822Sdim                    packageArtifacts.put(pkgName, as);
103218822Sdim                }
104218822Sdim                as.add(dest.toURI());
105218822Sdim
106218822Sdim                if (dest.exists() && dest.lastModified() > src.lastModified()) {
107218822Sdim                    // A copied file exists, and its timestamp is newer than the source.
108218822Sdim                    continue;
109218822Sdim                }
110218822Sdim
111218822Sdim                Log.info("Copying "+pkgNameF+File.separator+src.getName());
112218822Sdim
113218822Sdim                try (InputStream fin = new FileInputStream(src);
114218822Sdim                     OutputStream fout = new FileOutputStream(dest)) {
115218822Sdim                    byte[] buf = new byte[1024];
116218822Sdim                    int len;
117218822Sdim                    while ((len = fin.read(buf)) > 0){
118218822Sdim                        fout.write(buf, 0, len);
119218822Sdim                    }
120218822Sdim                }
121218822Sdim                catch(IOException e){
122218822Sdim                    Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());
123218822Sdim                    rc = false;
124218822Sdim                }
125218822Sdim            }
126218822Sdim        }
127218822Sdim        return rc;
128218822Sdim    }
129218822Sdim}
130218822Sdim